From 9ec22fc115f39f930072000eb91f40a00317c8b1 Mon Sep 17 00:00:00 2001 From: Kevin On <40454531+kevin-on@users.noreply.github.com> Date: Thu, 5 Dec 2024 14:36:00 +0900 Subject: [PATCH] fix: add empty message filtering for anthropic (#156) --- src/core/llm/anthropic.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/core/llm/anthropic.ts b/src/core/llm/anthropic.ts index 7ab256e..9575f7d 100644 --- a/src/core/llm/anthropic.ts +++ b/src/core/llm/anthropic.ts @@ -46,7 +46,9 @@ export class AnthropicProvider implements BaseLLMProvider { ) } - const systemMessage = this.validateSystemMessages(request.messages) + const systemMessage = AnthropicProvider.validateSystemMessages( + request.messages, + ) try { const response = await this.client.messages.create( @@ -54,6 +56,7 @@ export class AnthropicProvider implements BaseLLMProvider { model: request.model, messages: request.messages .filter((m) => m.role !== 'system') + .filter((m) => !AnthropicProvider.isMessageEmpty(m)) .map((m) => AnthropicProvider.parseRequestMessage(m)), system: systemMessage, max_tokens: @@ -89,7 +92,9 @@ export class AnthropicProvider implements BaseLLMProvider { ) } - const systemMessage = this.validateSystemMessages(request.messages) + const systemMessage = AnthropicProvider.validateSystemMessages( + request.messages, + ) try { const stream = await this.client.messages.create( @@ -97,6 +102,7 @@ export class AnthropicProvider implements BaseLLMProvider { model: request.model, messages: request.messages .filter((m) => m.role !== 'system') + .filter((m) => !AnthropicProvider.isMessageEmpty(m)) .map((m) => AnthropicProvider.parseRequestMessage(m)), system: systemMessage, max_tokens: @@ -261,7 +267,7 @@ export class AnthropicProvider implements BaseLLMProvider { } } - private validateSystemMessages( + private static validateSystemMessages( messages: RequestMessage[], ): string | undefined { const systemMessages = messages.filter((m) => m.role === 'system') @@ -278,6 +284,13 @@ export class AnthropicProvider implements BaseLLMProvider { return systemMessage } + private static isMessageEmpty(message: RequestMessage) { + if (typeof message.content === 'string') { + return message.content.trim() === '' + } + return message.content.length === 0 + } + private static validateImageType(mimeType: string) { const SUPPORTED_IMAGE_TYPES = [ 'image/jpeg',