LangChain 中强制让模型结构化输出

By | 11 1 月, 2025

可以通过 langchain 中的 StructuredOutputParser/ResponseSchema 来在 prompt_template 中添加对应的结构化输出 Prompt 引导说明,来强制模型按要求输出。

示例代码:

from langchain.prompts import PromptTemplate
from langchain.output_parsers import StructuredOutputParser, ResponseSchema

prompt_template = """您是一位专业的鲜花店文案撰写员。\n
对于售价为 {price} 元的 {flower_name},您能提供一个吸引人的简短描述吗?

{format_instructions}
"""
response_schemas = [
    ResponseSchema(name="description", description="鲜花的描述文案"),
    ResponseSchema(name="reason", description="问为什么要这样写这个文案"),
]
output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
format_instructions = output_parser.get_format_instructions()
prompt = PromptTemplate.from_template(
    prompt_template,
    partial_variables={"format_instructions": format_instructions},
)
input = prompt.format(flower_name="玫瑰", price="50")
output = model.invoke(input)
print("原始输出: \n{output}\n".format(output=output.content))
parsed_output = output_parser.parse(output.content)
print("结构化输出:\n{output}\n".format(output=json.dumps(parsed_output, ensure_ascii=False, indent=4)))

运行结果:

原始输出: 
```json
{
    "description": "这束由绚丽玫瑰组成的花束,每一朵花瓣都承载着浓烈而纯粹的爱意,适合表达最真挚的情感。",
    "reason": "该文案强调玫瑰花寓意的爱情,突出其情感价值,以吸引想表达爱意的顾客。"
}
```

结构化输出:
{
    "description": "这束由绚丽玫瑰组成的花束,每一朵花瓣都承载着浓烈而纯粹的爱意,适合表达最真挚的情感。",
    "reason": "该文案强调玫瑰花寓意的爱情,突出其情感价值,以吸引想表达爱意的顾客。"
}

88 thoughts on “LangChain 中强制让模型结构化输出

  1. Pingback: MyBlog

  2. Pingback: get androxal generic form

  3. Pingback: buy cheap enclomiphene generic health

  4. Pingback: cheap rifaximin how to purchase viagra

  5. Pingback: how to order xifaxan australia over the counter

  6. Pingback: ordering staxyn cost per tablet

  7. Pingback: buying avodart generic overnight delivery

  8. Pingback: how to order dutasteride usa discount

  9. Pingback: buy cheap flexeril cyclobenzaprine generic india

  10. Pingback: how to order gabapentin purchase online uk

  11. Pingback: comprar kamagra pela internet

  12. Pingback: medicament kamagra tablette pharmacie

  13. Pingback: cheap fildena us overnight delivery

  14. Pingback: discount itraconazole uk where buy

  15. Fran

    how to hack australian pokies, what is a blackjack dealer
    called (Fran) there a casino in sydney united states
    and safest online casino in australia, or usa $200 no deposit bonus 200 free spins

    Reply
  16. Shayne

    top bingo site uk, gambling stocks in united states and real money poker
    united states app, or top usa online poker sites

    Also visit my web-site :: hoyle casino games for mac (Shayne)

    Reply
  17. Augustus

    usa casino queen (Augustus) providers, casino toronto canada and
    same-day withdrawal online casinos united states, or big poker tournaments in australia

    Reply
  18. Miquel

    no wager united statesn grand casino tycoon cheats (Miquel), usa blackjack online free and play free
    usa bingo, or usa real money slots no deposit bonus

    Reply
  19. Avis

    online casino games australia free, uk no deposit casino and best sign up bonus does the queen mary 2 have a casino (Avis) united kingdom, or canadian casino
    guide 2021 review

    Reply
  20. Wilmer

    free australian pokies wheres the gold, poker site usa and jackpot pokies canada, or new zealandn online gambling
    pokies

    Feel free to surf to my website – epiphone casino john lennon edition (Wilmer)

    Reply

回复 wettbüro nüRnberg 取消回复

您的邮箱地址不会被公开。 必填项已用 * 标注