news 2026/6/20 5:33:15

【vLLM 学习】Structured Outputs

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【vLLM 学习】Structured Outputs

vLLM 是一款专为大语言模型推理加速而设计的框架,实现了 KV 缓存内存几乎零浪费,解决了内存管理瓶颈问题。

更多 vLLM 中文文档及教程可访问 →https://go.hyper.ai/Wa62f

*在线运行 vLLM 入门教程:零基础分步指南

源码 examples/offline_inference/structured_outputs.py

from enum import Enum from pydantic import BaseModel from vllm import LLM, SamplingParams from vllm.sampling_params import GuidedDecodingParams llm = LLM(model="Qwen/Qwen2.5-3B-Instruct", max_model_len=100) # 使用候选选项列表的引导式解码 guided_decoding_params = GuidedDecodingParams(choice=["Positive", "Negative"]) sampling_params = SamplingParams(guided_decoding=guided_decoding_params) outputs = llm.generate( prompts="Classify this sentiment: vLLM is wonderful!", sampling_params=sampling_params, ) print(outputs[0].outputs[0].text) # 使用 Regex 的引导式解码 guided_decoding_params = GuidedDecodingParams(regex="\w+@\w+\.com\n") sampling_params = SamplingParams(guided_decoding=guided_decoding_params, stop=["\n"]) prompt = ("Generate an email address for Alan Turing, who works in Enigma." "End in .com and new line. Example result:" "alan.turing@enigma.com\n") outputs = llm.generate(prompts=prompt, sampling_params=sampling_params) print(outputs[0].outputs[0].text) # 使用 Pydantic 模式的 JSON 引导式解码 class CarType(str, Enum): sedan = "sedan" suv = "SUV" truck = "Truck" coupe = "Coupe" class CarDescription(BaseModel): brand: str model: str car_type: CarType json_schema = CarDescription.model_json_schema() guided_decoding_params = GuidedDecodingParams(json=json_schema) sampling_params = SamplingParams(guided_decoding=guided_decoding_params) prompt = ("Generate a JSON with the brand, model and car_type of" "the most iconic car from the 90's") outputs = llm.generate( prompts=prompt, sampling_params=sampling_params, ) print(outputs[0].outputs[0].text) # 使用 Grammar 的引导式解码 simplified_sql_grammar = """ ?start: select_statement ?select_statement: "SELECT " column_list " FROM " table_name ?column_list: column_name ("," column_name)* ?table_name: identifier ?column_name: identifier ?identifier: /[a-zA-Z_][a-zA-Z0-9_]*/ """ guided_decoding_params = GuidedDecodingParams(grammar=simplified_sql_grammar) sampling_params = SamplingParams(guided_decoding=guided_decoding_params) prompt = ("Generate an SQL query to show the 'username' and 'email'" "from the 'users' table.") outputs = llm.generate( prompts=prompt, sampling_params=sampling_params, ) print(outputs[0].outputs[0].text)
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/15 17:22:41

故障排除ComfyUI工作流异常:从异常识别到根源修复的实战手册

故障排除ComfyUI工作流异常:从异常识别到根源修复的实战手册 【免费下载链接】ComfyUI-Impact-Pack 项目地址: https://gitcode.com/gh_mirrors/co/ComfyUI-Impact-Pack ComfyUI工作流异常修复是创作者在使用Impact-Pack扩展过程中必备的技能。本文将以技术…

作者头像 李华
网站建设 2026/6/13 4:02:40

好写作AI:写作拖延症晚期?三招让你秒入“心流高速路”

别装了,我知道你此刻的状态: 文档打开半小时,标题写了又删——就等灵感那“临门一脚”,结果灵感比老板的加薪承诺还遥远。 刷会手机“找灵感”吧,结果从微博吃瓜到抖音,两小时过去了,字数还是零…

作者头像 李华
网站建设 2026/6/13 18:24:30

渗透神器 - BurpSuite - 基础篇

渗透神器 - BurpSuite - 基础篇 一、什么是BurpSuite? 因为这个kali系统里面都是自带的,我这里就不讲安装方法了 BurpSuite是一款集成化的渗透测试工具,包含了很多功能,可以帮助我们高效地完成对Web应用程序的渗透测试和攻击。 …

作者头像 李华
网站建设 2026/6/18 16:58:01

Node.js面试常见问题与高频考点解析

作为多年参与Node.js技术招聘的面试官,我发现很多候选人对面试考察的重点缺乏清晰认识。Node.js面试不仅考查语法熟练度,更关注对运行时特性、异步模型和生态工具的理解深度。以下是几个高频出现的核心考察领域。 Node.js面试中事件循环如何考察 事件循环…

作者头像 李华
网站建设 2026/6/16 17:16:36

leetcode 941. Valid Mountain Array 有效的山脉数组-耗时100

Problem: 941. Valid Mountain Array 有效的山脉数组 耗时100%&#xff0c;数组长度需要>3&#xff0c;且存在上升至少需要arr[0] < arr[1]&#xff0c;然后遍历数组&#xff0c;若arr[i] < arr[i-1]则改变方向&#xff0c;若dir<0 && arr[i] > arr[i-1…

作者头像 李华