assafelovic / gpt-researcher

GPT based autonomous agent that does online comprehensive research on any given topic
https://gptr.dev
MIT License
13.01k stars 1.61k forks source link

report Variable Not Assigned for custom_report in write_report Method #534

Closed mowkalim closed 3 weeks ago

mowkalim commented 1 month ago

I encountered an issue with the write_report method in the gpt-researcher library. When the report_type is set to custom_report, the report variable is not instantiated, which leads to the following error:

Error while writing report: local variable 'report' referenced before assignment

Reproduction Steps:

  1. Set report_type to custom_report.
  2. Call the write_report method.
  3. Observe the error indicating that the report variable is referenced before assignment.

Code Snippet: Here is the relevant portion of the code:

async def write_report(self, existing_headers: list = []): """ Writes the report based on research conducted

Returns:
    str: The report
"""

if self.verbose:
    await stream_output("logs", f"✍️ Writing summary for research task: {self.query}...", self.websocket)

if self.report_type == "custom_report":
    **_self.role = self.cfg.agent_role if self.cfg.agent_role else self.role_**
elif self.report_type == "subtopic_report":
    report = await generate_report(
        query=self.query,
        context=self.context,
        agent_role_prompt=self.role,
        report_type=self.report_type,
        report_source=self.report_source,
        websocket=self.websocket,
        cfg=self.cfg,
        main_topic=self.parent_query,
        existing_headers=existing_headers
    )
else:
    report = await generate_report(
        query=self.query,
        context=self.context,
        agent_role_prompt=self.role,
        report_type=self.report_type,
        report_source=self.report_source,
        websocket=self.websocket,
        cfg=self.cfg
    )

return report

Suggested Fix: Include a call to generate_report in the custom_report block to ensure report is always assigned. For example:

if self.report_type == "custom_report": self.role = self.cfg.agent_role if self.cfg.agent_role else self.role report = await generate_report( query=self.query, context=self.context, agent_role_prompt=self.role, report_type=self.report_type, report_source=self.report_source, websocket=self.websocket, cfg=self.cfg )

Thank you for looking into this issue!

assafelovic commented 1 month ago

Hey @mowkalim seems super relevant. how about assisting with a PR for this?

mowkalim commented 1 month ago

Great work on GPT-Researcher @assafelovic ! I have submitted a pull request for this issue, numbered #551