TensorRT-LLM 如何在 trtllm-serve 中启用 Guided Decoding 输出 JSON Schema 约束结果
发布时间:2026/9/15 14:47:26 锦皓数字建站

TensorRT-LLM 如何在 trtllm-serve 中启用 Guided Decoding 输出 JSON Schema 约束结果【免费下载链接】TensorRT-LLMTensorRT LLM provides users with an easy-to-use Python API to define Large Language Models (LLMs) and supports state-of-the-art optimizations to perform inference efficiently on NVIDIA GPUs. TensorRT LLM also contains components to create Python and C runtimes that orchestrate the inference execution in a performant way.项目地址: https://gitcode.com/GitHub_Trending/te/TensorRT-LLM如果你的服务需要模型稳定地输出可被程序解析的 JSON例如固定字段、类型和取值范围的响应自由文本生成往往无法满足要求。TensorRT-LLM 的 Guided Decoding也称 constrained decoding、structured generation可以在生成阶段把输出约束到你指定的语法包括 JSON schema、正则表达式和 EBNF。本文对应的项目文档是 Guided Decoding聚焦其中一条路径在线服务模式trtllm-serve下启用 Guided Decoding 后端并通过 OpenAI 兼容的 chat completion 接口传入 JSON schema让返回内容符合约束。准备条件按 快速开始指南 完成 TensorRT-LLM 的安装。该指南给出的最快方式是拉取并运行 NGC 预构建镜像安装步骤见其中的 Installation Guide 链接。使用trtllm-serve启动一个 OpenAI 兼容服务。文档示例使用模型nvidia/Llama-3.1-8B-Instruct-FP8这是预量化模型运行 FP8 量化模型前需要先确认 GPU 支持 FP8 量化。客户端请求会发往http://localhost:8000/v1。如果trtllm-serve运行在 Docker 容器内要么把 8000 端口映射出去要么用docker exec -it container_id bash进入容器后在同一环境内发请求。用--config启用 guided decoding 后端TensorRT-LLM 提供两个语法后端在配置文件中通过guided_decoding_backend指定取值为xgrammar或llguidanceXGrammar支持 JSON schema、正则表达式、EBNF 和 structural tagLLGuidance支持 JSON schema、正则表达式、EBNF。JSON Schema 约束两个后端都支持示例使用xgrammar。创建 YAML 配置文件并通过--config传给trtllm-servecat config.yaml EOF guided_decoding_backend: xgrammar EOF trtllm-serve nvidia/Llama-3.1-8B-Instruct-FP8 --config config.yaml这里--config file.yaml是传递 YAML 配置文件的推荐参数已有工作流使用的--extra_llm_api_options file.yaml是它的等价别名可以继续工作见 note_sections.rst。服务启动后在日志中确认后端已成功初始化。文档给出的日志形态如下文档示例...... [TRT-LLM] [I] Guided decoder initialized with backend: GuidedDecodingBackend.XGRAMMAR ......看到这行日志说明 guided decoding 后端已启用没有这行时再检查config.yaml的内容和--config参数是否生效。发送带 JSON Schema 约束的请求定义 JSON schema创建 chat completion 请求时通过response_format传入。文档示例中的 schema 要求返回一个对象包含字符串类型的name匹配^[\w]$和整数类型的population且两者都是必填字段。JSON schema 也可以改用 pydantic 生成。from openai import OpenAI client OpenAI( base_urlhttp://localhost:8000/v1, api_keytensorrt_llm, ) json_schema { type: object, properties: { name: { type: string, pattern: ^[\\w]$ }, population: { type: integer }, }, required: [name, population], } messages [ { role: system, content: You are a helpful assistant., }, { role: user, content: Give me the information of the capital of France in the JSON format., }, ] chat_completion client.chat.completions.create( modelnvidia/Llama-3.1-8B-Instruct-FP8, messagesmessages, max_completion_tokens256, response_format{ type: json, schema: json_schema }, ) message chat_completion.choices[0].message print(message.content)其中base_url、model要与你的服务实际地址和模型名一致示例请求指向http://localhost:8000/v1上的nvidia/Llama-3.1-8B-Instruct-FP8。验证输出是否符合 schema文档给出的示例输出文档示例实际字段值会随模型输出变化但结构应受 schema 约束{ name: Paris, population: 2145200 }判断方式返回内容应能按json_schema解析包含name、population两个必填字段类型分别为字符串和整数name满足 pattern 约束。文档示例中population的数值不要当作固定预期值。后端选择与约束类型的边界两个后端都支持 JSON schema、正则表达式、EBNF如果你的约束不止 JSON例如结构化 function calling 的 custom formatstructural_tag只在xgrammar后端下支持此时guided_decoding_backend应选xgrammar。正则和 EBNF 的传入方式与 JSON schema 相同都是走response_format分别用{type: regex, regex: ...}和{type: ebnf, ebnf: ...}Guided Decoding 文档 对每种约束类型都有完整示例。如果你不在trtllm-serve上而是直接用 Python 的 LLM API 做离线推理同样指定guided_decoding_backend构造LLM实例时传入并在SamplingParams中用GuidedDecodingParams(jsonjson_schema)传递 schema文档的 Offline API 一节给出了对应写法。参考docs/source/features/guided-decoding.md本场景的主文档含 JSON schema、正则、EBNF、structural tag 的完整在线/离线示例。docs/source/quick-start-guide.mdtrtllm-serve的基础启动方式、端口访问与容器内请求方式。【免费下载链接】TensorRT-LLMTensorRT LLM provides users with an easy-to-use Python API to define Large Language Models (LLMs) and supports state-of-the-art optimizations to perform inference efficiently on NVIDIA GPUs. TensorRT LLM also contains components to create Python and C runtimes that orchestrate the inference execution in a performant way.项目地址: https://gitcode.com/GitHub_Trending/te/TensorRT-LLM创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
锦
锦皓数字建站
深耕本土企业品牌数字化升级,专注原创端正雅致商务官网,从视觉设计到稳定运维全程保驾护航。