File size: 6,361 Bytes
a741f9e | 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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | {
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "64b2c237",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from langchain_openai import ChatOpenAI\n",
"from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace\n",
"\n",
"import sys\n",
"\n",
"sys.path.append(os.path.abspath(\"../src\"))\n",
"\n",
"from agent import SmartAgent"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "8a1ece26",
"metadata": {},
"outputs": [],
"source": [
"HUGGINGFACEHUB_API_TOKEN = os.getenv(\"HUGGINGFACEHUB_API_TOKEN\")\n",
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\")\n",
"TAVILY_API_KEY = os.getenv(\"TAVILY_API_KEY\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "1d5bd941",
"metadata": {},
"outputs": [],
"source": [
"MODEL_ID = \"gpt-4o\"\n",
"PROVIDER_TYPE = \"openai\" # \"openai\" or \"huggingface\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "711e347b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Agent initialized.\n",
"Telemetry initialized.\n"
]
}
],
"source": [
"# Instantiate Agent\n",
"try:\n",
" if PROVIDER_TYPE == \"huggingface\":\n",
" llm = HuggingFaceEndpoint(\n",
" repo_id=MODEL_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=MODEL_ID, 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": 5,
"id": "57656b10",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Agent received question: The attached Excel file contains the sales of menu items for a local fast-food chain. What were the total sales that the chain made from food (not including drinks)? Express your answer in USD with two decimal places..\n",
"Provided file: ../data/sales.xlsx.\n",
"Agent returning answer: 29623.00\n"
]
}
],
"source": [
"# Run Agent\n",
"\n",
"question = \"The attached Excel file contains the sales of menu items for a local fast-food chain. What were the total sales that the chain made from food (not including drinks)? Express your answer in USD with two decimal places.\"\n",
"filename = \"../data/sales.xlsx\"\n",
"answer = agent(question, filename)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "0f82fc20",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Agent received question: Here is a scanned invoice. Please extract the vendor name, invoice number, and total amount..\n",
"Provided file: ../data/invoice.png.\n",
"Agent returning answer: Adeline Palmerston, 01234, 440\n"
]
}
],
"source": [
"# Run Agent\n",
"\n",
"question = \"Here is a scanned invoice. Please extract the vendor name, invoice number, and total amount.\"\n",
"filename = \"../data/invoice.png\"\n",
"answer = agent(question, filename)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "833cb42b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Agent received question: Explain in detail what the conclusion of the paper Attention is all you need.\n",
"Agent returning answer: The conclusion of the paper \"Attention is All You Need\" is that the Transformer model, which relies entirely on attention mechanisms and does not use recurrence or convolution, achieves state-of-the-art results on translation tasks and is highly efficient in terms of parallelization, making it suitable for training on large datasets. The paper demonstrates that attention mechanisms alone are sufficient for achieving high performance in sequence transduction tasks.\n"
]
}
],
"source": [
"# Run Agent\n",
"\n",
"question = (\n",
" \"Explain in detail what the conclusion of the paper Attention is all you need\"\n",
")\n",
"answer = agent(question)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "9077c2b0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Agent received question: Examine the video at https://www.youtube.com/watch?v=1htKBjuUWec. What does Teal say in response to the question Isn't that hot?.\n",
" \r"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/giulia/Progetti/Agent_Course_Final_Assignment/.venv/lib/python3.10/site-packages/whisper/transcribe.py:126: UserWarning: FP16 is not supported on CPU; using FP32 instead\n",
" warnings.warn(\"FP16 is not supported on CPU; using FP32 instead\")\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Agent returning answer: Extremely\n"
]
}
],
"source": [
"# Run Agent\n",
"\n",
"question = \"Examine the video at https://www.youtube.com/watch?v=1htKBjuUWec. What does Teal say in response to the question Isn't that hot?\"\n",
"answer = agent(question)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7ee42f12",
"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
}
|