Skip to content

Commit

Permalink
feat: Custom Open AI API Endpoint (#62)
Browse files Browse the repository at this point in the history
* 实现可切换的 OpenAI 请求地址功能
* Update README.md

---------

Co-authored-by: huchangzhi <857114841@qq.com>
Co-authored-by: Jazee <jazee@jaze.top>
  • Loading branch information
3 people authored Sep 20, 2024
1 parent 675c11d commit 1b74aef
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ https://developers.cloudflare.com/workers-ai/models/
| CF_TOKEN | Cloudflare Workers AI Token |
| CF_GATEWAY | Cloudflare AI Gateway URL |
| OPENAI_API_KEY | OpenAI API Key (需要ChatGPT时填写) |
| OPENAI_API_URL | 自定义OpenAI API请求地址 |
| G_API_KEY | Google AI API Key (需要GeminiPro时填写) |
| G_API_URL | Google AI 反代 (不支持地区填写,或参考以下配置) |
| PASSWORD | 访问密码 (可选) |
Expand Down
27 changes: 16 additions & 11 deletions server/api/auth/openai.post.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
import {handleErr, openaiParser, streamResponse} from "~/utils/helper";
import {OpenAIBody, OpenAIReq} from "~/utils/types";
import { handleErr, openaiParser, streamResponse } from "~/utils/helper";
import { OpenAIBody, OpenAIReq } from "~/utils/types";

export default defineEventHandler(async (event) => {
const body: OpenAIReq = await readBody(event)
const {model, endpoint, messages, key} = body
const body: OpenAIReq = await readBody(event);
const { model, messages, key } = body;

const openAIBody: OpenAIBody = {
stream: true,
model,
model, // 使用传入的模型参数
messages,
}
};

// 检查是否提供了自定义的 OPENAI_API_URL
const apiUrl = process.env.OPENAI_API_URL ?
`${process.env.OPENAI_API_URL}/v1/chat/completions` :
`${process.env.CF_GATEWAY}/openai/${endpoint}`;

const res = await fetch(`${process.env.CF_GATEWAY}/openai/${endpoint}`, {
const res = await fetch(apiUrl, {
method: 'POST',
headers: {
Authorization: key === undefined ? `Bearer ${process.env.OPENAI_API_KEY}` : `Bearer ${key}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(openAIBody),
})
});

if (!res.ok) {
return handleErr(res)
return handleErr(res);
}

return streamResponse(res, openaiParser)
})
return streamResponse(res, openaiParser);
});

0 comments on commit 1b74aef

Please sign in to comment.