Skip to content

Simplify createChatCompletion #673

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
65 changes: 65 additions & 0 deletions examples/external_clients/aisdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ import {
generateText,
ImagePart,
LanguageModel,
streamText,
TextPart,
} from "ai";
import { CreateChatCompletionOptions, LLMClient, AvailableModel } from "@/dist";
import { ChatCompletion } from "openai/resources";
import {
GenerateObjectOptions,
GenerateTextOptions,
ObjectResponse,
StreamingTextResponse,
TextResponse,
} from "@/lib";

export class AISdkClient extends LLMClient {
public type = "aisdk" as const;
Expand Down Expand Up @@ -119,4 +127,61 @@ export class AISdkClient extends LLMClient {
},
} as T;
}

async streamText<T = StreamingTextResponse>({
prompt,
options = {},
}: GenerateTextOptions): Promise<T> {
const tools: Record<string, CoreTool> = {};
if (options.tools) {
for (const rawTool of options.tools) {
tools[rawTool.name] = {
description: rawTool.description,
parameters: rawTool.parameters,
};
}
}

const response = await streamText({
model: this.model,
prompt: prompt,
tools,
});
return response as T;
}

async generateText<T = TextResponse>({
prompt,
options = {},
}: GenerateTextOptions): Promise<T> {
const tools: Record<string, CoreTool> = {};
if (options.tools) {
for (const rawTool of options.tools) {
tools[rawTool.name] = {
description: rawTool.description,
parameters: rawTool.parameters,
};
}
}

const response = await generateText({
model: this.model,
prompt: prompt,
tools,
});
return response as T;
}
async generateObject<T = ObjectResponse>({
prompt,
schema,
options = {},
}: GenerateObjectOptions): Promise<T> {
const response = await generateObject({
model: this.model,
prompt: prompt,
schema: schema,
...options,
});
Comment on lines +179 to +184
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: options are spread after required parameters, which could accidentally override them

return response as T;
}
}
Loading