Spaces:
Runtime error
Runtime error
File size: 6,483 Bytes
94fe1d2 aa065e1 94fe1d2 aa065e1 94fe1d2 aa065e1 94fe1d2 aa065e1 94fe1d2 aa065e1 94fe1d2 aa065e1 94fe1d2 aa065e1 94fe1d2 aa065e1 94fe1d2 aa065e1 94fe1d2 aa065e1 94fe1d2 aa065e1 94fe1d2 aa065e1 94fe1d2 aa065e1 94fe1d2 aa065e1 94fe1d2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
{
"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
}
|