Skip to content

Commit 24bac8e

Browse files
committed
update: openai struct
1 parent fb5b1a5 commit 24bac8e

File tree

2 files changed

+84
-81
lines changed

2 files changed

+84
-81
lines changed

pkg/claude/chat.go

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -143,51 +143,57 @@ func ChatMessages(c *gin.Context, chatReq *openai.ChatCompletionRequest) {
143143
claudReq.Model = ""
144144
}
145145

146-
var prompt string
147-
148146
var claudecontent []VisionContent
147+
var prompt string
149148
for _, msg := range chatReq.Messages {
150-
if msg.Role == "system" {
151-
claudReq.System = string(msg.Content)
152-
continue
153-
}
154-
155-
var oaivisioncontent []openai.VisionContent
156-
if err := json.Unmarshal(msg.Content, &oaivisioncontent); err != nil {
157-
prompt += "<" + msg.Role + ">: " + string(msg.Content) + "\n"
158-
159-
claudecontent = append(claudecontent, VisionContent{Type: "text", Text: msg.Role + ":" + string(msg.Content)})
160-
} else {
161-
if len(oaivisioncontent) > 0 {
162-
for _, content := range oaivisioncontent {
163-
if content.Type == "text" {
164-
prompt += "<" + msg.Role + ">: " + content.Text + "\n"
165-
claudecontent = append(claudecontent, VisionContent{Type: "text", Text: msg.Role + ":" + content.Text})
166-
} else if content.Type == "image_url" {
167-
if strings.HasPrefix(content.ImageURL.URL, "http") {
168-
fmt.Println("链接:", content.ImageURL.URL)
169-
} else if strings.HasPrefix(content.ImageURL.URL, "data:image") {
170-
fmt.Println("base64:", content.ImageURL.URL[:20])
171-
}
172-
// todo image tokens
173-
var mediaType string
174-
if strings.HasPrefix(content.ImageURL.URL, "data:image/jpeg") {
175-
mediaType = "image/jpeg"
176-
}
177-
if strings.HasPrefix(content.ImageURL.URL, "data:image/png") {
178-
mediaType = "image/png"
149+
switch ct := msg.Content.(type) {
150+
case string:
151+
prompt += "<" + msg.Role + ">: " + msg.Content.(string) + "\n"
152+
if msg.Role == "system" {
153+
claudReq.System = msg.Content.(string)
154+
continue
155+
}
156+
claudecontent = append(claudecontent, VisionContent{Type: "text", Text: msg.Role + ":" + msg.Content.(string)})
157+
case []any:
158+
for _, item := range ct {
159+
if m, ok := item.(map[string]interface{}); ok {
160+
if m["type"] == "text" {
161+
prompt += "<" + msg.Role + ">: " + m["text"].(string) + "\n"
162+
claudecontent = append(claudecontent, VisionContent{Type: "text", Text: msg.Role + ":" + m["text"].(string)})
163+
} else if m["type"] == "image_url" {
164+
if url, ok := m["image_url"].(map[string]interface{}); ok {
165+
fmt.Printf(" URL: %v\n", url["url"])
166+
if strings.HasPrefix(url["url"].(string), "http") {
167+
fmt.Println("网络图片:", url["url"].(string))
168+
} else if strings.HasPrefix(url["url"].(string), "data:image") {
169+
fmt.Println("base64:", url["url"].(string)[:20])
170+
var mediaType string
171+
if strings.HasPrefix(url["url"].(string), "data:image/jpeg") {
172+
mediaType = "image/jpeg"
173+
}
174+
if strings.HasPrefix(url["url"].(string), "data:image/png") {
175+
mediaType = "image/png"
176+
}
177+
claudecontent = append(claudecontent, VisionContent{Type: "image", Source: &VisionSource{Type: "base64", MediaType: mediaType, Data: strings.Split(url["url"].(string), ",")[1]}})
178+
}
179179
}
180-
claudecontent = append(claudecontent, VisionContent{Type: "image", Source: &VisionSource{Type: "base64", MediaType: mediaType, Data: strings.Split(content.ImageURL.URL, ",")[1]}})
181180
}
182181
}
183-
184182
}
183+
default:
184+
c.JSON(http.StatusInternalServerError, gin.H{
185+
"error": gin.H{
186+
"message": "Invalid content type",
187+
},
188+
})
189+
return
190+
}
191+
if len(chatReq.Tools) > 0 {
192+
tooljson, _ := json.Marshal(chatReq.Tools)
193+
prompt += "<tools>: " + string(tooljson) + "\n"
185194
}
186-
// if len(chatReq.Tools) > 0 {
187-
// tooljson, _ := json.Marshal(chatReq.Tools)
188-
// prompt += "<tools>: " + string(tooljson) + "\n"
189-
// }
190195
}
196+
191197
claudReq.Messages = []VisionMessages{{Role: "user", Content: claudecontent}}
192198

193199
usagelog.PromptCount = tokenizer.NumTokensFromStr(prompt, chatReq.Model)

pkg/google/chat.go

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -97,59 +97,55 @@ func ChatProxy(c *gin.Context, chatReq *openai.ChatCompletionRequest) {
9797
var prompts []genai.Part
9898
var prompt string
9999
for _, msg := range chatReq.Messages {
100-
var visioncontent []openai.VisionContent
101-
if err := json.Unmarshal(msg.Content, &visioncontent); err != nil {
102-
prompt += "<" + msg.Role + ">: " + string(msg.Content) + "\n"
103-
prompts = append(prompts, genai.Text("<"+msg.Role+">: "+string(msg.Content)))
104-
} else {
105-
if len(visioncontent) > 0 {
106-
for _, content := range visioncontent {
107-
if content.Type == "text" {
108-
prompt += "<" + msg.Role + ">: " + content.Text + "\n"
109-
prompts = append(prompts, genai.Text("<"+msg.Role+">: "+content.Text))
110-
} else if content.Type == "image_url" {
111-
if strings.HasPrefix(content.ImageURL.URL, "http") {
112-
fmt.Println("链接:", content.ImageURL.URL)
113-
} else if strings.HasPrefix(content.ImageURL.URL, "data:image") {
114-
fmt.Println("base64:", content.ImageURL.URL[:20])
115-
if chatReq.Model != "gemini-pro-vision" {
116-
chatReq.Model = "gemini-pro-vision"
100+
switch ct := msg.Content.(type) {
101+
case string:
102+
prompt += "<" + msg.Role + ">: " + msg.Content.(string) + "\n"
103+
prompts = append(prompts, genai.Text("<"+msg.Role+">: "+msg.Content.(string)))
104+
case []any:
105+
for _, item := range ct {
106+
if m, ok := item.(map[string]interface{}); ok {
107+
if m["type"] == "text" {
108+
prompt += "<" + msg.Role + ">: " + m["text"].(string) + "\n"
109+
prompts = append(prompts, genai.Text("<"+msg.Role+">: "+m["text"].(string)))
110+
} else if m["type"] == "image_url" {
111+
if url, ok := m["image_url"].(map[string]interface{}); ok {
112+
if strings.HasPrefix(url["url"].(string), "http") {
113+
fmt.Println("网络图片:", url["url"].(string))
114+
} else if strings.HasPrefix(url["url"].(string), "data:image") {
115+
fmt.Println("base64:", url["url"].(string)[:20])
116+
var mime string
117+
// openai 会以 data:image 开头,则去掉 data:image/png;base64, 和 data:image/jpeg;base64,
118+
if strings.HasPrefix(url["url"].(string), "data:image/png") {
119+
mime = "image/png"
120+
} else if strings.HasPrefix(url["url"].(string), "data:image/jpeg") {
121+
mime = "image/jpeg"
122+
} else {
123+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unsupported image format"})
124+
return
125+
}
126+
imageString := strings.Split(url["url"].(string), ",")[1]
127+
imageBytes, err := base64.StdEncoding.DecodeString(imageString)
128+
if err != nil {
129+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
130+
return
131+
}
132+
prompts = append(prompts, genai.Blob{MIMEType: mime, Data: imageBytes})
117133
}
118-
119-
var mime string
120-
// openai 会以 data:image 开头,则去掉 data:image/png;base64, 和 data:image/jpeg;base64,
121-
if strings.HasPrefix(content.ImageURL.URL, "data:image/png") {
122-
mime = "image/png"
123-
} else if strings.HasPrefix(content.ImageURL.URL, "data:image/jpeg") {
124-
mime = "image/jpeg"
125-
} else {
126-
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unsupported image format"})
127-
return
128-
}
129-
imageString := strings.Split(content.ImageURL.URL, ",")[1]
130-
imageBytes, err := base64.StdEncoding.DecodeString(imageString)
131-
if err != nil {
132-
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
133-
return
134-
}
135-
prompts = append(prompts, genai.Blob{MIMEType: mime, Data: imageBytes})
136134
}
137-
138-
// todo image tokens
139135
}
140-
141136
}
142-
143137
}
138+
default:
139+
c.JSON(http.StatusInternalServerError, gin.H{
140+
"error": gin.H{
141+
"message": "Invalid content type",
142+
},
143+
})
144+
return
144145
}
145146
if len(chatReq.Tools) > 0 {
146147
tooljson, _ := json.Marshal(chatReq.Tools)
147148
prompt += "<tools>: " + string(tooljson) + "\n"
148-
149-
// for _, tool := range chatReq.Tools {
150-
151-
// }
152-
153149
}
154150
}
155151

@@ -171,6 +167,7 @@ func ChatProxy(c *gin.Context, chatReq *openai.ChatCompletionRequest) {
171167
defer client.Close()
172168

173169
model := client.GenerativeModel(chatReq.Model)
170+
model.Tools = []*genai.Tool{}
174171

175172
iter := model.GenerateContentStream(ctx, prompts...)
176173
datachan := make(chan string)

0 commit comments

Comments
 (0)