Support OpenRouter reasoning when using env var

This commit is contained in:
xsun2001 2025-02-25 16:35:03 +08:00
parent 6e082ad7ac
commit a87ec75ba6
No known key found for this signature in database
GPG Key ID: 64E172564706AEA8
2 changed files with 14 additions and 10 deletions

View File

@ -90,6 +90,14 @@ export async function requestOpenai(req: NextRequest) {
const fetchUrl = cloudflareAIGatewayUrl(`${baseUrl}/${path}`);
console.log("fetchUrl", fetchUrl);
let payload = await req.text();
if (baseUrl.includes("openrouter.ai")) {
const body = JSON.parse(payload);
body["include_reasoning"] = true;
payload = JSON.stringify(body);
}
const fetchOptions: RequestInit = {
headers: {
"Content-Type": "application/json",
@ -100,7 +108,7 @@ export async function requestOpenai(req: NextRequest) {
}),
},
method: req.method,
body: req.body,
body: payload,
// to fix #2485: https://stackoverflow.com/questions/55920957/cloudflare-worker-typeerror-one-time-use-body
redirect: "manual",
// @ts-ignore
@ -111,10 +119,7 @@ export async function requestOpenai(req: NextRequest) {
// #1815 try to refuse gpt4 request
if (serverConfig.customModels && req.body) {
try {
const clonedBody = await req.text();
fetchOptions.body = clonedBody;
const jsonBody = JSON.parse(clonedBody) as { model?: string };
const jsonBody = JSON.parse(payload) as { model?: string };
// not undefined and is false
if (

View File

@ -296,8 +296,7 @@ export class ChatGPTApi implements LLMApi {
// console.log("getAsTools", tools, funcs);
// Add "include_reasoning" for OpenRouter: https://openrouter.ai/announcements/reasoning-tokens-for-thinking-models
const isOpenRouter = chatPath.includes("openrouter.ai");
if (isOpenRouter) {
if (chatPath.includes("openrouter.ai")) {
// @ts-ignore
requestPayload["include_reasoning"] = true;
}
@ -344,9 +343,9 @@ export class ChatGPTApi implements LLMApi {
}
}
const reasoning = isOpenRouter
? choices[0]?.delta?.reasoning
: choices[0]?.delta?.reasoning_content;
const reasoning =
choices[0]?.delta?.reasoning_content ||
choices[0]?.delta?.reasoning;
const content = choices[0]?.delta?.content;
// Skip if both content and reasoning_content are empty or null