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.
This commit is contained in:
n4ze3m 2024-11-10 13:29:41 +05:30
parent 9c7a3f5ddc
commit f52e3d564a

View File

@ -1,43 +1,46 @@
import { isCustomModel } from "@/db/models" import { isCustomModel } from "@/db/models"
import { HumanMessage, type MessageContent } from "@langchain/core/messages" import { HumanMessage, type MessageContent } from "@langchain/core/messages"
type HumanMessageType = { type HumanMessageType = {
content: MessageContent, content: MessageContent
model: string model: string
} }
export const humanMessageFormatter = ({ content, model }: HumanMessageType) => { export const humanMessageFormatter = ({ content, model }: HumanMessageType) => {
const isCustom = isCustomModel(model)
const isCustom = isCustomModel(model) if (isCustom) {
if (typeof content !== "string") {
if(isCustom) { if (content.length > 1) {
if(typeof content !== 'string') { // this means that we need to reformat the image_url
if(content.length > 1) { const newContent: MessageContent = [
// this means that we need to reformat the image_url {
const newContent: MessageContent = [ type: "text",
{ //@ts-ignore
type: "text", text: content[0].text
//@ts-ignore },
text: content[0].text {
}, type: "image_url",
{ image_url: {
type: "image_url", //@ts-ignore
image_url: { url: content[1].image_url
//@ts-ignore
url: content[1].image_url
}
}
]
return new HumanMessage({
content: newContent
})
} }
} }
} ]
return new HumanMessage({ return new HumanMessage({
content, content: newContent
}) })
} else {
return new HumanMessage({
//@ts-ignore
content: content[0].text
})
}
}
}
return new HumanMessage({
content
})
} }