Skip to content

Commit

Permalink
add basic support for deepseek
Browse files Browse the repository at this point in the history
Signed-off-by: oilbeater <[email protected]>
  • Loading branch information
oilbeater committed Oct 10, 2024
1 parent d42172e commit 210c37a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/providers/azureOpenAI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const initMiddleware = async (c: Context, next: Next) => {
await next();
};


azureOpenAIRoute.use(initMiddleware, metricsMiddleware, loggingMiddleware, bufferMiddleware, virtualKeyMiddleware, rateLimiterMiddleware, guardMiddleware, cacheMiddleware, fallbackMiddleware);

azureOpenAIRoute.post('/*', async (c: Context) => {
Expand Down
76 changes: 76 additions & 0 deletions src/providers/deepseek.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Hono, Context, Next } from 'hono';
import { AIProvider } from '../types';
import {
cacheMiddleware,
metricsMiddleware,
bufferMiddleware,
loggingMiddleware,
virtualKeyMiddleware,
rateLimiterMiddleware,
guardMiddleware,
fallbackMiddleware
} from '../middlewares';

const BasePath = '/deepseek';
const ProviderName = 'deepseek';
const deepseekRoute = new Hono();

const initMiddleware = async (c: Context, next: Next) => {
c.set('endpoint', ProviderName);
c.set('getModelName', getModelName);
c.set('getTokenCount', getTokenCount);
await next();
};

deepseekRoute.post('/*', async (c: Context) => {
return deepseekProvider.handleRequest(c);
});

deepseekRoute.get('/*', async (c: Context) => {
return c.text('DeepSeek endpoint on Malacca.', 200, { 'Content-Type': 'text/plain' });
});

export const deepseekProvider: AIProvider = {
name: ProviderName,
basePath: BasePath,
route: deepseekRoute,
getModelName: getModelName,
getTokenCount: getTokenCount,
handleRequest: async (c: Context) => {
const functionName = c.req.path.slice(`/deepseek/`.length);
const deepseekEndpoint = `https://api.deepseek.com/${functionName}`;
console.log(`DeepSeek endpoint: ${deepseekEndpoint}`);

const response = await fetch(deepseekEndpoint, {
method: c.req.method,
body: JSON.stringify(await c.req.json()),
headers: c.req.header()
});

return response;
}
};

function getModelName(c: Context): string {
const model = c.req.param('model');
return model || "unknown";
}

function getTokenCount(c: Context): { input_tokens: number, output_tokens: number } {
const buf = c.get('buffer') || "";
if (c.res.status === 200) {
try {
const jsonResponse = JSON.parse(buf);
const usage = jsonResponse.usage;
if (usage) {
return {
input_tokens: usage.prompt_tokens || 0,
output_tokens: usage.completion_tokens || 0
};
}
} catch (error) {
console.error("Error parsing response:", error);
}
}
return { input_tokens: 0, output_tokens: 0 };
}
2 changes: 2 additions & 0 deletions src/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { azureOpenAIProvider } from './azureOpenAI';
import { workersAIProvider } from './workersAI';
import { deepseekProvider } from './deepseek';

export const providers = {
'azure-openai': azureOpenAIProvider,
'workers-ai': workersAIProvider,
'deepseek': deepseekProvider,
};
7 changes: 5 additions & 2 deletions src/providers/workersAI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const workersAIProvider: AIProvider = {
handleRequest: async (c: Context<{ Bindings: Env }>) => {
const provider = c.req.param('provider');
const repo = c.req.param('repo');
const model = c.req.param('model')
const model = c.req.param('model');
const response = await c.env.AI.run(`${provider}/${repo}/${model}`,
await c.req.json());

Expand All @@ -30,7 +30,10 @@ export const workersAIProvider: AIProvider = {
};

function getModelName(c: Context) {
return "workers-ai";
const provider = c.req.param('provider');
const repo = c.req.param('repo');
const model = c.req.param('model');
return `${provider}/${repo}/${model}`;
}

function getTokenCount(c: Context) {
Expand Down

0 comments on commit 210c37a

Please sign in to comment.