Skip to content

Structured Report Generation -- Structured LLM calls sometimes fail, add retry mechanism #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions cookbook/structured_report_generation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,18 @@
"\n",
"Consider which sections require web research. For example, introduction and conclusion will not require research because they will distill information from other parts of the report.\"\"\"\n",
"\n",
"\n",
"def invoke_structured_llm_with_retry(structured_llm, queries, max_attempts=3):\n",
" \"\"\"\n",
" Not all LLMs support structured generation.\n",
" Retry max_attempts to get a result back\n",
" \"\"\"\n",
" for _ in range(max_attempts):\n",
" results = structured_llm.invoke(queries)\n",
" if results:\n",
" return results\n",
" return results\n",
"\n",
"async def generate_report_plan(state: ReportState):\n",
"\n",
" # Inputs\n",
Expand All @@ -557,7 +569,8 @@
" system_instructions_query = report_planner_query_writer_instructions.format(topic=topic, report_organization=report_structure, number_of_queries=number_of_queries)\n",
" \n",
" # Generate queries \n",
" results = structured_llm.invoke([SystemMessage(content=system_instructions_query)]+[HumanMessage(content=\"Generate search queries that will help with planning the sections of the report.\")])\n",
" results = invoke_structured_llm_with_retry(structured_llm,\n",
" [SystemMessage(content=system_instructions_query)]+[HumanMessage(content=\"Generate search queries that will help with planning the sections of the report.\")])\n",
" \n",
" # Web search\n",
" query_list = [query.search_query for query in results.queries]\n",
Expand All @@ -571,7 +584,8 @@
"\n",
" # Generate sections \n",
" structured_llm = llm.with_structured_output(Sections)\n",
" report_sections = structured_llm.invoke([SystemMessage(content=system_instructions_sections)]+[HumanMessage(content=\"Generate the sections of the report. Your response must include a 'sections' field containing a list of sections. Each section must have: name, description, plan, research, and content fields.\")])\n",
" report_sections = invoke_structured_llm_with_retry(structured_llm,\n",
" [SystemMessage(content=system_instructions_sections)]+[HumanMessage(content=\"Generate the sections of the report. Your response must include a 'sections' field containing a list of sections. Each section must have: name, description, plan, research, and content fields.\")])\n",
" \n",
" return {\"sections\": report_sections.sections}"
]
Expand Down Expand Up @@ -790,7 +804,8 @@
" system_instructions = query_writer_instructions.format(section_topic=section.description, number_of_queries=number_of_queries)\n",
"\n",
" # Generate queries \n",
" queries = structured_llm.invoke([SystemMessage(content=system_instructions)]+[HumanMessage(content=\"Generate search queries on the provided topic.\")])\n",
" queries = invoke_structured_llm_with_retry(structured_llm,\n",
" [SystemMessage(content=system_instructions)]+[HumanMessage(content=\"Generate search queries on the provided topic.\")])\n",
"\n",
" return {\"search_queries\": queries.queries}\n",
"\n",
Expand Down