Add CustomLogsHandler for JSON logging in GPTResearcher

Introduced a CustomLogsHandler class to handle and log JSON data during research or search operations. Updated GPTResearcher initialization to include the CustomLogsHandler for improved logging and debugging.
This commit is contained in:
ThomasTaroni 2025-04-26 19:28:43 +02:00
parent 0892ecdc8e
commit 51eecd2830

View file

@ -44,6 +44,16 @@ if not hasattr(mcp, "researchers"):
mcp.researchers = {}
class CustomLogsHandler:
"""A custom Logs handler class to handle JSON 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"MCP Log: {data}") # For demonstration, print the log
@mcp.resource("research://{topic}")
async def research_resource(topic: str) -> str:
"""
@ -64,9 +74,10 @@ async def research_resource(topic: str) -> str:
# If not, conduct the research
logger.info(f"Conducting new research for resource on topic: {topic}")
custom_logs_handler = CustomLogsHandler()
# Initialize GPT Researcher
researcher = GPTResearcher(topic, report_type=research_type)
researcher = GPTResearcher(query=topic, report_type=research_type, websocket=custom_logs_handler)
try:
# Conduct the research
@ -105,9 +116,10 @@ async def deep_research(query: str) -> Dict[str, Any]:
# Generate a unique ID for this research session
research_id = str(uuid.uuid4())
custom_logs_handler = CustomLogsHandler()
# Initialize GPT Researcher
researcher = GPTResearcher(query, report_type=research_type)
researcher = GPTResearcher(query=query, report_type=research_type, websocket=custom_logs_handler)
# Start research
try:
@ -152,9 +164,9 @@ async def quick_search(query: str) -> Dict[str, Any]:
# Generate a unique ID for this search session
search_id = str(uuid.uuid4())
custom_logs_handler = CustomLogsHandler()
# Initialize GPT Researcher
researcher = GPTResearcher(query, report_type=research_type)
researcher = GPTResearcher(query=query, report_type=research_type, websocket=custom_logs_handler)
try:
# Perform quick search