@@ -281,10 +281,7 @@ func toMessages(request types.CompletionRequest, compat bool) (result []openai.C
281
281
chatMessage .ToolCalls = append (chatMessage .ToolCalls , toToolCall (* content .ToolCall ))
282
282
}
283
283
if content .Text != "" {
284
- chatMessage .MultiContent = append (chatMessage .MultiContent , openai.ChatMessagePart {
285
- Type : openai .ChatMessagePartTypeText ,
286
- Text : content .Text ,
287
- })
284
+ chatMessage .MultiContent = append (chatMessage .MultiContent , textToMultiContent (content .Text )... )
288
285
}
289
286
}
290
287
@@ -306,6 +303,35 @@ func toMessages(request types.CompletionRequest, compat bool) (result []openai.C
306
303
return
307
304
}
308
305
306
+ const imagePrefix = "data:image/png;base64,"
307
+
308
+ func textToMultiContent (text string ) []openai.ChatMessagePart {
309
+ var chatParts []openai.ChatMessagePart
310
+ parts := strings .Split (text , "\n " )
311
+ for i := len (parts ) - 1 ; i >= 0 ; i -- {
312
+ if strings .HasPrefix (parts [i ], imagePrefix ) {
313
+ chatParts = append (chatParts , openai.ChatMessagePart {
314
+ Type : openai .ChatMessagePartTypeImageURL ,
315
+ ImageURL : & openai.ChatMessageImageURL {
316
+ URL : parts [i ],
317
+ },
318
+ })
319
+ parts = parts [:i ]
320
+ } else {
321
+ break
322
+ }
323
+ }
324
+ if len (parts ) > 0 {
325
+ chatParts = append (chatParts , openai.ChatMessagePart {
326
+ Type : openai .ChatMessagePartTypeText ,
327
+ Text : strings .Join (parts , "\n " ),
328
+ })
329
+ }
330
+
331
+ slices .Reverse (chatParts )
332
+ return chatParts
333
+ }
334
+
309
335
func (c * Client ) Call (ctx context.Context , messageRequest types.CompletionRequest , env []string , status chan <- types.CompletionStatus ) (* types.CompletionMessage , error ) {
310
336
if err := c .ValidAuth (); err != nil {
311
337
if err := c .RetrieveAPIKey (ctx , env ); err != nil {
0 commit comments