Support OpenRouter reasoning when using env var
This commit is contained in:
parent
6e082ad7ac
commit
a87ec75ba6
|
@ -90,6 +90,14 @@ export async function requestOpenai(req: NextRequest) {
|
||||||
|
|
||||||
const fetchUrl = cloudflareAIGatewayUrl(`${baseUrl}/${path}`);
|
const fetchUrl = cloudflareAIGatewayUrl(`${baseUrl}/${path}`);
|
||||||
console.log("fetchUrl", fetchUrl);
|
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 = {
|
const fetchOptions: RequestInit = {
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
|
@ -100,7 +108,7 @@ export async function requestOpenai(req: NextRequest) {
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
method: req.method,
|
method: req.method,
|
||||||
body: req.body,
|
body: payload,
|
||||||
// to fix #2485: https://stackoverflow.com/questions/55920957/cloudflare-worker-typeerror-one-time-use-body
|
// to fix #2485: https://stackoverflow.com/questions/55920957/cloudflare-worker-typeerror-one-time-use-body
|
||||||
redirect: "manual",
|
redirect: "manual",
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
@ -111,10 +119,7 @@ export async function requestOpenai(req: NextRequest) {
|
||||||
// #1815 try to refuse gpt4 request
|
// #1815 try to refuse gpt4 request
|
||||||
if (serverConfig.customModels && req.body) {
|
if (serverConfig.customModels && req.body) {
|
||||||
try {
|
try {
|
||||||
const clonedBody = await req.text();
|
const jsonBody = JSON.parse(payload) as { model?: string };
|
||||||
fetchOptions.body = clonedBody;
|
|
||||||
|
|
||||||
const jsonBody = JSON.parse(clonedBody) as { model?: string };
|
|
||||||
|
|
||||||
// not undefined and is false
|
// not undefined and is false
|
||||||
if (
|
if (
|
||||||
|
|
|
@ -296,8 +296,7 @@ export class ChatGPTApi implements LLMApi {
|
||||||
// console.log("getAsTools", tools, funcs);
|
// console.log("getAsTools", tools, funcs);
|
||||||
|
|
||||||
// Add "include_reasoning" for OpenRouter: https://openrouter.ai/announcements/reasoning-tokens-for-thinking-models
|
// Add "include_reasoning" for OpenRouter: https://openrouter.ai/announcements/reasoning-tokens-for-thinking-models
|
||||||
const isOpenRouter = chatPath.includes("openrouter.ai");
|
if (chatPath.includes("openrouter.ai")) {
|
||||||
if (isOpenRouter) {
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
requestPayload["include_reasoning"] = true;
|
requestPayload["include_reasoning"] = true;
|
||||||
}
|
}
|
||||||
|
@ -344,9 +343,9 @@ export class ChatGPTApi implements LLMApi {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const reasoning = isOpenRouter
|
const reasoning =
|
||||||
? choices[0]?.delta?.reasoning
|
choices[0]?.delta?.reasoning_content ||
|
||||||
: choices[0]?.delta?.reasoning_content;
|
choices[0]?.delta?.reasoning;
|
||||||
const content = choices[0]?.delta?.content;
|
const content = choices[0]?.delta?.content;
|
||||||
|
|
||||||
// Skip if both content and reasoning_content are empty or null
|
// Skip if both content and reasoning_content are empty or null
|
||||||
|
|
Loading…
Reference in New Issue