-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
150 lines (139 loc) · 3.61 KB
/
main.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"bytes"
"encoding/binary"
"encoding/hex"
"flag"
"fmt"
"net"
"net/netip"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/sirupsen/logrus"
para "github.com/fumiama/go-hide-param"
tea "github.com/fumiama/gofastTEA"
"github.com/fumiama/alpacapi"
)
const endmark = `
### Response:
You: `
// prompt: role, default, message
const prompt = `You are a Chinese %s and got messages with foramt "User(Name): Content". Reply with one sentence in Chinese. Don't repeat given information. If you don't know how to reply, just say "%s".
### Instruction:
%s` + endmark
func main() {
addr := flag.String("l", "0.0.0.0:31471", "listening endpoint")
mpth := flag.String("m", "/dataset/Alpaca/ggml/13B-ggml-model-q4_1.bin", "alpaca ggml model path")
threadcnt := flag.Uint("t", 24, "use threads count")
llamapath := flag.String("p", "./src/llama.cpp/main", "llama.cpp main path")
bufsz := flag.Uint("b", 4096, "udp buffer size")
sumtablepath := flag.String("s", "sumtable.bin", "tea sumtable file")
flag.Parse()
if len(flag.Args()) < 1 {
panic("must give tea key (16 bytes hex string)")
}
kstr := flag.Args()[0]
k, err := hex.DecodeString(kstr)
if err != nil {
panic(err)
}
for i, a := range os.Args {
if a == kstr {
para.Hide(i)
}
}
tk := tea.NewTeaCipherLittleEndian(k)
data, err := os.ReadFile(*sumtablepath)
if err != nil {
panic(err)
}
var sumtable [16]uint32
for i := range sumtable {
sumtable[i] = binary.LittleEndian.Uint32(data[i*4 : (i+1)*4])
}
listener, err := net.ListenUDP("udp", net.UDPAddrFromAddrPort(netip.MustParseAddrPort(*addr)))
if err != nil {
panic(err)
}
logrus.Infoln("listening on", listener.LocalAddr())
buf := make([]byte, *bufsz)
bufd := make([]byte, *bufsz+16)
for {
n, from, err := listener.ReadFromUDP(buf)
if err != nil {
break
}
logrus.Infoln("recv from", from)
if n == 0 {
logrus.Infoln("skip empty body")
continue
}
a, b := tk.DecryptLittleEndianTo(buf[:n], sumtable, bufd)
if a >= b || b == 0 || b-a <= 16+2 {
logrus.Infoln("skip dirty udp body")
continue
}
body := bytes.TrimSuffix(bufd[a:b], []byte{' '})
req, err := alpacapi.ParseWorkerRequest(body)
if err != nil {
logrus.Infoln("parse body err:", err)
continue
}
logrus.Infoln("get request:", &req)
repl := alpacapi.WorkerReply{
ID: uint32(req.ID),
IsPending: true,
Msg: "pending...",
}
go func() {
data := tk.EncryptLittleEndian(repl.Pack(), sumtable)
for i := 0; i < 8; i++ {
_, _ = listener.WriteToUDP(data, from)
time.Sleep(time.Second * 4)
}
}()
cmd := exec.Command(
*llamapath, "-m", *mpth, "-p",
fmt.Sprintf(prompt, req.Config.Role, req.Config.Default, req.Message),
"--ctx_size", "2048", "-b", "512", "--top_k", "10000",
"-t", strconv.Itoa(int(*threadcnt)),
)
buffer := bytes.NewBuffer(bufd[:0])
cmd.Stdout = buffer
// cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
logrus.Infoln("get reply err:", err)
continue
}
reply := buffer.String()
i := strings.LastIndex(reply, endmark)
if i <= 0 {
logrus.Infoln("get reply err: invalid prompt")
continue
}
reply = strings.TrimSpace(reply[i+len(endmark):])
b = strings.Index(reply, "\n")
if b > 0 {
reply = strings.TrimSpace(reply[:b])
}
b = strings.Index(reply, `\n`)
if b > 0 {
reply = strings.TrimSpace(reply[:b])
}
logrus.Infoln("get reply:", reply)
repl.IsPending = false
repl.Msg = reply
data = tk.EncryptLittleEndian(repl.Pack(), sumtable)
go func() {
for i := 0; i < 8; i++ {
_, _ = listener.WriteToUDP(data, from)
time.Sleep(time.Second)
}
}()
}
logrus.Fatal(err)
}