From e3206ff19b850d40dd9cbbe53d9b7a93418bf622 Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Mon, 8 Apr 2024 15:13:20 +0200 Subject: [PATCH] ai/core: remove provider field from errors (#1296) --- .../anthropic-messages-language-model.ts | 7 +------ .../convert-to-anthropic-messages-prompt.ts | 11 +++-------- .../convert-to-google-generative-ai-messages.ts | 11 +++-------- .../google-generative-ai-language-model.ts | 8 +------- .../mistral/convert-to-mistral-chat-messages.ts | 11 +++-------- .../core/mistral/mistral-chat-language-model.ts | 6 +----- .../convert-to-openai-completion-prompt.ts | 5 ----- .../core/openai/openai-chat-language-model.ts | 1 - .../openai/openai-completion-language-model.ts | 10 +--------- .../spec/errors/invalid-response-data-error.ts | 4 ++-- .../errors/unsupported-functionality-error.ts | 16 ++-------------- .../spec/errors/unsupported-json-schema-error.ts | 7 ------- 12 files changed, 17 insertions(+), 80 deletions(-) diff --git a/packages/core/anthropic/anthropic-messages-language-model.ts b/packages/core/anthropic/anthropic-messages-language-model.ts index b6ce34f2dbd3..bafd33f1f744 100644 --- a/packages/core/anthropic/anthropic-messages-language-model.ts +++ b/packages/core/anthropic/anthropic-messages-language-model.ts @@ -83,10 +83,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV1 { }); } - const messagesPrompt = convertToAnthropicMessagesPrompt({ - provider: this.provider, - prompt, - }); + const messagesPrompt = convertToAnthropicMessagesPrompt(prompt); const baseArgs = { // model id: @@ -126,7 +123,6 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV1 { case 'object-json': { throw new UnsupportedFunctionalityError({ functionality: 'json-mode object generation', - provider: this.provider, }); } @@ -151,7 +147,6 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV1 { case 'object-grammar': { throw new UnsupportedFunctionalityError({ functionality: 'grammar-mode object generation', - provider: this.provider, }); } diff --git a/packages/core/anthropic/convert-to-anthropic-messages-prompt.ts b/packages/core/anthropic/convert-to-anthropic-messages-prompt.ts index d61fffc58684..a23b554c02f3 100644 --- a/packages/core/anthropic/convert-to-anthropic-messages-prompt.ts +++ b/packages/core/anthropic/convert-to-anthropic-messages-prompt.ts @@ -8,13 +8,9 @@ import { AnthropicMessagesPrompt, } from './anthropic-messages-prompt'; -export function convertToAnthropicMessagesPrompt({ - prompt, - provider, -}: { - prompt: LanguageModelV1Prompt; - provider: string; -}): AnthropicMessagesPrompt { +export function convertToAnthropicMessagesPrompt( + prompt: LanguageModelV1Prompt, +): AnthropicMessagesPrompt { let system: string | undefined; const messages: AnthropicMessage[] = []; @@ -36,7 +32,6 @@ export function convertToAnthropicMessagesPrompt({ case 'image': { if (part.image instanceof URL) { throw new UnsupportedFunctionalityError({ - provider, functionality: 'URL image parts', }); } else { diff --git a/packages/core/google/convert-to-google-generative-ai-messages.ts b/packages/core/google/convert-to-google-generative-ai-messages.ts index 561c69a30b84..824247a75918 100644 --- a/packages/core/google/convert-to-google-generative-ai-messages.ts +++ b/packages/core/google/convert-to-google-generative-ai-messages.ts @@ -8,13 +8,9 @@ import { GoogleGenerativeAIPrompt, } from './google-generative-ai-prompt'; -export function convertToGoogleGenerativeAIMessages({ - prompt, - provider, -}: { - prompt: LanguageModelV1Prompt; - provider: string; -}): GoogleGenerativeAIPrompt { +export function convertToGoogleGenerativeAIMessages( + prompt: LanguageModelV1Prompt, +): GoogleGenerativeAIPrompt { const messages: GoogleGenerativeAIPrompt = []; for (const { role, content } of prompt) { @@ -40,7 +36,6 @@ export function convertToGoogleGenerativeAIMessages({ case 'image': { if (part.image instanceof URL) { throw new UnsupportedFunctionalityError({ - provider, functionality: 'URL image parts', }); } else { diff --git a/packages/core/google/google-generative-ai-language-model.ts b/packages/core/google/google-generative-ai-language-model.ts index ba5febba960e..d9af8588a739 100644 --- a/packages/core/google/google-generative-ai-language-model.ts +++ b/packages/core/google/google-generative-ai-language-model.ts @@ -96,10 +96,7 @@ export class GoogleGenerativeAILanguageModel implements LanguageModelV1 { }, // prompt: - contents: convertToGoogleGenerativeAIMessages({ - provider: this.provider, - prompt, - }), + contents: convertToGoogleGenerativeAIMessages(prompt), }; switch (type) { @@ -125,21 +122,18 @@ export class GoogleGenerativeAILanguageModel implements LanguageModelV1 { case 'object-json': { throw new UnsupportedFunctionalityError({ functionality: 'object-json mode', - provider: this.provider, }); } case 'object-tool': { throw new UnsupportedFunctionalityError({ functionality: 'object-tool mode', - provider: this.provider, }); } case 'object-grammar': { throw new UnsupportedFunctionalityError({ functionality: 'object-grammar mode', - provider: this.provider, }); } diff --git a/packages/core/mistral/convert-to-mistral-chat-messages.ts b/packages/core/mistral/convert-to-mistral-chat-messages.ts index 127b25109701..fb9de1fe1b30 100644 --- a/packages/core/mistral/convert-to-mistral-chat-messages.ts +++ b/packages/core/mistral/convert-to-mistral-chat-messages.ts @@ -1,13 +1,9 @@ import { LanguageModelV1Prompt, UnsupportedFunctionalityError } from '../spec'; import { MistralChatPrompt } from './mistral-chat-prompt'; -export function convertToMistralChatMessages({ - prompt, - provider, -}: { - prompt: LanguageModelV1Prompt; - provider: string; -}): MistralChatPrompt { +export function convertToMistralChatMessages( + prompt: LanguageModelV1Prompt, +): MistralChatPrompt { const messages: MistralChatPrompt = []; for (const { role, content } of prompt) { @@ -28,7 +24,6 @@ export function convertToMistralChatMessages({ } case 'image': { throw new UnsupportedFunctionalityError({ - provider, functionality: 'image-part', }); } diff --git a/packages/core/mistral/mistral-chat-language-model.ts b/packages/core/mistral/mistral-chat-language-model.ts index b3da693f4ea8..1018988359b5 100644 --- a/packages/core/mistral/mistral-chat-language-model.ts +++ b/packages/core/mistral/mistral-chat-language-model.ts @@ -90,10 +90,7 @@ export class MistralChatLanguageModel implements LanguageModelV1 { random_seed: seed, // messages: - messages: convertToMistralChatMessages({ - provider: this.provider, - prompt, - }), + messages: convertToMistralChatMessages(prompt), }; switch (type) { @@ -141,7 +138,6 @@ export class MistralChatLanguageModel implements LanguageModelV1 { case 'object-grammar': { throw new UnsupportedFunctionalityError({ functionality: 'object-grammar mode', - provider: this.provider, }); } diff --git a/packages/core/openai/convert-to-openai-completion-prompt.ts b/packages/core/openai/convert-to-openai-completion-prompt.ts index f77c7d7dcef7..61d12b4f41ae 100644 --- a/packages/core/openai/convert-to-openai-completion-prompt.ts +++ b/packages/core/openai/convert-to-openai-completion-prompt.ts @@ -7,13 +7,11 @@ import { export function convertToOpenAICompletionPrompt({ prompt, inputFormat, - provider, user = 'user', assistant = 'assistant', }: { prompt: LanguageModelV1Prompt; inputFormat: 'prompt' | 'messages'; - provider: string; user?: string; assistant?: string; }): { @@ -58,7 +56,6 @@ export function convertToOpenAICompletionPrompt({ } case 'image': { throw new UnsupportedFunctionalityError({ - provider, functionality: 'images', }); } @@ -79,7 +76,6 @@ export function convertToOpenAICompletionPrompt({ } case 'tool-call': { throw new UnsupportedFunctionalityError({ - provider, functionality: 'tool-call messages', }); } @@ -93,7 +89,6 @@ export function convertToOpenAICompletionPrompt({ case 'tool': { throw new UnsupportedFunctionalityError({ - provider, functionality: 'tool messages', }); } diff --git a/packages/core/openai/openai-chat-language-model.ts b/packages/core/openai/openai-chat-language-model.ts index ffd91f2e2fa0..db7f724424fe 100644 --- a/packages/core/openai/openai-chat-language-model.ts +++ b/packages/core/openai/openai-chat-language-model.ts @@ -131,7 +131,6 @@ export class OpenAIChatLanguageModel implements LanguageModelV1 { case 'object-grammar': { throw new UnsupportedFunctionalityError({ functionality: 'object-grammar mode', - provider: this.provider, }); } diff --git a/packages/core/openai/openai-completion-language-model.ts b/packages/core/openai/openai-completion-language-model.ts index 96fbd4205245..2f8afc12d964 100644 --- a/packages/core/openai/openai-completion-language-model.ts +++ b/packages/core/openai/openai-completion-language-model.ts @@ -60,11 +60,7 @@ export class OpenAICompletionLanguageModel implements LanguageModelV1 { const type = mode.type; const { prompt: completionPrompt, stopSequences } = - convertToOpenAICompletionPrompt({ - prompt, - inputFormat, - provider: this.provider, - }); + convertToOpenAICompletionPrompt({ prompt, inputFormat }); const baseArgs = { // model id: @@ -112,7 +108,6 @@ export class OpenAICompletionLanguageModel implements LanguageModelV1 { if (mode.tools?.length) { throw new UnsupportedFunctionalityError({ functionality: 'tools', - provider: this.provider, }); } @@ -122,21 +117,18 @@ export class OpenAICompletionLanguageModel implements LanguageModelV1 { case 'object-json': { throw new UnsupportedFunctionalityError({ functionality: 'object-json mode', - provider: this.provider, }); } case 'object-tool': { throw new UnsupportedFunctionalityError({ functionality: 'object-tool mode', - provider: this.provider, }); } case 'object-grammar': { throw new UnsupportedFunctionalityError({ functionality: 'object-grammar mode', - provider: this.provider, }); } diff --git a/packages/core/spec/errors/invalid-response-data-error.ts b/packages/core/spec/errors/invalid-response-data-error.ts index fcca7b9aa6c3..2b3ad136c15c 100644 --- a/packages/core/spec/errors/invalid-response-data-error.ts +++ b/packages/core/spec/errors/invalid-response-data-error.ts @@ -1,6 +1,6 @@ /** - * Server returned a response with invalid data content. This should be thrown by providers when they - * cannot parse the response from the API. +Server returned a response with invalid data content. This should be thrown by providers when they +cannot parse the response from the API. */ export class InvalidResponseDataError extends Error { readonly data: unknown; diff --git a/packages/core/spec/errors/unsupported-functionality-error.ts b/packages/core/spec/errors/unsupported-functionality-error.ts index 1d33d7bcb4ad..ee66a36b7156 100644 --- a/packages/core/spec/errors/unsupported-functionality-error.ts +++ b/packages/core/spec/errors/unsupported-functionality-error.ts @@ -1,21 +1,11 @@ export class UnsupportedFunctionalityError extends Error { readonly functionality: string; - readonly provider: string; - constructor({ - provider, - functionality, - }: { - provider: string; - functionality: string; - }) { - super( - `'${functionality}' functionality not supported by the '${provider}' provider.`, - ); + constructor({ functionality }: { functionality: string }) { + super(`'${functionality}' functionality not supported.`); this.name = 'AI_UnsupportedFunctionalityError'; - this.provider = provider; this.functionality = functionality; } @@ -25,7 +15,6 @@ export class UnsupportedFunctionalityError extends Error { return ( error instanceof Error && error.name === 'AI_UnsupportedFunctionalityError' && - typeof (error as UnsupportedFunctionalityError).provider === 'string' && typeof (error as UnsupportedFunctionalityError).functionality === 'string' ); } @@ -36,7 +25,6 @@ export class UnsupportedFunctionalityError extends Error { message: this.message, stack: this.stack, - provider: this.provider, functionality: this.functionality, }; } diff --git a/packages/core/spec/errors/unsupported-json-schema-error.ts b/packages/core/spec/errors/unsupported-json-schema-error.ts index d40c35aa6ab3..4ed6e7d8675e 100644 --- a/packages/core/spec/errors/unsupported-json-schema-error.ts +++ b/packages/core/spec/errors/unsupported-json-schema-error.ts @@ -1,15 +1,12 @@ export class UnsupportedJSONSchemaError extends Error { - readonly provider: string; readonly reason: string; readonly schema: unknown; constructor({ - provider, schema, reason, message = `Unsupported JSON schema: ${reason}`, }: { - provider: string; schema: unknown; reason: string; message?: string; @@ -18,7 +15,6 @@ export class UnsupportedJSONSchemaError extends Error { this.name = 'AI_UnsupportedJSONSchemaError'; - this.provider = provider; this.reason = reason; this.schema = schema; } @@ -29,8 +25,6 @@ export class UnsupportedJSONSchemaError extends Error { return ( error instanceof Error && error.name === 'AI_UnsupportedJSONSchemaError' && - 'provider' in error && - error.provider != undefined && 'reason' in error && error.reason != undefined && 'schema' in error && @@ -44,7 +38,6 @@ export class UnsupportedJSONSchemaError extends Error { message: this.message, stack: this.stack, - provider: this.provider, reason: this.reason, schema: this.schema, };