为嵌套JSON定义输出模式

为嵌套JSON定义输出模式的最佳方法是什么?我目前使用的方法感觉并不理想。

# 添加到规划器 -> from langchain.experimental.plan_and_execute import load_chat_planner
refinement_response_schemas = [
    ResponseSchema(name="plan", description="""{'1': {'step': '','tools': [],'data_sources': [],'sub_steps_needed': bool}, '2': {'step': '','tools': [<empty list>],'data_sources': [<>], 'sub_steps_needed': bool},}"""),
] # 在描述中定义JSON模式,工作正常但感觉不够恰当
refinement_output_parser = StructuredOutputParser.from_response_schemas(refinement_response_schemas)
refinement_format_instructions = refinement_output_parser.get_format_instructions()
refinement_output_parser.parse(output)

结果如下:

{'plan': {'1': {'step': 'Identify the top 5 strikers in La Liga',
   'tools': [],
   'data_sources': ['sports websites', 'official league statistics'],
   'sub_steps_needed': False},
  '2': {'step': 'Identify the top 5 strikers in the Premier League',
   'tools': [],
   'data_sources': ['sports websites', 'official league statistics'],
   'sub_steps_needed': False},
    ...
  '6': {'step': 'Given the above steps taken, please respond to the users original question',
   'tools': [],
   'data_sources': [],
   'sub_steps_needed': False}}}

虽然它能工作,但我想要知道是否有更好的方法来处理这个问题。


回答:

据我所知,推荐的方法是使用pydantic输出解析器而不是结构化输出解析器… python.langchain.com/docs/modules/model_io/output_parsers/…(处理嵌套的解释在这里… youtube.com/watch?v=yD_oDTeObJY)。

例如:

from langchain.output_parsers import PydanticOutputParser
from pydantic import BaseModel, Field, validator
from typing import List, Optional
...
class PlanItem(BaseModel):
    step: str
    tools: Optional[str] = []
    data_sources: Optional[str] = []
    sub_steps_needed: str
class Plan(BaseModel):
    plan: List[PlanItem]
parser = PydanticOutputParser(pydantic_object=Plan)
parser.get_format_instructions()

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

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