Skip to content

Commit

Permalink
support more cloudflare worker ai model
Browse files Browse the repository at this point in the history
Signed-off-by: oilbeater <[email protected]>
  • Loading branch information
oilbeater committed Oct 9, 2024
1 parent b36989b commit d42172e
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/providers/workersAI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AIProvider } from '../types';
const ProviderName = 'workers-ai';
const BasePath = '/workers-ai';
const workersAIRoute = new Hono();
workersAIRoute.all('/*', async (c) => {
workersAIRoute.post('/:provider/:repo/:model', async (c) => {
return workersAIProvider.handleRequest(c);
});

Expand All @@ -15,12 +15,17 @@ export const workersAIProvider: AIProvider = {
getModelName: getModelName,
getTokenCount: getTokenCount,
handleRequest: async (c: Context<{ Bindings: Env }>) => {
const response = await c.env.AI.run("@cf/meta/llama-3.1-8b-instruct",
const provider = c.req.param('provider');
const repo = c.req.param('repo');
const model = c.req.param('model')
const response = await c.env.AI.run(`${provider}/${repo}/${model}`,
await c.req.json());

if (response instanceof ReadableStream) {
return new Response(response);
}
return new Response(response.response);

return new Response(JSON.stringify(response));
}
};

Expand All @@ -31,4 +36,3 @@ function getModelName(c: Context) {
function getTokenCount(c: Context) {
return { input_tokens: 0, output_tokens: 0 };
}

0 comments on commit d42172e

Please sign in to comment.