Skip to content

Commit

Permalink
Add returnRawResponse to AI binding options
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaquinGimenez1 authored and anonrig committed Jan 29, 2025
1 parent 270a365 commit 101e0c0
Show file tree
Hide file tree
Showing 23 changed files with 225 additions and 64 deletions.
7 changes: 6 additions & 1 deletion src/cloudflare/internal/ai-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export type SessionOptions = {

export type AiOptions = {
gateway?: GatewayOptions;

/** If true it will return a Response object */
returnRawResponse?: boolean;
prefix?: string;
extraHeaders?: object;
/*
Expand Down Expand Up @@ -142,6 +143,10 @@ export class Ai {
this.aiGatewayLogId = res.headers.get('cf-aig-log-id');
this.lastRequestHttpStatusCode = res.status;

if (this.options.returnRawResponse) {
return res;
}

if (!res.ok) {
throw await this._parseError(res);
}
Expand Down
11 changes: 11 additions & 0 deletions src/cloudflare/internal/test/ai/ai-api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,16 @@ export const tests = {
},
]);
}

{
// Test `returnRawResponse` option is returning a Response object
const resp = await env.ai.run(
'rawInputs',
{ prompt: 'test' },
{ returnRawResponse: true }
);

assert.ok(resp instanceof Response);
}
},
};
11 changes: 8 additions & 3 deletions types/defines/ai.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ export interface AiModels {
}
export type AiOptions = {
gateway?: GatewayOptions;
returnRawResponse?: boolean;
prefix?: string;
extraHeaders?: object;
};
Expand Down Expand Up @@ -720,10 +721,14 @@ export type AiModelListType = Record<string, any>;
export declare abstract class Ai<AiModelList extends AiModelListType = AiModels> {
aiGatewayLogId: string | null;
gateway(gatewayId: string): AiGateway;
run<Name extends keyof AiModelList>(
run<Name extends keyof AiModelList, Options extends AiOptions>(
model: Name,
inputs: AiModelList[Name]["inputs"],
options?: AiOptions,
): Promise<AiModelList[Name]["postProcessedOutputs"]>;
options?: Options
): Promise<
Options extends { returnRawResponse: true }
? Response
: AiModelList[Name]["postProcessedOutputs"]
>;
public models(params?: AiModelsSearchParams): Promise<AiModelsSearchObject[]>;
}
13 changes: 10 additions & 3 deletions types/generated-snapshot/2021-11-03/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4110,6 +4110,7 @@ interface AiModels {
}
type AiOptions = {
gateway?: GatewayOptions;
returnRawResponse?: boolean;
prefix?: string;
extraHeaders?: object;
};
Expand Down Expand Up @@ -4144,11 +4145,17 @@ type AiModelListType = Record<string, any>;
declare abstract class Ai<AiModelList extends AiModelListType = AiModels> {
aiGatewayLogId: string | null;
gateway(gatewayId: string): AiGateway;
run<Name extends keyof AiModelList>(
run<Name extends keyof AiModelList, Options extends AiOptions>(
model: Name,
inputs: AiModelList[Name]["inputs"],
options?: AiOptions,
): Promise<AiModelList[Name]["postProcessedOutputs"]>;
options?: Options,
): Promise<
Options extends {
returnRawResponse: true;
}
? Response
: AiModelList[Name]["postProcessedOutputs"]
>;
public models(params?: AiModelsSearchParams): Promise<AiModelsSearchObject[]>;
}
type GatewayOptions = {
Expand Down
13 changes: 10 additions & 3 deletions types/generated-snapshot/2021-11-03/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4122,6 +4122,7 @@ export interface AiModels {
}
export type AiOptions = {
gateway?: GatewayOptions;
returnRawResponse?: boolean;
prefix?: string;
extraHeaders?: object;
};
Expand Down Expand Up @@ -4158,11 +4159,17 @@ export declare abstract class Ai<
> {
aiGatewayLogId: string | null;
gateway(gatewayId: string): AiGateway;
run<Name extends keyof AiModelList>(
run<Name extends keyof AiModelList, Options extends AiOptions>(
model: Name,
inputs: AiModelList[Name]["inputs"],
options?: AiOptions,
): Promise<AiModelList[Name]["postProcessedOutputs"]>;
options?: Options,
): Promise<
Options extends {
returnRawResponse: true;
}
? Response
: AiModelList[Name]["postProcessedOutputs"]
>;
public models(params?: AiModelsSearchParams): Promise<AiModelsSearchObject[]>;
}
export type GatewayOptions = {
Expand Down
13 changes: 10 additions & 3 deletions types/generated-snapshot/2022-01-31/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4136,6 +4136,7 @@ interface AiModels {
}
type AiOptions = {
gateway?: GatewayOptions;
returnRawResponse?: boolean;
prefix?: string;
extraHeaders?: object;
};
Expand Down Expand Up @@ -4170,11 +4171,17 @@ type AiModelListType = Record<string, any>;
declare abstract class Ai<AiModelList extends AiModelListType = AiModels> {
aiGatewayLogId: string | null;
gateway(gatewayId: string): AiGateway;
run<Name extends keyof AiModelList>(
run<Name extends keyof AiModelList, Options extends AiOptions>(
model: Name,
inputs: AiModelList[Name]["inputs"],
options?: AiOptions,
): Promise<AiModelList[Name]["postProcessedOutputs"]>;
options?: Options,
): Promise<
Options extends {
returnRawResponse: true;
}
? Response
: AiModelList[Name]["postProcessedOutputs"]
>;
public models(params?: AiModelsSearchParams): Promise<AiModelsSearchObject[]>;
}
type GatewayOptions = {
Expand Down
13 changes: 10 additions & 3 deletions types/generated-snapshot/2022-01-31/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4148,6 +4148,7 @@ export interface AiModels {
}
export type AiOptions = {
gateway?: GatewayOptions;
returnRawResponse?: boolean;
prefix?: string;
extraHeaders?: object;
};
Expand Down Expand Up @@ -4184,11 +4185,17 @@ export declare abstract class Ai<
> {
aiGatewayLogId: string | null;
gateway(gatewayId: string): AiGateway;
run<Name extends keyof AiModelList>(
run<Name extends keyof AiModelList, Options extends AiOptions>(
model: Name,
inputs: AiModelList[Name]["inputs"],
options?: AiOptions,
): Promise<AiModelList[Name]["postProcessedOutputs"]>;
options?: Options,
): Promise<
Options extends {
returnRawResponse: true;
}
? Response
: AiModelList[Name]["postProcessedOutputs"]
>;
public models(params?: AiModelsSearchParams): Promise<AiModelsSearchObject[]>;
}
export type GatewayOptions = {
Expand Down
13 changes: 10 additions & 3 deletions types/generated-snapshot/2022-03-21/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4161,6 +4161,7 @@ interface AiModels {
}
type AiOptions = {
gateway?: GatewayOptions;
returnRawResponse?: boolean;
prefix?: string;
extraHeaders?: object;
};
Expand Down Expand Up @@ -4195,11 +4196,17 @@ type AiModelListType = Record<string, any>;
declare abstract class Ai<AiModelList extends AiModelListType = AiModels> {
aiGatewayLogId: string | null;
gateway(gatewayId: string): AiGateway;
run<Name extends keyof AiModelList>(
run<Name extends keyof AiModelList, Options extends AiOptions>(
model: Name,
inputs: AiModelList[Name]["inputs"],
options?: AiOptions,
): Promise<AiModelList[Name]["postProcessedOutputs"]>;
options?: Options,
): Promise<
Options extends {
returnRawResponse: true;
}
? Response
: AiModelList[Name]["postProcessedOutputs"]
>;
public models(params?: AiModelsSearchParams): Promise<AiModelsSearchObject[]>;
}
type GatewayOptions = {
Expand Down
13 changes: 10 additions & 3 deletions types/generated-snapshot/2022-03-21/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4173,6 +4173,7 @@ export interface AiModels {
}
export type AiOptions = {
gateway?: GatewayOptions;
returnRawResponse?: boolean;
prefix?: string;
extraHeaders?: object;
};
Expand Down Expand Up @@ -4209,11 +4210,17 @@ export declare abstract class Ai<
> {
aiGatewayLogId: string | null;
gateway(gatewayId: string): AiGateway;
run<Name extends keyof AiModelList>(
run<Name extends keyof AiModelList, Options extends AiOptions>(
model: Name,
inputs: AiModelList[Name]["inputs"],
options?: AiOptions,
): Promise<AiModelList[Name]["postProcessedOutputs"]>;
options?: Options,
): Promise<
Options extends {
returnRawResponse: true;
}
? Response
: AiModelList[Name]["postProcessedOutputs"]
>;
public models(params?: AiModelsSearchParams): Promise<AiModelsSearchObject[]>;
}
export type GatewayOptions = {
Expand Down
13 changes: 10 additions & 3 deletions types/generated-snapshot/2022-08-04/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4162,6 +4162,7 @@ interface AiModels {
}
type AiOptions = {
gateway?: GatewayOptions;
returnRawResponse?: boolean;
prefix?: string;
extraHeaders?: object;
};
Expand Down Expand Up @@ -4196,11 +4197,17 @@ type AiModelListType = Record<string, any>;
declare abstract class Ai<AiModelList extends AiModelListType = AiModels> {
aiGatewayLogId: string | null;
gateway(gatewayId: string): AiGateway;
run<Name extends keyof AiModelList>(
run<Name extends keyof AiModelList, Options extends AiOptions>(
model: Name,
inputs: AiModelList[Name]["inputs"],
options?: AiOptions,
): Promise<AiModelList[Name]["postProcessedOutputs"]>;
options?: Options,
): Promise<
Options extends {
returnRawResponse: true;
}
? Response
: AiModelList[Name]["postProcessedOutputs"]
>;
public models(params?: AiModelsSearchParams): Promise<AiModelsSearchObject[]>;
}
type GatewayOptions = {
Expand Down
13 changes: 10 additions & 3 deletions types/generated-snapshot/2022-08-04/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4174,6 +4174,7 @@ export interface AiModels {
}
export type AiOptions = {
gateway?: GatewayOptions;
returnRawResponse?: boolean;
prefix?: string;
extraHeaders?: object;
};
Expand Down Expand Up @@ -4210,11 +4211,17 @@ export declare abstract class Ai<
> {
aiGatewayLogId: string | null;
gateway(gatewayId: string): AiGateway;
run<Name extends keyof AiModelList>(
run<Name extends keyof AiModelList, Options extends AiOptions>(
model: Name,
inputs: AiModelList[Name]["inputs"],
options?: AiOptions,
): Promise<AiModelList[Name]["postProcessedOutputs"]>;
options?: Options,
): Promise<
Options extends {
returnRawResponse: true;
}
? Response
: AiModelList[Name]["postProcessedOutputs"]
>;
public models(params?: AiModelsSearchParams): Promise<AiModelsSearchObject[]>;
}
export type GatewayOptions = {
Expand Down
13 changes: 10 additions & 3 deletions types/generated-snapshot/2022-10-31/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4165,6 +4165,7 @@ interface AiModels {
}
type AiOptions = {
gateway?: GatewayOptions;
returnRawResponse?: boolean;
prefix?: string;
extraHeaders?: object;
};
Expand Down Expand Up @@ -4199,11 +4200,17 @@ type AiModelListType = Record<string, any>;
declare abstract class Ai<AiModelList extends AiModelListType = AiModels> {
aiGatewayLogId: string | null;
gateway(gatewayId: string): AiGateway;
run<Name extends keyof AiModelList>(
run<Name extends keyof AiModelList, Options extends AiOptions>(
model: Name,
inputs: AiModelList[Name]["inputs"],
options?: AiOptions,
): Promise<AiModelList[Name]["postProcessedOutputs"]>;
options?: Options,
): Promise<
Options extends {
returnRawResponse: true;
}
? Response
: AiModelList[Name]["postProcessedOutputs"]
>;
public models(params?: AiModelsSearchParams): Promise<AiModelsSearchObject[]>;
}
type GatewayOptions = {
Expand Down
13 changes: 10 additions & 3 deletions types/generated-snapshot/2022-10-31/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4177,6 +4177,7 @@ export interface AiModels {
}
export type AiOptions = {
gateway?: GatewayOptions;
returnRawResponse?: boolean;
prefix?: string;
extraHeaders?: object;
};
Expand Down Expand Up @@ -4213,11 +4214,17 @@ export declare abstract class Ai<
> {
aiGatewayLogId: string | null;
gateway(gatewayId: string): AiGateway;
run<Name extends keyof AiModelList>(
run<Name extends keyof AiModelList, Options extends AiOptions>(
model: Name,
inputs: AiModelList[Name]["inputs"],
options?: AiOptions,
): Promise<AiModelList[Name]["postProcessedOutputs"]>;
options?: Options,
): Promise<
Options extends {
returnRawResponse: true;
}
? Response
: AiModelList[Name]["postProcessedOutputs"]
>;
public models(params?: AiModelsSearchParams): Promise<AiModelsSearchObject[]>;
}
export type GatewayOptions = {
Expand Down
13 changes: 10 additions & 3 deletions types/generated-snapshot/2022-11-30/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4170,6 +4170,7 @@ interface AiModels {
}
type AiOptions = {
gateway?: GatewayOptions;
returnRawResponse?: boolean;
prefix?: string;
extraHeaders?: object;
};
Expand Down Expand Up @@ -4204,11 +4205,17 @@ type AiModelListType = Record<string, any>;
declare abstract class Ai<AiModelList extends AiModelListType = AiModels> {
aiGatewayLogId: string | null;
gateway(gatewayId: string): AiGateway;
run<Name extends keyof AiModelList>(
run<Name extends keyof AiModelList, Options extends AiOptions>(
model: Name,
inputs: AiModelList[Name]["inputs"],
options?: AiOptions,
): Promise<AiModelList[Name]["postProcessedOutputs"]>;
options?: Options,
): Promise<
Options extends {
returnRawResponse: true;
}
? Response
: AiModelList[Name]["postProcessedOutputs"]
>;
public models(params?: AiModelsSearchParams): Promise<AiModelsSearchObject[]>;
}
type GatewayOptions = {
Expand Down
Loading

0 comments on commit 101e0c0

Please sign in to comment.