惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
Cisco Talos Blog
Cisco Talos Blog
Y
Y Combinator Blog
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
GbyAI
GbyAI
The GitHub Blog
The GitHub Blog
S
SegmentFault 最新的问题
J
Java Code Geeks
C
CXSECURITY Database RSS Feed - CXSecurity.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园 - 聂微东
B
Blog RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Privacy & Cybersecurity Law Blog
L
LangChain Blog
L
LINUX DO - 热门话题
Hugging Face - Blog
Hugging Face - Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Privacy International News Feed
F
Full Disclosure
S
Schneier on Security
T
Tenable Blog
量子位
NISL@THU
NISL@THU
Latest news
Latest news
V
Visual Studio Blog
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
博客园_首页
K
Kaspersky official blog
L
Lohrmann on Cybersecurity
美团技术团队
P
Proofpoint News Feed
P
Palo Alto Networks Blog
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
人人都是产品经理
人人都是产品经理
The Hacker News
The Hacker News
AWS News Blog
AWS News Blog
S
Securelist
The Register - Security
The Register - Security
G
GRAHAM CLULEY
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
Engineering at Meta
Engineering at Meta
C
Cisco Blogs

WAYJAM

告别 NPM Token:迁移到 Trusted Publishing 全记录 Sing-box:IPv6 优先出站与 Netflix 强制 IPv6 分流 [All In One] HomeLab 虚拟化 iKuai + Mihomo - IPv6 支持 [All In One] HomeLab 虚拟化 iKuai + Mihomo MacOS Bootstrp with Nix Fluency Support Full Rss [All In One] PVE 中 OpenWrt LXC 重启问题 Tabby 终端配置以及配置全平台同步 Cloudflare Tunnel + Nginx Proxy Manager:服务穿透 PVE 7 升级 8 笔记 PVE 安装 Android x86 & 安装 Magisk Fly.io 部署 Artalk 评论系统 [All In One] HomeLab 2023 搭建总结 [All In One] 轻量的文件 & 媒体服务器配置 [All In One] R71s 主机散热改造 [All In One] PVE LXC 安装 OpenWrt 软路由以及网络规划 [All In One] 安装 Proxmox VE 7.4 从零开始配置Rime - 2023 Self Hosted Bitwarden + Cloudfare HTTPS Debian NFS Server 以及 MacOS Client AdguardHome 和 Clash 透明代理配合使用 使用 Kind 部署 Prow 手记 BackBlaze B2 + Cloudfare Worker 图床 PicGo Amazon S3 插件 自动给模板生成 Inline-Svg Hugo Theme WeUI 发布! Hugo Theme Fluency 发布! Docker Clash 和 透明代理 有限状态机与分布式熔断器 「家庭娱乐」N1刷 EmuELEC 做游戏盒子 「家庭娱乐」N1刷 Armbian 做家庭服务器 分布式限流器之令牌桶实现 Kustomize 管理 Kubernetes 集群 Mixedpaper 主题发布到 Hugo 官方仓库 About 优化 Docker 构建时间 解决 etcd 与 grpc 不兼容问题 Kubernetes 部署 ElasticSearch7 集群 从零开始配置Rime - 2019 转移域名,更换评论系统,更换图床 搭建 NPM Proxy 之预编译包 搭建 NPM Proxy 美区Apple ID终于绑定PayPal成功 无可奈何的页面增强方案 LogStash的调试 GitLab Shell如何通过SSH工作 Python打开类 荒原 [Theme] Bitcron主题Mixedpaper发布! Ngrok端口转发 故事之五 - 柠檬茶 故事之四 - 我是天才 故事之一 Arch滚挂了怎么办? OpenWrt内网IPv6之二 某次更新后Primusrun不能启动Dota2 OpenWrt的DNS智能解析方案 替换Google CDN库 使用Privoxy将socks代理转化为http代理 ArchLinux之软件篇 再次安装ArchLinux手记 博客再次重生 搞了个Minecraft服务器 【LnV工作室荣誉出品】小苹果MV华农大版 雨梦 醉臥石中笑人間 路由器内网IPV6地址分配 OPENWRT安装软件的两个问题 路由器上进行锐捷认证 改了BIOS的LOGO Windows下搭建我的C/C++的开发环境 Linux下用Mentohust认证校园网 Archlinux+Windows 双系统安装手记(UEFI+GPT) Google-code-prettify代码高亮 《末世迷踪》 Hello Net World!
LiteLLM 多模型 API 中转
2025-02-03 · via WAYJAM

