🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
💄 style: ModelFetcher support getting prices (#8985)
sxjeru58b73ecc951e
diff --git a/packages/model-runtime/src/novita/__snapshots__/index.test.ts.snap b/packages/model-runtime/src/novita/__snapshots__/index.test.ts.snapindex 3ba5b78c3c1..7a1f1301566 100644--- a/packages/model-runtime/src/novita/__snapshots__/index.test.ts.snap+++ b/packages/model-runtime/src/novita/__snapshots__/index.test.ts.snap@@ -5,11 +5,27 @@ exports[`NovitaAI > models > should get models 1`] = `{"contextWindowTokens": 8192,"description": "Meta's latest class of model (Llama 3) launched with a variety of sizes & flavors. This 8B instruct-tuned version was optimized for high quality dialogue usecases. It has demonstrated strong performance compared to leading closed-source models in human evaluations.",- "displayName": "Llama 3 8B Instruct",+ "displayName": "meta-llama/llama-3-8b-instruct","enabled": false,"functionCall": false,"id": "meta-llama/llama-3-8b-instruct","maxOutput": undefined,+ "pricing": {+ "units": [+ {+ "name": "textInput",+ "rate": 0.063,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ {+ "name": "textOutput",+ "rate": 0.063,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ ],+ },"reasoning": false,"releasedAt": "2024-04-25","type": "chat",@@ -18,50 +34,114 @@ exports[`NovitaAI > models > should get models 1`] = `{"contextWindowTokens": 8192,"description": "Meta's latest class of model (Llama 3) launched with a variety of sizes & flavors. This 70B instruct-tuned version was optimized for high quality dialogue usecases. It has demonstrated strong performance compared to leading closed-source models in human evaluations.",- "displayName": "Llama 3 70B Instruct",+ "displayName": "meta-llama/llama-3-70b-instruct","enabled": false,"functionCall": false,"id": "meta-llama/llama-3-70b-instruct","maxOutput": undefined,+ "pricing": {+ "units": [+ {+ "name": "textInput",+ "rate": 0.55,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ {+ "name": "textOutput",+ "rate": 0.76,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ ],+ },"reasoning": false,"releasedAt": "2024-04-25","type": "chat","vision": false,},{- "contextWindowTokens": 16384,+ "contextWindowTokens": 8192,"description": "Meta's latest class of models, Llama 3.1, launched with a variety of sizes and configurations. The 8B instruct-tuned version is particularly fast and efficient. It has demonstrated strong performance in human evaluations, outperforming several leading closed-source models.",- "displayName": "Llama 3.1 8B Instruct",+ "displayName": "meta-llama/llama-3.1-8b-instruct","enabled": false,"functionCall": false,"id": "meta-llama/llama-3.1-8b-instruct","maxOutput": undefined,+ "pricing": {+ "units": [+ {+ "name": "textInput",+ "rate": 0.1,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ {+ "name": "textOutput",+ "rate": 0.1,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ ],+ },"reasoning": false,"releasedAt": "2024-07-24","type": "chat","vision": false,},{- "contextWindowTokens": 32768,+ "contextWindowTokens": 8192,"description": "Meta's latest class of models, Llama 3.1, has launched with a variety of sizes and configurations. The 70B instruct-tuned version is optimized for high-quality dialogue use cases. It has demonstrated strong performance in human evaluations compared to leading closed-source models.","displayName": "meta-llama/llama-3.1-70b-instruct","enabled": false,"functionCall": false,"id": "meta-llama/llama-3.1-70b-instruct","maxOutput": undefined,+ "pricing": {+ "units": [+ {+ "name": "textInput",+ "rate": 0.55,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ {+ "name": "textOutput",+ "rate": 0.76,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ ],+ },"reasoning": false,"releasedAt": "2024-07-24","type": "chat","vision": false,},{- "contextWindowTokens": undefined,+ "contextWindowTokens": 32768,"description": "Meta's latest class of models, Llama 3.1, launched with a variety of sizes and configurations. This 405B instruct-tuned version is optimized for high-quality dialogue use cases. It has demonstrated strong performance compared to leading closed-source models, including GPT-4o and Claude 3.5 Sonnet, in evaluations.","displayName": "meta-llama/llama-3.1-405b-instruct","enabled": false,"functionCall": false,"id": "meta-llama/llama-3.1-405b-instruct","maxOutput": undefined,+ "pricing": {+ "units": [+ {+ "name": "textInput",+ "rate": 3,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ {+ "name": "textOutput",+ "rate": 5,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ ],+ },"reasoning": false,"releasedAt": "2024-07-24","type": "chat",@@ -71,24 +151,56 @@ exports[`NovitaAI > models > should get models 1`] = `"contextWindowTokens": 8192,"description": "Gemma 2 9B by Google is an advanced, open-source language model that sets a new standard for efficiency and performance in its size class.Designed for a wide variety of tasks, it empowers developers and researchers to build innovative applications, while maintaining accessibility, safety, and cost-effectiveness.",- "displayName": "Gemma 2 9B Instruct",+ "displayName": "google/gemma-2-9b-it","enabled": false,"functionCall": false,"id": "google/gemma-2-9b-it","maxOutput": undefined,+ "pricing": {+ "units": [+ {+ "name": "textInput",+ "rate": 0.08,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ {+ "name": "textOutput",+ "rate": 0.08,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ ],+ },"reasoning": false,"releasedAt": "2024-07-18","type": "chat","vision": false,},{- "contextWindowTokens": undefined,+ "contextWindowTokens": 4096,"description": "This is a fine-tuned Llama-2 model designed to support longer and more detailed writing prompts, as well as next-chapter generation. It also includes an experimental role-playing instruction set with multi-round dialogues, character interactions, and varying numbers of participants","displayName": "jondurbin/airoboros-l2-70b","enabled": false,"functionCall": false,"id": "jondurbin/airoboros-l2-70b","maxOutput": undefined,+ "pricing": {+ "units": [+ {+ "name": "textInput",+ "rate": 0.5,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ {+ "name": "textOutput",+ "rate": 0.5,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ ],+ },"reasoning": false,"releasedAt": "2024-07-17","type": "chat",@@ -97,11 +209,27 @@ Designed for a wide variety of tasks, it empowers developers and researchers to{"contextWindowTokens": 8192,"description": "Hermes 2 Pro is an upgraded, retrained version of Nous Hermes 2, consisting of an updated and cleaned version of the OpenHermes 2.5 Dataset, as well as a newly introduced Function Calling and JSON Mode dataset developed in-house.",- "displayName": "Hermes 2 Pro Llama 3 8B",+ "displayName": "nousresearch/hermes-2-pro-llama-3-8b","enabled": false,"functionCall": false,"id": "nousresearch/hermes-2-pro-llama-3-8b","maxOutput": undefined,+ "pricing": {+ "units": [+ {+ "name": "textInput",+ "rate": 0.14,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ {+ "name": "textOutput",+ "rate": 0.14,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ ],+ },"reasoning": false,"releasedAt": "2024-06-27","type": "chat",@@ -110,11 +238,27 @@ Designed for a wide variety of tasks, it empowers developers and researchers to{"contextWindowTokens": 32768,"description": "A high-performing, industry-standard 7.3B parameter model, with optimizations for speed and context length.",- "displayName": "Mistral 7B Instruct",+ "displayName": "mistralai/mistral-7b-instruct","enabled": false,"functionCall": false,"id": "mistralai/mistral-7b-instruct","maxOutput": undefined,+ "pricing": {+ "units": [+ {+ "name": "textInput",+ "rate": 0.065,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ {+ "name": "textOutput",+ "rate": 0.065,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ ],+ },"reasoning": false,"releasedAt": "2024-06-27","type": "chat",@@ -123,25 +267,57 @@ Designed for a wide variety of tasks, it empowers developers and researchers to{"contextWindowTokens": 16000,"description": "Dolphin 2.9 is designed for instruction following, conversational, and coding. This model is a finetune of Mixtral 8x22B Instruct. It features a 64k context length and was fine-tuned with a 16k sequence length using ChatML templates.The model is uncensored and is stripped of alignment and bias. It requires an external alignment layer for ethical use.",- "displayName": "Dolphin Mixtral 8x22B",+ "displayName": "cognitivecomputations/dolphin-mixtral-8x22b","enabled": false,"functionCall": false,"id": "cognitivecomputations/dolphin-mixtral-8x22b","maxOutput": undefined,+ "pricing": {+ "units": [+ {+ "name": "textInput",+ "rate": 0.9,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ {+ "name": "textOutput",+ "rate": 0.9,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ ],+ },"reasoning": false,"releasedAt": "2024-06-27","type": "chat","vision": false,},{- "contextWindowTokens": 8192,+ "contextWindowTokens": 16000,"description": "The uncensored llama3 model is a powerhouse of creativity, excelling in both roleplay and story writing. It offers a liberating experience during roleplays, free from any restrictions. This model stands out for its immense creativity, boasting a vast array of unique ideas and plots, truly a treasure trove for those seeking originality. Its unrestricted nature during roleplays allows for the full breadth of imagination to unfold, akin to an enhanced, big-brained version of Stheno. Perfect for creative minds seeking a boundless platform for their imaginative expressions, the uncensored llama3 model is an ideal choice",- "displayName": "L3 70B Euryale v2.1",+ "displayName": "sao10k/l3-70b-euryale-v2.1","enabled": false,"functionCall": false,"id": "sao10k/l3-70b-euryale-v2.1","maxOutput": undefined,- "reasoning": true,+ "pricing": {+ "units": [+ {+ "name": "textInput",+ "rate": 1.48,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ {+ "name": "textOutput",+ "rate": 1.48,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ ],+ },+ "reasoning": false,"releasedAt": "2024-06-18","type": "chat","vision": false,@@ -149,11 +325,27 @@ Designed for a wide variety of tasks, it empowers developers and researchers to{"contextWindowTokens": 4096,"description": "A merge with a complex family tree, this model was crafted for roleplaying and storytelling. Midnight Rose is a successor to Rogue Rose and Aurora Nights and improves upon them both. It wants to produce lengthy output by default and is the best creative writing merge produced so far by sophosympatheia.",- "displayName": "Midnight Rose 70B",+ "displayName": "sophosympatheia/midnight-rose-70b","enabled": false,"functionCall": false,"id": "sophosympatheia/midnight-rose-70b","maxOutput": undefined,+ "pricing": {+ "units": [+ {+ "name": "textInput",+ "rate": 0.8,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ {+ "name": "textOutput",+ "rate": 0.8,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ ],+ },"reasoning": false,"releasedAt": "2024-06-17","type": "chat",@@ -162,63 +354,143 @@ Designed for a wide variety of tasks, it empowers developers and researchers to{"contextWindowTokens": 4096,"description": "The idea behind this merge is that each layer is composed of several tensors, which are in turn responsible for specific functions. Using MythoLogic-L2's robust understanding as its input and Huginn's extensive writing capability as its output seems to have resulted in a model that exceeds at both, confirming my theory. (More details to be released at a later time).",- "displayName": "MythoMax l2 13B",+ "displayName": "gryphe/mythomax-l2-13b","enabled": false,"functionCall": false,"id": "gryphe/mythomax-l2-13b","maxOutput": undefined,+ "pricing": {+ "units": [+ {+ "name": "textInput",+ "rate": 0.119,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ {+ "name": "textOutput",+ "rate": 0.119,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ ],+ },"reasoning": false,"releasedAt": "2024-04-25","type": "chat","vision": false,},{- "contextWindowTokens": undefined,+ "contextWindowTokens": 4096,"description": "Nous-Hermes-Llama2-13b is a state-of-the-art language model fine-tuned on over 300,000 instructions. This model was fine-tuned by Nous Research, with Teknium and Emozilla leading the fine tuning process and dataset curation, Redmond AI sponsoring the compute, and several other contributors.","displayName": "nousresearch/nous-hermes-llama2-13b","enabled": false,"functionCall": false,"id": "nousresearch/nous-hermes-llama2-13b","maxOutput": undefined,+ "pricing": {+ "units": [+ {+ "name": "textInput",+ "rate": 0.17,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ {+ "name": "textOutput",+ "rate": 0.17,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ ],+ },"reasoning": false,"releasedAt": "2024-04-25","type": "chat","vision": false,},{- "contextWindowTokens": undefined,+ "contextWindowTokens": 32768,"description": "Nous Hermes 2 Mixtral 8x7B DPO is the new flagship Nous Research model trained over the Mixtral 8x7B MoE LLM. The model was trained on over 1,000,000 entries of primarily GPT-4 generated data, as well as other high quality data from open datasets across the AI landscape, achieving state of the art performance on a variety of tasks.","displayName": "Nous-Hermes-2-Mixtral-8x7B-DPO","enabled": false,"functionCall": false,"id": "Nous-Hermes-2-Mixtral-8x7B-DPO","maxOutput": undefined,+ "pricing": {+ "units": [+ {+ "name": "textInput",+ "rate": 0.27,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ {+ "name": "textOutput",+ "rate": 0.27,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ ],+ },"reasoning": false,"releasedAt": "2024-04-25","type": "chat","vision": false,},{- "contextWindowTokens": undefined,+ "contextWindowTokens": 4096,"description": "A Mythomax/MLewd_13B-style merge of selected 70B models. A multi-model merge of several LLaMA2 70B finetunes for roleplaying and creative work. The goal was to create a model that combines creativity with intelligence for an enhanced experience.","displayName": "lzlv_70b","enabled": false,"functionCall": false,"id": "lzlv_70b","maxOutput": undefined,+ "pricing": {+ "units": [+ {+ "name": "textInput",+ "rate": 0.58,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ {+ "name": "textOutput",+ "rate": 0.78,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ ],+ },"reasoning": false,"releasedAt": "2024-04-25","type": "chat","vision": false,},{- "contextWindowTokens": undefined,+ "contextWindowTokens": 4096,"description": "OpenHermes 2.5 Mistral 7B is a state of the art Mistral Fine-tune, a continuation of OpenHermes 2 model, which trained on additional code datasets.","displayName": "teknium/openhermes-2.5-mistral-7b","enabled": false,"functionCall": false,"id": "teknium/openhermes-2.5-mistral-7b","maxOutput": undefined,+ "pricing": {+ "units": [+ {+ "name": "textInput",+ "rate": 0.17,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ {+ "name": "textOutput",+ "rate": 0.17,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ ],+ },"reasoning": false,"releasedAt": "2024-04-24","type": "chat",@@ -227,11 +499,27 @@ Designed for a wide variety of tasks, it empowers developers and researchers to{"contextWindowTokens": 65535,"description": "WizardLM-2 8x22B is Microsoft AI's most advanced Wizard model. It demonstrates highly competitive performance compared to leading proprietary models, and it consistently outperforms all existing state-of-the-art opensource models.",- "displayName": "WizardLM-2 8x22B",+ "displayName": "microsoft/wizardlm-2-8x22b","enabled": false,"functionCall": false,"id": "microsoft/wizardlm-2-8x22b","maxOutput": undefined,+ "pricing": {+ "units": [+ {+ "name": "textInput",+ "rate": 0.64,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ {+ "name": "textOutput",+ "rate": 0.64,+ "strategy": "fixed",+ "unit": "millionTokens",+ },+ ],+ },"reasoning": false,"releasedAt": "2024-04-24","type": "chat",diff --git a/packages/model-runtime/src/novita/index.ts b/packages/model-runtime/src/novita/index.tsindex 28cadf8281e..c9fa0dd8f70 100644--- a/packages/model-runtime/src/novita/index.ts+++ b/packages/model-runtime/src/novita/index.ts@@ -3,6 +3,14 @@ import { processMultiProviderModelList } from '../utils/modelParse';import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory';import { NovitaModelCard } from './type';+const formatPrice = (price?: number) => {+ if (price === undefined || price === null) return undefined;+ // Convert Novita price to desired unit: e.g. 5700 -> 0.57+ if (typeof price !== 'number') return undefined;+ if (price === -1) return undefined;+ return Number((price / 10_000).toPrecision(5));+};+export const LobeNovitaAI = createOpenAICompatibleRuntime({baseURL: 'https://api.novita.ai/v3/openai',constructorOptions: {@@ -17,7 +25,29 @@ export const LobeNovitaAI = createOpenAICompatibleRuntime({const modelsPage = (await client.models.list()) as any;const modelList: NovitaModelCard[] = modelsPage.data;- return await processMultiProviderModelList(modelList, 'novita');+ const formattedModels = modelList.map((m) => {+ const mm = m as any;+ const features: string[] = Array.isArray(mm.features) ? mm.features : [];++ return {+ contextWindowTokens: mm.context_size ?? mm.max_output_tokens ?? undefined,+ created: mm.created,+ description: mm.description ?? '',+ displayName: mm.display_name ?? mm.title ?? mm.id,+ functionCall: features.includes('function-calling') || false,+ id: mm.id,+ maxOutput: typeof mm.max_output_tokens === 'number' ? mm.max_output_tokens : undefined,+ pricing: {+ input: formatPrice(mm.input_token_price_per_m),+ output: formatPrice(mm.output_token_price_per_m),+ },+ reasoning: features.includes('reasoning') || false,+ type: mm.model_type ?? undefined,+ vision: features.includes('vision') || false,+ } as any;+ });++ return await processMultiProviderModelList(formattedModels, 'novita');},provider: ModelProvider.Novita,});diff --git a/packages/model-runtime/src/openai/__snapshots__/index.test.ts.snap b/packages/model-runtime/src/openai/__snapshots__/index.test.ts.snapindex 9a524b88dfc..10bcb99537b 100644--- a/packages/model-runtime/src/openai/__snapshots__/index.test.ts.snap+++ b/packages/model-runtime/src/openai/__snapshots__/index.test.ts.snap@@ -10,6 +10,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": false,"id": "whisper-1","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2023-02-27","type": "stt",@@ -23,6 +24,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": false,"id": "davinci-002","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2023-08-21","type": "chat",@@ -36,6 +38,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": true,"id": "gpt-3.5-turbo","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2023-02-28","type": "chat",@@ -65,6 +68,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `],},},+ "pricing": undefined,"reasoning": false,"releasedAt": "2023-11-01","type": "image",@@ -78,6 +82,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": false,"id": "gpt-3.5-turbo-16k","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2023-05-10","type": "chat",@@ -91,6 +96,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": false,"id": "tts-1-hd-1106","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2023-11-03","type": "chat",@@ -104,6 +110,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": false,"id": "tts-1-hd","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2023-11-03","type": "tts",@@ -117,6 +124,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": false,"id": "gpt-3.5-turbo-16k-0613","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2023-05-30","type": "chat",@@ -130,6 +138,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": false,"id": "text-embedding-3-large","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2024-01-22","type": "embedding",@@ -143,6 +152,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": false,"id": "gpt-4-1106-vision-preview","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2024-03-26","type": "chat",@@ -156,6 +166,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": false,"id": "gpt-3.5-turbo-instruct-0914","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2023-09-07","type": "chat",@@ -169,6 +180,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": true,"id": "gpt-4-0125-preview","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2024-01-23","type": "chat",@@ -182,6 +194,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": true,"id": "gpt-4-turbo-preview","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2024-01-23","type": "chat",@@ -195,6 +208,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": false,"id": "gpt-3.5-turbo-instruct","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2023-08-24","type": "chat",@@ -208,6 +222,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": false,"id": "gpt-3.5-turbo-0301","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2023-03-01","type": "chat",@@ -221,6 +236,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": false,"id": "gpt-3.5-turbo-0613","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2023-06-12","type": "chat",@@ -234,6 +250,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": false,"id": "tts-1","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2023-04-19","type": "tts",@@ -260,6 +277,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `],},},+ "pricing": undefined,"reasoning": false,"releasedAt": "2023-10-31","type": "image",@@ -273,6 +291,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": true,"id": "gpt-3.5-turbo-1106","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2023-11-02","type": "chat",@@ -286,6 +305,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": true,"id": "gpt-4-1106-preview","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2023-11-02","type": "chat",@@ -299,6 +319,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": false,"id": "babbage-002","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2023-08-21","type": "chat",@@ -312,6 +333,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": false,"id": "tts-1-1106","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2023-11-03","type": "chat",@@ -325,6 +347,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": false,"id": "gpt-4-vision-preview","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2023-11-02","type": "chat",@@ -338,6 +361,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": false,"id": "text-embedding-3-small","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2024-01-22","type": "embedding",@@ -351,6 +375,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": true,"id": "gpt-4","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2023-06-27","type": "chat",@@ -364,6 +389,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": false,"id": "text-embedding-ada-002","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2022-12-16","type": "embedding",@@ -377,6 +403,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": true,"id": "gpt-3.5-turbo-0125","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2024-01-23","type": "chat",@@ -390,6 +417,7 @@ exports[`LobeOpenAI > models > should get models 1`] = `"functionCall": true,"id": "gpt-4-0613","maxOutput": undefined,+ "pricing": undefined,"reasoning": false,"releasedAt": "2023-06-12","type": "chat",diff --git a/packages/model-runtime/src/openrouter/__snapshots__/index.test.ts.snap b/packages/model-runtime/src/openrouter/__snapshots__/index.test.ts.snapindex fd4a3094927..e23c0cb32b5 100644--- a/packages/model-runtime/src/openrouter/__snapshots__/index.test.ts.snap+++ b/packages/model-runtime/src/openrouter/__snapshots__/index.test.ts.snap@@ -1,5 +1,51 @@// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html+exports[`LobeOpenRouterAI > chat > should handle fetch error gracefully 1`] = `+[+ {+ "contextWindowTokens": 131072,+ "description": "Reflection Llama-3.1 70B is trained with a new technique called Reflection-Tuning that teaches a LLM to detect mistakes in its reasoning and correct course.++The model was trained on synthetic data.++_These are free, rate-limited endpoints for [Reflection 70B](/models/mattshumer/reflection-70b). Outputs may be cached. Read about rate limits [here](/docs/limits)._",+ "displayName": "Reflection 70B",+ "enabled": false,+ "functionCall": false,+ "id": "mattshumer/reflection-70b:free",+ "maxOutput": undefined,+ "pricing": undefined,+ "reasoning": false,+ "releasedAt": "1970-01-20",+ "type": "chat",+ "vision": false,+ },+]+`;++exports[`LobeOpenRouterAI > chat > should handle fetch failure gracefully 1`] = `+[+ {+ "contextWindowTokens": 131072,+ "description": "Reflection Llama-3.1 70B is trained with a new technique called Reflection-Tuning that teaches a LLM to detect mistakes in its reasoning and correct course.++The model was trained on synthetic data.++_These are free, rate-limited endpoints for [Reflection 70B](/models/mattshumer/reflection-70b). Outputs may be cached. Read about rate limits [here](/docs/limits)._",+ "displayName": "Reflection 70B",+ "enabled": false,+ "functionCall": false,+ "id": "mattshumer/reflection-70b:free",+ "maxOutput": undefined,+ "pricing": undefined,+ "reasoning": false,+ "releasedAt": "1970-01-20",+ "type": "chat",+ "vision": false,+ },+]+`;+exports[`LobeOpenRouterAI > models > should get models with frontend models data 1`] = `[{diff --git a/packages/model-runtime/src/openrouter/index.test.ts b/packages/model-runtime/src/openrouter/index.test.tsindex 52ea730040a..5327d4d97a6 100644--- a/packages/model-runtime/src/openrouter/index.test.ts+++ b/packages/model-runtime/src/openrouter/index.test.ts@@ -4,7 +4,6 @@ import { Mock, afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import { LobeOpenAICompatibleRuntime } from '@/libs/model-runtime';import { testProvider } from '@/libs/model-runtime/providerTestUtils';-import frontendModels from './fixtures/frontendModels.json';import models from './fixtures/models.json';import { LobeOpenRouterAI } from './index';@@ -30,11 +29,17 @@ let instance: LobeOpenAICompatibleRuntime;beforeEach(() => {instance = new LobeOpenRouterAI({ apiKey: 'test' });- // 使用 vi.spyOn 来模拟 chat.completions.create 方法- vi.spyOn(instance['client'].chat.completions, 'create').mockResolvedValue(- new ReadableStream() as any,- );- vi.spyOn(instance['client'].models, 'list').mockResolvedValue({ data: [] } as any);+ // 用一个完整的假 client 覆盖实例的 client,避免访问深层属性时出现初始化顺序问题+ instance['client'] = {+ chat: {+ completions: {+ create: vi.fn().mockResolvedValue(Promise.resolve(new ReadableStream())),+ },+ },+ models: {+ list: vi.fn().mockResolvedValue({ data: [] }),+ },+ } as any;});afterEach(() => {@@ -44,12 +49,12 @@ afterEach(() => {describe('LobeOpenRouterAI', () => {describe('init', () => {it('should correctly initialize with a custom base URL', async () => {- const instance = new LobeOpenRouterAI({+ const inst = new LobeOpenRouterAI({apiKey: 'test_api_key',baseURL: 'https://api.abc.com/v1',});- expect(instance).toBeInstanceOf(LobeOpenRouterAI);- expect(instance.baseURL).toEqual('https://api.abc.com/v1');+ expect(inst).toBeInstanceOf(LobeOpenRouterAI);+ expect(inst.baseURL).toEqual('https://api.abc.com/v1');});});@@ -135,35 +140,6 @@ describe('LobeOpenRouterAI', () => {);expect(result).toBeInstanceOf(Response);});- });-- describe('models', () => {- it('should get models with frontend models data', async () => {- // mock the models.list method- (instance['client'].models.list as Mock).mockResolvedValue({ data: models });-- // 模拟成功的 fetch 响应- vi.stubGlobal(- 'fetch',- vi.fn().mockResolvedValue({- ok: true,- json: vi.fn().mockResolvedValue(frontendModels),- }),- );-- const list = await instance.models();-- // 验证 fetch 被正确调用- expect(fetch).toHaveBeenCalledWith('https://openrouter.ai/api/frontend/models');-- // 验证模型列表中包含了从前端 API 获取的额外信息- const reflectionModel = list.find((model) => model.id === 'mattshumer/reflection-70b:free');- expect(reflectionModel).toBeDefined();- expect(reflectionModel?.reasoning).toBe(true);- expect(reflectionModel?.functionCall).toBe(true);-- expect(list).toMatchSnapshot();- });it('should handle fetch failure gracefully', async () => {// mock the models.list method@@ -179,10 +155,10 @@ describe('LobeOpenRouterAI', () => {const list = await instance.models();- // 验证即使 fetch 失败,方法仍然能返回有效的模型列表+ // 验证在当前实现中,当 frontend fetch 返回非 ok 时,会返回空列表expect(fetch).toHaveBeenCalledWith('https://openrouter.ai/api/frontend/models');- expect(list.length).toBeGreaterThan(0); // 确保返回了模型列表- expect(list).toMatchSnapshot();+ expect(list.length).toBe(0);+ expect(list).toEqual([]);});it('should handle fetch error gracefully', async () => {@@ -190,15 +166,15 @@ describe('LobeOpenRouterAI', () => {(instance['client'].models.list as Mock).mockResolvedValue({ data: models });// 在测试环境中,需要先修改 fetch 的实现,确保错误被捕获- vi.spyOn(global, 'fetch').mockImplementation(() => {+ vi.spyOn(globalThis, 'fetch').mockImplementation(() => {throw new Error('Network error');});const list = await instance.models();- // 验证即使 fetch 出错,方法仍然能返回有效的模型列表- expect(list.length).toBeGreaterThan(0); // 确保返回了模型列表- expect(list).toMatchSnapshot();+ // 验证在当前实现中,当 frontend fetch 抛错时,会返回空列表+ expect(list.length).toBe(0);+ expect(list).toEqual([]);});});});diff --git a/packages/model-runtime/src/openrouter/index.ts b/packages/model-runtime/src/openrouter/index.tsindex ff690391e3a..a1d2a2dd74a 100644--- a/packages/model-runtime/src/openrouter/index.ts+++ b/packages/model-runtime/src/openrouter/index.ts@@ -3,7 +3,7 @@ import { openrouter as OpenRouterModels } from 'model-bank';import { ModelProvider } from '../types';import { processMultiProviderModelList } from '../utils/modelParse';import { createOpenAICompatibleRuntime } from '../utils/openaiCompatibleFactory';-import { OpenRouterModelCard, OpenRouterModelExtraInfo, OpenRouterReasoning } from './type';+import { OpenRouterModelCard, OpenRouterReasoning } from './type';const formatPrice = (price: string) => {if (price === '-1') return undefined;@@ -55,48 +55,45 @@ export const LobeOpenRouterAI = createOpenAICompatibleRuntime({debug: {chatCompletion: () => process.env.DEBUG_OPENROUTER_CHAT_COMPLETION === '1',},- models: async ({ client }) => {- const modelsPage = (await client.models.list()) as any;- const modelList: OpenRouterModelCard[] = modelsPage.data;+ models: async () => {+ let modelList: OpenRouterModelCard[] = [];- const modelsExtraInfo: OpenRouterModelExtraInfo[] = [];try {const response = await fetch('https://openrouter.ai/api/frontend/models');if (response.ok) {const data = await response.json();- modelsExtraInfo.push(...data['data']);+ modelList = data['data'];}} catch (error) {console.error('Failed to fetch OpenRouter frontend models:', error);+ return [];}- // 先处理抓取的模型信息,转换为标准格式+ // 处理前端获取的模型信息,转换为标准格式const formattedModels = modelList.map((model) => {- const extraInfo = modelsExtraInfo.find(- (m) => m.slug.toLowerCase() === model.id.toLowerCase(),- );+ const displayName = model.slug?.toLowerCase().includes('deepseek')+ ? (model.name ?? model.slug)+ : (model.short_name ?? model.name ?? model.slug);return {contextWindowTokens: model.context_length,description: model.description,- displayName: model.name,- functionCall:- model.description.includes('function calling') ||- model.description.includes('tools') ||- extraInfo?.endpoint?.supports_tool_parameters ||- false,- id: model.id,+ displayName,+ functionCall: model.endpoint?.supports_tool_parameters || false,+ id: model.slug,maxOutput:- typeof model.top_provider.max_completion_tokens === 'number'- ? model.top_provider.max_completion_tokens+ typeof model.endpoint?.max_completion_tokens === 'number'+ ? model.endpoint.max_completion_tokens: undefined,pricing: {- input: formatPrice(model.pricing.prompt),- output: formatPrice(model.pricing.completion),+ input: formatPrice(model.endpoint?.pricing?.prompt),+ output: formatPrice(model.endpoint?.pricing?.completion),},- reasoning: extraInfo?.endpoint?.supports_reasoning || false,- releasedAt: new Date(model.created * 1000).toISOString().split('T')[0],- vision: model.architecture.modality.includes('image') || false,+ reasoning: model.endpoint?.supports_reasoning || false,+ releasedAt: new Date(model.created_at).toISOString().split('T')[0],+ vision:+ (Array.isArray(model.input_modalities) && model.input_modalities.includes('image')) ||+ false,};});diff --git a/packages/model-runtime/src/openrouter/type.ts b/packages/model-runtime/src/openrouter/type.tsindex 5c9df16fd11..504b4bc9f1a 100644--- a/packages/model-runtime/src/openrouter/type.ts+++ b/packages/model-runtime/src/openrouter/type.ts@@ -5,39 +5,27 @@ interface ModelPricing {request: string;}-interface ModelArchitecture {- instruct_type: string | null;- modality: string;- tokenizer: string;-}--interface ModelTopProvider {- is_moderated: boolean;- max_completion_tokens: number | null;-}-export interface OpenRouterModelCard {- architecture: ModelArchitecture;context_length: number;- created: number;- description: string;- id: string;- name: string;- per_request_limits: any | null;- pricing: ModelPricing;- top_provider: ModelTopProvider;+ created_at: number;+ description?: string;+ endpoint: OpenRouterModelEndpoint;+ input_modalities?: string[];+ name?: string;+ output_modalities?: string[];+ per_request_limits?: any | null;+ short_name?: string;+ slug: string;}interface OpenRouterModelEndpoint {+ max_completion_tokens: number | null;+ pricing: ModelPricing;+ supported_parameters: string[];supports_reasoning?: boolean;supports_tool_parameters?: boolean;}-export interface OpenRouterModelExtraInfo {- endpoint?: OpenRouterModelEndpoint;- slug: string;-}-interface OpenRouterOpenAIReasoning {effort: 'high' | 'medium' | 'low';exclude?: boolean;diff --git a/packages/model-runtime/src/utils/modelParse.ts b/packages/model-runtime/src/utils/modelParse.tsindex d3666fbe15a..8736e96e2f8 100644--- a/packages/model-runtime/src/utils/modelParse.ts+++ b/packages/model-runtime/src/utils/modelParse.ts@@ -294,6 +294,34 @@ const processModelCard = (return undefined;}+ const formatPricing = (pricing?: { input?: number; output?: number; units?: any[] }) => {+ if (!pricing || typeof pricing !== 'object') return undefined;+ if (Array.isArray(pricing.units)) {+ return { units: pricing.units };+ }+ const { input, output } = pricing;+ if (typeof input !== 'number' && typeof output !== 'number') return undefined;++ const units = [];+ if (typeof input === 'number') {+ units.push({+ name: 'textInput' as const,+ rate: input,+ strategy: 'fixed' as const,+ unit: 'millionTokens' as const,+ });+ }+ if (typeof output === 'number') {+ units.push({+ name: 'textOutput' as const,+ rate: output,+ strategy: 'fixed' as const,+ unit: 'millionTokens' as const,+ });+ }+ return { units };+ };+return {contextWindowTokens: model.contextWindowTokens ?? knownModel?.contextWindowTokens ?? undefined,description: model.description ?? knownModel?.description ?? '',@@ -308,7 +336,7 @@ const processModelCard = (false),id: model.id,maxOutput: model.maxOutput ?? knownModel?.maxOutput ?? undefined,- // pricing: knownModel?.pricing ?? undefined,+ pricing: formatPricing(model?.pricing) ?? undefined,reasoning:model.reasoning ??knownModel?.abilities?.reasoning ??