Ziz*_*i96 6 python openai-api langchain py-langchain
为嵌套 json 定义输出模式的推荐方法是什么,我使用的方法感觉不太理想。
# adding to planner -> 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},}"""),] #define json schema in description, works but doesn't feel proper
refinement_output_parser = StructuredOutputParser.from_response_schemas(refinement_response_schemas)
refinement_format_instructions = refinement_output_parser.get_format_instructions()
refinement_output_parser.parse(output)
Run Code Online (Sandbox Code Playgroud)
给出:
{'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}}}
Run Code Online (Sandbox Code Playgroud)
它有效,但我想知道是否有更好的方法来解决这个问题。
小智 7
据我所知,推荐的方法是使用 pydantic 输出解析器而不是结构化输出解析器... python.langchain.com/docs/modules/model_io/output_parsers /\xe2\x80\xa6 (并处理嵌套此处解释... youtube.com/watch?v=yD_oDTeObJY)。
\n例如
\nfrom langchain.output_parsers import PydanticOutputParser\nfrom pydantic import BaseModel, Field, validator\nfrom typing import List, Optional\n\n...\n\nclass PlanItem(BaseModel):\n step: str\n tools: Optional[str] = []\n data_sources: Optional[str] = []\n sub_steps_needed: str\n\nclass Plan(BaseModel):\n plan: List[PlanItem]\n\n\nparser = PydanticOutputParser(pydantic_object=Plan)\nparser.get_format_instructions()\n\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
4251 次 |
| 最近记录: |