Skip to content

Commit bdbc32a

Browse files
committed
feat(vercel-adapter): add Vercel AI adapter
1 parent 9698e87 commit bdbc32a

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { ApolloLink } from "@apollo/client";
2+
import { type LanguageModel, generateObject } from "ai";
3+
import { AIAdapter } from "@apollo/client-ai";
4+
5+
namespace VercelAIAdapter {
6+
export interface Options extends AIAdapter.Options {
7+
model: LanguageModel;
8+
}
9+
}
10+
11+
type RequiredGenerateObjectOptions = {
12+
model: LanguageModel;
13+
mode: "json";
14+
prompt: string;
15+
system?: string;
16+
};
17+
18+
export class VercelAIAdapter extends AIAdapter {
19+
public model: LanguageModel;
20+
21+
constructor(options: VercelAIAdapter.Options) {
22+
super(options);
23+
24+
this.model = options.model;
25+
}
26+
27+
public async generateResponseForOperation(
28+
operation: ApolloLink.Operation,
29+
prompt: string
30+
): Promise<AIAdapter.Result> {
31+
const promptOptions: RequiredGenerateObjectOptions = {
32+
mode: "json",
33+
model: this.model,
34+
prompt: this.createPrompt(operation, prompt),
35+
system: this.systemPrompt,
36+
};
37+
38+
return generateObject(promptOptions as any).then(
39+
({ object: result, finishReason, usage, warnings }) => {
40+
if (!result || typeof result !== "object") {
41+
return { data: null };
42+
}
43+
if (!Object.prototype.hasOwnProperty.call(result, "data")) {
44+
return { data: result };
45+
}
46+
return result;
47+
}
48+
);
49+
}
50+
}

0 commit comments

Comments
 (0)