Anthropic: fix #618

This commit is contained in:
Enrico Ros
2024-08-12 15:03:33 -07:00
parent 8ab35a5fab
commit dfe6d3cf72
2 changed files with 12 additions and 6 deletions
@@ -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}`);
}
@@ -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