Include chat, weather, image, travel, streaming ASR and text examples, shared model initialization, documentation, and existing tests. Co-authored-by: TRAE CLI <traecli@bytedance.com>
71 lines
1.2 KiB
Go
71 lines
1.2 KiB
Go
package model
|
|
|
|
import "strings"
|
|
|
|
type ChatInputType string
|
|
|
|
const (
|
|
ChatInputTypeTxt ChatInputType = "text"
|
|
ChatInputTypeImg ChatInputType = "image"
|
|
)
|
|
|
|
type ChatInput struct {
|
|
Type ChatInputType
|
|
// 文字为原文,图片为图片地址或 base64 data URL。
|
|
Data string
|
|
}
|
|
|
|
type ChatInputs []*ChatInput
|
|
|
|
// Text 返回用于保存历史的文字,图片用占位文字表示。
|
|
func (i ChatInputs) Text() string {
|
|
var texts []string
|
|
for _, input := range i {
|
|
if input == nil {
|
|
continue
|
|
}
|
|
switch input.Type {
|
|
case ChatInputTypeTxt:
|
|
texts = append(texts, input.Data)
|
|
case ChatInputTypeImg:
|
|
texts = append(texts, "[图片]")
|
|
}
|
|
}
|
|
return strings.Join(texts, "\n")
|
|
}
|
|
|
|
func (i ChatInputs) Format() *ChatMessage {
|
|
if len(i) == 0 {
|
|
return nil
|
|
}
|
|
|
|
cs := make([]any, 0)
|
|
|
|
for _, item := range i {
|
|
if item == nil || item.Data == "" {
|
|
continue
|
|
}
|
|
switch item.Type {
|
|
case ChatInputTypeTxt:
|
|
cs = append(cs, map[string]string{
|
|
"type": "text",
|
|
"text": item.Data,
|
|
})
|
|
case ChatInputTypeImg:
|
|
cs = append(cs, map[string]any{
|
|
"type": "image_url",
|
|
"image_url": map[string]string{"url": item.Data},
|
|
})
|
|
}
|
|
}
|
|
|
|
if len(cs) == 0 {
|
|
return nil
|
|
}
|
|
|
|
return &ChatMessage{
|
|
Role: "user",
|
|
Content: cs,
|
|
}
|
|
}
|