在Langchain中为嵌套的JSON定义一个输出模式。

huangapple go评论198阅读模式
英文:

define an output schema for a nested json in langchain

问题

以下是您要翻译的内容:

"it works but I want to know if theres a better way to go about this."

英文:

Whats the recommended way to define an output schema for a nested json, the method I use doesn't feel ideal.

  1. # adding to planner -> from langchain.experimental.plan_and_execute import load_chat_planner
  2. refinement_response_schemas = [
  3. ResponseSchema(name="plan", description="""{'1': {'step': '','tools': [],'data_sources': [],'sub_steps_needed': bool},
  4. '2': {'step': '','tools': [<empty list>],'data_sources': [<>], 'sub_steps_needed': bool},}"""),] #define json schema in description, works but doesn't feel proper
  5. refinement_output_parser = StructuredOutputParser.from_response_schemas(refinement_response_schemas)
  6. refinement_format_instructions = refinement_output_parser.get_format_instructions()
  7. refinement_output_parser.parse(output)

gives:

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

it works but I want to know if theres a better way to go about this.

答案1

得分: 5

从我所看到的情况来看,建议的方法是使用Pydantic输出解析器,而不是结构化输出解析器... python.langchain.com/docs/modules/model_io/output_parsers/...(有关嵌套处理的说明在这里... youtube.com/watch?v=yD_oDTeObJY)。

例如:

  1. from langchain.output_parsers import PydanticOutputParser
  2. from pydantic import BaseModel, Field, validator
  3. from typing import List, Optional
  4. ...
  5. class PlanItem(BaseModel):
  6. step: str
  7. tools: Optional[str] = []
  8. data_sources: Optional[str] = []
  9. sub_steps_needed: str
  10. class Plan(BaseModel):
  11. plan: List[PlanItem]
  12. parser = PydanticOutputParser(pydantic_object=Plan)
  13. parser.get_format_instructions()
英文:

From what I can see the recommended approach is to use the pydantic output parser as opposed to the structured output parser... python.langchain.com/docs/modules/model_io/output_parsers/… (and dealing with nesting explained here... youtube.com/watch?v=yD_oDTeObJY).

e.g.

  1. from langchain.output_parsers import PydanticOutputParser
  2. from pydantic import BaseModel, Field, validator
  3. from typing import List, Optional
  4. ...
  5. class PlanItem(BaseModel):
  6. step: str
  7. tools: Optional[str] = []
  8. data_sources: Optional[str] = []
  9. sub_steps_needed: str
  10. class Plan(BaseModel):
  11. plan: List[PlanItem]
  12. parser = PydanticOutputParser(pydantic_object=Plan)
  13. parser.get_format_instructions()

huangapple
  • 本文由 发表于 2023年6月5日 23:11:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定