跳至主要內容

[舊版 Proxy 👉 新版 proxy 在這裡] 本機 LiteLLM Proxy Server

一個快速且輕量、相容 OpenAI 的伺服器,可呼叫 100+ 個 LLM API。

資訊

文件已過時。新文件 👉 這裡

使用方式

uv tool install 'litellm[proxy]'
$ litellm --model ollama/codellama 

#INFO: Ollama running on http://0.0.0.0:8000

測試

在新的 shell 中執行:

$ litellm --test

取代 openai base

import openai 

openai.api_base = "http://0.0.0.0:8000"

print(openai.ChatCompletion.create(model="test", messages=[{"role":"user", "content":"Hey!"}]))

其他支援的模型:

假設您正在本機執行 vllm

$ litellm --model vllm/facebook/opt-125m

教學:搭配 Multiple LLMs + LibreChat/Chatbot-UI/Auto-Gen/ChatDev/Langroid 等使用

取代 openai base:

import openai 

openai.api_key = "any-string-here"
openai.api_base = "http://0.0.0.0:8080" # your proxy url

# call openai
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hey"}])

print(response)

# call cohere
response = openai.ChatCompletion.create(model="command-nightly", messages=[{"role": "user", "content": "Hey"}])

print(response)

本機 Proxy

以下說明如何使用本機 proxy,來測試不同 github repo 的 codellama/mistral 等模型

uv add litellm
$ ollama pull codellama # OUR Local CodeLlama  

$ litellm --model ollama/codellama --temperature 0.3 --max_tokens 2048

教學:搭配 Multiple LLMs + Aider/AutoGen/Langroid 等使用

$ litellm

#INFO: litellm proxy running on http://0.0.0.0:8000

將請求送至您的 proxy

import openai 

openai.api_key = "any-string-here"
openai.api_base = "http://0.0.0.0:8080" # your proxy url

# call gpt-3.5-turbo
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hey"}])

print(response)

# call ollama/llama2
response = openai.ChatCompletion.create(model="ollama/llama2", messages=[{"role": "user", "content": "Hey"}])

print(response)
備註

貢獻 您有在專案中使用這個伺服器嗎?請在這裡!貢獻您的教學

進階

記錄

$ litellm --logs

這會回傳最新一筆記錄(送往 LLM API 的請求 + 收到的回應)。

所有記錄都會儲存在目前目錄中一個名為 api_logs.json 的檔案裡。

設定 Proxy

如果您需要:

  • 儲存 API 金鑰
  • 設定 litellm 參數(例如:捨棄未對應的參數、設定 fallback models 等)
  • 設定特定模型參數(max tokens、temperature、api base、prompt template)

您可以只為該 session 設定這些內容(透過 cli),或透過設定檔讓這些設定在重新啟動後仍然保留。

儲存 API 金鑰

$ litellm --api_key OPENAI_API_KEY=sk-...

LiteLLM 會將其儲存到本機儲存的設定檔中,並在各次 session 之間保留。

LiteLLM Proxy 支援所有 litellm 支援的 api keys。若要為特定提供者新增金鑰,請查看此清單:

$ litellm --add_key HUGGINGFACE_API_KEY=my-api-key #[OPTIONAL]

例如:設定 api base、max tokens 和 temperature。

針對該 session

litellm --model ollama/llama2 \
--api_base http://localhost:11434 \
--max_tokens 250 \
--temperature 0.5

# OpenAI-compatible server running on http://0.0.0.0:8000

效能

我們使用 wrk 對 FastAPI server 進行了 500,000 個 HTTP 連線、持續 1 分鐘的負載測試。

結果如下:

Thread Stats   Avg      Stdev     Max   +/- Stdev
Latency 156.38ms 25.52ms 361.91ms 84.73%
Req/Sec 13.61 5.13 40.00 57.50%
383625 requests in 1.00m, 391.10MB read
Socket errors: connect 0, read 1632, write 1, timeout 0

支援/與創辦人聯絡