Spaces:
Paused
Paused
File size: 6,153 Bytes
48d903a 5a55e77 48d903a 5a55e77 48d903a 5a55e77 48d903a 5a55e77 48d903a 8646505 48d903a 5a55e77 48d903a 5a55e77 48d903a | 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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | package model
import "encoding/json"
// OpenAI 格式的消息内容项
type ContentPart struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
ImageURL *ImageURL `json:"image_url,omitempty"`
}
type ImageURL struct {
URL string `json:"url"`
}
// Tool 工具定义(OpenAI 兼容)
type Tool struct {
Type string `json:"type"`
Function ToolFunction `json:"function"`
}
// ToolFunction 函数定义
type ToolFunction struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Parameters interface{} `json:"parameters,omitempty"`
}
// ToolCall 模型返回的工具调用
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function FunctionCall `json:"function"`
Index int `json:"index"`
}
// FunctionCall 函数调用(名称 + 参数 JSON 字符串)
type FunctionCall struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
}
// Message 支持纯文本和多模态内容
type Message struct {
Role string `json:"role"`
Content interface{} `json:"content"` // string 或 []ContentPart
ToolCallID string `json:"tool_call_id,omitempty"` // role: "tool" 时使用
ToolCalls []ToolCall `json:"tool_calls,omitempty"` // role: "assistant" 时使用
}
// 解析消息内容,返回文本和图片URL列表
func (m *Message) ParseContent() (text string, imageURLs []string) {
switch content := m.Content.(type) {
case string:
return content, nil
case []interface{}:
for _, item := range content {
if part, ok := item.(map[string]interface{}); ok {
partType, _ := part["type"].(string)
if partType == "text" {
if t, ok := part["text"].(string); ok {
text += t
}
} else if partType == "image_url" {
if imgURL, ok := part["image_url"].(map[string]interface{}); ok {
if url, ok := imgURL["url"].(string); ok {
imageURLs = append(imageURLs, url)
}
}
}
}
}
}
return text, imageURLs
}
// 转换为上游消息格式,支持多模态
func (m *Message) ToUpstreamMessage(urlToFileID map[string]string) map[string]interface{} {
// tool 消息:包含 tool_call_id
if m.Role == "tool" {
msg := map[string]interface{}{
"role": m.Role,
"content": m.Content,
"tool_call_id": m.ToolCallID,
}
return msg
}
// assistant 消息带 tool_calls
if m.Role == "assistant" && len(m.ToolCalls) > 0 {
msg := map[string]interface{}{
"role": m.Role,
"content": m.Content,
}
var toolCalls []map[string]interface{}
for _, tc := range m.ToolCalls {
toolCalls = append(toolCalls, map[string]interface{}{
"id": tc.ID,
"type": tc.Type,
"function": map[string]interface{}{
"name": tc.Function.Name,
"arguments": tc.Function.Arguments,
},
})
}
msg["tool_calls"] = toolCalls
return msg
}
text, imageURLs := m.ParseContent()
// 无图片,返回纯文本
if len(imageURLs) == 0 {
return map[string]interface{}{
"role": m.Role,
"content": text,
}
}
// 有图片,构建多模态内容
var content []interface{}
if text != "" {
content = append(content, map[string]interface{}{
"type": "text",
"text": text,
})
}
for _, imgURL := range imageURLs {
if fileID, ok := urlToFileID[imgURL]; ok {
content = append(content, map[string]interface{}{
"type": "image_url",
"image_url": map[string]interface{}{
"url": fileID,
},
})
}
}
return map[string]interface{}{
"role": m.Role,
"content": content,
}
}
type ChatRequest struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
Stream bool `json:"stream"`
Tools []Tool `json:"tools,omitempty"`
ToolChoice interface{} `json:"tool_choice,omitempty"`
}
type ChatCompletionChunk struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []Choice `json:"choices"`
}
type Choice struct {
Index int `json:"index"`
Delta *Delta `json:"delta,omitempty"`
Message *MessageResp `json:"message,omitempty"`
FinishReason *string `json:"finish_reason"`
}
type Delta struct {
Content string `json:"content,omitempty"`
ReasoningContent string `json:"reasoning_content,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
}
type MessageResp struct {
Role string `json:"role"`
Content string `json:"content"`
ReasoningContent string `json:"reasoning_content,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
}
type ChatCompletionResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []Choice `json:"choices"`
}
type ModelsResponse struct {
Object string `json:"object"`
Data []ModelInfo `json:"data"`
}
type ModelInfo struct {
ID string `json:"id"`
Object string `json:"object"`
OwnedBy string `json:"owned_by"`
}
// SearchResult 搜索结果
type SearchResult struct {
Title string `json:"title"`
URL string `json:"url"`
Index int `json:"index"`
RefID string `json:"ref_id"`
}
// ImageSearchResult 图片搜索结果
type ImageSearchResult struct {
Title string `json:"title"`
Link string `json:"link"`
Thumbnail string `json:"thumbnail"`
}
// UpstreamData 上游返回的数据结构
type UpstreamData struct {
Type string `json:"type"`
Data struct {
DeltaContent string `json:"delta_content"`
EditContent string `json:"edit_content"`
Phase string `json:"phase"`
Done bool `json:"done"`
} `json:"data"`
}
func (u *UpstreamData) GetEditContent() string {
editContent := u.Data.EditContent
if editContent == "" {
return ""
}
if len(editContent) > 0 && editContent[0] == '"' {
var unescaped string
if err := json.Unmarshal([]byte(editContent), &unescaped); err == nil {
return unescaped
}
}
return editContent
}
|