From dfe6d3cf72e1fa34fe2f14a3d5d9e3ccfe3d0490 Mon Sep 17 00:00:00 2001 From: Enrico Ros Date: Mon, 12 Aug 2024 15:03:33 -0700 Subject: [PATCH] Anthropic: fix #618 --- .../adapters/anthropic.messageCreate.ts | 13 ++++++++----- .../dispatch/wiretypes/anthropic.wiretypes.ts | 5 ++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/modules/aix/server/dispatch/chatGenerate/adapters/anthropic.messageCreate.ts b/src/modules/aix/server/dispatch/chatGenerate/adapters/anthropic.messageCreate.ts index e695776f4..ef1d33ea8 100644 --- a/src/modules/aix/server/dispatch/chatGenerate/adapters/anthropic.messageCreate.ts +++ b/src/modules/aix/server/dispatch/chatGenerate/adapters/anthropic.messageCreate.ts @@ -6,6 +6,7 @@ import { AnthropicWire_API_Message_Create, AnthropicWire_Blocks } from '../../wi // configuration const hackyHotFixStartWithUser = true; // "Bad Request - messages: first message must use the "user" role" +const hotFixDownmixSystemText = true; const hotFixImagePartsFirst = true; const hotFixMapModelImagesToUser = true; const hotFixMissingTokens = 4096; // [2024-07-12] max from https://docs.anthropic.com/en/docs/about-claude/models @@ -16,9 +17,10 @@ type TRequest = AnthropicWire_API_Message_Create.Request; export function aixToAnthropicMessageCreate(model: AixAPI_Model, chatGenerate: AixAPIChatGenerate_Request, streaming: boolean): TRequest { // Convert the system message - const systemMessage: TRequest['system'] = chatGenerate.systemMessage?.parts.length - ? chatGenerate.systemMessage.parts.map((part) => AnthropicWire_Blocks.TextBlock(part.text)) - : undefined; + const systemMessage: TRequest['system'] = + !chatGenerate.systemMessage?.parts.length ? undefined + : hotFixDownmixSystemText ? chatGenerate.systemMessage.parts.map(part => part.text).join('\n') + : chatGenerate.systemMessage.parts.map((part) => AnthropicWire_Blocks.TextBlock(part.text)); // Transform the chat messages into Anthropic's format const chatMessages: TRequest['messages'] = []; @@ -37,8 +39,9 @@ export function aixToAnthropicMessageCreate(model: AixAPI_Model, chatGenerate: A chatMessages.push(currentMessage); // If the first (user) message is missing, copy the first line of the system message - if (hackyHotFixStartWithUser && chatMessages.length && chatMessages[0].role !== 'user' && systemMessage?.length) { - const hackSystemMessageFirstLine = (systemMessage[0]?.text || '').split('\n')[0]; + if (hackyHotFixStartWithUser && chatMessages.length && chatMessages[0].role !== 'user' && systemMessage) { + const hackSystemMessageFirstLine = typeof systemMessage === 'string' ? systemMessage.split('\n')[0] + : systemMessage[0]?.text?.split('\n')[0] || ''; chatMessages.unshift({ role: 'user', content: [AnthropicWire_Blocks.TextBlock(hackSystemMessageFirstLine)] }); console.log(`Anthropic: hotFixStartWithUser (${chatMessages.length} messages) - ${hackSystemMessageFirstLine}`); } diff --git a/src/modules/aix/server/dispatch/wiretypes/anthropic.wiretypes.ts b/src/modules/aix/server/dispatch/wiretypes/anthropic.wiretypes.ts index 1c2ce39c6..3ffa26412 100644 --- a/src/modules/aix/server/dispatch/wiretypes/anthropic.wiretypes.ts +++ b/src/modules/aix/server/dispatch/wiretypes/anthropic.wiretypes.ts @@ -129,7 +129,10 @@ export namespace AnthropicWire_API_Message_Create { /** * If you want to include a system prompt, you can use the top-level system parameter — there is no "system" role for input messages in the Messages API. */ - system: z.array(AnthropicWire_Blocks.TextBlock_schema).optional(), + system: z.union([ + z.array(AnthropicWire_Blocks.TextBlock_schema), + z.string(), // only adding this for: https://github.com/enricoros/big-AGI/issues/618 - we shall just have parts in the future + ]).optional(), /** * (required) Input messages. - operates on alternating user and assistant conversational turns - the first message must always use the user role