|
| 1 | +import axios from "axios" |
| 2 | +import { z } from "zod" |
| 3 | + |
| 4 | +import { ApiHandlerOptions, ModelInfo } from "../../../shared/api" |
| 5 | +import { parseApiPrice } from "../../../utils/cost" |
| 6 | + |
| 7 | +// https://openrouter.ai/api/v1/models |
| 8 | +export const openRouterModelSchema = z.object({ |
| 9 | + id: z.string(), |
| 10 | + name: z.string(), |
| 11 | + description: z.string().optional(), |
| 12 | + context_length: z.number(), |
| 13 | + max_completion_tokens: z.number().nullish(), |
| 14 | + architecture: z |
| 15 | + .object({ |
| 16 | + modality: z.string().nullish(), |
| 17 | + tokenizer: z.string().nullish(), |
| 18 | + }) |
| 19 | + .optional(), |
| 20 | + pricing: z |
| 21 | + .object({ |
| 22 | + prompt: z.string().nullish(), |
| 23 | + completion: z.string().nullish(), |
| 24 | + input_cache_write: z.string().nullish(), |
| 25 | + input_cache_read: z.string().nullish(), |
| 26 | + }) |
| 27 | + .optional(), |
| 28 | + top_provider: z |
| 29 | + .object({ |
| 30 | + max_completion_tokens: z.number().nullish(), |
| 31 | + }) |
| 32 | + .optional(), |
| 33 | +}) |
| 34 | + |
| 35 | +export type OpenRouterModel = z.infer<typeof openRouterModelSchema> |
| 36 | + |
| 37 | +const openRouterModelsResponseSchema = z.object({ |
| 38 | + data: z.array(openRouterModelSchema), |
| 39 | +}) |
| 40 | + |
| 41 | +type OpenRouterModelsResponse = z.infer<typeof openRouterModelsResponseSchema> |
| 42 | + |
| 43 | +export async function getOpenRouterModels(options?: ApiHandlerOptions) { |
| 44 | + const models: Record<string, ModelInfo> = {} |
| 45 | + const baseURL = options?.openRouterBaseUrl || "https://openrouter.ai/api/v1" |
| 46 | + |
| 47 | + try { |
| 48 | + const response = await axios.get<OpenRouterModelsResponse>(`${baseURL}/models`) |
| 49 | + const result = openRouterModelsResponseSchema.safeParse(response.data) |
| 50 | + const rawModels = result.success ? result.data.data : response.data.data |
| 51 | + |
| 52 | + if (!result.success) { |
| 53 | + console.error("OpenRouter models response is invalid", result.error.format()) |
| 54 | + } |
| 55 | + |
| 56 | + for (const rawModel of rawModels) { |
| 57 | + const cacheWritesPrice = rawModel.pricing?.input_cache_write |
| 58 | + ? parseApiPrice(rawModel.pricing?.input_cache_write) |
| 59 | + : undefined |
| 60 | + |
| 61 | + const cacheReadsPrice = rawModel.pricing?.input_cache_read |
| 62 | + ? parseApiPrice(rawModel.pricing?.input_cache_read) |
| 63 | + : undefined |
| 64 | + |
| 65 | + const supportsPromptCache = !!cacheWritesPrice && !!cacheWritesPrice |
| 66 | + |
| 67 | + const modelInfo: ModelInfo = { |
| 68 | + maxTokens: rawModel.top_provider?.max_completion_tokens, |
| 69 | + contextWindow: rawModel.context_length, |
| 70 | + supportsImages: rawModel.architecture?.modality?.includes("image"), |
| 71 | + supportsPromptCache, |
| 72 | + inputPrice: parseApiPrice(rawModel.pricing?.prompt), |
| 73 | + outputPrice: parseApiPrice(rawModel.pricing?.completion), |
| 74 | + cacheWritesPrice, |
| 75 | + cacheReadsPrice, |
| 76 | + description: rawModel.description, |
| 77 | + thinking: rawModel.id === "anthropic/claude-3.7-sonnet:thinking", |
| 78 | + } |
| 79 | + |
| 80 | + // NOTE: This needs to be synced with api.ts/openrouter default model info. |
| 81 | + switch (true) { |
| 82 | + case rawModel.id.startsWith("anthropic/claude-3.7-sonnet"): |
| 83 | + modelInfo.supportsComputerUse = true |
| 84 | + modelInfo.maxTokens = rawModel.id === "anthropic/claude-3.7-sonnet:thinking" ? 128_000 : 8192 |
| 85 | + break |
| 86 | + case rawModel.id.startsWith("anthropic/claude-3.5-sonnet-20240620"): |
| 87 | + modelInfo.maxTokens = 8192 |
| 88 | + break |
| 89 | + case rawModel.id.startsWith("anthropic/claude-3.5-sonnet"): |
| 90 | + modelInfo.supportsComputerUse = true |
| 91 | + modelInfo.maxTokens = 8192 |
| 92 | + break |
| 93 | + case rawModel.id.startsWith("anthropic/claude-3-5-haiku"): |
| 94 | + case rawModel.id.startsWith("anthropic/claude-3-opus"): |
| 95 | + case rawModel.id.startsWith("anthropic/claude-3-haiku"): |
| 96 | + modelInfo.maxTokens = 8192 |
| 97 | + break |
| 98 | + default: |
| 99 | + break |
| 100 | + } |
| 101 | + |
| 102 | + models[rawModel.id] = modelInfo |
| 103 | + } |
| 104 | + } catch (error) { |
| 105 | + console.error( |
| 106 | + `Error fetching OpenRouter models: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`, |
| 107 | + ) |
| 108 | + } |
| 109 | + |
| 110 | + return models |
| 111 | +} |
0 commit comments