Skip to content

Commit

Permalink
ai/core: remove provider field from errors (#1296)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel authored Apr 8, 2024
1 parent 27d9979 commit e3206ff
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 80 deletions.
7 changes: 1 addition & 6 deletions packages/core/anthropic/anthropic-messages-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV1 {
});
}

const messagesPrompt = convertToAnthropicMessagesPrompt({
provider: this.provider,
prompt,
});
const messagesPrompt = convertToAnthropicMessagesPrompt(prompt);

const baseArgs = {
// model id:
Expand Down Expand Up @@ -126,7 +123,6 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV1 {
case 'object-json': {
throw new UnsupportedFunctionalityError({
functionality: 'json-mode object generation',
provider: this.provider,
});
}

Expand All @@ -151,7 +147,6 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV1 {
case 'object-grammar': {
throw new UnsupportedFunctionalityError({
functionality: 'grammar-mode object generation',
provider: this.provider,
});
}

Expand Down
11 changes: 3 additions & 8 deletions packages/core/anthropic/convert-to-anthropic-messages-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];

Expand All @@ -36,7 +32,6 @@ export function convertToAnthropicMessagesPrompt({
case 'image': {
if (part.image instanceof URL) {
throw new UnsupportedFunctionalityError({
provider,
functionality: 'URL image parts',
});
} else {
Expand Down
11 changes: 3 additions & 8 deletions packages/core/google/convert-to-google-generative-ai-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -40,7 +36,6 @@ export function convertToGoogleGenerativeAIMessages({
case 'image': {
if (part.image instanceof URL) {
throw new UnsupportedFunctionalityError({
provider,
functionality: 'URL image parts',
});
} else {
Expand Down
8 changes: 1 addition & 7 deletions packages/core/google/google-generative-ai-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,7 @@ export class GoogleGenerativeAILanguageModel implements LanguageModelV1 {
},

// prompt:
contents: convertToGoogleGenerativeAIMessages({
provider: this.provider,
prompt,
}),
contents: convertToGoogleGenerativeAIMessages(prompt),
};

switch (type) {
Expand All @@ -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,
});
}

Expand Down
11 changes: 3 additions & 8 deletions packages/core/mistral/convert-to-mistral-chat-messages.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -28,7 +24,6 @@ export function convertToMistralChatMessages({
}
case 'image': {
throw new UnsupportedFunctionalityError({
provider,
functionality: 'image-part',
});
}
Expand Down
6 changes: 1 addition & 5 deletions packages/core/mistral/mistral-chat-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@ export class MistralChatLanguageModel implements LanguageModelV1 {
random_seed: seed,

// messages:
messages: convertToMistralChatMessages({
provider: this.provider,
prompt,
}),
messages: convertToMistralChatMessages(prompt),
};

switch (type) {
Expand Down Expand Up @@ -141,7 +138,6 @@ export class MistralChatLanguageModel implements LanguageModelV1 {
case 'object-grammar': {
throw new UnsupportedFunctionalityError({
functionality: 'object-grammar mode',
provider: this.provider,
});
}

Expand Down
5 changes: 0 additions & 5 deletions packages/core/openai/convert-to-openai-completion-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}): {
Expand Down Expand Up @@ -58,7 +56,6 @@ export function convertToOpenAICompletionPrompt({
}
case 'image': {
throw new UnsupportedFunctionalityError({
provider,
functionality: 'images',
});
}
Expand All @@ -79,7 +76,6 @@ export function convertToOpenAICompletionPrompt({
}
case 'tool-call': {
throw new UnsupportedFunctionalityError({
provider,
functionality: 'tool-call messages',
});
}
Expand All @@ -93,7 +89,6 @@ export function convertToOpenAICompletionPrompt({

case 'tool': {
throw new UnsupportedFunctionalityError({
provider,
functionality: 'tool messages',
});
}
Expand Down
1 change: 0 additions & 1 deletion packages/core/openai/openai-chat-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ export class OpenAIChatLanguageModel implements LanguageModelV1 {
case 'object-grammar': {
throw new UnsupportedFunctionalityError({
functionality: 'object-grammar mode',
provider: this.provider,
});
}

Expand Down
10 changes: 1 addition & 9 deletions packages/core/openai/openai-completion-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -112,7 +108,6 @@ export class OpenAICompletionLanguageModel implements LanguageModelV1 {
if (mode.tools?.length) {
throw new UnsupportedFunctionalityError({
functionality: 'tools',
provider: this.provider,
});
}

Expand All @@ -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,
});
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core/spec/errors/invalid-response-data-error.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
16 changes: 2 additions & 14 deletions packages/core/spec/errors/unsupported-functionality-error.ts
Original file line number Diff line number Diff line change
@@ -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;
}

Expand All @@ -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'
);
}
Expand All @@ -36,7 +25,6 @@ export class UnsupportedFunctionalityError extends Error {
message: this.message,
stack: this.stack,

provider: this.provider,
functionality: this.functionality,
};
}
Expand Down
7 changes: 0 additions & 7 deletions packages/core/spec/errors/unsupported-json-schema-error.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,7 +15,6 @@ export class UnsupportedJSONSchemaError extends Error {

this.name = 'AI_UnsupportedJSONSchemaError';

this.provider = provider;
this.reason = reason;
this.schema = schema;
}
Expand All @@ -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 &&
Expand All @@ -44,7 +38,6 @@ export class UnsupportedJSONSchemaError extends Error {
message: this.message,
stack: this.stack,

provider: this.provider,
reason: this.reason,
schema: this.schema,
};
Expand Down

0 comments on commit e3206ff

Please sign in to comment.