LiteLLM 统一多模型调用接口
LiteLLM 用 OpenAI 格式统一调用 Bedrock、Huggingface、VertexAI、Azure、OpenAI、Groq 等多家模型服务,并统一输入输出结构,同时提供跨部署的重试与回退路由,以及按项目、API key 和模型设置预算与速率限制。
## LiteLLM[¶](<https://docs.vllm.ai/en/latest/deployment/frameworks/litellm/#litellm>) [LiteLLM](<https://github.com/BerriAI/litellm>) 使用 OpenAI 格式调用所有 LLM API [Bedrock, Huggingface, VertexAI, TogetherAI, Azure, OpenAI, 格罗克等.] LiteLLM管理 : - 将输入翻译为提供者 `completion`, `embedding`,以及 `image_generation` 结束点 - [一致产出](<https://docs.litellm.ai/docs/completion/output>)中,将始终提供文本回复。 `['choices'][0]['message']['content']` - 跨多个部署(例如Azure/OpenAI)的重试/倒置逻辑 - [路由器](<https://docs.litellm.ai/docs/routing>) - 每个工程、 api 密钥、 模型设置预算限制和利率限制( R) [LiteLLM 代理服务器( LLM Gateway)](<https://docs.litellm.ai/docs/simple_proxy>) 而LiteLLM支持VLLM上的所有模型. ## 先决条件[¶](<https://docs.vllm.ai/en/latest/deployment/frameworks/litellm/#prerequisites>) 设置 vLLM 和 litellm 环境 : ``` pip install vllm litellm ``` ## 部署[¶](<https://docs.vllm.ai/en/latest/deployment/frameworks/litellm/#deploy>) ### 聊天补全[¶](<https://docs.vllm.ai/en/latest/deployment/frameworks/litellm/#chat-completion>) 1. 启动 vLLM 服务器使用支持的聊天补全模式,例如. ``` vllm serve qwen/Qwen1.5-0.5B-Chat ``` 2. 用Littellm调用它: 代码 ``` import litellm messages = [{"content": "Hello, how are you?", "role": "user"}] # hosted_vllm is prefix key word and necessary response = litellm.completion( model="hosted_vllm/qwen/Qwen1.5-0.5B-Chat", # pass the vllm model name messages=messages, api_base="http://{your-vllm-server-host}:{your-vllm-server-port}/v1", temperature=0.2, max_tokens=80, ) print(response) ``` ### 嵌入层[¶](<https://docs.vllm.ai/en/latest/deployment/frameworks/litellm/#embeddings>) 1. 用支持的嵌入模型启动 vLLM 服务器, 例如 . ``` vllm serve BAAI/bge-base-en-v1.5 ``` 2. 用Littellm调用它: ``` from litellm import embedding import os os.environ["HOSTED_VLLM_API_BASE"] = "http://{your-vllm-server-host}:{your-vllm-server-port}/v1" # hosted_vllm is prefix key word and necessary # pass the vllm model name embedding = embedding(model="hosted_vllm/BAAI/bge-base-en-v1.5", input=["Hello world"]) print(embedding) ``` 详情请见教程 [在 LiteLLM 中使用 vLLM](<https://docs.litellm.ai/docs/providers/vllm>). 10月 14, 2025 (英语).
查看原始来源