-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Custom Open AI API Endpoint (#62)
* 实现可切换的 OpenAI 请求地址功能 * Update README.md --------- Co-authored-by: huchangzhi <857114841@qq.com> Co-authored-by: Jazee <jazee@jaze.top>
- Loading branch information
1 parent
675c11d
commit 1b74aef
Showing
2 changed files
with
17 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |