{ "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 }