Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking β€œSign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reasoning for OpenRouter using OpenAI provider #6251

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Support OpenRouter reasoning when using env var
xsun2001 committed Feb 25, 2025

Verified

This commit was signed with the committer’s verified signature.
xsun2001 Xu Chenxi
commit a87ec75ba6b28fbb37e0d6c140ec211dc9455609
15 changes: 10 additions & 5 deletions app/api/common.ts
Original file line number Diff line number Diff line change
@@ -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 (
9 changes: 4 additions & 5 deletions app/client/platforms/openai.ts
Original file line number Diff line number Diff line change
@@ -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