@@ -97,59 +97,55 @@ func ChatProxy(c *gin.Context, chatReq *openai.ChatCompletionRequest) {
97
97
var prompts []genai.Part
98
98
var prompt string
99
99
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 })
117
133
}
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 })
136
134
}
137
-
138
- // todo image tokens
139
135
}
140
-
141
136
}
142
-
143
137
}
138
+ default :
139
+ c .JSON (http .StatusInternalServerError , gin.H {
140
+ "error" : gin.H {
141
+ "message" : "Invalid content type" ,
142
+ },
143
+ })
144
+ return
144
145
}
145
146
if len (chatReq .Tools ) > 0 {
146
147
tooljson , _ := json .Marshal (chatReq .Tools )
147
148
prompt += "<tools>: " + string (tooljson ) + "\n "
148
-
149
- // for _, tool := range chatReq.Tools {
150
-
151
- // }
152
-
153
149
}
154
150
}
155
151
@@ -171,6 +167,7 @@ func ChatProxy(c *gin.Context, chatReq *openai.ChatCompletionRequest) {
171
167
defer client .Close ()
172
168
173
169
model := client .GenerativeModel (chatReq .Model )
170
+ model .Tools = []* genai.Tool {}
174
171
175
172
iter := model .GenerateContentStream (ctx , prompts ... )
176
173
datachan := make (chan string )
0 commit comments