File size: 5,681 Bytes
7a40d3a | 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 192 193 194 195 196 197 198 199 200 201 202 203 204 | {
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "abf90ca5",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import requests\n",
"from langchain_openai import ChatOpenAI\n",
"from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b4299f37",
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"\n",
"sys.path.append(os.path.abspath(\"../src\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "73b38064",
"metadata": {},
"outputs": [],
"source": [
"from agent import SmartAgent"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0f925adb",
"metadata": {},
"outputs": [],
"source": [
"# --- Constants ---\n",
"DEFAULT_API_URL = \"https://agents-course-unit4-scoring.hf.space\"\n",
"HUGGINGFACEHUB_API_TOKEN = os.getenv(\"HUGGINGFACEHUB_API_TOKEN\")\n",
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\")\n",
"\n",
"REPO_ID = \"meta-llama/Llama-3.1-8B-Instruct\"\n",
"PROVIDER_TYPE = \"openai\" # \"openai\" or \"huggingface\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "541ebb1b",
"metadata": {},
"outputs": [],
"source": [
"TAVILY_API_KEY = os.getenv(\"TAVILY_API_KEY\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "320e99b7",
"metadata": {},
"outputs": [],
"source": [
"api_url = DEFAULT_API_URL\n",
"questions_url = f\"{api_url}/questions\"\n",
"submit_url = f\"{api_url}/submit\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f31b88db",
"metadata": {},
"outputs": [],
"source": [
"# 1. Instantiate Agent\n",
"try:\n",
" if PROVIDER_TYPE == \"huggingface\":\n",
" llm = HuggingFaceEndpoint(\n",
" repo_id=REPO_ID,\n",
" huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,\n",
" )\n",
" chat = ChatHuggingFace(llm=llm, verbose=True)\n",
" elif PROVIDER_TYPE == \"openai\":\n",
" chat = ChatOpenAI(model=\"gpt-4o\", temperature=0.2)\n",
" else:\n",
" print(f\"Provider {PROVIDER_TYPE} not supported.\")\n",
"\n",
" agent = SmartAgent(chat)\n",
"\n",
"except Exception as e:\n",
" print(f\"Error instantiating agent: {e}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b4d18d12",
"metadata": {},
"outputs": [],
"source": [
"# 2. Fetch Questions\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",
" if not questions_data:\n",
" print(\"Fetched questions list is empty.\")\n",
" print(f\"Fetched {len(questions_data)} questions.\")\n",
"except requests.exceptions.RequestException as e:\n",
" print(f\"Error fetching questions: {e}\")\n",
"except requests.exceptions.JSONDecodeError as e:\n",
" print(f\"Error decoding JSON response from questions endpoint: {e}\")\n",
" print(f\"Response text: {response.text[:500]}\")\n",
"except Exception as e:\n",
" print(f\"An unexpected error occurred fetching questions: {e}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9627e327",
"metadata": {},
"outputs": [],
"source": [
"# 3. Run your Agent\n",
"results_log = []\n",
"answers_payload = []\n",
"\n",
"item = questions_data[0]\n",
"print(f\"Running agent on question: {item}\")\n",
"\n",
"task_id = item.get(\"task_id\")\n",
"question_text = item.get(\"question\")\n",
"file_name = item.get(\"file_name\")\n",
"if file_name != \"\":\n",
" files_url = f\"{api_url}/files/{task_id}\"\n",
" file = requests.get(files_url, timeout=15)\n",
" with open(file_name, \"wb\") as f:\n",
" f.write(file.content)\n",
" print(f\"Downloaded {files_url}.\")\n",
"if not task_id or question_text is None:\n",
" print(f\"Skipping item with missing task_id or question: {item}\")\n",
"try:\n",
" submitted_answer = agent(question_text, file_name)\n",
" answers_payload.append({\"task_id\": task_id, \"submitted_answer\": submitted_answer})\n",
" results_log.append(\n",
" {\n",
" \"Task ID\": task_id,\n",
" \"Question\": question_text,\n",
" \"Submitted Answer\": submitted_answer,\n",
" }\n",
" )\n",
"except Exception as e:\n",
" print(f\"Error running agent on task {task_id}: {e}\")\n",
" results_log.append(\n",
" {\n",
" \"Task ID\": task_id,\n",
" \"Question\": question_text,\n",
" \"Submitted Answer\": f\"AGENT ERROR: {e}\",\n",
" }\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "699cba0f",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|