Skip to content

Commit

Permalink
Merge pull request #1066 from kga245/reduce-docker-logging
Browse files Browse the repository at this point in the history
Logging update. Fixed regression and update to Docker log handling.
  • Loading branch information
assafelovic authored Jan 9, 2025
2 parents b4ef288 + 96ec1f1 commit 10bdeec
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 11 deletions.
27 changes: 17 additions & 10 deletions backend/server/websocket_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from gpt_researcher.utils.enum import ReportType, Tone
from multi_agents.main import run_research_task
from gpt_researcher.actions import stream_output # Import stream_output
from backend.server.server_utils import CustomLogsHandler


class WebSocketManager:
Expand Down Expand Up @@ -78,10 +79,21 @@ async def chat(self, message, websocket):
async def run_agent(task, report_type, report_source, source_urls, document_urls, tone: Tone, websocket, headers=None, config_path=""):
"""Run the agent."""
start_time = datetime.datetime.now()
# Instead of running the agent directly run it through the different report type classes

# Create logs handler for this research task
logs_handler = CustomLogsHandler(websocket, task)

# Initialize researcher based on report type
if report_type == "multi_agents":
report = await run_research_task(query=task, websocket=websocket, stream_output=stream_output, tone=tone, headers=headers)
report = await run_research_task(
query=task,
websocket=logs_handler, # Use logs_handler instead of raw websocket
stream_output=stream_output,
tone=tone,
headers=headers
)
report = report.get("report", "")

elif report_type == ReportType.DetailedReport.value:
researcher = DetailedReport(
query=task,
Expand All @@ -91,10 +103,11 @@ async def run_agent(task, report_type, report_source, source_urls, document_urls
document_urls=document_urls,
tone=tone,
config_path=config_path,
websocket=websocket,
websocket=logs_handler, # Use logs_handler instead of raw websocket
headers=headers
)
report = await researcher.run()

else:
researcher = BasicReport(
query=task,
Expand All @@ -104,15 +117,9 @@ async def run_agent(task, report_type, report_source, source_urls, document_urls
document_urls=document_urls,
tone=tone,
config_path=config_path,
websocket=websocket,
websocket=logs_handler, # Use logs_handler instead of raw websocket
headers=headers
)
report = await researcher.run()

# measure time
end_time = datetime.datetime.now()
await websocket.send_json(
{"type": "logs", "output": f"\nTotal run time: {end_time - start_time}\n"}
)

return report
8 changes: 7 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ services:
OPENAI_API_KEY: ${OPENAI_API_KEY}
TAVILY_API_KEY: ${TAVILY_API_KEY}
LANGCHAIN_API_KEY: ${LANGCHAIN_API_KEY}
LOGGING_LEVEL: INFO
volumes:
- ./outputs:/usr/src/app/outputs
restart: always
ports:
- 8000:8000
Expand All @@ -15,13 +18,15 @@ services:
image: gptresearcher/gptr-nextjs
stdin_open: true
environment:
- CHOKIDAR_USEPOLLING=true
CHOKIDAR_USEPOLLING: true
LOGGING_LEVEL: INFO
build:
dockerfile: Dockerfile.dev
context: frontend/nextjs
volumes:
- /app/node_modules
- ./frontend/nextjs:/app
- ./outputs:/app/outputs
restart: always
ports:
- 3000:3000
Expand All @@ -33,6 +38,7 @@ services:
OPENAI_API_KEY: ${OPENAI_API_KEY}
TAVILY_API_KEY: ${TAVILY_API_KEY}
LANGCHAIN_API_KEY: ${LANGCHAIN_API_KEY}
LOGGING_LEVEL: INFO
profiles: ["test"]
command: >
/bin/sh -c "
Expand Down
5 changes: 5 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
]
)

# Suppress verbose fontTools logging
logging.getLogger('fontTools').setLevel(logging.WARNING)
logging.getLogger('fontTools.subset').setLevel(logging.WARNING)
logging.getLogger('fontTools.ttLib').setLevel(logging.WARNING)

# Create logger instance
logger = logging.getLogger(__name__)

Expand Down
49 changes: 49 additions & 0 deletions tests/test_logging_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import pytest
import asyncio
from pathlib import Path
import json
import logging
from fastapi import WebSocket

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class TestWebSocket(WebSocket):
def __init__(self):
self.events = []

async def accept(self):
pass

async def send_json(self, event):
logger.info(f"WebSocket received event: {event}")
self.events.append(event)

@pytest.mark.asyncio
async def test_log_output_file():
"""Test to verify logs are properly written to output file"""
from gpt_researcher.agent import GPTResearcher

# 1. Setup like the main app
websocket = TestWebSocket()
await websocket.accept()

# 2. Initialize researcher like main app
query = "What is the capital of France?"
researcher = GPTResearcher(query=query, websocket=websocket)

# 3. Run research
await researcher.conduct_research()

# 4. Verify events were captured
logger.info(f"Events captured: {len(websocket.events)}")
assert len(websocket.events) > 0, "No events were captured"

# 5. Check output file
output_dir = Path("outputs")
output_files = list(output_dir.glob(f"task_*_{query.replace(' ', '_')[:50]}.json"))
assert len(output_files) > 0, "No output file was created"

with open(output_files[-1]) as f:
data = json.load(f)
assert len(data.get('events', [])) > 0, "No events in output file"

0 comments on commit 10bdeec

Please sign in to comment.