Skip to content

Commit

Permalink
add basic workers ai code
Browse files Browse the repository at this point in the history
Signed-off-by: oilbeater <[email protected]>
  • Loading branch information
oilbeater committed Oct 5, 2024
1 parent f52668c commit 2722143
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { azureOpenAIProvider } from './azureOpenAI';
import { workersAIProvider } from './workersAI';

export const providers = {
'azure-openai': azureOpenAIProvider,
};
'workers-ai': workersAIProvider,
};
32 changes: 32 additions & 0 deletions src/providers/workersAI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Context, Hono } from 'hono';
import { AIProvider } from '../types';

const ProviderName = 'workers-ai';
const BasePath = '/workers-ai';
const workersAIRoute = new Hono();
workersAIRoute.all('/*', async (c) => {
return workersAIProvider.handleRequest(c);
});

export const workersAIProvider: AIProvider = {
name: ProviderName,
basePath: BasePath,
route: workersAIRoute,
getModelName: getModelName,
getTokenCount: getTokenCount,
handleRequest: async (c: Context<{ Bindings: Env }>) => {
const response = await c.env.AI.run("@cf/meta/llama-3.1-8b-instruct",
await c.req.json());

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

function getModelName(c: Context) {
return "workers-ai";
}

function getTokenCount(c: Context) {
return { input_tokens: 0, output_tokens: 0 };
}

1 change: 1 addition & 0 deletions worker-configuration.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ interface Env {
MALACCA_CACHE: KVNamespace;
MALACCA: AnalyticsEngineDataset;
MY_RATE_LIMITER: RateLimit;
AI: Ai;
}
7 changes: 5 additions & 2 deletions wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#:schema node_modules/wrangler/config-schema.json
name = "malacca"
main = "src/index.ts"
compatibility_date = "2024-08-06"
compatibility_date = "2024-09-02"
compatibility_flags = ["nodejs_compat"]
minify = true
logpush = false
Expand Down Expand Up @@ -34,4 +34,7 @@ namespace_id = "1001"
# Limit: the number of tokens allowed within a given period in a single
# Cloudflare location
# Period: the duration of the period, in seconds. Must be either 10 or 60
simple = { limit = 100, period = 60 }
simple = { limit = 100, period = 60 }

[ai]
binding = "AI"

0 comments on commit 2722143

Please sign in to comment.