-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon.go
205 lines (166 loc) · 6.67 KB
/
common.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
package coze
// Message represents a message in conversation
type Message struct {
// The entity that sent this message.
Role MessageRole `json:"role"`
// The type of message.
Type MessageType `json:"type"`
// The content of the message. It supports various types of content, including plain text,
// multimodal (a mix of text, images, and files), message cards, and more.
Content string `json:"content"`
// The type of message content.
ContentType MessageContentType `json:"content_type"`
// Additional information when creating a message, and this additional information will also be
// returned when retrieving messages. Custom key-value pairs should be specified in Map object
// format, with a length of 16 key-value pairs. The length of the key should be between 1 and 64
// characters, and the length of the value should be between 1 and 512 characters.
MetaData map[string]string `json:"meta_data,omitempty"`
ID string `json:"id"`
ConversationID string `json:"conversation_id"`
// section_id is used to distinguish the context sections of the session history. The same section
// is one context.
SectionID string `json:"section_id"`
BotID string `json:"bot_id"`
ChatID string `json:"chat_id"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}
// BuildUserQuestionText builds a text message for user question
func BuildUserQuestionText(content string, metaData map[string]string) *Message {
return &Message{
Role: MessageRoleUser,
Type: MessageTypeQuestion,
Content: content,
ContentType: MessageContentTypeText,
MetaData: metaData,
}
}
// BuildUserQuestionObjects builds an object message for user question
func BuildUserQuestionObjects(objects []*MessageObjectString, metaData map[string]string) *Message {
return &Message{
Role: MessageRoleUser,
Type: MessageTypeQuestion,
Content: mustToJson(objects),
ContentType: MessageContentTypeObjectString,
MetaData: metaData,
}
}
// BuildAssistantAnswer builds an answer message from assistant
func BuildAssistantAnswer(content string, metaData map[string]string) *Message {
return &Message{
Role: MessageRoleAssistant,
Type: MessageTypeAnswer,
Content: content,
ContentType: MessageContentTypeText,
MetaData: metaData,
}
}
// MessageRole represents the role of message sender
type MessageRole string
const (
MessageRoleUnknown MessageRole = "unknown"
// MessageRoleUser Indicates that the content of the message is sent by the user.
MessageRoleUser MessageRole = "user"
// MessageRoleAssistant Indicates that the content of the message is sent by the bot.
MessageRoleAssistant MessageRole = "assistant"
)
func (m MessageRole) String() string {
return string(m)
}
// MessageType represents the type of message
type MessageType string
const (
// MessageTypeQuestion User input content.
MessageTypeQuestion MessageType = "question"
// MessageTypeAnswer The message content returned by the Bot to the user, supporting incremental return.
MessageTypeAnswer MessageType = "answer"
// MessageTypeFunctionCall Intermediate results of the function (function call) called during the
// Bot conversation process.
MessageTypeFunctionCall MessageType = "function_call"
// MessageTypeToolOutput Results returned after calling the tool (function call).
MessageTypeToolOutput MessageType = "tool_output"
// MessageTypeToolResponse Results returned after calling the tool (function call).
MessageTypeToolResponse MessageType = "tool_response"
// MessageTypeFollowUp If the user question suggestion switch is turned on in the Bot configuration,
// the reply content related to the recommended questions will be returned.
MessageTypeFollowUp MessageType = "follow_up"
MessageTypeUnknown MessageType = ""
)
// MessageContentType represents the type of message content
type MessageContentType string
const (
// MessageContentTypeText Text.
MessageContentTypeText MessageContentType = "text"
// MessageContentTypeObjectString Multimodal content, that is, a combination of text and files,
// or a combination of text and images.
MessageContentTypeObjectString MessageContentType = "object_string"
// MessageContentTypeCard This enum value only appears in the interface response and is not supported as an
// input parameter.
MessageContentTypeCard MessageContentType = "card"
// MessageContentTypeAudio If there is a audioVoices message in the input message,
// the conversation.audio.delta event will be returned in the streaming response event.
MessageContentTypeAudio MessageContentType = "audio"
)
// MessageObjectString represents a multimodal message object
type MessageObjectString struct {
// The content type of the multimodal message.
Type MessageObjectStringType `json:"type"`
// Text content. Required when type is text.
Text string `json:"text,omitempty"`
// The ID of the file or image content.
FileID string `json:"file_id,omitempty"`
// The online address of the file or image content. Must be a valid address that is publicly
// accessible. file_id or file_url must be specified when type is file or image.
FileURL string `json:"file_url,omitempty"`
}
// MessageObjectStringType represents the type of multimodal message content
type MessageObjectStringType string
const (
MessageObjectStringTypeText MessageObjectStringType = "text"
MessageObjectStringTypeFile MessageObjectStringType = "file"
MessageObjectStringTypeImage MessageObjectStringType = "image"
MessageObjectStringTypeAudio MessageObjectStringType = "audio"
)
// NewTextMessageObject Helper functions for creating MessageObjectString
func NewTextMessageObject(text string) *MessageObjectString {
return &MessageObjectString{
Type: MessageObjectStringTypeText,
Text: text,
}
}
func NewImageMessageObjectByURL(fileURL string) *MessageObjectString {
return &MessageObjectString{
Type: MessageObjectStringTypeImage,
FileURL: fileURL,
}
}
func NewImageMessageObjectByID(fileID string) *MessageObjectString {
return &MessageObjectString{
Type: MessageObjectStringTypeImage,
FileID: fileID,
}
}
func NewFileMessageObjectByID(fileID string) *MessageObjectString {
return &MessageObjectString{
Type: MessageObjectStringTypeFile,
FileID: fileID,
}
}
func NewFileMessageObjectByURL(fileURL string) *MessageObjectString {
return &MessageObjectString{
Type: MessageObjectStringTypeFile,
FileURL: fileURL,
}
}
func NewAudioMessageObjectByID(fileID string) *MessageObjectString {
return &MessageObjectString{
Type: MessageObjectStringTypeAudio,
FileID: fileID,
}
}
func NewAudioMessageObjectByURL(fileURL string) *MessageObjectString {
return &MessageObjectString{
Type: MessageObjectStringTypeAudio,
FileURL: fileURL,
}
}