Skip to content

Commit

Permalink
Move specification into @ai-sdk/specification package. (#1303)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel authored Apr 9, 2024
1 parent ddcfc1b commit 1d38d2a
Show file tree
Hide file tree
Showing 76 changed files with 407 additions and 407 deletions.
6 changes: 6 additions & 0 deletions .changeset/eight-bananas-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@ai-sdk/specification': minor
'ai': patch
---

Move Language Model Specification to separate package.
6 changes: 3 additions & 3 deletions docs/pages/docs/ai-core/custom-provider.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Callout } from 'nextra-theme-docs';
The AI SDK provides a language model specification.
You can write your own providers that adhere to the AI SDK language model specification and they will be compatible with the AI Core functions.

You can find the Language Model Specification in the [AI SDK repository](https://github.com/vercel/ai/tree/main/packages/core/spec/language-model/v1).
You can find the Language Model Specification in the [AI SDK repository](https://github.com/vercel/ai/tree/main/packages/specification/src/language-model/v1).
It can be imported from `ai/spec`.

We provide an [OpenAI reference implementation](https://github.com/vercel/ai/tree/main/packages/core/openai)
Expand Down Expand Up @@ -76,14 +76,14 @@ export const customprovider = new CustomProvider();

## Language Model Implementation

Please refer to the Language Model Specification in the [AI SDK repository](https://github.com/vercel/ai/tree/main/packages/core/spec/language-model/v1).
Please refer to the Language Model Specification in the [AI SDK repository](https://github.com/vercel/ai/tree/main/packages/specification/src/language-model/v1).

We provide an [OpenAI reference implementation](https://github.com/vercel/ai/tree/main/packages/core/openai)
and a [Mistral reference implementation](https://github.com/vercel/ai/tree/main/packages/core/mistral).

### Errors

The AI SDK provides [standardized errors](https://github.com/vercel/ai/tree/main/packages/core/spec/errors) that should be used by providers where possible.
The AI SDK provides [standardized errors](https://github.com/vercel/ai/tree/main/packages/specification/src/errors) that should be used by providers where possible.
This will make it easy for user to debug them.

### Retries, timeouts, and abort signals
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LanguageModelV1Prompt } from '../spec';
import { LanguageModelV1Prompt } from '@ai-sdk/specification';
import { convertStreamToArray } from '../spec/test/convert-stream-to-array';
import { JsonTestServer } from '../spec/test/json-test-server';
import { StreamingTestServer } from '../spec/test/streaming-test-server';
Expand Down
6 changes: 4 additions & 2 deletions packages/core/anthropic/anthropic-messages-language-model.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { z } from 'zod';
import {
LanguageModelV1,
LanguageModelV1CallWarning,
LanguageModelV1FinishReason,
LanguageModelV1FunctionToolCall,
LanguageModelV1StreamPart,
ParseResult,
UnsupportedFunctionalityError,
} from '@ai-sdk/specification';
import { z } from 'zod';
import {
ParseResult,
createEventSourceResponseHandler,
createJsonResponseHandler,
postJsonToApi,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
LanguageModelV1Prompt,
UnsupportedFunctionalityError,
convertUint8ArrayToBase64,
} from '../spec';
} from '@ai-sdk/specification';
import { convertUint8ArrayToBase64 } from '../spec';
import {
AnthropicMessage,
AnthropicMessagesPrompt,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/anthropic/map-anthropic-stop-reason.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LanguageModelV1FinishReason } from '../spec';
import { LanguageModelV1FinishReason } from '@ai-sdk/specification';

export function mapAnthropicStopReason(
finishReason: string | null | undefined,
Expand Down
6 changes: 3 additions & 3 deletions packages/core/core/generate-object/generate-object.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { z } from 'zod';
import {
LanguageModelV1,
LanguageModelV1CallWarning,
LanguageModelV1FinishReason,
NoTextGeneratedError,
safeParseJSON,
} from '../../spec';
} from '@ai-sdk/specification';
import { z } from 'zod';
import { safeParseJSON } from '../../spec';
import { TokenUsage, calculateTokenUsage } from '../generate-text/token-usage';
import { CallSettings } from '../prompt/call-settings';
import { convertToLanguageModelPrompt } from '../prompt/convert-to-language-model-prompt';
Expand Down
4 changes: 2 additions & 2 deletions packages/core/core/generate-object/stream-object.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { z } from 'zod';
import {
LanguageModelV1,
LanguageModelV1CallOptions,
LanguageModelV1CallWarning,
LanguageModelV1StreamPart,
} from '../../spec';
} from '@ai-sdk/specification';
import { z } from 'zod';
import { CallSettings } from '../prompt/call-settings';
import { convertToLanguageModelPrompt } from '../prompt/convert-to-language-model-prompt';
import { getValidatedPrompt } from '../prompt/get-validated-prompt';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/core/generate-text/generate-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
LanguageModelV1,
LanguageModelV1CallWarning,
LanguageModelV1FinishReason,
} from '../../spec';
} from '@ai-sdk/specification';
import { CallSettings } from '../prompt/call-settings';
import { convertToLanguageModelPrompt } from '../prompt/convert-to-language-model-prompt';
import { getValidatedPrompt } from '../prompt/get-validated-prompt';
Expand Down
5 changes: 4 additions & 1 deletion packages/core/core/generate-text/run-tools-transformation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { LanguageModelV1StreamPart, NoSuchToolError } from '../../spec';
import {
LanguageModelV1StreamPart,
NoSuchToolError,
} from '@ai-sdk/specification';
import { generateId } from '../../shared/generate-id';
import { ExperimentalTool } from '../tool';
import { TextStreamPart } from './stream-text';
Expand Down
7 changes: 5 additions & 2 deletions packages/core/core/generate-text/stream-text.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { LanguageModelV1FinishReason } from '../../spec';
import { LanguageModelV1, LanguageModelV1CallWarning } from '../../spec/index';
import {
LanguageModelV1,
LanguageModelV1CallWarning,
LanguageModelV1FinishReason,
} from '@ai-sdk/specification';
import {
AIStreamCallbacksAndOptions,
createCallbacksTransformer,
Expand Down
6 changes: 3 additions & 3 deletions packages/core/core/generate-text/tool-call.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { z } from 'zod';
import {
InvalidToolArgumentsError,
LanguageModelV1FunctionToolCall,
NoSuchToolError,
safeParseJSON,
} from '../../spec';
} from '@ai-sdk/specification';
import { z } from 'zod';
import { safeParseJSON } from '../../spec';
import { ExperimentalTool } from '../tool';
import { ValueOf } from '../util/value-of';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
LanguageModelV1Message,
LanguageModelV1Prompt,
LanguageModelV1TextPart,
} from '../../spec';
} from '@ai-sdk/specification';
import { detectImageMimeType } from '../util/detect-image-mimetype';
import { convertDataContentToUint8Array } from './data-content';
import { ValidatedPrompt } from './get-validated-prompt';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/core/prompt/data-content.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InvalidDataContentError } from '@ai-sdk/specification';
import {
InvalidDataContentError,
convertBase64ToUint8Array,
convertUint8ArrayToBase64,
} from '../../spec';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/core/prompt/get-validated-prompt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InvalidPromptError } from '../../spec';
import { InvalidPromptError } from '@ai-sdk/specification';
import { ExperimentalMessage } from './message';
import { Prompt } from './prompt';