前言

随着大语言模型(LLM)应用场景的不断扩展,管理和统一调用多个 AI 模型接口的需求日益突出。目前市面上有多种解决方案,每个都有其独特的优势和适用场景:

  • one-api/new-api: 这类项目提供完整的 Web UI 界面,支持多用户管理、使用量统计等功能,适合团队或小型组织使用。
  • uni-api: 采用 Provider-First 的配置思路,通过简单的配置文件启动,特别适合个人用户快速配置多个模型服务。
  • openrouter: 作为一个集中式的 AI 模型网关,支持多个主流服务商,但对某些区域性服务(如国内的 siliconflow)支持有限,且无法配置同一服务商的多个账号。
  • LiteLLM: 既可作为 SDK 使用,也可作为独立的 LLM Gateway 部署。采用 Model-First 的配置方式,虽然在多模型配置上略显繁琐,但提供了更细粒度的模型控制能力,而且代码质量也比 uni-api 更好。

所以我使用了一段时间的 new-api 和 uni-api 之后,现在切换到 LiteLLM。本文将介绍 LiteLLM 的部署方案和一些实用的配置技巧。

Docker Compose 部署

LiteLLM 支持多种部署方式,使用 Docker Compose 是最简单和可靠的方案之一。以下是一个基础的部署配置:

name: litellm
services:
  litellm:
    image: ghcr.io/berriai/litellm:main-latest # 或者选用 stable
    volumes:
     - ./config.yaml:/app/config.yaml
    command:
     - "--config=/app/config.yaml"
    ports:
      - "4000:4000" # Map the container port to the host, change the host port if necessary
    environment:
        DATABASE_URL: "postgresql://llmproxy:dbpassword9090@db:5432/litellm"
        STORE_MODEL_IN_DB: "True" # allows adding models to proxy via UI
  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_DB: litellm
      POSTGRES_USER: llmproxy
      POSTGRES_PASSWORD: dbpassword9090
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -d litellm -U llmproxy"]
      interval: 1s
      timeout: 5s
      retries: 10

配置技巧

LiteLLM 采用 Model-First 的配置方式,这在配置多个相同提供商的模型时可能会显得重复。虽然官方推荐使用环境变量来复用配置:

model_list:
  - model_name: gpt-3.5-turbo
    litellm_params:
      model: azure/gpt-turbo-small-e
      api_base: https://my-endpoint-europe-berri-992.openai.azure.com/
      api_key: "os.environ/AZURE_API_KEY_EU"

但这种方式会导致配置信息分散在配置文件和环境变量中。对于个人用户来说,我们可以利用 YAML 的锚点(anchor)特性来实现更优雅的配置:

my_providers:
  gemini_1: &gemini_1
    api_key: xxx
  gemini_2: &gemini_2
    api_key: yyy
  provider_1: &provider_1
    api_key: zzz
    api_base: 'http://example.com/v1/chat/completions'

model_list:
  - model_name: gemini-1.5-pro
    litellm_params:
      model: gemini/gemini-1.5-pro
      <<: *gemini_1
  - model_name: gemini-1.5-pro
    litellm_params:
      model: gemini/gemini-1.5-pro
      <<: *gemini_2
  - model_name: gpt-4
    litellm_params:
      model: openai/gpt-4
      <<: *provider_1

这种配置方式有以下优势:

  1. 配置集中管理,易于维护
  2. 减少重复代码,提高配置效率
  3. 方便快速切换或更新 API 密钥
  4. 清晰的配置结构,便于理解和修改

部署完成之后就可以直接通过 MasterKey 和 API 进行使用了,如果需要访问 UI 界面则打开 http://your-endpoint-example.com/ui 即可。

结语

LiteLLM 作为一个灵活的 LLM 网关解决方案,能够很好地满足个人用户和小型团队的需求。通过合理使用 YAML 的高级特性,我们可以构建出更加优雅和易维护的配置。在选择 LLM 网关工具时,建议根据实际使用场景、规模和管理需求来权衡不同方案的优劣。