使用 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 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
谷歌-手掌 | ✅ | ✅ | ✅ | ✅ | ||
谷歌 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! ?
对于需要更好的安全性、用户管理和专业支持的公司
与创始人交谈
这涵盖: