Skip to content

Commit

Permalink
Update gptr-logs-handler.py
Browse files Browse the repository at this point in the history
1. Logging Configuration: Integrated logging for better output management.

2. Clear Logs Method: Added a method to clear logs.

3. Error Handling: Enhanced error handling in the send_json method.

4. Logging Report Generation: Added logging for successful report generation.

5. Improved Readability: Overall improvements for clarity and maintainability.
  • Loading branch information
DhruvKadam-git authored Oct 29, 2024
1 parent 99ba03a commit 5b0aa21
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions tests/gptr-logs-handler.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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

Expand Down

0 comments on commit 5b0aa21

Please sign in to comment.