This repository has been archived by the owner on Feb 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrwkv_service.go
85 lines (74 loc) · 1.62 KB
/
rwkv_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
package main
import (
"fmt"
"log"
"github.com/wailovet/go-rwkv.cpp-winbin/gorwkv"
)
type RWKV struct {
model *gorwkv.RWKV
modelfile string
}
func (l *RWKV) ModelFile() string {
return l.modelfile
}
func (l *RWKV) StartUp(modelfile string) error {
if l.model != nil {
l.model.Free()
}
var err error
// cpuNum := runtime.NumCPU()
l.model, err = gorwkv.NewRWKV(modelfile, 2, true)
if err != nil {
return err
}
l.modelfile = modelfile
return nil
}
func (l *RWKV) Free() {
if l.model != nil {
l.model.Free()
l.model = nil
l.modelfile = ""
}
}
func (l *RWKV) Predict(p Prompts, his ChatHistory, opts *PredictOption) (string, error) {
if l.ModelFile() == "" {
return "", fmt.Errorf("model_not_loaded")
}
if his == nil {
return "", fmt.Errorf("history is nil")
}
if his[len(his)-1].Role == "assistant" {
if len(his)-1 > 0 {
his = his[:len(his)-1]
} else {
return "", fmt.Errorf("history is nil")
}
}
text := fmt.Sprintln(p.Instruct)
for _, v := range his {
if v.Role == "assistant" {
text += p.AssistantPrefix + v.Content + "\n\n"
} else {
text += p.UserPrefix + v.Content + "\n\n"
}
}
text += p.AssistantPrefix
log.Println(text)
err := l.model.GenerateWithCache(
text,
gorwkv.RWKV_Config{
MaxSeqLength: uint32(opts.MaxTokens),
MaxTokens: uint32(opts.MaxTokens),
TopP: float32(opts.TopP),
TopK: float32(opts.TopK),
Temperature: float32(opts.Temperature),
NoRepeatNgramSize: uint32(opts.Repeat),
Stream: opts.StreamFn,
},
)
return text, err
}
func (l *RWKV) IsReady() bool {
return l.model != nil
}