Repository navigation
Add OpenRouter as a configurable inference provider #803
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
Open
ngoiyaeric
wants to merge
1
commit into
main
Choose a base branch
from
feat/openrouter-inference
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { createOpenAI } from '@ai-sdk/openai'; | ||
|
|
||
| const OPENROUTER_BASE_URL = 'https://openrouter.ai/api/v1'; | ||
| const DEFAULT_OPENROUTER_MODEL = 'openai/gpt-4o'; | ||
| // Keep this at ada-002 so new document/query vectors remain in the same vector space as existing rows. | ||
| const DEFAULT_OPENROUTER_EMBEDDING_MODEL = 'openai/text-embedding-ada-002'; | ||
|
|
||
| function createOpenRouterProvider() { | ||
| const apiKey = process.env.OPENROUTER_API_KEY?.trim(); | ||
| if (!apiKey) { | ||
| throw new Error('INFERENCE_PROVIDER is "openrouter" but OPENROUTER_API_KEY is not configured.'); | ||
| } | ||
|
|
||
| const headers: Record<string, string> = { | ||
| 'X-Title': process.env.OPENROUTER_APP_NAME?.trim() || 'QCX', | ||
| }; | ||
| const siteUrl = process.env.OPENROUTER_SITE_URL?.trim() || process.env.NEXT_PUBLIC_APP_URL?.trim(); | ||
| if (siteUrl) headers['HTTP-Referer'] = siteUrl; | ||
|
|
||
| return createOpenAI({ | ||
| apiKey, | ||
| baseURL: OPENROUTER_BASE_URL, | ||
| headers, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Creates an OpenRouter model using the OpenAI-compatible Chat Completions API. | ||
| * The default model supports the text, vision, tool-call, and structured-output | ||
| * paths used by QCX. Deployments may choose a different model through env vars. | ||
| */ | ||
| export function createOpenRouterModel(requireVision = false) { | ||
| const textModel = process.env.OPENROUTER_MODEL?.trim() || DEFAULT_OPENROUTER_MODEL; | ||
| const modelId = requireVision | ||
| ? process.env.OPENROUTER_VISION_MODEL?.trim() || textModel | ||
| : textModel; | ||
| // OpenRouter exposes Chat Completions compatibility, not OpenAI Responses. | ||
| return createOpenRouterProvider().chat(modelId); | ||
| } | ||
|
|
||
| export function createOpenRouterEmbeddingModel() { | ||
| const modelId = process.env.OPENROUTER_EMBEDDING_MODEL?.trim() || DEFAULT_OPENROUTER_EMBEDDING_MODEL; | ||
| return createOpenRouterProvider().embedding(modelId); | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { createOpenRouterEmbeddingModel, createOpenRouterModel } from '../lib/utils/openrouter'; | ||
|
|
||
| const envKeys = [ | ||
| 'OPENROUTER_API_KEY', | ||
| 'OPENROUTER_MODEL', | ||
| 'OPENROUTER_VISION_MODEL', | ||
| 'OPENROUTER_EMBEDDING_MODEL', | ||
| 'OPENROUTER_SITE_URL', | ||
| 'OPENROUTER_APP_NAME', | ||
| ] as const; | ||
| const previousValues = Object.fromEntries(envKeys.map((key) => [key, process.env[key]])); | ||
|
|
||
| try { | ||
| for (const key of envKeys) delete process.env[key]; | ||
| assert.throws(() => createOpenRouterModel(), /OPENROUTER_API_KEY is not configured/); | ||
|
|
||
| process.env.OPENROUTER_API_KEY = 'test-key'; | ||
| const defaultModel = createOpenRouterModel(); | ||
| assert.equal(defaultModel.modelId, 'openai/gpt-4o'); | ||
|
|
||
| process.env.OPENROUTER_MODEL = 'google/gemini-2.5-pro'; | ||
| const textModel = createOpenRouterModel(); | ||
| assert.equal(textModel.modelId, 'google/gemini-2.5-pro'); | ||
|
|
||
| process.env.OPENROUTER_VISION_MODEL = 'openai/gpt-4o'; | ||
| const visionModel = createOpenRouterModel(true); | ||
| assert.equal(visionModel.modelId, 'openai/gpt-4o'); | ||
|
|
||
| const embeddingModel = createOpenRouterEmbeddingModel(); | ||
| assert.equal(embeddingModel.modelId, 'openai/text-embedding-ada-002'); | ||
| } finally { | ||
| for (const key of envKeys) { | ||
| const value = previousValues[key]; | ||
| if (value === undefined) delete process.env[key]; | ||
| else process.env[key] = value; | ||
| } | ||
| } | ||
|
|
||
| console.log('OpenRouter provider tests passed.'); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: QueueLab/QCX
Length of output: 1755
🏁 Script executed:
Repository: QueueLab/QCX
Length of output: 11216
🏁 Script executed:
Repository: QueueLab/QCX
Length of output: 20518
Resolve OpenRouter before the preference lookup.
getSelectedModel()callsauth()before its error handling. If that call rejects during a chat request,getModel()exits before selecting OpenRouter. Move the explicit provider branch beforegetSelectedModel().🐛 Suggested fix
🤖 Prompt for AI Agents