Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for structured outputs to the provider #1159

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions packages/vercel-ai-provider/src/edgedb-chat-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class EdgeDBChatLanguageModel implements LanguageModelV1 {
// providerMetadata: exists in the Vercel SDK d.ts but none of the providers use it
}: Parameters<LanguageModelV1["doGenerate"]>[0]) {
const type = mode.type;
const isAnthropic = isAnthropicModel(this.modelId);

const warnings: LanguageModelV1CallWarning[] = [];

Expand All @@ -102,41 +103,33 @@ export class EdgeDBChatLanguageModel implements LanguageModelV1 {
});
}

if (isAnthropicModel(this.modelId) && seed != null) {
if (isAnthropic && seed != null) {
warnings.push({
type: "unsupported-setting",
setting: "seed",
});
}

if (!isAnthropicModel(this.modelId) && topK != null) {
if (!isAnthropic && topK != null) {
warnings.push({
type: "unsupported-setting",
setting: "topK",
});
}

if (responseFormat != null && responseFormat.type !== "text") {
if (
(isAnthropic && responseFormat?.type !== "text") ||
(!isAnthropic &&
responseFormat?.type === "json" &&
responseFormat?.schema)
) {
warnings.push({
type: "unsupported-setting",
setting: "responseFormat",
details: "JSON response format is not supported.",
details: "JSON response format schema is not supported.",
});
}

// if (
// responseFormat != null &&
// responseFormat.type === "json"
// // && responseFormat.schema != null
// ) {
// warnings.push({
// type: "unsupported-setting",
// setting: "responseFormat",
// details: "JSON response is not supported",
// // details: "JSON response format schema is not supported",
// });
// }

const baseArgs = {
model: this.modelId,
messages: convertToEdgeDBMessages(prompt),
Expand All @@ -161,17 +154,30 @@ export class EdgeDBChatLanguageModel implements LanguageModelV1 {

switch (type) {
case "regular": {
const { tools, tool_choice, toolWarnings } = prepareTools(
mode,
this.modelId,
);

return {
args: {
...baseArgs,
...prepareTools(mode, this.modelId),
tools,
tool_choice,
},
warnings,
warnings: [...warnings, ...toolWarnings],
};
}

// case 'object-json': {
// }
case "object-json": {
return {
args: {
...baseArgs,
response_format: { type: "json_object" },
},
warnings,
};
}

// case 'object-tool': {
// }
Expand Down
16 changes: 10 additions & 6 deletions packages/vercel-ai-provider/src/edgedb-prepare-tools.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type {
JSONSchema7,
LanguageModelV1,
LanguageModelV1CallWarning,
import {
type JSONSchema7,
type LanguageModelV1,
type LanguageModelV1CallWarning,
UnsupportedFunctionalityError,
} from "@ai-sdk/provider";
import {
type EdgeDBChatModelId,
Expand Down Expand Up @@ -110,6 +111,7 @@ export function prepareTools(
: isOpenAI
? "required"
: "any",
toolWarnings,
};

// mistral does not support tool mode directly,
Expand All @@ -121,8 +123,8 @@ export function prepareTools(
tool_choice: {
type: "tool",
name: toolChoice.toolName,
toolWarnings,
},
toolWarnings,
}
: isOpenAI
? {
Expand All @@ -145,7 +147,9 @@ export function prepareTools(

default: {
const _exhaustiveCheck: never = type;
throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`);
throw new UnsupportedFunctionalityError({
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`,
});
}
}
}
Loading