/
chat.go
326 lines (284 loc) · 9.24 KB
/
chat.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package sparkclient
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/gorilla/websocket"
"github.com/iflytek/spark-ai-go/log"
"github.com/iflytek/spark-ai-go/sparkai/messages"
"io/ioutil"
"net/http"
"time"
)
var (
defaultDomain = "general"
defaultTemperature float64 = float64(0.8)
defaultTopK = int64(6)
defaultMaxTokens = int64(2048)
)
var ErrContentExclusive = errors.New("only one of Content / MultiContent allowed in message")
// ChatRequest is a request to complete a chat completion..
type ChatRequest struct {
Domain *string `json:"domain"`
Messages []messages.ChatMessage `json:"messages"`
Temperature *float64 `json:"temperature,omitempty"`
TopK *int64 `json:"top_p,omitempty"`
MaxTokens *int64 `json:"max_tokens,omitempty"`
Audit *string `json:"audit,omitempty"`
Functions []messages.FunctionDefinition `json:"functions,omitempty"`
//// Function definitions to include in the request.
//// FunctionCallBehavior is the behavior to use when calling functions.
////
//// If a specific function should be invoked, use the format:
//// `{"name": "my_function"}`
//FunctionCallBehavior FunctionCallBehavior `json:"function_call,omitempty"`
//
//// StreamingFunc is a function to be called for each chunk of a streaming response.
//// Return an error to stop streaming early.
//StreamingFunc func(ctx context.Context, chunk []byte) error `json:"-"`
}
// ChatMessage is a message in a chat request.
type ChatMessage struct { //nolint:musttag
// The role of the author of this message. One of system, user, or assistant.
Role string
// The content of the message.
Content string
//MultiContent []llms.ContentPart
//
//// The name of the author of this message. May contain a-z, A-Z, 0-9, and underscores,
//// with a maximum length of 64 characters.
//Name string
// FunctionCall represents a function call to be made in the message.
FunctionCall *messages.FunctionCall
}
func (m ChatMessage) GetType() messages.ChatMessageType {
return messages.ChatMessageType(m.Role)
}
func (m ChatMessage) GetContent() string {
return m.Content
}
func (m ChatMessage) MarshalJSON() ([]byte, error) {
msg := struct {
Role string `json:"role"`
Content string `json:"content"`
FunctionCall *messages.FunctionCall `json:"function_call"`
}{
Role: m.Role,
Content: m.Content,
FunctionCall: m.FunctionCall,
}
return json.Marshal(msg)
}
func (m *ChatMessage) UnmarshalJSON(data []byte) error {
msg := struct {
Role string `json:"role"`
Content string `json:"content"`
FunctionCall *messages.FunctionCall `json:"function_call"`
}{}
err := json.Unmarshal(data, &msg)
if err != nil {
return err
}
*m = ChatMessage(msg)
return nil
}
// ChatChoice is a choice in a chat response.
type ChatChoice struct {
Index int `json:"index"`
Message ChatMessage `json:"message"`
FinishReason string `json:"finish_reason"`
}
// ChatUsage is the usage of a chat completion request.
type ChatUsage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
// ChatResponse is a response to a chat request.
type ChatResponse struct {
//Choices []*ChatChoice `json:"choices,omitempty"`
Role string `json:"role"`
Content string `json:"content,omitempty"`
FunctionCall *messages.FunctionCall `json:"function_call"`
Usage struct {
CompletionTokens float64 `json:"completion_tokens,omitempty"`
PromptTokens float64 `json:"prompt_tokens,omitempty"`
TotalTokens float64 `json:"total_tokens,omitempty"`
} `json:"usage,omitempty"`
}
func (c *ChatResponse) GetType() messages.ChatMessageType {
if c.FunctionCall != nil {
return messages.ChatMessageTypeFunction
}
return messages.ChatMessageType(c.Role)
}
func (c *ChatResponse) GetContent() string {
return c.Content
}
func (c *ChatResponse) UpdateContent(msg string) {
c.Content = msg
}
// StreamedChatResponsePayload is a chunk from the stream.
type StreamedChatResponsePayload struct {
ID string `json:"id,omitempty"`
Created float64 `json:"created,omitempty"`
Model string `json:"model,omitempty"`
Object string `json:"object,omitempty"`
Choices []struct {
Index float64 `json:"index,omitempty"`
Delta struct {
Role string `json:"role,omitempty"`
Content string `json:"content,omitempty"`
FunctionCall *FunctionCall `json:"function_call,omitempty"`
} `json:"delta,omitempty"`
FinishReason string `json:"finish_reason,omitempty"`
} `json:"choices,omitempty"`
}
// FunctionDefinition is a definition of a function that can be called by the model.
type FunctionDefinition struct {
// Name is the name of the function.
Name string `json:"name"`
// Description is a description of the function.
Description string `json:"description"`
// Parameters is a list of parameters for the function.
Parameters any `json:"parameters"`
}
// FunctionCallBehavior is the behavior to use when calling functions.
type FunctionCallBehavior string
const (
// FunctionCallBehaviorUnspecified is the empty string.
FunctionCallBehaviorUnspecified FunctionCallBehavior = ""
// FunctionCallBehaviorNone will not call any functions.
FunctionCallBehaviorNone FunctionCallBehavior = "none"
// FunctionCallBehaviorAuto will call functions automatically.
FunctionCallBehaviorAuto FunctionCallBehavior = "auto"
)
// FunctionCall is a call to a function.
type FunctionCall struct {
// Name is the name of the function to call.
Name string `json:"name"`
// Arguments is the set of arguments to pass to the function.
Arguments string `json:"arguments"`
}
func (c *Client) createChat(ctx context.Context, payload *ChatRequest, cb func(msg messages.ChatMessage) error) (messages.ChatMessage, error) {
// Build request payload
// Build request
if c.baseURL == "" {
return nil, errors.New("No API Url set")
}
d := websocket.Dialer{
HandshakeTimeout: 5 * time.Second,
}
ua_str := ""
user_agent := ctx.Value("user_agent")
if user_agent != nil {
ua_str = user_agent.(string)
}
//握手并建立websocket 连接
conn, resp, err := d.Dial(c.assembleAuthUrl1(c.baseURL, c.apiKey, c.apiSecret), map[string][]string{"User-Agent": []string{fmt.Sprintf("SparkAISdk/golang %s", ua_str)}})
if err != nil {
return nil, errors.New(err.Error())
} else if resp.StatusCode != 101 {
return nil, errors.New(GetSparkErrorMsgByCode(ErrorUpgradeToWs))
}
go func() {
data := c.constructSparkReq(c.appId, payload)
conn.WriteJSON(data)
}()
var answer = ""
var code int
// Parse response
var response messages.ChatMessage
//获取返回的数据
for {
_, msg, err := conn.ReadMessage()
if err != nil {
fmt.Println("read message error:", err)
break
}
var sparkResp = messages.SparkResponse{}
err1 := json.Unmarshal(msg, &sparkResp)
if err1 != nil {
return nil, errors.New(err1.Error())
}
//fmt.Println(string(msg))
//解析数据
//header := data["header"].(map[string]interface{})
code = sparkResp.Header.Code
if code != 0 {
//fmt.Println(data["payload"])
return nil, errors.New(fmt.Sprintf("code: %d, message: %s\n", code, GetSparkErrorMsg(code)))
}
payload := &sparkResp.Payload
//choices := payload["choices"].(map[string]interface{})
choices := payload.Choices
status := choices.Status
text := choices.Text
content := text[0].Content
role := text[0].Role
fc := text[0].FunctionCall
if fc != nil {
response = messages.AIChatMessage{
Content: fc.GetContent(),
FunctionCall: fc,
}
} else {
response = messages.GenericChatMessage{
Content: content,
Role: role,
}
}
// 处理 cb todo cb 规范化
if cb != nil {
err = cb(response)
if err != nil {
fmt.Println("callback error ")
}
}
if status != 2 {
answer += content
} else {
answer += content
usage := payload.Usage
//totalTokens = temp["total_tokens"].(float64)
//fmt.Println("total_tokens:", totalTokens)
if fc == nil {
response = &ChatResponse{
Role: role,
Content: answer,
FunctionCall: fc,
Usage: struct {
CompletionTokens float64 `json:"completion_tokens,omitempty"`
PromptTokens float64 `json:"prompt_tokens,omitempty"`
TotalTokens float64 `json:"total_tokens,omitempty"`
}{
CompletionTokens: usage.CompletionTokens,
PromptTokens: usage.PromptTokens,
TotalTokens: usage.TotalTokens,
},
}
}
log.GetLogger().Info("Sid: ", sparkResp.Header.Sid)
conn.Close()
break
}
}
if code != 0 {
msg := fmt.Sprintf("API returned unexpected status code: %d \n message: %s\n", code, GetSparkErrorMsg(code))
// No need to check the error here: if it fails, we'll just return the
// status code.
return nil, fmt.Errorf("%s", msg) // nolint:goerr113
}
return response, nil
}
func readResp(resp *http.Response) string {
if resp == nil {
return ""
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
return fmt.Sprintf("code=%d,body=%s", resp.StatusCode, string(b))
}