-
Notifications
You must be signed in to change notification settings - Fork 3
/
conversation.go
218 lines (185 loc) · 4.82 KB
/
conversation.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
package chatgptclient
import (
"fmt"
"time"
"github.com/go-zoox/core-utils/safe"
"github.com/go-zoox/core-utils/strings"
"github.com/go-zoox/logger"
openai "github.com/go-zoox/openai-client"
"github.com/go-zoox/uuid"
)
// Conversation is the conversation interface.
type Conversation interface {
Ask(question []byte, cfg ...*ConversationAskConfig) (answer []byte, err error)
IsQuestionAsked(id string) (err error)
//
ID() string
Messages() *safe.List
//
BuildPrompt() (prompt []byte, err error)
//
SetModel(model string) error
GetModel() string
}
type conversation struct {
client *client
id string
messages *safe.List
messagesMap *safe.Map
//
cfg *ConversationConfig
}
// ConversationConfig is the configuration for creating a new Conversation.
type ConversationConfig struct {
ID string
Context string
Language string
MaxMessages int
MaxAge time.Duration
MaxRequestResponseTokens int64
Model string `json:"model"`
Temperature float64 `json:"temperature"`
ChatGPTName string `json:"chatgpt_name"`
//
MaxRequestTokens int64
MaxResponseTokens int64
}
// ConversationAskConfig is the configuration for ask question.
type ConversationAskConfig struct {
ID string `json:"id"`
User string `json:"user"`
Temperature float64 `json:"temperature"`
CreatedAt time.Time `json:"created_at"`
}
// NewConversation creates a new Conversation.
func NewConversation(client *client, cfg *ConversationConfig) (Conversation, error) {
if cfg.ID == "" {
cfg.ID = uuid.V4()
}
// ensure language
if cfg.Language != "" {
cfg.Language = strings.ToUpper(cfg.Language)
// @TODO
if vc, ok := DefaultContextIntl[cfg.Language]; ok {
DefaultContext = vc
}
}
if cfg.Context == "" {
cfg.Context = DefaultContext
if cfg.Language != "" {
cfg.Context = fmt.Sprintf("%s\nLanuage: %s", cfg.Context, cfg.Language)
}
}
if cfg.MaxMessages == 0 {
cfg.MaxMessages = 100
}
c := &conversation{
client: client,
id: cfg.ID,
messages: safe.NewList(cfg.MaxMessages),
messagesMap: safe.NewMap(),
cfg: cfg,
}
if c.cfg.Model == "" {
c.SetModel(openai.ModelGPT_4)
// c.SetModel(openai.ModelGPT3_5Turbo)
} else {
c.SetModel(c.cfg.Model)
}
return c, nil
}
func (c *conversation) IsQuestionAsked(id string) (err error) {
if c.messagesMap.Has(id) {
return fmt.Errorf("duplicate message(id: %s) to ask", id)
}
return nil
}
func (c *conversation) Ask(question []byte, cfg ...*ConversationAskConfig) (answer []byte, err error) {
cfgX := &ConversationAskConfig{}
if len(cfg) > 0 && cfg[0] != nil {
cfgX = cfg[0]
}
if cfgX.ID == "" {
logger.Warnf("question id is recommand")
cfgX.ID = uuid.V4()
}
if cfgX.CreatedAt.IsZero() {
cfgX.CreatedAt = time.Now()
}
if c.messagesMap.Has(cfgX.ID) {
return nil, fmt.Errorf("duplicate message(id: %s) to ask", cfgX.ID)
}
c.messagesMap.Set(cfgX.ID, true)
c.messages.Push(&Message{
ID: cfgX.ID,
Text: string(question),
IsChatGPT: false,
ConversationID: c.id,
User: cfgX.User,
Role: "user",
CreatedAt: cfgX.CreatedAt,
})
prompt, err := c.BuildPrompt()
if err != nil {
return nil, fmt.Errorf("failed to build prompt: %v", err)
}
messages, err := c.BuildMessages()
if err != nil {
return nil, fmt.Errorf("failed to build messages: %v", err)
}
temperature := c.cfg.Temperature
if cfgX.Temperature != 0 {
temperature = cfgX.Temperature
}
answer, err = c.client.Ask(&AskConfig{
Model: c.cfg.Model,
Prompt: string(prompt),
Messages: messages,
//
MaxRequestResponseTokens: int(c.cfg.MaxRequestResponseTokens),
//
Temperature: temperature,
})
if err != nil {
return nil, fmt.Errorf("failed to ask: %v", err)
}
c.messages.Push(&Message{
ID: uuid.V4(),
Text: string(answer),
IsChatGPT: true,
ConversationID: c.id,
Role: "assistant",
})
return answer, nil
}
func (c *conversation) ID() string {
return c.id
}
func (c *conversation) Messages() *safe.List {
return c.messages
}
func (c *conversation) BuildPrompt() (prompt []byte, err error) {
return buildPrompt(
c.cfg.Context,
c.messages,
int(c.cfg.MaxRequestTokens),
c.cfg.ChatGPTName,
)
}
func (c *conversation) BuildMessages() (messages []*Message, err error) {
return buildMessages(
c.cfg.Context,
c.messages,
int(c.cfg.MaxRequestTokens),
)
}
func (c *conversation) GetModel() string {
return c.cfg.Model
}
func (c *conversation) SetModel(model string) error {
c.cfg.Model = model
// generate MaxRequestResponseTokens
c.cfg.MaxRequestResponseTokens = openai.GetMaxTokens(c.cfg.Model)
c.cfg.MaxRequestTokens = c.cfg.MaxRequestResponseTokens - c.cfg.MaxResponseTokens
return nil
}