-
Notifications
You must be signed in to change notification settings - Fork 45
/
chat.go
208 lines (184 loc) · 5.47 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
package openai
import (
"encoding/json"
"fmt"
"github.com/yaklang/yaklang/common/ai/aispec"
"strconv"
"strings"
"time"
"github.com/samber/lo"
"github.com/yaklang/yaklang/common/consts"
"github.com/yaklang/yaklang/common/jsonextractor"
"github.com/yaklang/yaklang/common/log"
"github.com/yaklang/yaklang/common/utils"
"github.com/yaklang/yaklang/common/utils/lowhttp"
"github.com/yaklang/yaklang/common/utils/lowhttp/poc"
)
type Client struct {
Proxy string
APIKey string
Organization string
ChatModel string
// Role in Org! public model, the role is user
Role string
Domain string
// function call
Functions []aispec.Function
Parameters aispec.Parameters
}
type Session struct {
client *Client
messages []aispec.ChatDetail
}
func NewSession(opt ...ConfigOption) *Session {
return &Session{
client: NewOpenAIClient(opt...),
messages: make([]aispec.ChatDetail, 0),
}
}
func (s *Session) Chat(message aispec.ChatDetail, opts ...ConfigOption) (aispec.ChatDetails, error) {
c := NewRawOpenAIClient(opts...)
// if the message is a tool call, and the tool call ID is not set, set it to the latest tool call ID
if message.Role == "tool" && message.ToolCallID == "" && len(s.messages) > 0 {
latestMessage := s.messages[len(s.messages)-1]
for _, toolCall := range latestMessage.ToolCalls {
if toolCall == nil || toolCall.ID == "" {
continue
}
message.ToolCallID = toolCall.ID
break
}
}
s.messages = append(s.messages, message)
choices, err := s.client.ChatEx(s.messages, c.Functions...)
details := lo.Map(choices, func(c aispec.ChatChoice, _ int) aispec.ChatDetail {
return c.Message
})
s.messages = append(s.messages, details...)
return details, err
}
func NewRawOpenAIClient(opts ...ConfigOption) *Client {
c := &Client{
Functions: make([]aispec.Function, 0),
Parameters: aispec.Parameters{
Type: "object",
Properties: make(map[string]aispec.Property),
},
}
for _, o := range opts {
o(c)
}
return c
}
func NewOpenAIClient(opts ...ConfigOption) *Client {
c := &Client{
Functions: make([]aispec.Function, 0),
Parameters: aispec.Parameters{
Type: "object",
Properties: make(map[string]aispec.Property),
},
}
for _, o := range opts {
o(c)
}
config := consts.GetThirdPartyApplicationConfig("openai")
if config.APIKey != "" && c.APIKey == "" {
verbose := "sk-...."
if len(config.APIKey) > 10 {
verbose = config.APIKey[:10] + "..."
}
log.OnceInfoLog("ai-config-apikey", "use openai apikey config: %v", verbose)
c.APIKey = config.APIKey
}
if model := config.GetExtraParam("model"); model != "" && c.ChatModel == "" {
c.ChatModel = model
}
if domain := config.GetExtraParam("domain"); domain != "" && c.Domain == "" {
c.Domain = utils.ExtractHostPort(domain)
}
if proxy := config.GetExtraParam("proxy"); proxy != "" && c.Proxy == "" {
log.OnceInfoLog("ai-config-proxy", "use openai apikey config: %v", proxy)
c.Proxy = proxy
}
if c.APIKey == "" {
log.Warn("openai api key is empty")
}
return c
}
func (c *Client) TranslateToChinese(data string) (string, error) {
prompt := fmt.Sprintf(`把下面内容翻译成中文并放在JSON中(以result存结果):\n%v`, strconv.Quote(data))
results, err := c.Chat(prompt)
if err != nil {
return "", err
}
transData, _ := jsonextractor.ExtractJSONWithRaw(results)
if len(transData) > 0 {
raw := jsonextractor.FixJson([]byte(transData[0]))
data := make(map[string]string)
err := json.Unmarshal(raw, &data)
if err != nil {
return "", err
}
return utils.MapGetString2(data, "result"), nil
}
return strings.Trim(results, "\r\n \v\f\""), nil
}
func (c *Client) ChatEx(messages []aispec.ChatDetail, funcs ...aispec.Function) ([]aispec.ChatChoice, error) {
chatModel := c.ChatModel
if chatModel == "" {
chatModel = "gpt-3.5-turbo"
}
role := c.Role
if role == "" {
role = "user"
}
domain := "api.openai.com"
if c.Domain != "" {
domain = c.Domain
}
c.Functions = append(c.Functions, funcs...)
chatMessage := aispec.NewChatMessage(chatModel, messages, c.Functions...)
raw, err := json.Marshal(chatMessage)
if err != nil {
return nil, err
}
rsp, _, err := poc.DoPOST(
fmt.Sprintf("https://%v/v1/chat/completions", domain),
poc.WithReplaceHttpPacketHeader("Authorization", fmt.Sprintf("Bearer %v", c.APIKey)),
poc.WithReplaceHttpPacketHeader("Content-Type", "application/json"),
poc.WithReplaceHttpPacketBody(raw, false),
poc.WithProxy(c.Proxy),
poc.WithConnPool(true),
)
if err != nil {
return nil, utils.Wrapf(err, "OpenAI Chat failed: http error")
}
rspRaw := lowhttp.GetHTTPPacketBody(rsp.RawPacket)
var comp aispec.ChatCompletion
err = json.Unmarshal(rspRaw, &comp)
if err != nil {
log.Errorf("OpenAI Chat Error: unmarshal completion failed: %#v", string(rspRaw))
return nil, utils.Wrapf(err, "unmarshal completion failed")
}
if comp.Error != nil && comp.Error.Message != "" {
errorMsg := comp.Error.Message
if strings.Contains(errorMsg, "increase your rate limit") {
log.Infof("reach rate limit... sleep 7s")
time.Sleep(7 * time.Second) // 20 / min
}
return nil, utils.Errorf("OpenAI Chat Error: %s", errorMsg)
}
return comp.Choices, nil
}
func (c *Client) Chat(data string, funcs ...aispec.Function) (string, error) {
choices, err := c.ChatEx([]aispec.ChatDetail{
{
Role: "user",
Content: data,
},
}, funcs...)
if err != nil {
return "", err
}
return aispec.DetailsToString(lo.Map(choices, func(c aispec.ChatChoice, index int) aispec.ChatDetail { return c.Message })), nil
}