-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
114 lines (101 loc) · 2.62 KB
/
common.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
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"encoding/binary"
"fmt"
"log"
"os"
"strings"
)
type ChatContent struct {
Role string `json:"role"`
Content string `json:"content"`
}
type ChatHistory []ChatContent
type ModelStatus string
const (
ModelStatusNotReady ModelStatus = "not_ready"
ModelStatusReady ModelStatus = "ready"
)
// 中文标点符号转英文标点符号
func ChinesePunctuationToEnglishPunctuation(text string) string {
text = strings.ReplaceAll(text, ",", ",")
text = strings.ReplaceAll(text, "。", ".")
text = strings.ReplaceAll(text, "!", "!")
text = strings.ReplaceAll(text, "?", "?")
text = strings.ReplaceAll(text, ";", ";")
text = strings.ReplaceAll(text, ":", ":")
text = strings.ReplaceAll(text, "“", "\"")
text = strings.ReplaceAll(text, "”", "\"")
text = strings.ReplaceAll(text, "‘", "'")
text = strings.ReplaceAll(text, "’", "'")
text = strings.ReplaceAll(text, "(", "(")
text = strings.ReplaceAll(text, ")", ")")
text = strings.ReplaceAll(text, "《", "<")
text = strings.ReplaceAll(text, "》", ">")
text = strings.ReplaceAll(text, "【", "[")
text = strings.ReplaceAll(text, "】", "]")
text = strings.ReplaceAll(text, "、", ",")
text = strings.ReplaceAll(text, "—", "-")
text = strings.ReplaceAll(text, "——", "-")
text = strings.ReplaceAll(text, "…", "...")
text = strings.ReplaceAll(text, "·", ".")
return text
}
func modelType(filename string) string {
//open a file
f, err := os.Open(filename)
if err != nil {
return ""
}
defer f.Close()
// take the first byte,type is u32
var b uint32
err = binary.Read(f, binary.LittleEndian, &b)
if err != nil {
log.Println("binary.Read failed:", err)
return ""
}
var c uint32
err = binary.Read(f, binary.LittleEndian, &c)
if err != nil {
log.Println("binary.Read failed:", err)
return ""
}
switch b {
case 0x746A6767:
return "llama"
case 0x67676A74:
return "llama"
case 0x666D6767:
return "rwkv"
case 0x67676D66:
return "rwkv"
default:
return fmt.Sprintf("unknown:%x", b)
}
}
type Prompts struct {
Instruct string `json:"instruct"`
AssistantPrefix string `json:"assistant_prefix"` //助手前缀
UserPrefix string `json:"user_prefix"` //用户前缀
}
type NLP interface {
ModelFile() string
StartUp(modelfile string) error
Free()
Predict(p Prompts, his ChatHistory, opts *PredictOption) (string, error)
IsReady() bool
}
type PredictOption struct {
TopK int
Repeat int
BatchSize int
Penalty float64
Temperature float64
TopP float64
Tokens int
MaxTokens int
Threads int
Stop []string
StreamFn func(outputText string) (stop bool)
}