-
Notifications
You must be signed in to change notification settings - Fork 0
/
implement_qwen.go
264 lines (221 loc) · 6.74 KB
/
implement_qwen.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package pkg_ai
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"github.com/jinzhu/copier"
"io"
)
/**
* 【通义千问】qwen
* Doc : https://help.aliyun.com/zh/dashscope/developer-reference/use-qwen
*/
type QwenConf struct {
Url string `json:"url"`
Key string `json:"key"`
}
func NewQwenConf(url, key string) *Config {
return &Config{QwenUrl: url, QwenKey: key}
}
type QwenServer struct {
Conf QwenConf `json:"conf"`
}
func newQwenServer(url, key string) *QwenServer {
return &QwenServer{
Conf: QwenConf{
Url: url,
Key: key,
},
}
}
func (q *QwenServer) Supplier() string {
return "qwen"
}
func (q *QwenServer) RequestPath() string {
return q.Conf.Url
}
type QwenRequestBody struct {
Messages []Message `json:"messages"`
Model string `json:"model"`
MaxTokens int64 `json:"max_tokens,omitempty"`
Temperature float64 `json:"temperature,omitempty"`
TopP float64 `json:"top_p,omitempty"`
StreamOptions *StreamOptions `json:"stream_options,omitempty"`
Stream bool `json:"stream"`
}
func (q *QwenServer) build(data RequestData, isStream bool) ([]byte, error) {
if data.UserQuery == "" || data.Model == "" {
return []byte{}, errors.New("问题、模型为必传字段")
}
request := &QwenRequestBody{Stream: isStream, Messages: make([]Message, 0)}
if err := copier.Copy(request, &data); err != nil {
return nil, err
}
if data.SystemQuery != "" {
request.Messages = append(request.Messages, Message{Role: MessageSystem, Content: data.SystemQuery})
}
if data.History != nil && len(data.History) > 0 {
for _, detail := range data.History {
request.Messages = append(request.Messages, Message{Role: MessageUSer, Content: detail[0]})
request.Messages = append(request.Messages, Message{Role: MessageAssistant, Content: detail[1]})
}
}
request.Messages = append(request.Messages, Message{Role: MessageUSer, Content: data.UserQuery})
if isStream {
request.StreamOptions = &StreamOptions{IncludeUsage: true}
}
return json.Marshal(request)
}
type QwenChatResponse struct {
Choices []struct {
Message struct {
Role string `json:"role"`
Content string `json:"content"`
} `json:"message"`
FinishReason string `json:"finish_reason"`
Index int64 `json:"index"`
Logprobs interface{} `json:"logprobs"`
} `json:"choices"`
Object string `json:"object"`
Usage struct {
PromptTokens int64 `json:"prompt_tokens"`
CompletionTokens int64 `json:"completion_tokens"`
TotalTokens int64 `json:"total_tokens"`
} `json:"usage"`
Created int64 `json:"created"`
SystemFingerprint interface{} `json:"system_fingerprint"`
Model string `json:"model"`
Id string `json:"id"`
Error struct {
Message string `json:"message"`
Type string `json:"type"`
} `json:"error"`
}
func (q *QwenServer) Chat(requestPath string, data []byte) (*Response, error) {
headers := map[string]string{"Authorization": "Bearer " + q.Conf.Key, "Content-Type": "application/json"}
ret := &Response{RequestHeader: make([]byte, 0), RequestBody: data, ResponseData: make([][]byte, 0)}
ret.RequestHeader, _ = json.Marshal(headers)
response, err := postBase(requestPath, string(data), headers)
if err != nil {
return ret, err
}
defer func() {
_ = response.Body.Close()
}()
retBytes, err := io.ReadAll(response.Body)
ret.ResponseData = append(ret.ResponseData, retBytes)
if err != nil {
return ret, err
}
retStruct := QwenChatResponse{}
if err := json.Unmarshal(retBytes, &retStruct); err != nil {
return ret, err
}
ret.RequestId = retStruct.Id
ret.PromptTokens = retStruct.Usage.PromptTokens
ret.CompletionTokens = retStruct.Usage.CompletionTokens
if len(retStruct.Error.Message) > 0 {
return ret, errors.New(retStruct.Error.Message)
}
if len(retStruct.Choices) == 0 {
return ret, errors.New("无有效响应数据")
}
ret.ResponseText = retStruct.Choices[0].Message.Content
return ret, nil
}
type QwenResponse struct {
Choices []struct {
FinishReason string `json:"finish_reason"`
Delta struct {
Content string `json:"content"`
} `json:"delta"`
Index int64 `json:"index"`
Logprobs interface{} `json:"logprobs"`
} `json:"choices"`
Object string `json:"object"`
Usage struct {
PromptTokens int64 `json:"prompt_tokens"`
CompletionTokens int64 `json:"completion_tokens"`
TotalTokens int64 `json:"total_tokens"`
} `json:"usage"`
Created int64 `json:"created"`
SystemFingerprint interface{} `json:"system_fingerprint"`
Model string `json:"model"`
Id string `json:"id"`
Error struct {
Message string `json:"message"`
Type string `json:"type"`
} `json:"error"`
}
type QwenErrorInfo struct {
Error struct {
Message string `json:"message"`
Type string `json:"type"`
} `json:"error"`
}
func (q *QwenServer) ChatStream(requestPath string, data []byte, msgCh chan string, errChan chan error) (*Response, error) {
headers := map[string]string{"Authorization": "Bearer " + q.Conf.Key, "Content-Type": "application/json"}
ret := &Response{RequestHeader: make([]byte, 0), RequestBody: data, ResponseData: make([][]byte, 0)}
ret.RequestHeader, _ = json.Marshal(headers)
response, err := postBase(requestPath, string(data), headers)
if err != nil {
errChan <- err
return ret, err
}
defer func() {
_ = response.Body.Close()
}()
reader := bufio.NewReader(response.Body)
for {
line, err := reader.ReadBytes('\n')
ret.ResponseData = append(ret.ResponseData, line)
line = bytes.TrimSuffix(line, []byte("\n"))
if err != nil {
if errors.Is(err, io.EOF) {
resErr := err
errStruct := &QwenErrorInfo{}
if err := json.Unmarshal(line, errStruct); err == nil {
resErr = errors.New(errStruct.Error.Message)
}
errChan <- resErr
return ret, resErr
}
errChan <- err
return ret, err
}
if string(line) == "" {
continue
}
headerData := []byte("data: ")
if !bytes.HasPrefix(line, headerData) {
continue
}
line = bytes.TrimPrefix(line, headerData)
if string(line) == "[DONE]" {
close(msgCh)
break
}
retStruct := QwenResponse{}
if err := json.Unmarshal(line, &retStruct); err != nil {
errChan <- err
return ret, err
}
if len(retStruct.Error.Message) > 0 {
err := errors.New(retStruct.Error.Message)
errChan <- err
return ret, err
}
if retStruct.Usage.TotalTokens > 0 {
ret.RequestId = retStruct.Id
ret.PromptTokens = retStruct.Usage.PromptTokens
ret.CompletionTokens = retStruct.Usage.CompletionTokens
}
if len(retStruct.Choices) == 0 {
continue
}
ret.ResponseText += retStruct.Choices[0].Delta.Content
msgCh <- retStruct.Choices[0].Delta.Content
}
return ret, nil
}