| import os |
| import sys |
| import tempfile |
| import requests |
| import asyncio |
| from dotenv import load_dotenv |
|
|
| |
| load_dotenv() |
|
|
| |
| sys.path.append(os.path.dirname(os.path.abspath(__file__))) |
|
|
| |
| from langgraph_agent_system import run_agent_system |
| from observability import flush_traces, shutdown_observability |
|
|
| |
| DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" |
|
|
| def fetch_random_question(api_base: str = DEFAULT_API_URL): |
| """Return JSON of /random-question.""" |
| resp = requests.get(f"{api_base}/random-question", timeout=30) |
| resp.raise_for_status() |
| return resp.json() |
|
|
|
|
| def maybe_download_file(task_id: str, api_base: str = DEFAULT_API_URL) -> str | None: |
| """Try to download the file associated with a given task id. Returns local path or None.""" |
| url = f"{api_base}/files/{task_id}" |
| try: |
| resp = requests.get(url, timeout=60) |
| if resp.status_code != 200: |
| print(f"No file associated with task {task_id} (status {resp.status_code}).") |
| return None |
| |
| filename = resp.headers.get("content-disposition", "").split("filename=")[-1].strip("\"") or f"{task_id}_attachment" |
| tmp_path = os.path.join(tempfile.gettempdir(), filename) |
| with open(tmp_path, "wb") as f: |
| f.write(resp.content) |
| print(f"Downloaded attachment to {tmp_path}") |
| return tmp_path |
| except requests.HTTPError as e: |
| print(f"Could not download file for task {task_id}: {e}") |
| except Exception as e: |
| print(f"Error downloading file: {e}") |
| return None |
|
|
|
|
| async def main(): |
| print("Random Agent Test - Latest LangGraph Multi-Agent System") |
| print("=" * 60) |
| |
| try: |
| |
| q = fetch_random_question() |
| task_id = str(q["task_id"]) |
| question_text = q["question"] |
| print("\n=== Random Question ===") |
| print(f"Task ID : {task_id}") |
| print(f"Question: {question_text}") |
|
|
| |
| attachment_path = maybe_download_file(task_id) |
| if attachment_path: |
| question_text += f"\n\nAttachment available at: {attachment_path}" |
|
|
| |
| print("\n=== Running Latest LangGraph Multi-Agent System ===") |
| result = await run_agent_system( |
| query=question_text, |
| user_id="test_user", |
| session_id=f"session_{task_id}" |
| ) |
| |
| print("\n=== Agent Answer ===") |
| print(result) |
| |
| except Exception as e: |
| print(f"Error in main execution: {e}") |
| import traceback |
| traceback.print_exc() |
| |
| finally: |
| |
| try: |
| flush_traces(background=False) |
| shutdown_observability() |
| print("\n✅ Agent cleanup completed") |
| except Exception as e: |
| print(f"⚠️ Cleanup warning: {e}") |
|
|
|
|
| if __name__ == "__main__": |
| asyncio.run(main()) |