返回顶部
l

llmrouter智能LLM路由

Intelligent LLM proxy that routes requests to appropriate models based on complexity. Save money by using cheaper models for simple tasks. Tested with Anthropic, OpenAI, Gemini, Kimi/Moonshot, and Ollama.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 0.1.1
安全检测
已通过
2,507
下载量
免费
免费
6
收藏
概述
安装方式
版本历史

llmrouter

LLM Router

一个智能代理,能够按复杂度对传入请求进行分类,并将其路由到合适的LLM模型。对于简单任务使用更便宜/更快的模型,将昂贵模型保留给复杂任务。

OpenClaw 配合使用,通过将简单请求路由到较小模型来减少Token用量和API成本。

状态: 已在Anthropic、OpenAI、Google Gemini、Kimi/Moonshot和Ollama上测试通过。

快速开始

前置条件

  1. 1. Python 3.10+ 及 pip
  2. Ollama(可选 - 仅在使用本地分类时需要)
  3. Anthropic API密钥 或 Claude Code OAuth令牌(或其他提供商密钥)

安装

bash

如果尚未克隆,请先克隆


git clone https://github.com/alexrudloff/llmrouter.git
cd llmrouter

创建虚拟环境(现代Python必需)

python3 -m venv venv source venv/bin/activate

安装依赖

pip install -r requirements.txt

拉取分类模型(如果使用本地分类)

ollama pull qwen2.5:3b

复制并自定义配置

cp config.yaml.example config.yaml

编辑config.yaml,填入你的API密钥和模型偏好

验证安装

bash

启动服务器


source venv/bin/activate
python server.py

在另一个终端中,测试健康检查端点

curl http://localhost:4001/health

应返回:{status: ok, ...}

启动服务器

bash
python server.py

选项:

  • - --port PORT - 监听端口(默认:4001)
  • --host HOST - 绑定主机(默认:127.0.0.1)
  • --config PATH - 配置文件路径(默认:config.yaml)
  • --log - 启用详细日志
  • --openclaw - 启用OpenClaw兼容性(重写系统提示中的模型名称)

配置

编辑 config.yaml 进行自定义:

模型路由

yaml

Anthropic路由


models:
super_easy: anthropic:claude-haiku-4-5-20251001
easy: anthropic:claude-haiku-4-5-20251001
medium: anthropic:claude-sonnet-4-20250514
hard: anthropic:claude-opus-4-20250514
super_hard: anthropic:claude-opus-4-20250514

OpenAI路由

models: super_easy: openai:gpt-4o-mini easy: openai:gpt-4o-mini medium: openai:gpt-4o hard: openai:o3-mini super_hard: openai:o3

Google Gemini路由

models: super_easy: google:gemini-2.0-flash easy: google:gemini-2.0-flash medium: google:gemini-2.0-flash hard: google:gemini-2.0-flash super_hard: google:gemini-2.0-flash

注意: 推理模型会被自动检测并使用正确的API参数。

分类器

三种请求复杂度分类选项:

本地(默认) - 免费,需要Ollama:
yaml
classifier:
provider: local
model: qwen2.5:3b

Anthropic - 使用Haiku,快速且便宜:
yaml
classifier:
provider: anthropic
model: claude-haiku-4-5-20251001

OpenAI - 使用GPT-4o-mini:
yaml
classifier:
provider: openai
model: gpt-4o-mini

Google - 使用Gemini:
yaml
classifier:
provider: google
model: gemini-2.0-flash

Kimi - 使用Moonshot:
yaml
classifier:
provider: kimi
model: moonshot-v1-8k

如果您的机器无法运行本地模型,请使用远程(anthropic/openai/google/kimi)分类器。

支持的提供商

  • - anthropic:claude- - Anthropic Claude模型(已测试)
  • openai:gpt-、openai:o1-、openai:o3- - OpenAI模型(已测试)
  • google:gemini- - Google Gemini模型(已测试)
  • kimi:kimi-k2.5、kimi:moonshot- - Kimi/Moonshot模型(已测试)
  • local:model-name - 本地Ollama模型(已测试)

复杂度级别

级别使用场景默认模型
super_easy问候、确认Haiku
easy
简单问答、提醒 | Haiku | | medium | 编程、邮件、研究 | Sonnet | | hard | 复杂推理、调试 | Opus | | super_hard | 系统架构、证明 | Opus |

自定义分类

编辑 ROUTES.md 来调整消息的分类方式。分类器读取此文件中的表格来确定复杂度级别。

API使用

路由器暴露了一个兼容OpenAI的API:

bash
curl http://localhost:4001/v1/chat/completions \
-H Authorization: Bearer $ANTHROPICAPIKEY \
-H Content-Type: application/json \
-d {
model: llm-router,
messages: [{role: user, content: Hello!}]
}

测试分类

bash
python classifier.py Write a Python sort function

输出:medium

python classifier.py --test

运行测试套件

作为macOS服务运行

创建 ~/Library/LaunchAgents/com.llmrouter.plist:

xml




Label
com.llmrouter
ProgramArguments

/path/to/llmrouter/venv/bin/python
/path/to/llmrouter/server.py
--openclaw

RunAtLoad

KeepAlive

WorkingDirectory
/path/to/llmrouter
StandardOutPath
/path/to/llmrouter/logs/stdout.log
StandardErrorPath
/path/to/llmrouter/logs/stderr.log

重要: 将 /path/to/llmrouter 替换为您的实际安装路径。必须使用venv中的python,而不是系统python。

bash

创建日志目录


mkdir -p ~/path/to/llmrouter/logs

加载服务

launchctl load ~/Library/LaunchAgents/com.llmrouter.plist

验证服务正在运行

curl http://localhost:4001/health

停止/重启

launchctl unload ~/Library/LaunchAgents/com.llmrouter.plist launchctl load ~/Library/LaunchAgents/com.llmrouter.plist

OpenClaw配置

在 ~/.openclaw/openclaw.json 中将路由器添加为提供商:

json
{
models: {
providers: {
localrouter: {
baseUrl: http://localhost:4001/v1,
apiKey: via-router,
api: openai-completions,
models: [
{
id: llm-router,
name: LLM Router (按复杂度自动路由),
reasoning: false,
input: [text, image],
cost: {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0
},
contextWindow: 200000,

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 llmrouter-1776370644 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 llmrouter-1776370644 技能

通过命令行安装

skillhub install llmrouter-1776370644

下载

⬇ 下载 llmrouter v0.1.1(免费)

文件大小: 4.08 KB | 发布时间: 2026-4-17 16:05

v0.1.1 最新 2026-4-17 16:05
llmrouter v0.1.1

- Expanded provider support: now tested with Anthropic, OpenAI, Google Gemini, Kimi/Moonshot, and Ollama.
- Added provider-agnostic classification: classifier can run locally on Ollama or remotely on Anthropic, OpenAI, Google, or Kimi.
- Updated configuration instructions and defaults for broader provider compatibility.
- Improved OpenClaw integration documentation and setup.
- Minor dependency and environment requirements changes (Ollama now optional; Python 3.10+ and venv use encouraged).
- No functional code changes—README/metadata/documentation only.

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部