如何用 LiteLLM 的 passthrough 暴露 vLLM /classify 等非 OpenAI 端点
2026/9/9 22:22:10 网站建设 项目流程

如何用 LiteLLM 的 passthrough 暴露 vLLM /classify 等非 OpenAI 端点

【免费下载链接】litellmThe fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]项目地址: https://gitcode.com/GitHub_Trending/li/litellm

vLLM 的/classify等接口不是 OpenAI 兼容格式,LiteLLM 的统一翻译层(completionchat/completions)覆盖不到它们。LiteLLM 的 passthrough 模块就是为此设计的:它把请求原样转发给上游 LLM 服务的任意端点,不做 OpenAI 格式转换,同时仍走 LiteLLM 的 provider 配置、鉴权和日志路径。本文以 vLLM 的/classify为例,给出 SDK 直连和 Proxy 两种暴露方式,读者可以在自己的 vLLM 服务上跑通一次分类请求。

示例基于 litellm/passthrough/README.md,其中 vLLM 服务运行在http://localhost:8090,模型为papluca/xlm-roberta-base-language-detection

方式一:SDK 直连 llm_passthrough_route

最简单的用法是直接调用litellm.llm_passthrough_route,指定methodendpoint和上游地址:

import litellm response = litellm.llm_passthrough_route( model="hosted_vllm/papluca/xlm-roberta-base-language-detection", method="POST", endpoint="classify", api_base="http://localhost:8090", api_key=None, json={ "model": "swapped-for-litellm-model", "input": "Hello, world!", } ) print(response)

参数含义:

  • modelprovider/模型名形式,hosted_vllm前缀告诉 LiteLLM 走 vLLM 的 passthrough 配置;
  • endpoint:上游路径片段,这里即 vLLM 的classify,最终请求目标是api_base拼接上的该端点;
  • json:原样转发给上游的请求体。

注意请求体里的"model": "swapped-for-litellm-model"。这不是要发给 vLLM 的模型名——litellm/passthrough/main.py 中的实现会在发送前把 JSON body 的model字段替换为model参数的值(源码标注为 “SWAP MODEL IN JSON BODY”)。所以 body 中该字段写什么都会被覆盖,示例中就是一个占位标记。

print(response)输出的是httpx.Response对象,可以直接读.status_code.text判断上游返回。

可选:走 Router 的异步入口

如果模型已经配在 LiteLLM Router 的model_list里(例如需要统一管理与多部署),可以用allm_passthrough_route异步调用:

import asyncio from litellm import Router router = Router( model_list=[ { "model_name": "roberta-base-language-detection", "litellm_params": { "model": "hosted_vllm/papluca/xlm-roberta-base-language-detection", "api_base": "http://localhost:8090", } } ] ) request_data = { "model": "roberta-base-language-detection", "method": "POST", "endpoint": "classify", "api_base": "http://localhost:8090", "api_key": None, "json": { "model": "roberta-base-language-detection", "input": "Hello, world!", } } async def main(): response = await router.allm_passthrough_route(**request_data) print(response) if __name__ == "__main__": asyncio.run(main())

与直连版本的差异:model传的是model_list里注册的model_name,由 Router 解析到具体的litellm_params(含api_base)。

方式二:通过 Proxy 暴露 /vllm/{endpoint}

团队场景下更常见的是启动 LiteLLM Proxy,让客户端用 HTTP 请求访问 vLLM 端点。

1. 准备 config.yaml

model_list: - model_name: roberta-base-language-detection litellm_params: model: hosted_vllm/papluca/xlm-roberta-base-language-detection api_base: http://localhost:8090

model_name是请求体中要传的名字;litellm_params指明 provider(hosted_vllm)和上游 vLLM 地址。

2. 启动 proxy

litellm proxy --config config.yaml # RUNNING on http://localhost:4000

3. 发请求验证

curl -X POST http://localhost:4000/vllm/classify \ -H "Content-Type: application/json" \ -H "Authorization: Bearer <your-api-key>" \ -d '{"model": "roberta-base-language-detection", "input": "Hello, world!"}'

<your-api-key>是占位符,替换为你在 LiteLLM Proxy 上创建的 API key。请求命中 proxy 中注册的/vllm/{endpoint:path}路由(见 llm_passthrough_endpoints.py 中的vllm_proxy_route),该路由支持 GET、POST、PUT、DELETE、PATCH 五种方法,因此除/vllm/classify外,vLLM 的其他非 OpenAI 端点也可以按同样模式暴露。

判断请求是否走通了:

  • 请求体里的modelmodel_list中的model_name一致时,proxy 走 Router 分支(is_passthrough_request_using_router_model),把请求转发到配置的api_base
  • 正常情况下 curl 会收到 vLLM 原样的响应体与状态码,响应由 proxy 透传回客户端;
  • 请求体带stream字段时会被识别为流式请求(见 VLLMPassthroughConfig.is_streaming_request)。

遇到 “Provider not found” 时

llm_passthrough_route内部会通过ProviderConfigManagercustom_llm_provider查找 provider 的 passthrough 配置,找不到时抛出Exception(f"Provider {custom_llm_provider} not found")(见 litellm/passthrough/main.py)。这类报错通常意味着 provider 前缀写错、或该 provider 还没有注册 passthrough 配置。

要给一个新 provider 增加 passthrough 支持,litellm/passthrough/README.md 给出的做法可参考 VLLMModelInfo 的实现:

  1. 让 provider 的 ModelInfo 继承BaseLLMModelInfo
from litellm.llms.base_llm.base_utils import BaseLLMModelInfo class VLLMModelInfo(BaseLLMModelInfo): pass
  1. 通过ProviderConfigManager.get_provider_model_info注册并可查询:
from litellm.utils import ProviderConfigManager from litellm.types.utils import LlmProviders provider_config = ProviderConfigManager.get_provider_model_info( model="my-test-model", provider=LlmProviders.VLLM ) print(provider_config)

vLLM 侧的完整 passthrough 配置在 litellm/llms/vllm/passthrough/transformation.py,它的get_complete_url要求api_base必须存在,缺失时抛出VLLM api base not found——如果你的请求报这个错,检查config.yaml或调用参数里的api_base是否配置。

小结

  • SDK 直连:litellm.llm_passthrough_route(endpoint="classify", ...)一步到位,适合本地脚本,body 中的model字段会被自动替换;
  • Proxy:config.yaml 注册hosted_vllm/...模型后,客户端直接POST http://localhost:4000/vllm/classify,路由由/vllm/{endpoint:path}动态匹配;
  • 两条路径共用同一套 provider passthrough 配置,排查时优先确认model前缀、api_base和 provider 注册是否齐全。

【免费下载链接】litellmThe fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]项目地址: https://gitcode.com/GitHub_Trending/li/litellm

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询