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

Send Workers AI + AI Gateway requests to new endpoint #3285

Merged
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
33 changes: 24 additions & 9 deletions src/cloudflare/internal/ai-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface AiError {
message: string;
name: string;
description: string;
errors?: Array<{ code: number; message: string }>;
}

export type SessionOptions = {
Expand Down Expand Up @@ -96,10 +97,12 @@ export class Ai {
},
};

const res = await this.fetcher.fetch(
'https://workers-binding.ai/run?version=3',
fetchOptions
);
let endpointUrl = 'https://workers-binding.ai/run?version=3';
if (options.gateway?.id) {
endpointUrl = 'https://workers-binding.ai/ai-gateway/run?version=3';
}

const res = await this.fetcher.fetch(endpointUrl, fetchOptions);

this.lastRequestId = res.headers.get('cf-ai-req-id');
this.aiGatewayLogId = res.headers.get('cf-aig-log-id');
Expand Down Expand Up @@ -138,11 +141,23 @@ export class Ai {

try {
const parsedContent = JSON.parse(content) as AiError;
this.lastRequestInternalStatusCode = parsedContent.internalCode;
return new InferenceUpstreamError(
`${parsedContent.internalCode}: ${parsedContent.description}`,
parsedContent.name
);
if (parsedContent.internalCode) {
this.lastRequestInternalStatusCode = parsedContent.internalCode;
return new InferenceUpstreamError(
`${parsedContent.internalCode}: ${parsedContent.description}`,
parsedContent.name
);
} else if (
parsedContent.errors &&
parsedContent.errors.length > 0 &&
parsedContent.errors[0]
) {
return new InferenceUpstreamError(
`${parsedContent.errors[0].code}: ${parsedContent.errors[0].message}`
);
} else {
return new InferenceUpstreamError(content);
}
} catch {
return new InferenceUpstreamError(content);
}
Expand Down
8 changes: 7 additions & 1 deletion src/cloudflare/internal/test/ai/ai-api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ export const tests = {
// Test raw input
const resp = await env.ai.run('rawInputs', { prompt: 'test' });

assert.deepStrictEqual(resp, { inputs: { prompt: 'test' }, options: {} });
assert.deepStrictEqual(resp, {
inputs: { prompt: 'test' },
options: {},
requestUrl: 'https://workers-binding.ai/run?version=3',
});
}

{
Expand All @@ -105,6 +109,7 @@ export const tests = {
assert.deepStrictEqual(resp, {
inputs: { prompt: 'test' },
options: { gateway: { id: 'my-gateway', skipCache: true } },
requestUrl: 'https://workers-binding.ai/ai-gateway/run?version=3',
});
}

Expand All @@ -126,6 +131,7 @@ export const tests = {
example: 123,
gateway: { id: 'my-gateway', metadata: { employee: 1233 } },
},
requestUrl: 'https://workers-binding.ai/ai-gateway/run?version=3',
});
}
},
Expand Down
12 changes: 9 additions & 3 deletions src/cloudflare/internal/test/ai/ai-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ export default {
}

if (modelName === 'rawInputs') {
return Response.json(data, {
headers: respHeaders,
});
return Response.json(
{
...data,
requestUrl: request.url,
},
{
headers: respHeaders,
}
);
}

if (modelName === 'inputErrorModel') {
Expand Down
Loading