更新时间:2025-07-29 GMT+08:00
分享

guided-decoding

什么是guided-decoding

Guided Decoding是一种用于生成文本的策略,通过提供额外的上下文或约束,来引导模型生成更符合预期的结果。

比如使用openai启动服务,通过配置guided_json参数使用JSON Schema的架构来举例。

JSON Schema使用专门的关键字来描述数据结构,例如标题title、 类型type、属性properties,必须属性required 、定义definitions等,JSON Schema通过定义对象属性、类型、格式的方式来引导模型生成一个包含用户信息的JSON对象。

其优势主要如下:

  • 上下文引导:通过提供特定的提示或上下文信息,模型可以更好地理解生成内容的方向。
  • 约束生成:可以设定某些限制条件,如关键词、主题或风格,使生成的内容更加一致和相关。
  • 提高质量:通过引导,生成的文本通常更具逻辑性和连贯性,减少无关信息的出现。

约束限制

Guided Decoding特性不能和multi-step同时使用。

离线推理使用Guided Decoding

离线推理,要使用guided-decoding,需要通过SamplingParams类中的GuidedDecodingParams进行配置。

下面是一种离线使用方式示例:
from vllm import LLM, SamplingParams
from vllm.sampling_params import GuidedDecodingParams

MODEL_NAME = ${MODEL_NAME}
llm = LLM(model=MODEL_NAME)

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)

MODEL_NAME表示对应模型路径。

在线推理使用Guided Decoding

启动推理服务请参考启动推理服务章节。

在线推理使用Guided Decoding时,在发送的请求中包含上述guided_json架构,具体示例可参考以下代码。
curl -X POST http://${docker_ip}:8080/v1/completions \
-H "Content-Type: application/json" \
-d '{
    "model": "${container_model_path}",
    "prompt": "Meet our valorous character, named Knight, who has reached the age of 32. Clad in impenetrable plate armor, Knight is well-prepared for any battle. Armed with a trusty sword and boasting a strength score of 90, this character stands as a formidable warrior on the field.Please provide details for this character, including their Name, Age, preferred Armor, Weapon, and Strength",
    "max_tokens": 200,
    "temperature": 0,
    "guided_json": "{\"title\": \"Character\", \"type\": \"object\", \"properties\": {\"name\": {\"title\": \"Name\", \"maxLength\": 10, \"type\": \"string\"}, \"age\": {\"title\": \"Age\", \"type\": \"integer\"}, \"armor\": {\"$ref\": \"#/definitions/Armor\"}, \"weapon\": {\"$ref\": \"#/definitions/Weapon\"}, \"strength\": {\"title\": \"Strength\", \"type\": \"integer\"}}, \"required\": [\"name\", \"age\", \"armor\", \"weapon\", \"strength\"], \"definitions\": {\"Armor\": {\"title\": \"Armor\", \"description\": \"An enumeration.\", \"enum\": [\"leather\", \"chainmail\", \"plate\"], \"type\": \"string\"}, \"Weapon\": {\"title\": \"Weapon\", \"description\": \"An enumeration.\", \"enum\": [\"sword\", \"axe\", \"mace\", \"spear\", \"bow\", \"crossbow\"], \"type\": \"string\"}}}"
}'

相关文档