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,19 +1,17 @@
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 (isCustom) {
if(typeof content !== 'string') { if (typeof content !== "string") {
if(content.length > 1) { if (content.length > 1) {
// this means that we need to reformat the image_url // this means that we need to reformat the image_url
const newContent: MessageContent = [ const newContent: MessageContent = [
{ {
@ -33,11 +31,16 @@ export const humanMessageFormatter = ({ content, model }: HumanMessageType) => {
return new HumanMessage({ return new HumanMessage({
content: newContent content: newContent
}) })
} else {
return new HumanMessage({
//@ts-ignore
content: content[0].text
})
} }
} }
} }
return new HumanMessage({ return new HumanMessage({
content, content
}) })
} }