-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllama_service.go
101 lines (85 loc) · 1.96 KB
/
llama_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"fmt"
"log"
llama "github.com/wailovet/go-llama.cpp-winbin"
)
type Llama struct {
model *llama.LLama
modelfile string
}
func (l *Llama) ModelFile() string {
return l.modelfile
}
func (l *Llama) StartUp(modelfile string) error {
if l.model != nil {
l.model.Free()
}
var err error
l.model, err = llama.New(modelfile, llama.SetContext(2048), llama.SetParts(-1), llama.SetNGPULayers(0))
if err != nil {
return err
}
l.modelfile = modelfile
return nil
}
func (l *Llama) Free() {
if l.model != nil {
l.model.Free()
l.model = nil
l.modelfile = ""
}
}
func (l *Llama) Predict(p Prompts, his ChatHistory, opts *PredictOption) (string, error) {
if l.ModelFile() == "" {
return "", fmt.Errorf("model_not_loaded")
}
text := fmt.Sprintln(p.Instruct)
if his == nil || len(his) == 0 {
} else {
if his[len(his)-1].Role == "assistant" {
if len(his)-1 > 0 {
his = his[:len(his)-1]
} else {
return "", fmt.Errorf("history is nil")
}
}
for _, v := range his {
if v.Role == "assistant" {
text += fmt.Sprintln(p.AssistantPrefix, v.Content)
} else {
text += fmt.Sprintln(p.UserPrefix, v.Content)
}
}
text += p.AssistantPrefix
}
log.Println(text)
var mopts []llama.PredictOption
if opts != nil {
if opts.BatchSize > 0 {
mopts = append(mopts, llama.SetBatchSize(opts.BatchSize))
}
if opts.Penalty > 0 {
mopts = append(mopts, llama.SetPenalty(opts.Penalty))
}
if opts.Temperature > 0 {
mopts = append(mopts, llama.SetTemperature(opts.Temperature))
}
if opts.TopP > 0 {
mopts = append(mopts, llama.SetTopP(opts.TopP))
}
if opts.Tokens > 0 {
mopts = append(mopts, llama.SetTokens(opts.Tokens))
}
if opts.Threads > 0 {
mopts = append(mopts, llama.SetThreads(opts.Threads))
}
if opts.StreamFn != nil {
mopts = append(mopts, llama.SetStreamFn(opts.StreamFn))
}
}
return l.model.Predict(text, mopts...)
}
func (l *Llama) IsReady() bool {
return l.model != nil
}