From f52e3d564acdb0cb83a9fe0de43876fa7752e518 Mon Sep 17 00:00:00 2001 From: n4ze3m Date: Sun, 10 Nov 2024 13:29:41 +0530 Subject: [PATCH] Fix: Handle image URLs in custom model responses Improves the formatting of image URLs within responses from custom models. This ensures that image URLs are correctly presented in the user interface. --- src/utils/human-message.tsx | 69 +++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/src/utils/human-message.tsx b/src/utils/human-message.tsx index 6712339..9c1200f 100644 --- a/src/utils/human-message.tsx +++ b/src/utils/human-message.tsx @@ -1,43 +1,46 @@ import { isCustomModel } from "@/db/models" import { HumanMessage, type MessageContent } from "@langchain/core/messages" - type HumanMessageType = { - content: MessageContent, - model: string + content: MessageContent + model: string } export const humanMessageFormatter = ({ content, model }: HumanMessageType) => { - - const isCustom = isCustomModel(model) + const isCustom = isCustomModel(model) - if(isCustom) { - if(typeof content !== 'string') { - if(content.length > 1) { - // this means that we need to reformat the image_url - const newContent: MessageContent = [ - { - type: "text", - //@ts-ignore - text: content[0].text - }, - { - type: "image_url", - image_url: { - //@ts-ignore - url: content[1].image_url - } - } - ] - - return new HumanMessage({ - content: newContent - }) + if (isCustom) { + if (typeof content !== "string") { + if (content.length > 1) { + // this means that we need to reformat the image_url + const newContent: MessageContent = [ + { + type: "text", + //@ts-ignore + text: content[0].text + }, + { + type: "image_url", + image_url: { + //@ts-ignore + url: content[1].image_url } - } + } + ] + + return new HumanMessage({ + content: newContent + }) + } else { + return new HumanMessage({ + //@ts-ignore + content: content[0].text + }) + } } - - return new HumanMessage({ - content, - }) -} \ No newline at end of file + } + + return new HumanMessage({ + content + }) +}