From 936eb6f39454e3119b00dca7d4acabb006b00d86 Mon Sep 17 00:00:00 2001 From: ThomasTaroni Date: Fri, 25 Apr 2025 18:56:41 +0200 Subject: [PATCH] Refactor report generation and logging handlers. Introduced `CustomLogsHandler` to streamline log management and updated `ReportGenerator` to simplify report generation output. Removed unused asynchronous iterator methods, improving code clarity and reducing complexity. --- src/main.py | 3 ++- .../gptresearch/deepresearch.py | 23 +++---------------- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/src/main.py b/src/main.py index f147b19..41ac4aa 100644 --- a/src/main.py +++ b/src/main.py @@ -37,7 +37,8 @@ async def get_report_endpoint(request: ReportRequest): try: # Call the asynchronous get_report function generator = ReportGenerator(request.query, request.report_type) - await generator.generate_report() + custom_logs_handler = await generator.generate_report() + yield "Report generation completed successfully!\n" except Exception as e: yield f"Error: {str(e)}" diff --git a/src/phoenix_technologies/gptresearch/deepresearch.py b/src/phoenix_technologies/gptresearch/deepresearch.py index f10497e..4a21a1e 100644 --- a/src/phoenix_technologies/gptresearch/deepresearch.py +++ b/src/phoenix_technologies/gptresearch/deepresearch.py @@ -4,15 +4,13 @@ from typing import Dict, Any, AsyncGenerator, Coroutine class CustomLogsHandler: """A custom Logs handler class to handle JSON data.""" - def __init__(self, logs=None): - if logs is None: - logs = [] - self.logs = logs # Initialize logs to store data + def __init__(self): + self.logs = [] # Initialize logs to store data async def send_json(self, data: Dict[str, Any]) -> None: """Send JSON data and log it.""" self.logs.append(data) # Append data to logs - print(f"My custom Log: {data}") + print(f"My custom Log: {data}") # For demonstration, print the log class ReportGenerator: def __init__(self, query: str, report_type: str): @@ -51,18 +49,3 @@ class ReportGenerator: "query": self.query, "report_type": self.report_type } - - # Implementing the asynchronous iterator protocol - def __aiter__(self): - """Initialize and return the asynchronous iterator.""" - self._log_index = 0 # Iterator index - return self - - async def __anext__(self): - """Return the next log asynchronously.""" - if self._log_index < len(self.logs): - log = self.logs[self._log_index] - self._log_index += 1 - yield log # Return the next log - else: - raise StopAsyncIteration # Stop when logs are exhausted