diff --git a/tests/gptr-logs-handler.py b/tests/gptr-logs-handler.py index db84af0a1..fb05694ce 100644 --- a/tests/gptr-logs-handler.py +++ b/tests/gptr-logs-handler.py @@ -1,27 +1,35 @@ -from typing import Dict, Any +import logging +from typing import List, Dict, Any import asyncio from gpt_researcher import GPTResearcher class CustomLogsHandler: """A custom Logs handler class to handle JSON data.""" def __init__(self): - self.logs = [] # Initialize logs to store data + self.logs: List[Dict[str, Any]] = [] # Initialize logs to store data + logging.basicConfig(level=logging.INFO) # Set up logging configuration 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}") # For demonstration, print the log - -async def run(): - # Define the necessary parameters with sample values - + """Send JSON data and log it, with error handling.""" + try: + self.logs.append(data) # Append data to logs + logging.info(f"My custom Log: {data}") # Use logging instead of print + except Exception as e: + logging.error(f"Error logging data: {e}") # Log any errors + + def clear_logs(self) -> None: + """Clear the logs.""" + self.logs.clear() # Clear the logs list + logging.info("Logs cleared.") # Log the clearing action + +async def run() -> None: + """Run the research process and generate a report.""" query = "What happened in the latest burning man floods?" - report_type = "research_report" # Type of report to generate - report_source = "online" # Could specify source like 'online', 'books', etc. - tone = "informative" # Tone of the report ('informative', 'casual', etc.) - config_path = None # Path to a config file, if needed + report_type = "research_report" + report_source = "online" + tone = "informative" + config_path = None - # Initialize researcher with a custom WebSocket custom_logs_handler = CustomLogsHandler() researcher = GPTResearcher( @@ -35,6 +43,7 @@ async def run(): await researcher.conduct_research() # Conduct the research report = await researcher.write_report() # Write the research report + logging.info("Report generated successfully.") # Log report generation return report