多模型 API Key 管理与模型切换实战

多模型 API Key 管理与模型切换实战 的模型而采集的过程当中不可避免的会使用众多厂商的模型那怎么管理这些模型的key并且做到 随时切换了本文就来解决这个问题本文使用liteLLM来管理进行模型key的管理安装liteLLM1安装liteLLM非常的简单pip3 install litellm2配置文件litellm_config.yamlmodel_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 40004测试 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: {} } ... } }, ... }配置完成重启一下gatewayopenclaw gateway restart1查看当前模型2切换模型3验证切换后的模型完成多模型部署监控token使用多模型需要随时监控token的使用量litellm需要将数据持久化重新使用docker部署并且加入postgresql数据库1创建docker网络docker network create litellm-network2创建postgresql数据库docker run -d \ --name litellm-postgres \ --network litellm-network \ -e POSTGRES_USERlitellm \ -e POSTGRES_PASSWORDlitellm123 \ -e POSTGRES_DBlitellm \ -p 5432:5432 \ -v litellm-postgres-data:/var/lib/postgresql/data \ --restart unless-stopped \ postgres:153创建litellm并且指向数据库修改配置文件litellm_config.yaml新增数据库指向model_list: ... general_settings: master_key: wilson-litellm-private-key database_url: postgresql://litellm:litellm123litellm-postgres:5432/litellm创建litellmdocker run -d \ --name litellm-proxy \ --network litellm-network \ -p 4000:4000 \ -e DATABASE_URLpostgresql://litellm:litellm123litellm-postgres:5432/litellm \ -e LITELLM_MASTER_KEYwilson-litellm-private-key \ -e UI_USERNAMEadmin \ -e UI_PASSWORDwilson-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貌似不是很轻便。后面找时间优化一下现在就先将就这样吧