Expand Down
2 changes: 1 addition & 1 deletion packages/core/core/prompt/prepare-call-settings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InvalidArgumentError } from '@ai-sdk/specification';
import { CallSettings } from './call-settings';
import { InvalidArgumentError } from '../../spec';

/**
* Validates call settings and sets default values.
Expand Down
2 changes: 1 addition & 1 deletion packages/core/core/test/mock-language-model-v1.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LanguageModelV1 } from '../../spec/index';
import { LanguageModelV1 } from '@ai-sdk/specification';

export class MockLanguageModelV1 implements LanguageModelV1 {
readonly specificationVersion = 'v1';
Expand Down
3 changes: 2 additions & 1 deletion packages/core/core/util/retry-with-exponential-backoff.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { APICallError, RetryError, getErrorMessage } from '../../spec';
import { APICallError, RetryError } from '@ai-sdk/specification';
import { getErrorMessage } from '../../spec';
import { delay } from './delay';

export type RetryFunction = <OUTPUT>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
LanguageModelV1Prompt,
UnsupportedFunctionalityError,
convertUint8ArrayToBase64,
} from '../spec';
} from '@ai-sdk/specification';
import { convertUint8ArrayToBase64 } from '../spec';
import {
GoogleGenerativeAIContentPart,
GoogleGenerativeAIPrompt,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LanguageModelV1Prompt } from '../spec';
import { LanguageModelV1Prompt } from '@ai-sdk/specification';
import { convertStreamToArray } from '../spec/test/convert-stream-to-array';
import { JsonTestServer } from '../spec/test/json-test-server';
import { StreamingTestServer } from '../spec/test/streaming-test-server';
Expand Down
6 changes: 4 additions & 2 deletions packages/core/google/google-generative-ai-language-model.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { z } from 'zod';
import {
LanguageModelV1,
LanguageModelV1CallWarning,
LanguageModelV1FinishReason,
LanguageModelV1StreamPart,
ParseResult,
UnsupportedFunctionalityError,
} from '@ai-sdk/specification';
import { z } from 'zod';
import {
ParseResult,
createEventSourceResponseHandler,
createJsonResponseHandler,
postJsonToApi,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LanguageModelV1FinishReason } from '../spec';
import { LanguageModelV1FinishReason } from '@ai-sdk/specification';

export function mapGoogleGenerativeAIFinishReason({
finishReason,
Expand Down
5 changes: 4 additions & 1 deletion packages/core/mistral/convert-to-mistral-chat-messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { LanguageModelV1Prompt, UnsupportedFunctionalityError } from '../spec';
import {
LanguageModelV1Prompt,
UnsupportedFunctionalityError,
} from '@ai-sdk/specification';
import { MistralChatPrompt } from './mistral-chat-prompt';

export function convertToMistralChatMessages(
Expand Down
2 changes: 1 addition & 1 deletion packages/core/mistral/map-mistral-finish-reason.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LanguageModelV1FinishReason } from '../spec';
import { LanguageModelV1FinishReason } from '@ai-sdk/specification';

export function mapMistralFinishReason(
finishReason: string | null | undefined,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/mistral/mistral-chat-language-model.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LanguageModelV1Prompt } from '../spec';
import { LanguageModelV1Prompt } from '@ai-sdk/specification';
import { convertStreamToArray } from '../spec/test/convert-stream-to-array';
import { JsonTestServer } from '../spec/test/json-test-server';
import { StreamingTestServer } from '../spec/test/streaming-test-server';
Expand Down
6 changes: 4 additions & 2 deletions packages/core/mistral/mistral-chat-language-model.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { z } from 'zod';
import {
LanguageModelV1,
LanguageModelV1CallWarning,
LanguageModelV1FinishReason,
LanguageModelV1StreamPart,
ParseResult,
UnsupportedFunctionalityError,
} from '@ai-sdk/specification';
import { z } from 'zod';
import {
ParseResult,
createEventSourceResponseHandler,
createJsonResponseHandler,
postJsonToApi,
Expand Down
3 changes: 2 additions & 1 deletion packages/core/openai/convert-to-openai-chat-messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { LanguageModelV1Prompt, convertUint8ArrayToBase64 } from '../spec';
import { LanguageModelV1Prompt } from '@ai-sdk/specification';
import { convertUint8ArrayToBase64 } from '../spec';
import { OpenAIChatPrompt } from './openai-chat-prompt';

export function convertToOpenAIChatMessages(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
InvalidPromptError,
LanguageModelV1Prompt,
UnsupportedFunctionalityError,
} from '../spec';
} from '@ai-sdk/specification';

export function convertToOpenAICompletionPrompt({
prompt,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/openai/map-openai-finish-reason.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LanguageModelV1FinishReason } from '../spec';
import { LanguageModelV1FinishReason } from '@ai-sdk/specification';

export function mapOpenAIFinishReason(
finishReason: string | null | undefined,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/openai/openai-chat-language-model.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LanguageModelV1Prompt } from '../spec';
import { LanguageModelV1Prompt } from '@ai-sdk/specification';
import { convertStreamToArray } from '../spec/test/convert-stream-to-array';
import { JsonTestServer } from '../spec/test/json-test-server';
import { StreamingTestServer } from '../spec/test/streaming-test-server';
Expand Down
6 changes: 4 additions & 2 deletions packages/core/openai/openai-chat-language-model.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { z } from 'zod';
import {
InvalidResponseDataError,
LanguageModelV1,
LanguageModelV1FinishReason,
LanguageModelV1StreamPart,
ParseResult,
UnsupportedFunctionalityError,
} from '@ai-sdk/specification';
import { z } from 'zod';
import {
ParseResult,
createEventSourceResponseHandler,
createJsonResponseHandler,
generateId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LanguageModelV1Prompt } from '../spec';
import { LanguageModelV1Prompt } from '@ai-sdk/specification';
import { convertStreamToArray } from '../spec/test/convert-stream-to-array';
import { JsonTestServer } from '../spec/test/json-test-server';
import { StreamingTestServer } from '../spec/test/streaming-test-server';
Expand Down
6 changes: 4 additions & 2 deletions packages/core/openai/openai-completion-language-model.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { z } from 'zod';
import {
LanguageModelV1,
LanguageModelV1FinishReason,
LanguageModelV1StreamPart,
ParseResult,
UnsupportedFunctionalityError,
} from '@ai-sdk/specification';
import { z } from 'zod';
import {
ParseResult,
createEventSourceResponseHandler,
createJsonResponseHandler,
postJsonToApi,
Expand Down
12 changes: 3 additions & 9 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
"solid/dist/**/*",
"prompts/dist/**/*",
"rsc/dist/**/*",
"spec/dist/**/*",
"google/dist/**/*",
"openai/dist/**/*",
"anthropic/dist/**/*",
"mistral/dist/**/*"
],
"scripts": {
"build": "tsup && cat react/dist/index.server.d.ts >> react/dist/index.d.ts",
"clean": "rm -rf dist && rm -rf core/dist && rm -rf google/dist && rm -rf anthropic/dist && rm -rf openai/dist && rm -rf mistral/dist && rm -rf spec/dist && rm -rf react/dist && rm -rf svelte/dist && rm -rf vue/dist && rm -rf solid/dist && rm -rf rsc/dist",
"clean": "rm -rf dist && rm -rf core/dist && rm -rf google/dist && rm -rf anthropic/dist && rm -rf openai/dist && rm -rf mistral/dist && rm -rf react/dist && rm -rf svelte/dist && rm -rf vue/dist && rm -rf solid/dist && rm -rf rsc/dist",
"dev": "tsup --watch",
"lint": "eslint \"./**/*.ts*\"",
"type-check": "tsc --noEmit",
Expand Down Expand Up @@ -52,12 +51,6 @@
"import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"./spec": {
"types": "./spec/dist/index.d.ts",
"import": "./spec/dist/index.mjs",
"module": "./spec/dist/index.mjs",
"require": "./spec/dist/index.js"
},
"./anthropic": {
"types": "./anthropic/dist/index.d.ts",
"import": "./anthropic/dist/index.mjs",
Expand Down Expand Up @@ -115,7 +108,7 @@
}
},
"dependencies": {
"@types/json-schema": "7.0.15",
"@ai-sdk/specification": "0.1.0",
"secure-json-parse": "2.7.0",
"eventsource-parser": "1.1.2",
"jsondiffpatch": "0.6.0",
Expand All @@ -140,6 +133,7 @@
"@testing-library/react": "^14.0.0",
"@testing-library/user-event": "^14.5.1",
"@testing-library/vue": "^8.0.1",
"@types/json-schema": "7.0.15",
"@types/node": "^17.0.12",
"@types/react": "^18.2.8",
"@types/react-dom": "^18.2.0",
Expand Down
2 changes: 0 additions & 2 deletions packages/core/spec/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './errors/index';
export * from './language-model/index';
export * from './util/index';
2 changes: 1 addition & 1 deletion packages/core/spec/util/load-api-key.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LoadAPIKeyError } from '../errors/load-api-key-error';
import { LoadAPIKeyError } from '@ai-sdk/specification';

export function loadApiKey({
apiKey,
Expand Down
3 changes: 1 addition & 2 deletions packages/core/spec/util/parse-json.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { JSONParseError, TypeValidationError } from '@ai-sdk/specification';
import SecureJSON from 'secure-json-parse';
import { ZodSchema } from 'zod';
import { JSONParseError } from '../errors/json-parse-error';
import { TypeValidationError } from '../errors/type-validation-error';
import { safeValidateTypes, validateTypes } from './validate-types';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/core/spec/util/post-to-api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { APICallError } from '../errors/api-call-error';
import { APICallError } from '@ai-sdk/specification';
import { ResponseHandler } from './response-handler';

export const postJsonToApi = async <T>({
Expand Down
Loading

0 comments on commit 1d38d2a

Please sign in to comment.