Skip to content

Commit

Permalink
chore (ai/core): remove grammar mode (#2367)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel authored Jul 22, 2024
1 parent 1a93a75 commit 13b27ec
Show file tree
Hide file tree
Showing 16 changed files with 21 additions and 130 deletions.
13 changes: 13 additions & 0 deletions .changeset/violet-tips-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@ai-sdk/amazon-bedrock': patch
'@ai-sdk/google-vertex': patch
'@ai-sdk/anthropic': patch
'@ai-sdk/provider': patch
'@ai-sdk/mistral': patch
'@ai-sdk/cohere': patch
'@ai-sdk/google': patch
'@ai-sdk/openai': patch
'ai': patch
---

chore (ai/core): remove grammar mode
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ While some models (like OpenAI) natively support object generation, others requi

- `auto`: The provider will choose the best mode for the model. This recommended mode is used by default.
- `tool`: A tool with the JSON schema as parameters is provided and the provider is instructed to use it.
- `json`: The JSON schema and an instruction is injected into the prompt. If the provider supports JSON mode, it is enabled.
- `grammar`: The provider is instructed to convert the JSON schema into a provider specific grammar and use it to select the output tokens.
- `json`: The JSON schema and an instruction is injected into the prompt. If the provider supports JSON mode, it is enabled. If the provider supports JSON grammars, the grammar is used.

<Note>Please note that not every provider supports all generation modes.</Note>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ console.log(JSON.stringify(object, null, 2));
},
{
name: 'mode',
type: "'auto' | 'json' | 'grammar' | 'tool'",
type: "'auto' | 'json' | 'tool'",
description:
"The mode to use for object generation. Not every model supports all modes. Defaults to 'auto'.",
},
Expand Down
2 changes: 1 addition & 1 deletion content/docs/07-reference/ai-sdk-core/04-stream-object.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ for await (const partialObject of partialObjectStream) {
},
{
name: 'mode',
type: "'auto' | 'json' | 'grammar' | 'tool'",
type: "'auto' | 'json' | 'tool'",
description:
"The mode to use for object generation. Not every model supports all modes. Defaults to 'auto'.",
},
Expand Down
6 changes: 0 additions & 6 deletions packages/amazon-bedrock/src/bedrock-chat-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,6 @@ export class BedrockChatLanguageModel implements LanguageModelV1 {
} satisfies ConverseCommandInput;
}

case 'object-grammar': {
throw new UnsupportedFunctionalityError({
functionality: 'grammar-mode object generation',
});
}

default: {
const _exhaustiveCheck: never = type;
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
Expand Down
6 changes: 0 additions & 6 deletions packages/anthropic/src/anthropic-messages-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,6 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV1 {
};
}

case 'object-grammar': {
throw new UnsupportedFunctionalityError({
functionality: 'grammar-mode object generation',
});
}

default: {
const _exhaustiveCheck: never = type;
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
Expand Down
6 changes: 0 additions & 6 deletions packages/cohere/src/cohere-chat-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,6 @@ export class CohereChatLanguageModel implements LanguageModelV1 {
});
}

case 'object-grammar': {
throw new UnsupportedFunctionalityError({
functionality: 'object-grammar mode',
});
}

default: {
const _exhaustiveCheck: never = type;
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
Expand Down
37 changes: 2 additions & 35 deletions packages/core/core/generate-object/generate-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,13 @@ The Zod schema is converted in a JSON schema and used in one of the following wa
- 'auto': The provider will choose the best mode for the model.
- 'tool': A tool with the JSON schema as parameters is is provided and the provider is instructed to use it.
- 'json': The JSON schema and a instruction is injected into the prompt. If the provider supports JSON mode, it is enabled.
- 'grammar': The provider is instructed to converted the JSON schema into a provider specific grammar and use it to select the output tokens.
- 'json': The JSON schema and an instruction is injected into the prompt. If the provider supports JSON mode, it is enabled. If the provider supports JSON grammars, the grammar is used.
Please note that most providers do not support all modes.
Default and recommended: 'auto' (best mode for the model).
*/
mode?: 'auto' | 'json' | 'tool' | 'grammar';
mode?: 'auto' | 'json' | 'tool';
}): Promise<GenerateObjectResult<T>> {
const retry = retryWithExponentialBackoff({ maxRetries });
const jsonSchema = convertZodToJSONSchema(schema);
Expand Down Expand Up @@ -140,38 +139,6 @@ Default and recommended: 'auto' (best mode for the model).
break;
}

case 'grammar': {
const validatedPrompt = getValidatedPrompt({
system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
prompt,
messages,
});

const generateResult = await retry(() =>
model.doGenerate({
mode: { type: 'object-grammar', schema: jsonSchema },
...prepareCallSettings(settings),
inputFormat: validatedPrompt.type,
prompt: convertToLanguageModelPrompt(validatedPrompt),
abortSignal,
headers,
}),
);

if (generateResult.text === undefined) {
throw new NoObjectGeneratedError();
}

result = generateResult.text;
finishReason = generateResult.finishReason;
usage = generateResult.usage;
warnings = generateResult.warnings;
rawResponse = generateResult.rawResponse;
logprobs = generateResult.logprobs;

break;
}

