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