导入
跨多个提供商工作
通过使用 /api/v1/models 端点探索 OpenRouter 的模型目录,实时按能力、上下文长度和定价筛选模型。本课程涵盖提供方/模型名命名约定、路由变体(如 :nitro 和 :floor),以及如何编写不依赖提供方的应用程序代码,将模型选择视为配置。
...
Some key providers available on OpenRouter
1import requests 2 3def find_capable_models(): 4 """ 5 Fetches all models from OpenRouter and filters them 6 to find those that support tools and have a large context window. 7 """ 8 try: 9 response = requests.get("https://openrouter.ai/api/v1/models") 10 response.raise_for_status() # Raise an exception for bad status codes 11 12 models_data = response.json().get('data', []) 13 14 print("Found models that support tool calling and have >32k context length:") 15 16 for model in models_data: 17 # Check if 'tools' is listed in the model's supported parameters 18 supports_tools = "tools" in model.get("supported_parameters", []) 19 20 # Check if context length is greater than 32,000 21 context_length = model.get("context_length", 0) 22 23 if supports_tools and context_length > 32000: 24 model_id = model.get("id") 25 print(f"- {model_id} (Context: {context_length})") 26 27 except requests.exceptions.RequestException as e: 28 print(f"An error occurred: {e}") 29 30find_capable_models()
单个模型的示例输出对象
1{ 2 "id": "openai/gpt-5.4", 3 "canonical_slug": "openai/gpt-5.4-20260305", 4 "hugging_face_id": "", 5 "name": "OpenAI: GPT-5.4", 6 "created": 1772734352, 7 "description": "GPT-5.4 is OpenAI’s latest frontier model, unifying the Codex and GPT lines into a single system. It features a 1M+ token context window (922K input, 128K output) with support for text and image inputs, enabling high-context reasoning, coding, and multimodal analysis within the same workflow.\n\nThe model delivers improved performance in coding, document understanding, tool use, and instruction following. It is designed as a strong default for both general-purpose tasks and software engineering, capable of generating production-quality code, synthesizing information across multiple sources, and executing complex multi-step workflows with fewer iterations and greater token efficiency.", 8 "context_length": 1050000, 9 "architecture": { 10 "modality": "text+image+file->text", 11 "input_modalities": [ 12 "text", 13 "image", 14 "file" 15 ], 16 "output_modalities": [ 17 "text" 18 ], 19 "tokenizer": "GPT", 20 "instruct_type": null 21 }, 22 "pricing": { 23 "prompt": "0.0000025", 24 "completion": "0.000015", 25 "web_search": "0.01", 26 "input_cache_read": "0.00000025" 27 }, 28 "top_provider": { 29 "context_length": 1050000, 30 "max_completion_tokens": 128000, 31 "is_moderated": true 32 }, 33 "per_request_limits": null, 34 "supported_parameters": [ 35 "frequency_penalty", 36 "include_reasoning", 37 "logit_bias", 38 "logprobs", 39 "max_tokens", 40 "presence_penalty", 41 "reasoning", 42 "response_format", 43 "seed", 44 "stop", 45 "structured_outputs", 46 "tool_choice", 47 "tools", 48 "top_logprobs" 49 ], 50 "default_parameters": { 51 "temperature": null, 52 "top_p": null, 53 "top_k": null, 54 "frequency_penalty": null, 55 "presence_penalty": null, 56 "repetition_penalty": null 57 }, 58 "expiration_date": null 59}
在运行时选择模型的示例代码片段
1import os 2 3# Model name is loaded from an environment variable 4# Changing it requires no code change or redeployment 5SUMMARY_MODEL = os.getenv("SUMMARY_MODEL", "google/gemini-3.1-flash-lite-preview") 6 7def generate_summary(text): 8 response = call_openrouter( 9 model_name=SUMMARY_MODEL, 10 prompt=f"Summarize this: {text}" 11 ) 12 # ...