File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed
packages/ai-vercel-adapter/src Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments