Skip to content

Commit

Permalink
fix: add empty message filtering for anthropic (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-on authored Dec 5, 2024
1 parent 160377c commit 9ec22fc
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/core/llm/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,17 @@ 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(
{
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:
Expand Down Expand Up @@ -89,14 +92,17 @@ 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(
{
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:
Expand Down Expand Up @@ -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')
Expand All @@ -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',
Expand Down

0 comments on commit 9ec22fc

Please sign in to comment.