case 'tool': {
const validatedPrompt = getValidatedPrompt({
system,
Expand Down
38 changes: 2 additions & 36 deletions packages/core/core/generate-object/stream-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,13 @@ The Zod schema is converted in a JSON schema and used in one of the following wa
- 'auto': The provider will choose the best mode for the model.
- 'tool': A tool with the JSON schema as parameters is is provided and the provider is instructed to use it.
- 'json': The JSON schema and a instruction is injected into the prompt. If the provider supports JSON mode, it is enabled.
- 'grammar': The provider is instructed to converted the JSON schema into a provider specific grammar and use it to select the output tokens.
- 'json': The JSON schema and an instruction is injected into the prompt. If the provider supports JSON mode, it is enabled. If the provider supports JSON grammars, the grammar is used.
Please note that most providers do not support all modes.
Default and recommended: 'auto' (best mode for the model).
*/
mode?: 'auto' | 'json' | 'tool' | 'grammar';
mode?: 'auto' | 'json' | 'tool';

/**
Callback that is called when the LLM response and the final object validation are finished.
Expand Down Expand Up @@ -190,39 +189,6 @@ Warnings from the model provider (e.g. unsupported settings).
break;
}

case 'grammar': {
const validatedPrompt = getValidatedPrompt({
system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
prompt,
messages,
});

callOptions = {
mode: { type: 'object-grammar', schema: jsonSchema },
...prepareCallSettings(settings),
inputFormat: validatedPrompt.type,
prompt: convertToLanguageModelPrompt(validatedPrompt),
abortSignal,
headers,
};

transformer = {
transform: (chunk, controller) => {
switch (chunk.type) {
case 'text-delta':
controller.enqueue(chunk.textDelta);
break;
case 'finish':
case 'error':
controller.enqueue(chunk);
break;
}
},
};

break;
}

case 'tool': {
const validatedPrompt = getValidatedPrompt({
system,
Expand Down
6 changes: 0 additions & 6 deletions packages/google-vertex/src/google-vertex-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,6 @@ export class GoogleVertexLanguageModel implements LanguageModelV1 {
});
}

case 'object-grammar': {
throw new UnsupportedFunctionalityError({
functionality: 'object-grammar mode',
});
}

default: {
const _exhaustiveCheck: never = type;
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
Expand Down
6 changes: 0 additions & 6 deletions packages/google/src/google-generative-ai-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,6 @@ export class GoogleGenerativeAILanguageModel implements LanguageModelV1 {
};
}

case 'object-grammar': {
throw new UnsupportedFunctionalityError({
functionality: 'object-grammar mode',
});
}

default: {
const _exhaustiveCheck: never = type;
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
Expand Down
6 changes: 0 additions & 6 deletions packages/mistral/src/mistral-chat-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,6 @@ export class MistralChatLanguageModel implements LanguageModelV1 {
};
}

case 'object-grammar': {
throw new UnsupportedFunctionalityError({
functionality: 'object-grammar mode',
});
}

default: {
const _exhaustiveCheck: never = type;
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
Expand Down
6 changes: 0 additions & 6 deletions packages/openai/src/openai-chat-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,6 @@ export class OpenAIChatLanguageModel implements LanguageModelV1 {
};
}

case 'object-grammar': {
throw new UnsupportedFunctionalityError({
functionality: 'object-grammar mode',
});
}

default: {
const _exhaustiveCheck: never = type;
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
Expand Down
6 changes: 0 additions & 6 deletions packages/openai/src/openai-completion-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,6 @@ export class OpenAICompletionLanguageModel implements LanguageModelV1 {
});
}

case 'object-grammar': {
throw new UnsupportedFunctionalityError({
functionality: 'object-grammar mode',
});
}

default: {
const _exhaustiveCheck: never = type;
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { JSONSchema7 } from 'json-schema';
import { LanguageModelV1CallSettings } from './language-model-v1-call-settings';
import { LanguageModelV1FunctionTool } from './language-model-v1-function-tool';
import { LanguageModelV1Prompt } from './language-model-v1-prompt';
Expand Down Expand Up @@ -42,11 +41,6 @@ Specifies how the tool should be selected. Defaults to 'auto'.
// object generation with json mode enabled (streaming: text delta)
type: 'object-json';
}
| {
// object generation with grammar enabled (streaming: text delta)
type: 'object-grammar';
schema: JSONSchema7;
}
| {
// object generation with tool mode enabled (streaming: tool call deltas)
type: 'object-tool';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ model. `undefined` can be returned if object generation is not supported.
This is needed to generate the best objects possible w/o requiring the
user to explicitly specify the object generation mode.
*/
readonly defaultObjectGenerationMode: 'json' | 'tool' | 'grammar' | undefined;
readonly defaultObjectGenerationMode: 'json' | 'tool' | undefined;

/**
Generates a language model output (non-streaming).
Expand Down

0 comments on commit 13b27ec

Please sign in to comment.