兼容OpenAI的端点
要调用位于openai代理后面的模型,请进行以下两处更改:
对于
/chat/completions:在模型名称前加上openai/,这样litellm就知道你试图调用openai的/chat/completions端点。对于
/completions:在模型名称前加上text-completion-openai/,这样litellm就知道你试图调用openai的/completions端点。[通过/v1/completions路由调用openai/端点时不需要此操作]。不要在基础URL上添加任何额外内容,例如
/v1/embedding。LiteLLM使用openai-client来发起这些调用,并且会自动添加相关的端点。
使用 - 补全
import litellm
import os
response = litellm.completion(
model="openai/mistral", # 在模型前添加`openai/`前缀,以便litellm知道要路由到OpenAI
api_key="sk-1234", # 你openai兼容端点的api密钥
api_base="http://0.0.0.0:4000", # 设置你的自定义OpenAI端点的API Base
messages=[
{
"role": "user",
"content": "Hey, how's it going?",
}
],
)
print(response)
使用 - 嵌入
import litellm
import os
response = litellm.embedding(
model="openai/GPT-J", # 在模型前添加`openai/`前缀,以便litellm知道要路由到OpenAI
api_key="sk-1234", # 你openai兼容端点的api密钥
api_base="http://0.0.0.0:4000", # 设置你的自定义OpenAI端点的API Base
input=["good morning from litellm"]
)
print(response)
使用LiteLLM代理服务器
以下是如何使用LiteLLM代理服务器调用兼容OpenAI的端点
修改config.yaml
model_list:
- model_name: my-model
litellm_params:
model: openai/<your-model-name> # 添加openai/前缀以作为OpenAI提供者路由
api_base: <model-api-base> # 为OpenAI兼容提供者添加api基础
api_key: api-key # 发送模型的api密钥info如果在测试时看到
未找到错误,请确保你的api_base带有/v1后缀示例:
http://vllm-endpoint.xyz/v1启动代理
$ litellm --config /path/to/config.yaml向LiteLLM代理服务器发送请求
import openai
client = openai.OpenAI(
api_key="sk-1234", # 如果使用虚拟密钥,传递litellm代理密钥
base_url="http://0.0.0.0:4000" # litellm-proxy-base url
)
response = client.chat.completions.create(
model="my-model",
messages = [
{
"role": "user",
"content": "what llm are you"
}
],
)
print(response)curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{
"model": "my-model",
"messages": [
{
"role": "user",
"content": "what llm are you"
}
],
}'
高级 - 禁用系统消息
某些VLLM模型(例如gemma)不支持系统消息。要将这些请求映射到'用户'消息,请使用supports_system_message标志。
model_list:
- model_name: my-custom-model
litellm_params:
model: openai/google/gemma
api_base: http://my-custom-base
api_key: ""
supports_system_message: False # 👈 关键更改