ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

Windows环境安装LiteLLM+OpenCode,提高缓存命中率

Windows环境安装LiteLLM+OpenCode,提高缓存命中率 DeepSeek有专门的提高缓存命中率的工具Reasonix但是Qwen没有所以利用通用的LiteLLMOpenCode方案Windows中有两种方法一种是用Node.JSPython一种是用WSL使用NodeJSPython1. 前置条件Windows安装Python3安装pip安装Node.JS 22Redis不再赘述2. 安装LiteLLM管理员模式PowerShell或者CMD安装python -m pip install --no-cache-dir litellm[proxy]1.94.0 fastapi0.137.0 redisvl3. 创建LiteLLM配置文件我是放在了D:\litellm\litellm_config.yaml我这里用了Qwen的Token Plan所以api_base和model是Qwen的官方配置api_key: sk-xxxxxx填充你自己的API KeyRedis视情况修改model_list: - model_name: qwen3.8-max-preview litellm_params: model: dashscope/qwen3.8-max-preview api_key: sk-xxxxxx api_base: https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1 rpm: 30 tpm: 15000 headers: X-DashScope-Wait-Timeout: 60 - model_name: qwen3.7-max litellm_params: model: dashscope/qwen3.7-max api_key: sk-xxxxxx api_base: https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1 rpm: 30 tpm: 15000 headers: X-DashScope-Wait-Timeout: 60 - model_name: qwen3.7-plus litellm_params: model: dashscope/qwen3.7-plus api_key: sk-xxxxxx api_base: https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1 rpm: 30 tpm: 15000 headers: X-DashScope-Wait-Timeout: 60 - model_name: qwen3.6-flash litellm_params: model: dashscope/qwen3.6-flash api_key: sk-xxxxxx api_base: https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1 rpm: 60 tpm: 30000 headers: X-DashScope-Wait-Timeout: 60 litellm_settings: cache: True drop_params: true cache_params: caches: - type: in_memory namespace: litellm_in_memory max_size: 1000 ttl: 300 - type: redis namespace: litellm_redis_exact host: localhost port: 6379 ttl: 3600 - type: redis-semantic namespace: litellm_redis_semantic similarity_threshold: 0.92 redis_semantic_cache_embedding_model: text-embedding-3-small embedding_dimensions: 1536 host: localhost port: 6379 ttl: 3600 timeout: 5 retry_on_timeout: true router_settings: routing_strategy: simple-shuffle num_retries: 3 timeout: 60 cooldown_time: 10 allowed_fails: 5 general_settings: log_level: INFO disable_auth: true4. 创建服务启动脚本我是放在了D:\litellm\service_runner.pylitellm_exe和config_pat要按实际情况去修改因为我是用的管理员权限安装的LiteLLM所以在C:\Program Files\Python312\Scripts\litellm.exe下面import os import sys import subprocess os.environ[LITELLM_LOCAL_MODEL_COST_MAP] True os.environ[REDIS_PASSWORD] litellm_exe rC:\Program Files\Python312\Scripts\litellm.exe config_path rD:\litellm\litellm_config.yaml if not os.path.exists(litellm_exe): print(ERROR: litellm not found at, litellm_exe) sys.exit(1) if not os.path.exists(config_path): print(ERROR: Config file not found:, config_path) sys.exit(1) print(Using litellm:, litellm_exe) print(Using config:, config_path) print(REDIS_PASSWORD in env:, os.environ.get(REDIS_PASSWORD)) cmd [litellm_exe, --config, config_path, --port, 4000] my_env os.environ.copy() my_env[REDIS_PASSWORD] subprocess.run(cmd, envmy_env)5. 验证运行管理员模式打开CMD进入D:\litellm运行python service_runner.py打开PowerShell测试获取模型列表Invoke-RestMethod -Uri http://localhost:4000/v1/models | ConvertTo-Json -Depth 10发送聊天请求Invoke-RestMethod -Uri http://localhost:4000/v1/chat/completions -Method Post -Headers {Content-Typeapplication/json} -Body {model:qwen3.7-plus,messages:[{role:user,content:你好}]}6. LiteLLM自启动这里用比较简单的Windows任务计划方法按 Win R输入 taskschd.msc回车打开任务计划程序。右侧点击 “创建任务”不要选“创建基本任务”因为我们需要更多设置。常规 选项卡名称LiteLLM_Proxy描述自动启动 LiteLLM 代理服务勾选 “不管用户是否登录都要运行”如果想以系统账户运行或 “只在用户登录时运行”如果要使用当前用户的环境变量建议选这个。勾选 “使用最高权限运行”如果需要管理员权限。触发器 选项卡 → 新建开始任务启动时操作 选项卡 → 新建操作启动程序程序/脚本C:\Program Files\Python312\python.exe按自己的情况修改添加参数D:\litellm\service_runner.py按自己的情况修改起始于D:\litellm确保工作目录正确按自己的情况修改条件 选项卡如果想让任务在电池模式下也能运行取消勾选“只有在计算机使用交流电源时才启动”。点击 确定输入当前用户密码如果选择了“不管用户是否登录都要运行”则需要提供密码。重启再次测试获取模型列表和发送聊天请求7. 安装OpenCode 缓存插件npm安装OpenCodenpm install -g opencode-ai验证安装opencode --versionCMD一般没有问题如果PowerShell中npm不可用PS C:\Users\USER npm install -g opencode-ainpm : 无法加载文件 C:\Program Files\nodejs\npm.ps1因为在此系统上禁止运行脚本。有关详细信息请参阅 https:/go.microsoft.com/fwlink/?LinkID135170 中的 about_Execution_Policies。所在位置 行:1 字符: 1 npm install -g opencode-ai ~~~ CategoryInfo : SecurityError: (:) []PSSecurityException FullyQualifiedErrorId : UnauthorizedAccess管理员模式打开PowerShell执行选ASet-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser安装插件opencode plugin opencode-cache-injector opencode plugin opencode-visual-cache8. 配置OpenCode编写opencode.json放到某个文件夹下就需要在某个文件夹下运行OpenCode如果想都可以运行放到C:\Users\{你的用户名}\.config\opencode\{ $schema: https://opencode.ai/config.json, provider: { litellm: { npm: ai-sdk/openai-compatible, name: LiteLLM, options: { baseURL: http://localhost:4000/v1, litellmProxy: true }, models: { qwen3.8-max-preview: { name: Qwen 3.8 Max Preview }, qwen3.7-max: { name: Qwen 3.7 Max }, qwen3.7-plus: { name: Qwen 3.7 Plus, limit: { context: 131072, output: 8192 } }, qwen3.6-flash: { name: Qwen 3.6 Flash, limit: { context: 131072, output: 8192 } } }, defaultModel: qwen3.6-flash } }, compaction: { auto: true, prune: true }, plugin: [ opencode-cache-injector, opencode-visual-cache ] }9. OpenCode连接LiteLLMCMD或者PowerShell打开opencodeopencode在 OpenCode 聊天界面中执行连接/connectProvider nameLiteLLM与配置文件中的 name 一致API Key随意填写因为 LiteLLM 已关闭认证验证模型列表/models应显示所有已配置的模型可以自行切换10. 开始使用直接在聊天框输入编程任务即可例如写一个 Python 快速排序解释这段代码粘贴代码优化这个 SQL 查询可以看到Cache生效了如果要编辑工程则需要进入工程目录在CLI敲opencode即可使用11. Redis的向量功能由于原生Redis 8.0才开始带向量因此建议在Windows上安装8.0版本的Redis或者wsl中运行redis-stack-server否则litellm_settings需要降级为普通缓存litellm_settings:cache: Truecache_params:type: redishost: localhostport: 6379ttl: 3600使用WSL1. 启用 WSL 功能先要确保Windows支持WSL2Windows 11所有版本均完整支持 WSL 2。Windows 10 (x64 系统)版本1903或更高且内部版本号需为18362.1049或以上。Windows 10 (ARM64 系统)版本2004或更高内部版本号19041或以上。Windows Server从Windows Server 2022开始支持管理员模式打开PowerShell执行以下两条命令必须重启dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart重启后查询一下版本是否是WSL2默认版本2wsl --status升级设置WSL版本为2wsl --update wsl --set-default-version 2如果遇到微软商店异常wsl --update无法使用或者特别慢只能手动安装https://github.com/microsoft/WSL/releases/download/2.7.11/wsl.2.7.11.0.x64.msi2. 下载镜像这里用的清华源镜像Ubuntu 22.04版本https://mirrors.tuna.tsinghua.edu.cn/ubuntu-releases/22.04/ubuntu-22.04.5-wsl-amd64.wsl3. 建立安装文件夹安装镜像这里安装文件夹设置为C:\WSL\Ubuntu2204wsl文件地址换成自己的相应地址注意这种方式安装的Linux都是root权限New-Item -ItemType Directory -Path C:\WSL\Ubuntu2204 -Force wsl --import Ubuntu-22.04 C:\WSL\Ubuntu2204 C:\Users\USER\Downloads\ubuntu-22.04.5-wsl-amd64.wsl --version 2开始菜单打开Ubuntu-22.044. 换源更新安装基础软件cp /etc/apt/sources.list /etc/apt/sources.list.bak sed -i s|http://archive.ubuntu.com|http://mirrors.aliyun.com|g /etc/apt/sources.list sed -i s|http://security.ubuntu.com|http://mirrors.aliyun.com|g /etc/apt/sources.list apt update apt upgrade -y apt install -y curl wget gnupg lsb-release ca-certificates software-properties-common5. 安装LiteLLMapt install python3-pip -y pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple pip3 config set install.trusted-host pypi.tuna.tsinghua.edu.cn python3 -m pip install --no-cache-dir litellm[proxy]1.94.0 fastapi0.137.0 redisvl6. 安装Redis启动向量功能添加 Redis 官方 GPG 密钥curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg添加 Redis 仓库到 apt 源echo deb [signed-by/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main | sudo tee /etc/apt/sources.list.d/redis.list更新包列表并安装 redis-stack-serverapt-get update apt-get install redis-stack-server -y修改配置vim /opt/redis-stack/etc/redis-stack.conf增加bind 0.0.0.0protected-mode no后台运行systemctl enable redis-stack-server systemctl start redis-stack-server验证向量支持是否生效redis-cli MODULE LIST输出中应该能看到类似 name 为 search 或 redisearch 的记录。7. 配置LiteLLM我这里用了Qwen的Token Plan所以api_base和model是Qwen的官方配置api_key: sk-xxxxxx填充你自己的API Keymkdir -p /root/litellm cat /root/litellm/config.yaml EOF model_list: - model_name: qwen3.8-max-preview litellm_params: model: dashscope/qwen3.8-max-preview api_key: sk-xxxxxx api_base: https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1 rpm: 30 tpm: 15000 headers: X-DashScope-Wait-Timeout: 60 - model_name: qwen3.7-max litellm_params: model: dashscope/qwen3.7-max api_key: sk-xxxxxx api_base: https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1 rpm: 30 tpm: 15000 headers: X-DashScope-Wait-Timeout: 60 - model_name: qwen3.7-plus litellm_params: model: dashscope/qwen3.7-plus api_key: sk-xxxxxx api_base: https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1 rpm: 30 tpm: 15000 headers: X-DashScope-Wait-Timeout: 60 - model_name: qwen3.6-flash litellm_params: model: dashscope/qwen3.6-flash api_key: sk-xxxxxx api_base: https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1 rpm: 60 tpm: 30000 headers: X-DashScope-Wait-Timeout: 60 litellm_settings: cache: True drop_params: true cache_params: caches: - type: in_memory namespace: litellm_in_memory max_size: 1000 ttl: 300 - type: redis namespace: litellm_redis_exact host: localhost port: 6379 ttl: 3600 - type: redis-semantic namespace: litellm_redis_semantic similarity_threshold: 0.92 redis_semantic_cache_embedding_model: text-embedding-3-small embedding_dimensions: 1536 host: localhost port: 6379 ttl: 3600 timeout: 5 retry_on_timeout: true router_settings: routing_strategy: simple-shuffle num_retries: 3 timeout: 60 cooldown_time: 10 allowed_fails: 5 general_settings: log_level: INFO disable_auth: true EOF8. 启动LiteLLMexport REDIS_PASSWORD export LITELLM_LOCAL_MODEL_COST_MAPTrue litellm --config /root/litellm/config.yaml --port 40009. 测试LiteLLMcurl http://localhost:4000/v1/modelscurl -X POST http://localhost:4000/v1/chat/completions \ -H Content-Type: application/json \ -d { model: qwen3.8-max-preview, messages: [{role: user, content: 你好}] }10. 使用systemd服务长期并开机运行LiteLLMvim /etc/systemd/system/litellm.service内容[Unit] DescriptionLiteLLM Proxy Afternetwork.target redis-server.service [Service] Typesimple Userroot EnvironmentREDIS_PASSWORD EnvironmentLITELLM_LOCAL_MODEL_COST_MAPTrue ExecStart/usr/local/bin/litellm --config /root/litellm/config.yaml --port 4000 Restartalways RestartSec10 [Install] WantedBymulti-user.target启用并启动systemctl daemon-reload systemctl enable litellm systemctl start litellm查看状态和日志systemctl status litellm journalctl -u litellm -f11. 安装OpenCode缓存插件方法1curl -fsSL https://opencode.ai/install | bash echo export PATH/root/.opencode/bin:$PATH /root/.bashrc source /root/.bashrc方法2首先安装node 22补一个文件百度网盘 请输入提取码创建~/.nvm文件夹mkdir ~/.nvm下载后上传里面的nvm.sh文件和bash_completion到~/.nvm文件夹下复制命令如下# 将Windows下载目录的myfile.zip复制到WSL的用户主目录(~) cp /mnt/c/Users/你的Windows用户名/Downloads/myfile.zip ~/故cp /mnt/c/Users/USER/Downloads/nvm.sh ~/.nvm/nvm.sh cp /mnt/c/Users/USER/Downloads/bash_completion ~/.nvm/bash_completion执行vim ~/.bash_profile插入export NVM_DIR$HOME/.nvm [ -s $NVM_DIR/nvm.sh ] . $NVM_DIR/nvm.sh [ -s $NVM_DIR/bash_completion ] . $NVM_DIR/bash_completion执行source ~/.bash_profile安装nodejs22nvm install 22;tar.gz放在如下目录可以自行下载放置~/.nvm/.cache/bin/node-v22.23.2-linux-x64/node-v22.23.2-linux-x64.tar.xz查看安装情况node -v; npm -v;配置npm源阿里云的镜像站域名已经发生变化# npm淘宝源 npm config set registry https://registry.npmmirror.com;安装npm install -g opencode-ai检查opencode --version安装插件opencode plugin opencode-cache-injector opencode plugin opencode-visual-cache12. 配置OpenCodemkdir -p /root/code cat /root/code/opencode.json EOF { $schema: https://opencode.ai/config.json, provider: { litellm: { npm: ai-sdk/openai-compatible, name: LiteLLM, options: { baseURL: http://localhost:4000/v1, litellmProxy: true }, models: { qwen3.8-max-preview: { name: Qwen 3.8 Max Preview }, qwen3.7-max: { name: Qwen 3.7 Max }, qwen3.7-plus: { name: Qwen 3.7 Plus, limit: { context: 131072, output: 8192 } }, qwen3.6-flash: { name: Qwen 3.6 Flash, limit: { context: 131072, output: 8192 } } }, defaultModel: qwen3.6-flash } }, compaction: { auto: true, prune: true }, plugin: [ opencode-cache-injector, opencode-cache-hit ] } EOF13. 设置oc快捷别名echo alias occd /root/code OPENCODE_EXPERIMENTAL_CACHE_AUDIT1 opencode /root/.bashrc source /root/.bashrc14. OpenCode连接LiteLLM设置缓存审计export OPENCODE_EXPERIMENTAL_CACHE_AUDIT1oc在 OpenCode 聊天界面中执行连接/connectProvider nameLiteLLM与配置文件中的 name 一致API Key随意填写因为 LiteLLM 已关闭认证验证模型列表/models应显示所有已配置的模型可以自行切换15. 开始使用直接在聊天框输入编程任务即可例如写一个 Python 快速排序解释这段代码粘贴代码优化这个 SQL 查询16. Windows 连接WSL最简单的方案是使用VSCode安装VSCode安装插件WSLRemote DevelopmentOpenCode按 CtrlShiftP 打开命令面板输入 WSL选择 Remote-WSL: New Window 或 Remote-WSL: Reopen Folder in WSL选择你的 WSL 发行版如 Ubuntu在 VSCode 中使用 OpenCode运行终端输入oc17. 注意点1. 项目文件建议放在 WSL 原生目录如 ~/code而不是 Windows 挂载目录/mnt/c/...性能可提升约 10 倍。因为跨系统读写文件非常消耗IO2. 不能在WSL中直接打开Windows文件夹会直接跳出WSL环境3. Windows下的C盘D盘E盘要用/mnt/c//mnt/d//mnt/e/来代替打开文件夹要用cd4. 由于跨系统读写文件非常消耗IO因此建议用以下方法D盘E盘文件等的同步见3# Windows → WSL把 Windows 桌面的文件复制到 WSL 家目录 cp /mnt/c/Users/你的用户名/Desktop/文件.txt ~/ # WSL → Windows把 WSL 中的文件复制到 Windows 桌面 cp ~/文件.txt /mnt/c/Users/你的用户名/Desktop/
返回列表