前言
前文已经安装了openclaw,并且接入到了飞书,但是模型的免费额度很快就用完了,需要去市面上众多的模型厂商去选择适合自己的模型,而采集的过程当中,不可避免的会使用众多厂商的模型,那怎么管理这些模型的key,并且做到 随时切换了,本文就来解决这个问题
本文使用liteLLM来管理进行模型key的管理
安装liteLLM
1)安装liteLLM非常的简单
pip3 install litellm
2)配置文件
litellm_config.yaml
model_list:
- model_name: qwen-plus
litellm_params:
model: dashscope/qwen-plus
api_key: os.environ/QWEN_PLUS_API_KEY
api_base: https://dashscope.aliyuncs.com/compatible-mode/v1
- model_name: deepseek-chat
litellm_params:
model: deepseek/deepseek-chat
api_key: os.environ/DEEPSEEK_CHAT_API_KEY
api_base: https://api.deepseek.com/chat/completions
general_settings:
master_key: wilson-litellm-private-key
注:配置了两个大模型,并且对应的key都已经写在环境变量里面了
3)启动
litellm --config litellm_config.yaml --port 4000
4)测试
> curl http://localhost:4000/v1/chat/completions \
-H "Authorization: Bearer wilson-litellm-private-key" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen-plus",
"messages": [{"role": "user", "content": "你是谁"}]
}'
{"id":"chatcmpl-9403264e-0e1a-9d79-9f95-49bf2fa3a629","created":1772509820,"model":"qwen-plus","object":"chat.completion","choices":[{"finish_reason":"stop","index":0,"message":{"content":"你好!我是通义千
问(Qwen),阿里巴巴集团旗下的超大规模语言模型。我能够回答问题、创作文字,比如写故事、写公文、写邮件、写剧本、逻辑推理、编程等等,还能表达观点,玩游戏等。如果你有任何问题或需要帮助,欢迎随时告诉我!😊","role":"assistant","provider_specific_fields":{"refusal":null}},"provider_specific_fields":{}}],"usage":{"completion_tokens":66,"prompt_tokens":10,"total_tokens":76,"prompt_tokens_details":{"cached_tokens":0}}}
安装完成
接入openclaw
直接修改 ~/.openclaw/openclaw.json
{
...
"models": {
"mode": "merge",
"providers": {
"litellm": {
"baseUrl": "http://localhost:4000/v1",
"apiKey": "wilson-litellm-private-key",
"api": "openai-completions",
"models": [ # id 必须要与litellm_config.yaml中的model_name相同
{
"id": "qwen-plus",
"name": "通义千问-Plus"
},
{
"id": "deepseek-chat",
"name": "deepseek-chat"
}
]
}
}
},
"agents": {
"defaults": {
"model": {
"primary": "litellm/qwen-plus"
},
"models": { # 提供切换
"litellm/qwen-plus": {},
"litellm/deepseek-chat": {}
}
...
}
},
...
}
配置完成,重启一下gateway openclaw gateway restart
-
1)查看当前模型
-
2)切换模型
-
3)验证切换后的模型
完成多模型部署
监控token使用
多模型需要随时监控token的使用量
litellm需要将数据持久化,重新使用docker部署,并且加入postgresql数据库
-
1)创建docker网络
docker network create litellm-network -
2)创建postgresql数据库
docker run -d \ --name litellm-postgres \ --network litellm-network \ -e POSTGRES_USER=litellm \ -e POSTGRES_PASSWORD=litellm123 \ -e POSTGRES_DB=litellm \ -p 5432:5432 \ -v litellm-postgres-data:/var/lib/postgresql/data \ --restart unless-stopped \ postgres:15 -
3)创建litellm,并且指向数据库
-
修改配置文件
litellm_config.yaml,新增数据库指向model_list:
...general_settings:
master_key: wilson-litellm-private-key
database_url: postgresql://litellm:litellm123@litellm-postgres:5432/litellm -
创建litellm
docker run -d
--name litellm-proxy
--network litellm-network
-p 4000:4000
-e DATABASE_URL="postgresql://litellm:litellm123@litellm-postgres:5432/litellm"
-e LITELLM_MASTER_KEY="wilson-litellm-private-key"
-e UI_USERNAME="admin"
-e UI_PASSWORD="wilson-litellm-private-key"
-v ./litellm_config.yaml:/app/config.yaml
--restart unless-stopped
ghcr.io/berriai/litellm:main-latest
--config /app/config.yaml --port 4000
-
安装完成,打开控制台查看,http://localhost:4000/ui,使用admin/wilson-litellm-private-key登陆
功能还是非常多的,当先需要关注的就是token消耗,直奔Usage
主要观察token消耗,至于费用,不是很准,因为litellm的价格是存储在默认的文件中 /app/model_prices_and_context_window.json,文件更新的速度显然不及官网的变化,所以这里只需要观察token的消耗即可。但是为了观察token的消耗,又要装数据库、看web,貌似不是很轻便。后面找时间优化一下,现在就先将就这样吧
总结
至此,通过litellm管理多模型,并且配置在openclaw之中,切换起来也很方便
联系我
- 联系我,做深入的交流
至此,本文结束
在下才疏学浅,有撒汤漏水的,请各位不吝赐教...