-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Improve OpenRouter model fetching #2922
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
45b9778
Improve OpenRouter model fetching
cte d1f8764
Use a hardcoded list
cte 007f3df
Thanks ellipsis
cte 10371e9
Fix test
cte 0176cc5
Move constants so they can be used by the webview
cte 3265a39
Update openrouter.ts
cte 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
25 changes: 25 additions & 0 deletions
25
src/api/providers/fetchers/__tests__/fixtures/openrouter-models.json
Large diffs are not rendered by default.
Oops, something went wrong.
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,70 @@ | ||
| // npx jest src/api/providers/fetchers/__tests__/openrouter.test.ts | ||
|
|
||
| import path from "path" | ||
|
|
||
| import { back as nockBack } from "nock" | ||
|
|
||
| import { getOpenRouterModels, modelsSupportingPromptCache } from "../openrouter" | ||
|
|
||
| nockBack.fixtures = path.join(__dirname, "fixtures") | ||
| nockBack.setMode("dryrun") | ||
|
|
||
| describe("OpenRouter API", () => { | ||
| describe("getOpenRouterModels", () => { | ||
| it("fetches models and validates schema", async () => { | ||
| const { nockDone } = await nockBack("openrouter-models.json") | ||
|
|
||
| const models = await getOpenRouterModels() | ||
|
|
||
| expect( | ||
| Object.entries(models) | ||
| .filter(([_, model]) => model.supportsPromptCache) | ||
| .map(([id, _]) => id) | ||
| .sort(), | ||
| ).toEqual(Array.from(modelsSupportingPromptCache).sort()) | ||
|
|
||
| expect( | ||
| Object.entries(models) | ||
| .filter(([_, model]) => model.supportsComputerUse) | ||
| .map(([id, _]) => id) | ||
| .sort(), | ||
| ).toEqual([ | ||
| "anthropic/claude-3.5-sonnet", | ||
| "anthropic/claude-3.5-sonnet:beta", | ||
| "anthropic/claude-3.7-sonnet", | ||
| "anthropic/claude-3.7-sonnet:beta", | ||
| "anthropic/claude-3.7-sonnet:thinking", | ||
| ]) | ||
|
|
||
| expect(models["anthropic/claude-3.7-sonnet"]).toEqual({ | ||
| maxTokens: 8192, | ||
| contextWindow: 200000, | ||
| supportsImages: true, | ||
| supportsPromptCache: true, | ||
| inputPrice: 3, | ||
| outputPrice: 15, | ||
| cacheWritesPrice: 3.75, | ||
| cacheReadsPrice: 0.3, | ||
| description: expect.any(String), | ||
| thinking: false, | ||
| supportsComputerUse: true, | ||
| }) | ||
|
|
||
| expect(models["anthropic/claude-3.7-sonnet:thinking"]).toEqual({ | ||
| maxTokens: 128000, | ||
| contextWindow: 200000, | ||
| supportsImages: true, | ||
| supportsPromptCache: true, | ||
| inputPrice: 3, | ||
| outputPrice: 15, | ||
| cacheWritesPrice: 3.75, | ||
| cacheReadsPrice: 0.3, | ||
| description: expect.any(String), | ||
| thinking: true, | ||
| supportsComputerUse: true, | ||
| }) | ||
|
|
||
| nockDone() | ||
| }) | ||
| }) | ||
| }) |
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,135 @@ | ||
| import axios from "axios" | ||
| import { z } from "zod" | ||
|
|
||
| import { ApiHandlerOptions, ModelInfo } from "../../../shared/api" | ||
| import { parseApiPrice } from "../../../utils/cost" | ||
|
|
||
| // https://openrouter.ai/api/v1/models | ||
| export const openRouterModelSchema = z.object({ | ||
| id: z.string(), | ||
| name: z.string(), | ||
| description: z.string().optional(), | ||
| context_length: z.number(), | ||
| max_completion_tokens: z.number().nullish(), | ||
| architecture: z | ||
| .object({ | ||
| modality: z.string().nullish(), | ||
| tokenizer: z.string().nullish(), | ||
| }) | ||
| .optional(), | ||
| pricing: z | ||
| .object({ | ||
| prompt: z.string().nullish(), | ||
| completion: z.string().nullish(), | ||
| input_cache_write: z.string().nullish(), | ||
| input_cache_read: z.string().nullish(), | ||
| }) | ||
| .optional(), | ||
| top_provider: z | ||
| .object({ | ||
| max_completion_tokens: z.number().nullish(), | ||
| }) | ||
| .optional(), | ||
| }) | ||
|
|
||
| export type OpenRouterModel = z.infer<typeof openRouterModelSchema> | ||
|
|
||
| const openRouterModelsResponseSchema = z.object({ | ||
| data: z.array(openRouterModelSchema), | ||
| }) | ||
|
|
||
| type OpenRouterModelsResponse = z.infer<typeof openRouterModelsResponseSchema> | ||
|
|
||
| export async function getOpenRouterModels(options?: ApiHandlerOptions) { | ||
| const models: Record<string, ModelInfo> = {} | ||
| const baseURL = options?.openRouterBaseUrl || "https://openrouter.ai/api/v1" | ||
|
|
||
| try { | ||
| const response = await axios.get<OpenRouterModelsResponse>(`${baseURL}/models`) | ||
| const result = openRouterModelsResponseSchema.safeParse(response.data) | ||
| const rawModels = result.success ? result.data.data : response.data.data | ||
|
|
||
| if (!result.success) { | ||
| console.error("OpenRouter models response is invalid", result.error.format()) | ||
| } | ||
|
|
||
| for (const rawModel of rawModels) { | ||
| const cacheWritesPrice = rawModel.pricing?.input_cache_write | ||
| ? parseApiPrice(rawModel.pricing?.input_cache_write) | ||
| : undefined | ||
|
|
||
| const cacheReadsPrice = rawModel.pricing?.input_cache_read | ||
| ? parseApiPrice(rawModel.pricing?.input_cache_read) | ||
| : undefined | ||
|
|
||
| // Disable prompt caching for Gemini models for now. | ||
| const supportsPromptCache = !!cacheWritesPrice && !!cacheWritesPrice && !rawModel.id.startsWith("google") | ||
|
|
||
| const modelInfo: ModelInfo = { | ||
| maxTokens: rawModel.top_provider?.max_completion_tokens, | ||
| contextWindow: rawModel.context_length, | ||
| supportsImages: rawModel.architecture?.modality?.includes("image"), | ||
| supportsPromptCache, | ||
| inputPrice: parseApiPrice(rawModel.pricing?.prompt), | ||
| outputPrice: parseApiPrice(rawModel.pricing?.completion), | ||
| cacheWritesPrice, | ||
| cacheReadsPrice, | ||
| description: rawModel.description, | ||
| thinking: rawModel.id === "anthropic/claude-3.7-sonnet:thinking", | ||
| } | ||
|
|
||
| // NOTE: This needs to be synced with api.ts/openrouter default model info. | ||
| switch (true) { | ||
| case rawModel.id.startsWith("anthropic/claude-3.7-sonnet"): | ||
| modelInfo.supportsComputerUse = true | ||
| modelInfo.maxTokens = rawModel.id === "anthropic/claude-3.7-sonnet:thinking" ? 128_000 : 8192 | ||
| break | ||
| case rawModel.id.startsWith("anthropic/claude-3.5-sonnet-20240620"): | ||
| modelInfo.maxTokens = 8192 | ||
| break | ||
| case rawModel.id.startsWith("anthropic/claude-3.5-sonnet"): | ||
| modelInfo.supportsComputerUse = true | ||
| modelInfo.maxTokens = 8192 | ||
| break | ||
| case rawModel.id.startsWith("anthropic/claude-3-5-haiku"): | ||
| case rawModel.id.startsWith("anthropic/claude-3-opus"): | ||
| case rawModel.id.startsWith("anthropic/claude-3-haiku"): | ||
| modelInfo.maxTokens = 8192 | ||
| break | ||
| default: | ||
| break | ||
| } | ||
|
|
||
| models[rawModel.id] = modelInfo | ||
| } | ||
| } catch (error) { | ||
| console.error( | ||
| `Error fetching OpenRouter models: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`, | ||
| ) | ||
| } | ||
|
|
||
| return models | ||
| } | ||
|
|
||
| export const modelsSupportingPromptCache = new Set([ | ||
| "anthropic/claude-3-haiku", | ||
| "anthropic/claude-3-haiku:beta", | ||
| "anthropic/claude-3-opus", | ||
| "anthropic/claude-3-opus:beta", | ||
| "anthropic/claude-3-sonnet", | ||
| "anthropic/claude-3-sonnet:beta", | ||
| "anthropic/claude-3.5-haiku", | ||
| "anthropic/claude-3.5-haiku-20241022", | ||
| "anthropic/claude-3.5-haiku-20241022:beta", | ||
| "anthropic/claude-3.5-haiku:beta", | ||
| "anthropic/claude-3.5-sonnet", | ||
| "anthropic/claude-3.5-sonnet-20240620", | ||
| "anthropic/claude-3.5-sonnet-20240620:beta", | ||
| "anthropic/claude-3.5-sonnet:beta", | ||
| "anthropic/claude-3.7-sonnet", | ||
| "anthropic/claude-3.7-sonnet:beta", | ||
| "anthropic/claude-3.7-sonnet:thinking", | ||
| // "google/gemini-2.0-flash-001", | ||
| // "google/gemini-flash-1.5", | ||
| // "google/gemini-flash-1.5-8b", | ||
| ]) | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.