使用 OpenAI 格式呼叫所有 LLM API [Bedrock、Huggingface、VertexAI、TogetherAI、Azure、OpenAI、Groq 等]
LiteLLM 管理:
completion
、 embedding
和image_generation
端點['choices'][0]['message']['content']
處可用跳到 LiteLLM 代理程式(LLM 網關)文檔
跳轉至支援的 LLM 提供商
?穩定版本:使用帶有-stable
標籤的 docker 映像。這些在發布之前已經經過了 12 小時的負載測試。
支援更多提供者。缺少提供者或 LLM 平台,請提出功能請求。
重要的
LiteLLM v1.0.0 現在需要openai>=1.0.0
。遷移指南在這裡
LiteLLM v1.40.14+ 現在需要pydantic>=2.0.0
。無需更改。
pip install litellm
from litellm import completion
import os
## set ENV variables
os . environ [ "OPENAI_API_KEY" ] = "your-openai-key"
os . environ [ "COHERE_API_KEY" ] = "your-cohere-key"
messages = [{ "content" : "Hello, how are you?" , "role" : "user" }]
# openai call
response = completion ( model = "gpt-3.5-turbo" , messages = messages )
# cohere call
response = completion ( model = "command-nightly" , messages = messages )
print ( response )
使用model=
呼叫提供者支援的任何模型。這裡可能有特定於提供者的詳細信息,因此請參閱提供者文件以獲取更多信息
from litellm import acompletion
import asyncio
async def test_get_response ():
user_message = "Hello, how are you?"
messages = [{ "content" : user_message , "role" : "user" }]
response = await acompletion ( model = "gpt-3.5-turbo" , messages = messages )
return response
response = asyncio . run ( test_get_response ())
print ( response )
liteLLM 支援串流模型回應,傳遞stream=True
以取得串流迭代器作為回應。
所有模型均支援串流(Bedrock、Huggingface、TogetherAI、Azure、OpenAI 等)
from litellm import completion
response = completion ( model = "gpt-3.5-turbo" , messages = messages , stream = True )
for part in response :
print ( part . choices [ 0 ]. delta . content or "" )
# claude 2
response = completion ( 'claude-2' , messages , stream = True )
for part in response :
print ( part . choices [ 0 ]. delta . content or "" )
LiteLLM 公開預先定義的回呼以將資料傳送至 Lunary、Langfuse、DynamoDB、s3 Buckets、Helicone、Promptlayer、Traceloop、Athina、Slack
from litellm import completion
## set env variables for logging tools
os . environ [ "LUNARY_PUBLIC_KEY" ] = "your-lunary-public-key"
os . environ [ "HELICONE_API_KEY" ] = "your-helicone-auth-key"
os . environ [ "LANGFUSE_PUBLIC_KEY" ] = ""
os . environ [ "LANGFUSE_SECRET_KEY" ] = ""
os . environ [ "ATHINA_API_KEY" ] = "your-athina-api-key"
os . environ [ "OPENAI_API_KEY" ]
# set callbacks
litellm . success_callback = [ "lunary" , "langfuse" , "athina" , "helicone" ] # log input/output to lunary, langfuse, supabase, athina, helicone etc
#openai call
response = completion ( model = "gpt-3.5-turbo" , messages = [{ "role" : "user" , "content" : "Hi ? - i'm openai" }])
追蹤多個項目的支出+負載平衡
託管代理(預覽版)
代理商提供:
pip install ' litellm[proxy] '
$ litellm --model huggingface/bigcode/starcoder
# INFO: Proxy running on http://0.0.0.0:4000
重要的
將 LiteLLM 代理程式與 Langchain (Python, JS)、OpenAI SDK (Python, JS) Anthropic SDK、Mistral SDK、LlamaIndex、Instructor、Curl 結合使用
import openai # openai v1.0.0+
client = openai . OpenAI ( api_key = "anything" , base_url = "http://0.0.0.0:4000" ) # set proxy to base_url
# request sent to model set on litellm proxy, `litellm --model`
response = client . chat . completions . create ( model = "gpt-3.5-turbo" , messages = [
{
"role" : "user" ,
"content" : "this is a test request, write a short poem"
}
])
print ( response )
將代理與 Postgres DB 連接以建立代理金鑰
# Get the code
git clone https://github.com/BerriAI/litellm
# Go to folder
cd litellm
# Add the master key - you can change this after setup
echo ' LITELLM_MASTER_KEY="sk-1234" ' > .env
# Add the litellm salt key - you cannot change this after adding a model
# It is used to encrypt / decrypt your LLM API Key credentials
# We recommned - https://1password.com/password-generator/
# password generator to get a random hash for litellm salt key
echo ' LITELLM_SALT_KEY="sk-1234" ' > .env
source .env
# Start
docker-compose up
代理伺服器上/ui
上的 UI
設定多個項目的預算和速率限制POST /key/generate
curl ' http://0.0.0.0:4000/key/generate '
--header ' Authorization: Bearer sk-1234 '
--header ' Content-Type: application/json '
--data-raw ' {"models": ["gpt-3.5-turbo", "gpt-4", "claude-2"], "duration": "20m","metadata": {"user": "[email protected]", "team": "core-infra"}} '
{
" key " : " sk-kdEXbIqZRwEeEiHwdg7sFA " , # Bearer token
" expires " : " 2023-11-19T01:38:25.838000+00:00 " # datetime object
}
提供者 | 完成 | 串流媒體 | 非同步完成 | 非同步串流媒體 | 非同步嵌入 | 非同步影像生成 |
---|---|---|---|---|---|---|
開放性 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
天藍色的 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
AWS-Sagemaker | ✅ | ✅ | ✅ | ✅ | ✅ | |
aws-基岩 | ✅ | ✅ | ✅ | ✅ | ✅ | |
谷歌-vertex_ai | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
Google-手掌 | ✅ | ✅ | ✅ | ✅ | ||
Google AI Studio - 雙子座 | ✅ | ✅ | ✅ | ✅ | ||
米斯塔拉爾 ai api | ✅ | ✅ | ✅ | ✅ | ✅ | |
cloudflare 人工智慧工作者 | ✅ | ✅ | ✅ | ✅ | ||
連貫 | ✅ | ✅ | ✅ | ✅ | ✅ | |
人擇的 | ✅ | ✅ | ✅ | ✅ | ||
賦權 | ✅ | ✅ | ✅ | ✅ | ||
擁抱臉 | ✅ | ✅ | ✅ | ✅ | ✅ | |
複製 | ✅ | ✅ | ✅ | ✅ | ||
一起_ai | ✅ | ✅ | ✅ | ✅ | ||
開放路由器 | ✅ | ✅ | ✅ | ✅ | ||
艾21 | ✅ | ✅ | ✅ | ✅ | ||
巴塞滕 | ✅ | ✅ | ✅ | ✅ | ||
佛洛姆 | ✅ | ✅ | ✅ | ✅ | ||
nlp_雲 | ✅ | ✅ | ✅ | ✅ | ||
阿萊夫阿爾法 | ✅ | ✅ | ✅ | ✅ | ||
花瓣 | ✅ | ✅ | ✅ | ✅ | ||
奧拉馬 | ✅ | ✅ | ✅ | ✅ | ✅ | |
深度基礎設施 | ✅ | ✅ | ✅ | ✅ | ||
困惑-ai | ✅ | ✅ | ✅ | ✅ | ||
格羅克人工智慧 | ✅ | ✅ | ✅ | ✅ | ||
深度搜尋 | ✅ | ✅ | ✅ | ✅ | ||
任意規模 | ✅ | ✅ | ✅ | ✅ | ||
IBM - watsonx.ai | ✅ | ✅ | ✅ | ✅ | ✅ | |
航行艾 | ✅ | |||||
xinference [Xorbits 推理] | ✅ | |||||
友愛科技 | ✅ | ✅ | ✅ | ✅ |
閱讀文件
貢獻:在本地克隆儲存庫 -> 進行更改 -> 提交包含更改的 PR。
以下是在本機修改儲存庫的方法: 第 1 步:複製儲存庫
git clone https://github.com/BerriAI/litellm.git
第 2 步:導航至專案並安裝依賴項:
cd litellm
poetry install -E extra_proxy -E proxy
第 3 步:測試您的變更:
cd litellm/tests # pwd: Documents/litellm/litellm/tests
poetry run flake8
poetry run pytest .
第 4 步:提交包含您的更改的 PR! ?
對於需要更好的安全性、用戶管理和專業支援的公司
與創辦人交談
這涵蓋: