{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Agent Testing Notebook\n", "\n", "This notebook is designed to test the `Agent` on a single question fetched from the API. You can select which question to run by changing the `QUESTION_INDEX_TO_TEST` variable." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Optional: Install necessary packages if you haven't already\n", "# !pip install python-dotenv langchain langchain_openai langchain_community langchain-tavily langgraph pandas requests openai" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import sys\n", "import requests\n", "import json\n", "from pathlib import Path\n", "from dotenv import load_dotenv\n", "\n", "# Add the project root to the Python path to allow importing from 'agent.py'\n", "project_root = Path.cwd()\n", "if str(project_root) not in sys.path:\n", " sys.path.append(str(project_root))\n", "\n", "from agent import Agent" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Load environment variables from a .env file in the project root\n", "load_dotenv()\n", "\n", "# --- Langsmith Tracing Setup ---\n", "# Set the following environment variables to enable Langsmith tracing.\n", "# You can set them in your .env file or uncomment the lines below and add your keys.\n", "# os.environ[\"LANGSMITH_TRACING\"] = \"true\"\n", "# os.environ[\"LANGSMITH_API_KEY\"] = \"YOUR_LANGSMITH_API_KEY\"\n", "# os.environ[\"OPENAI_API_KEY\"] = \"YOUR_OPENAI_API_KEY\"\n", "# os.environ[\"TAVILY_API_KEY\"] = \"YOUR_TAVILY_API_KEY\"\n", "# os.environ[\"NEBIUS_API_KEY\"] = \"YOUR_NEBIUS_API_KEY\"\n", "\n", "# Verify that the necessary keys are set\n", "required_keys = [\"NEBIUS_API_KEY\", \"OPENAI_API_KEY\", \"TAVILY_API_KEY\", \"LANGSMITH_API_KEY\"]\n", "print(\"Checking for required environment variables...\")\n", "for key in required_keys:\n", " if not os.getenv(key):\n", " print(f\"⚠️ Warning: Environment variable {key} is not set. The agent might not work correctly.\")\n", " else:\n", " print(f\"✅ {key} is set.\")\n", "\n", "print(\"Environment variables loaded and Langsmith tracing enabled.\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# --- 1. Fetch Questions from API ---\n", "API_URL = \"https://agents-course-unit4-scoring.hf.space\"\n", "QUESTIONS_URL = f\"{API_URL}/questions\"\n", "\n", "print(f\"Fetching questions from {QUESTIONS_URL}...\")\n", "try:\n", " response = requests.get(QUESTIONS_URL, timeout=15)\n", " response.raise_for_status()\n", " questions_data = response.json()\n", " print(f\"Successfully fetched {len(questions_data)} questions.\")\n", " # Save to a local file for easy inspection\n", " with open(\"questions.json\", \"w\") as f:\n", " json.dump(questions_data, f, indent=4)\n", " print(\"Questions also saved to questions.json\")\n", "except Exception as e:\n", " print(f\"Failed to fetch questions: {e}\")\n", " questions_data = []" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# --- 2. Display Questions for Selection ---\n", "if questions_data:\n", " for i, q in enumerate(questions_data):\n", " print(f\"--- Question {i} ---\")\n", " print(f\" ID: {q.get('task_id')}\")\n", " print(f\" Question: {q.get('question')[:300]}...\")\n", " if q.get('file_name'):\n", " print(f\" File: {q.get('file_name')}\")\n", " print(\"-\" * (len(str(i)) + 14))\n", "else:\n", " print(\"No questions to display.\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# --- 3. Select a Question to Test ---\n", "# Change the index number to test a different question.\n", "# The question about the pie recipe is usually at index 4.\n", "QUESTION_INDEX_TO_TEST = 16 # <--- CHANGE THIS VALUE\n", "\n", "print(f\"Will test question at index: {QUESTION_INDEX_TO_TEST}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# --- 4. Run the Agent on the Selected Question ---\n", "if not questions_data:\n", " print(\"Cannot run agent, no questions were fetched.\")\n", "elif not 0 <= QUESTION_INDEX_TO_TEST < len(questions_data):\n", " print(f\"Invalid index: {QUESTION_INDEX_TO_TEST}. Please choose an index between 0 and {len(questions_data) - 1}.\")\n", "else:\n", " try:\n", " # Select the question item\n", " question_item = questions_data[QUESTION_INDEX_TO_TEST]\n", " print(f\"Testing with question ID: {question_item.get('task_id')}\")\n", " print(\"-\" * 30)\n", " \n", " # Instantiate the agent\n", " print(\"Instantiating agent...\")\n", " agent = Agent()\n", " print(\"Agent instantiated successfully.\")\n", " print(\"-\" * 30)\n", "\n", " # Run the agent\n", " print(\"Running agent on the question... (This may take a moment)\")\n", " final_answer = agent(item=question_item, api_url=API_URL)\n", " print(\"-\" * 30)\n", "\n", " # Display the result\n", " print(\"✅ Agent's Final Answer:\")\n", " print(final_answer)\n", "\n", " except Exception as e:\n", " print(f\"An error occurred while running the agent: {e}\")" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 4 }