当前位置: 首页 > news >正文

langchain 结构化输出

主要流程

1. 使用 Pydantic 定义结构化输出:

定义 AnswerWithJustification 类,用于描述输出的结构,包含以下字段:

  • answer:答案内容(字符串类型)。
  • justification:答案的理由或解释(字符串类型,部分场景为可选)。
    这种方法保证了 LLM 的输出符合预期的结构化格式。

2. with_structured_output 方法:

通过调用 with_structured_output 配置 LLM 以生成结构化的输出。
主要参数包括:

  • strict=True:严格模式,确保输出必须符合定义的结构。
  • include_raw=True:在返回的结果中同时包含模型的原始响应(未解析的JSON)以及解析后的对象。

3. 不同的输出结构实现方式:

基于 Pydantic:

  • 使用 AnswerWithJustification 类定义结构。
  • 输出严格符合类定义,模型会返回符合 BaseModel 结构的内容。

基于 JSON Schema:

  • 使用一个 oai_schema 字典直接定义输出的 JSON 格式(包括字段名称、描述及类型)。
  • JSON Schema 更灵活,适用于无需定义 Python 类的场景。

启用 json_mode:

  • 指定输出为纯 JSON 格式,并返回包含字段 answer 和 justification 的 JSON 数据。
  • 模型调用逻辑:

4. 配置一个名为 GLM-4-flash 的模型,使用指定的 API 密钥和自定义 API 地址。
使用 invoke 方法对问题生成答案,返回值包括:

  • raw:LLM 的原始 JSON 格式响应。
  • parsed:基于结构化输出模式解析后的对象(如符合 Pydantic 定义的实例)。
  • parsing_error:解析错误信息(若有)。

具体代码

1. strict=True

from typing import Optionalfrom langchain_openai import ChatOpenAI
from pydantic import BaseModel, Fieldclass AnswerWithJustification(BaseModel):'''An answer to the user question along with justification for the answer.'''answer: strjustification: Optional[str] = Field(default=..., description="A justification for the answer.")from langchain_openai import ChatOpenAI
llm = ChatOpenAI(temperature=0,model="GLM-4-flash",openai_api_key="your api key",openai_api_base="https://open.bigmodel.cn/api/paas/v4/"
)
structured_llm = llm.with_structured_output(AnswerWithJustification, strict=True
)structured_llm.invoke("What weighs more a pound of bricks or a pound of feathers"
)
AnswerWithJustification(answer='a pound of bricks', justification="The weight of an object is measured in pounds, and a pound of bricks and a pound of feathers both weigh the same amount, one pound. The phrase 'a pound of bricks or a pound of feathers' is often used to illustrate the concept of weight versus volume, as bricks are denser and have less volume compared to feathers, making them heavier per unit volume.")

2. include_raw=True

from langchain_openai import ChatOpenAI
from pydantic import BaseModelclass AnswerWithJustification(BaseModel):'''An answer to the user question along with justification for the answer.'''answer: strjustification: strstructured_llm = llm.with_structured_output(AnswerWithJustification, include_raw=True
)structured_llm.invoke("What weighs more a pound of bricks or a pound of feathers"
)
{'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_-9158287991605179419', 'function': {'arguments': '{"answer": "a pound of bricks", "justification": "The weight of an object is measured in pounds, and a pound of bricks and a pound of feathers both weigh the same amount, one pound. The phrase \'a pound of bricks or a pound of feathers\' is often used to illustrate the concept of weight versus volume, as bricks are denser and take up less volume than feathers for the same weight."}', 'name': 'AnswerWithJustification'}, 'type': 'function', 'index': 0}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 92, 'prompt_tokens': 187, 'total_tokens': 279, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'GLM-4-flash', 'system_fingerprint': None, 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-b26e455f-34b1-46bf-92b5-9a6be1aa4379-0', tool_calls=[{'name': 'AnswerWithJustification', 'args': {'answer': 'a pound of bricks', 'justification': "The weight of an object is measured in pounds, and a pound of bricks and a pound of feathers both weigh the same amount, one pound. The phrase 'a pound of bricks or a pound of feathers' is often used to illustrate the concept of weight versus volume, as bricks are denser and take up less volume than feathers for the same weight."}, 'id': 'call_-9158287991605179419', 'type': 'tool_call'}], usage_metadata={'input_tokens': 187, 'output_tokens': 92, 'total_tokens': 279, 'input_token_details': {}, 'output_token_details': {}}),'parsed': AnswerWithJustification(answer='a pound of bricks', justification="The weight of an object is measured in pounds, and a pound of bricks and a pound of feathers both weigh the same amount, one pound. The phrase 'a pound of bricks or a pound of feathers' is often used to illustrate the concept of weight versus volume, as bricks are denser and take up less volume than feathers for the same weight."),'parsing_error': None}

