feat: support custom models for messages

This commit introduces support for custom models in the message history generation process. Previously, the history would format messages using LangChain's standard message structure, which is not compatible with custom models. This change allows for correct history formatting regardless of the selected model type, enhancing compatibility and user experience.
This commit is contained in:
n4ze3m
2024-09-29 23:59:15 +05:30
parent c8620637f8
commit 192e3893bb
4 changed files with 148 additions and 80 deletions

View File

@@ -1,55 +1,66 @@
import { isCustomModel } from "@/db/models"
import {
HumanMessage,
AIMessage,
type MessageContent,
HumanMessage,
AIMessage,
type MessageContent
} from "@langchain/core/messages"
export const generateHistory = (
messages: {
role: "user" | "assistant" | "system"
content: string
image?: string
}[]
messages: {
role: "user" | "assistant" | "system"
content: string
image?: string
}[],
model: string
) => {
let history = []
for (const message of messages) {
if (message.role === "user") {
let content: MessageContent = [
{
type: "text",
text: message.content
}
]
if (message.image) {
content = [
{
type: "image_url",
image_url: message.image
},
{
type: "text",
text: message.content
}
]
let history = []
const isCustom = isCustomModel(model)
for (const message of messages) {
if (message.role === "user") {
let content: MessageContent = isCustom
? message.content
: [
{
type: "text",
text: message.content
}
history.push(
new HumanMessage({
content: content
})
)
} else if (message.role === "assistant") {
history.push(
new AIMessage({
content: [
{
type: "text",
text: message.content
}
]
})
)
}
]
if (message.image) {
content = [
{
type: "image_url",
image_url: !isCustom
? message.image
: {
url: message.image
}
},
{
type: "text",
text: message.content
}
]
}
history.push(
new HumanMessage({
content: content
})
)
} else if (message.role === "assistant") {
history.push(
new AIMessage({
content: isCustom
? message.content
: [
{
type: "text",
text: message.content
}
]
})
)
}
return history
}
}
return history
}

View File

@@ -0,0 +1,43 @@
import { isCustomModel } from "@/db/models"
import { HumanMessage, type MessageContent } from "@langchain/core/messages"
type HumanMessageType = {
content: MessageContent,
model: string
}
export const humanMessageFormatter = ({ content, model }: HumanMessageType) => {
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
})
}
}
}
return new HumanMessage({
content,
})
}