2024-02-17 16:15:31 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
type Message struct {
|
2024-07-01 16:12:01 +00:00
|
|
|
Role string `json:"role,omitempty"`
|
|
|
|
Content any `json:"content,omitempty"`
|
|
|
|
Name *string `json:"name,omitempty"`
|
|
|
|
ToolCalls []Tool `json:"tool_calls,omitempty"`
|
|
|
|
ToolCallId string `json:"tool_call_id,omitempty"`
|
2024-02-17 16:15:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m Message) IsStringContent() bool {
|
|
|
|
_, ok := m.Content.(string)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Message) StringContent() string {
|
|
|
|
content, ok := m.Content.(string)
|
|
|
|
if ok {
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
contentList, ok := m.Content.([]any)
|
|
|
|
if ok {
|
|
|
|
var contentStr string
|
|
|
|
for _, contentItem := range contentList {
|
|
|
|
contentMap, ok := contentItem.(map[string]any)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if contentMap["type"] == ContentTypeText {
|
|
|
|
if subStr, ok := contentMap["text"].(string); ok {
|
|
|
|
contentStr += subStr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return contentStr
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Message) ParseContent() []MessageContent {
|
|
|
|
var contentList []MessageContent
|
|
|
|
content, ok := m.Content.(string)
|
|
|
|
if ok {
|
|
|
|
contentList = append(contentList, MessageContent{
|
|
|
|
Type: ContentTypeText,
|
|
|
|
Text: content,
|
|
|
|
})
|
|
|
|
return contentList
|
|
|
|
}
|
|
|
|
anyList, ok := m.Content.([]any)
|
|
|
|
if ok {
|
|
|
|
for _, contentItem := range anyList {
|
|
|
|
contentMap, ok := contentItem.(map[string]any)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch contentMap["type"] {
|
|
|
|
case ContentTypeText:
|
|
|
|
if subStr, ok := contentMap["text"].(string); ok {
|
|
|
|
contentList = append(contentList, MessageContent{
|
|
|
|
Type: ContentTypeText,
|
|
|
|
Text: subStr,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
case ContentTypeImageURL:
|
|
|
|
if subObj, ok := contentMap["image_url"].(map[string]any); ok {
|
|
|
|
contentList = append(contentList, MessageContent{
|
|
|
|
Type: ContentTypeImageURL,
|
|
|
|
ImageURL: &ImageURL{
|
|
|
|
Url: subObj["url"].(string),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return contentList
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ImageURL struct {
|
|
|
|
Url string `json:"url,omitempty"`
|
|
|
|
Detail string `json:"detail,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type MessageContent struct {
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
ImageURL *ImageURL `json:"image_url,omitempty"`
|
|
|
|
}
|