litellm 支持灵活的proxy 模式可以更加方便得控制后端llm 以及特性,以下简单说明下

proxy 使用

对于proxy 实际上就是一个api web sever 目前可以通过两种模式运行,一种是直接cli 指定模型的,还有一种是基于配置文件的
对于简单项目基于cli 是比较快速的方法,如果希望更加灵活的控制以及实现更加强大的proxy 能力,基于配置文件是比较推荐的

  • cli 模式
litellm --model ollama/qwen2:0.5b
  • 1.

效果

litellm proxy 模式试用_redis

 

使用
openai 模式使用了instructor 结构化工具

from openai import OpenAI
import instructor
from pydantic import BaseModel
 
class Sentiment(BaseModel):
    negative: bool
    positive: bool
client = instructor.from_openai(OpenAI(
    base_url="http://localhost:4000",
    api_key="sk-af7SVjbhFIbOPWsgEvxvvw",
))
 
# Extract structured data from natural language
response = client.chat.completions.create(
    model= "qwen2", # 注意此名称可以随意,因为cli 模式已经明确了一个模型名称
    response_model=Sentiment,
    messages=[{"role": "user",  "content":  "今天天气很不错"},],
)
print(response)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
import instructor
  • 1.

效果 (注意会发现模型太小的理解上不行)

litellm proxy 模式试用_redis_02

  • 配置文件模式
    基于配置文件模式的功能很强大包含了配置lbb 策略,配置状态处理,配置db,配置trace 以及观测,同时还包含了一个ui 可以进行模型管理
    参考配置 configv2.yaml
model_list:
  - model_name: qwen2
    litellm_params: # params for litellm.completion() - https://docs.litellm.ai/docs/completion/input#input---request-body
      model: ollama/qwen2:1.5b
      api_base: http://localhost:11434
      api_key: demo
      rpm: 60
  - model_name: qwen2
    litellm_params: # params for litellm.completion() - https://docs.litellm.ai/docs/completion/input#input---request-body
      model: ollama/qwen2:0.5b
      api_base: http://localhost:11434
      api_key: demo
      rpm: 60
router_settings:
  routing_strategy: usage-based-routing-v2 
启动

litellm --config ./configv2.yaml
代码集成 (如果运行会发现有lb 的效果)

import openai
client = openai.OpenAI(
    api_key="sk-123",
    base_url="http://localhost:4000"
)
response = client.chat.completions.create(
    model="qwen2",
    stream=True,
    messages = [
        {
            "role": "user",
            "content": "你是谁"
        }
    ]
)
 
for chunk in response:
        print(chunk.choices[0].delta.content or "", end='',flush=True)
swagger api

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
- model_name: qwen2
  • 1.

litellm proxy 模式试用_配置文件_03

一些高级使用

  • 多实例参考配置(核心是redis 的配置,实现状态共享)
    参考配置
model_list:
  - model_name: gpt-3.5-turbo
    litellm_params:
      model: azure/<your-deployment-name>
      api_base: <your-azure-endpoint>
      api_key: <your-azure-api-key>
      rpm: 6      # Rate limit for this deployment: in requests per minute (rpm)
  - model_name: gpt-3.5-turbo
    litellm_params:
      model: azure/gpt-turbo-small-ca
      api_base: https://my-endpoint-canada-berri992.openai.azure.com/
      api_key: <your-azure-api-key>
      rpm: 6
router_settings:
  redis_host: <your redis host>
  redis_password: <your redis password>
  redis_port: 1992
key 维护模式,注意此模式需要db 支持,同时配置master key,注意key 必须sk 开头
对于需要配置ui 添加模型,的可以需要配置export STORE_MODEL_IN_DB='True', 同时注意要安装pynacl 包
model_list:
  - model_name: qwen2
    litellm_params: # params for litellm.completion() - https://docs.litellm.ai/docs/completion/input#input---request-body
      model: ollama/qwen2:1.5b
      api_base: http://localhost:11434
      api_key: demo
      rpm: 60
  - model_name: qwen2
    litellm_params: # params for litellm.completion() - https://docs.litellm.ai/docs/completion/input#input---request-body
      model: ollama/qwen2:0.5b
      api_base: http://localhost:11434
      api_key: demo
      rpm: 60
router_settings:
  routing_strategy: usage-based-routing-v2 
 
general_settings: 
  master_key: sk-1234 
  database_url: "postgresql://postgres:postgres@localhost:5432/postgres"
生成key

curl 'http://0.0.0.0:4000/key/generate' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data-raw '{"models": ["qwen2"]}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
- model_name: gpt-3.5-turbo
  • 1.

效果

litellm proxy 模式试用_redis_04

  • ui 配置
    尽管ui 部分可以选择不同的llm,但是配置实际的模型的时候还是需要明确指定下provider,比如ollama 的还是应该使用ollama/qwen2:1.5b 格式的

说明

对于实际上litellm 的使用还是推荐基于proxy 的玩法,此玩法,更加强大,灵活同时支持更好的控制, 同时也包含了ui 可以方便控制

参考资料

 https://docs.litellm.ai/docs/proxy/quick_start
 https://docs.litellm.ai/docs/proxy/virtual_keys
 https://docs.litellm.ai/docs/proxy/configs