|
import { Hono } from 'hono' |
|
import { Context } from 'hono'; |
|
import { bearerAuth } from 'hono/bearer-auth' |
|
import OpenAI from "openai"; |
|
|
|
const openai = new OpenAI({ |
|
apiKey: "sk-1234", |
|
baseURL: "https://openai-endpoint.ishaanjaffer0324.workers.dev" |
|
}); |
|
|
|
async function call_proxy() { |
|
const completion = await openai.chat.completions.create({ |
|
messages: [{ role: "system", content: "You are a helpful assistant." }], |
|
model: "gpt-3.5-turbo", |
|
}); |
|
|
|
return completion |
|
} |
|
|
|
const app = new Hono() |
|
|
|
|
|
const apiKeyAuth = async (c: Context, next: Function) => { |
|
const apiKey = c.req.header('Authorization'); |
|
if (!apiKey || apiKey !== 'Bearer sk-1234') { |
|
return c.text('Unauthorized', 401); |
|
} |
|
await next(); |
|
}; |
|
|
|
|
|
app.use('/*', apiKeyAuth) |
|
|
|
|
|
app.get('/', (c) => { |
|
return c.text('Hello Hono!') |
|
}) |
|
|
|
|
|
|
|
|
|
|
|
const chatCompletionHandler = async (c: Context) => { |
|
|
|
|
|
const response = await call_proxy() |
|
return c.json(response); |
|
}; |
|
|
|
|
|
app.post('/v1/chat/completions', chatCompletionHandler); |
|
app.post('/chat/completions', chatCompletionHandler); |
|
|
|
|
|
|
|
app.post('/openai/deployments/:model*/chat/completions', chatCompletionHandler); |
|
|
|
|
|
export default app |
|
|