From 6a5ad0e5d09ea8f2fde7eea7b0a663c98eb45342 Mon Sep 17 00:00:00 2001 From: Peter Zaback Date: Sat, 16 Mar 2024 16:33:19 -0500 Subject: [PATCH] Fix chat msg formatting in Output for image models (#971) --- core/llm/index.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/core/llm/index.ts b/core/llm/index.ts index e6df8e2ba..8a5ecb442 100644 --- a/core/llm/index.ts +++ b/core/llm/index.ts @@ -29,6 +29,7 @@ import { compileChatMessages, countTokens, pruneRawPromptFromTop, + stripImages, } from "./countTokens"; import CompletionOptionsForModels from "./templates/options"; @@ -245,8 +246,13 @@ ${prompt}`; } private _formatChatMessages(messages: ChatMessage[]): string { + const msgsCopy = messages ? messages.map((msg) => ({ ...msg })) : []; let formatted = ""; - for (let msg of messages) { + for (let msg of msgsCopy) { + if ("content" in msg && Array.isArray(msg.content)) { + const content = stripImages(msg.content); + msg.content = content; + } formatted += `<${msg.role}>\n${msg.content || ""}\n\n`; } return formatted;