simonw/llm-openrouter
GitHub: simonw/llm-openrouter
该插件为 LLM 命令行工具扩展了对 OpenRouter 平台托管模型的访问能力,让用户可以在终端中统一调用并管理各类主流大语言模型。
Stars: 363 | Forks: 45
# llm-openrouter
[](https://pypi.org/project/llm-openrouter/)
[](https://github.com/simonw/llm-openrouter/releases)
[](https://github.com/simonw/llm-openrouter/actions?query=workflow%3ATest)
[](https://github.com/simonw/llm-openrouter/blob/main/LICENSE)
用于 [OpenRouter](https://openrouter.ai/) 托管模型的 [LLM](https://llm.datasette.io/) 插件
## 安装
首先,[安装 LLM 命令行工具](https://llm.datasette.io/en/stable/setup.html)。
现在,在与 LLM 相同的环境中安装此插件。
```
llm install llm-openrouter
```
## 配置
您需要一个来自 OpenRouter 的 API key。您可以[在此处获取](https://openrouter.ai/keys)。
您可以将其设置为名为 `OPENROUTER_KEY` 的环境变量,或者使用以下命令将其添加到 `llm` 保存的 key 集合中:
```
llm keys set openrouter
```
```
Enter key:
```
## 用法
要列出可用模型,请运行:
```
llm models list
```
您应该会看到类似以下列表:
```
OpenRouter: openrouter/openai/gpt-3.5-turbo
OpenRouter: openrouter/anthropic/claude-sonnet-4
OpenRouter: openrouter/meta-llama/llama-2-70b-chat
...
```
来自 OpenRouter 的模型列表会缓存一小时。您可以使用此命令强制刷新:
```
llm openrouter refresh
```
要对模型运行 prompt,请将其完整的模型 ID 传递给 `-m` 选项,如下所示:
```
llm -m openrouter/anthropic/claude-sonnet-4 "Five spooky names for a pet tarantula"
```
您可以使用 `llm aliases` 命令为模型设置较短的别名,如下所示:
```
llm aliases set claude openrouter/anthropic/claude-sonnet-4
```
现在您可以使用以下命令向 Claude 发送 prompt:
```
cat llm_openrouter.py | llm -m claude -s 'write some pytest tests for this'
```
部分模型也支持图像:
```
llm -m openrouter/anthropic/claude-3.5-sonnet 'describe this image' -a https://static.simonwillison.net/static/2024/pelicans.jpg
llm -m openrouter/anthropic/claude-3-haiku 'extract text' -a page.png
```
### 视觉模型
一些 OpenRouter 模型可以接受图像附件。运行此命令:
```
llm models --options -q openrouter
```
并查找列出了这些附件类型的模型:
```
Attachment types:
application/pdf, image/gif, image/jpeg, image/png, image/webp
```
您可以向这些模型提供 URL 或文件路径形式的图像,例如:
```
llm -m openrouter/google/gemini-flash-1.5 'describe image' \
-a https://static.simonwillison.net/static/2025/two-pelicans.jpg
```
### Schemas
LLM 包含对 [schemas](https://llm.datasette.io/en/stable/schemas.html) 的支持,允许您控制模型返回输出的 JSON 结构。
OpenRouter 提供的部分模型兼容此功能,详情请参阅[他们的结构化输出模型完整列表](https://openrouter.ai/models?order=newest&supported_parameters=structured_outputs)。
`llm-openrouter` 目前为该列表中的模型启用了 schema 支持。不同模型对 schema 支持的质量级别有所不同,因此请仔细测试,而不是假设所有模型都能以相同的方式正确工作。
```
llm -m openrouter/google/gemini-flash-1.5 'invent 3 cool capybaras' \
--schema-multi 'name,bio'
```
输出:
```
{
"items": [
{
"bio": "Chill vibes only. Spends most days floating on lily pads, occasionally accepting head scratches from passing frogs.",
"name": "Professor Fluffernutter"
},
{
"bio": "A thrill-seeker! Capybara extraordinaire known for her daring escapes from the local zoo and impromptu skateboarding sessions.",
"name": "Capybara-bara the Bold"
},
{
"bio": "A renowned artist, creating masterpieces using mud, leaves, and her own surprisingly dexterous paws.",
"name": "Michelangelo Capybara"
}
]
}
```
### Tools
大多数 OpenRouter 模型支持 [tool calls](https://llm.datasette.io/en/stable/tools.html)。您可以像这样进行试用:
```
llm -m openrouter/openai/gpt-5 \
-T llm_version -T llm_time \
"What version of LLM and what time is it?" \
--tools-debug
```
示例输出:
```
Tool call: llm_version({})
0.27.1
Tool call: llm_time({})
{
"utc_time": "2025-09-20 23:35:53 UTC",
"utc_time_iso": "2025-09-20T23:35:53.205247+00:00",
"local_timezone": "PDT",
"local_time": "2025-09-20 16:35:53",
"timezone_offset": "UTC-7:00",
"is_dst": true
}
LLM version: 0.27.1
Current time: 2025-09-20 16:35:53 PDT (2025-09-20 23:35:53 UTC)
```
### Reasoning
一些 OpenRouter 模型(例如 [GPT-5](https://openrouter.ai/openai/gpt-5))支持控制 reasoning 的选项:
- `-o reasoning_effort low|medium|high` - 控制 reasoning 强度
- `-o reasoning_max_tokens 2048` - 某些模型中指定强度的另一种方式
- `-o reasoning_enabled true` - 使用此选项可以在不通过其他两个选项设置强度的情况下启用 reasoning
例如:
```
llm -m openrouter/openai/gpt-5 \
'prove dogs exist' \
-o reasoning_effort high
```
### Provider 路由
OpenRouter 提供了[全面的选项](https://openrouter.ai/docs/features/provider-routing)来控制您的请求被路由到哪个底层 provider。
您可以使用 OpenRouter JSON 格式指定这些内容,然后使用 `-o provider '{JSON goes here}` 选项将其传递给 LLM:
```
llm -m openrouter/meta-llama/llama-3.1-8b-instruct hi \
-o provider '{"quantizations": ["fp8"]}'
```
这指定了您只希望针对该模型使用[支持 fp8 量化](https://openrouter.ai/docs/features/provider-routing#example-requesting-fp8-quantization)的 provider。
### 整合来自 Exa 的搜索结果
OpenRouter 与 [Exa](https://exa.ai/) 建立了[合作关系](https://openrouter.ai/docs/features/web-search),通过这种合作,对_任何_受支持模型的 prompt 都可以使用来自 Exa 索引的相关搜索结果进行增强——这是一种 RAG 形式。
使用 `-o online 1` 选项启用此功能:
```
llm -m openrouter/mistralai/mistral-small -o online 1 'key events on march 1st 2025'
```
请查阅 OpenRouter 文档以了解[当前定价](https://openrouter.ai/docs/features/web-search#pricing)。
### 列出模型
`llm models -q openrouter` 命令将显示所有可用模型,或者您可以使用此命令查看更详细的 JSON:
```
llm openrouter models
```
输出如下开始:
```
- id: latitudegames/wayfarer-large-70b-llama-3.3
name: LatitueGames: Wayfarer Large 70B Llama 3.3
context_length: 128,000
architecture: text->text Llama3
pricing: prompt $0.7/M, completion $0.7/M
- id: thedrummer/skyfall-36b-v2
name: TheDrummer: Skyfall 36B V2
context_length: 64,000
architecture: text->text Other
pricing: prompt $0.5/M, completion $0.5/M
- id: microsoft/phi-4-multimodal-instruct
name: Microsoft: Phi 4 Multimodal Instruct
context_length: 131,072
architecture: text+image->text Other
pricing: prompt $0.07/M, completion $0.14/M, image $0.2476/K
```
添加 `--json` 以改为返回 JSON,如下所示:
```
[
{
"id": "microsoft/phi-4-multimodal-instruct",
"name": "Microsoft: Phi 4 Multimodal Instruct",
"created": 1741396284,
"description": "Phi-4 Multimodal Instruct is a versatile...",
"context_length": 131072,
"architecture": {
"modality": "text+image->text",
"tokenizer": "Other",
"instruct_type": null
},
"pricing": {
"prompt": "0.00000007",
"completion": "0.00000014",
"image": "0.0002476",
"request": "0",
"input_cache_read": "0",
"input_cache_write": "0",
"web_search": "0",
"internal_reasoning": "0"
},
"top_provider": {
"context_length": 131072,
"max_completion_tokens": null,
"is_moderated": false
},
"per_request_limits": null
}
```
添加 `--free` 可获取仅包含[免费提供](https://openrouter.ai/models?max_price=0)的模型列表。
```
llm openrouter models --free
```
### 关于您的 API key 的信息
`llm openrouter key` 命令会显示有关您当前 API key 的信息,包括速率限制:
```
llm openrouter key
```
示例输出:
```
{
"label": "sk-or-v1-0fa...240",
"limit": null,
"usage": 0.65017511,
"limit_remaining": null,
"is_free_tier": false,
"rate_limit": {
"requests": 40,
"interval": "10s"
}
}
```
这将默认检查您使用 `llm keys set openrouter` 或 `OPENROUTER_KEY` 环境变量设置的 key。
您可以通过将 key 本身——或者 `llm keys` 列表中的 key 名称——作为 `--key` 选项传递来检查不同的 key:
```
llm openrouter key --key sk-xxx
```
## 开发
要在本地设置此插件,请先检出代码。然后使用 `uv` 运行测试:
```
cd llm-openrouter
uv run pytest
```
要在插件可用的情况下运行 LLM:
```
uv run llm models
```
要更新记录和快照,请运行:
```
PYTEST_OPENROUTER_KEY="$(llm keys get openrouter)" \
uv run pytest --record-mode=rewrite --inline-snapshot=fix
```
如果添加了针对其他模型的测试,请更新 `tests/models_persister.py` 以便在记录中保留这些模型 ID。
标签:API集成, DLL 劫持, LLM插件, SOC Prime, 人工智能, 可观测性, 大语言模型, 开发工具, 用户模式Hook绕过, 逆向工具