3. schema

from langchain_openai import ChatOpenAIoai_schema = {'name': 'AnswerWithJustification','description': 'An answer to the user question along with justification for the answer.','parameters': {'type': 'object','properties': {'answer': {'type': 'string'},'justification': {'description': 'A justification for the answer.', 'type': 'string'}},'required': ['answer']}
}structured_llm = llm.with_structured_output(oai_schema)structured_llm.invoke("What weighs more a pound of bricks or a pound of feathers"
)
{'answer': 'a pound of bricks','justification': "The weight of an object is measured in pounds, and a pound of bricks and a pound of feathers both weigh the same amount, one pound. The phrase 'a pound of bricks or a pound of feathers' is a riddle that plays on the fact that both weigh the same, but bricks are denser and heavier in volume compared to feathers. However, in terms of actual weight, they are equal."}

4. method=“json_mode”, include_raw=True

from langchain_openai import ChatOpenAI
from pydantic import BaseModelclass AnswerWithJustification(BaseModel):answer: strjustification: strstructured_llm = llm.with_structured_output(AnswerWithJustification,method="json_mode",include_raw=True
)structured_llm.invoke("Answer the following question. ""Make sure to return a JSON blob with keys 'answer' and 'justification'.\\n\\n""What's heavier a pound of bricks or a pound of feathers?"
)
{'raw': AIMessage(content='{"answer":"a pound of bricks","justification":"The question is a classic riddle. The answer is \\u0027a pound of bricks\\u0027 because both the bricks and the feathers weigh one pound, but bricks are denser and thus heavier in terms of mass. The riddle plays on the word \\u0027heavier,\\u0027 which can refer to mass rather than weight."}', additional_kwargs={'parsed': None, 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 85, 'prompt_tokens': 97, 'total_tokens': 182, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'GLM-4-flash', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-5532c43c-45ad-4ae3-8a4f-ea2eea980778-0', usage_metadata={'input_tokens': 97, 'output_tokens': 85, 'total_tokens': 182, 'input_token_details': {}, 'output_token_details': {}}),'parsed': AnswerWithJustification(answer='a pound of bricks', justification="The question is a classic riddle. The answer is 'a pound of bricks' because both the bricks and the feathers weigh one pound, but bricks are denser and thus heavier in terms of mass. The riddle plays on the word 'heavier,' which can refer to mass rather than weight."),'parsing_error': None}

http://www.mrgr.cn/news/80160.html

相关文章:

  • LeetCode hot100-74
  • React 内置的Hook学习
  • Flink CDC 读取oracle库数据性能优化
  • Z240001 基于Java+MySQL+SpringBoot+Vue实现的酒店管理系统的设计与实现
  • Java代审之常见的文件读取方法
  • 方法引用和lambda表达式的奥妙
  • 电感2222
  • #渗透测试#漏洞挖掘#红蓝攻防#护网#sql注入介绍01
  • freeswitch(开启支持MCU视频会议,使用mod_av模块)
  • 设计一个基础JWT的多开发语言分布式电商系统
  • Python课设-谁为影狂-豆瓣数据【数据获取与预处理课设】
  • 前端(五)css属性
  • C++知识整理day5容器——string容器
  • SQL server学习03-创建和管理数据表
  • 【arm】程序跑飞,SWD端口不可用修复(N32G435CBL7)
  • 40 list类 模拟实现
  • C#使用实体类Entity Framework Core操作mysql入门:从数据库反向生成模型2 处理连接字符串
  • 利用ESP-01S中继实现STM32F103C8T6与MQTT服务器的串口双向通信
  • Linux:Git
  • canal安装使用