File size: 17,494 Bytes
dd5fe55 |
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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load SQuAD data"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import json\n",
"import pandas as pd\n",
"\n",
"def display_text_df(df):\n",
" display(df.style.set_properties(**{'white-space': 'pre-wrap'}).set_table_styles(\n",
" [{'selector': 'th', 'props': [('text-align', 'left')]},\n",
" {'selector': 'td', 'props': [('text-align', 'left')]}\n",
" ]\n",
" ).hide())\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from data import get_data\n",
"data = get_data(download=False)\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France?',\n",
" 'Saint Bernadette Soubirous')"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.question_answer_pairs[0]"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style type=\"text/css\">\n",
"#T_fc111 th {\n",
" text-align: left;\n",
"}\n",
"#T_fc111 td {\n",
" text-align: left;\n",
"}\n",
"#T_fc111_row0_col0, #T_fc111_row0_col1, #T_fc111_row1_col0, #T_fc111_row1_col1, #T_fc111_row2_col0, #T_fc111_row2_col1, #T_fc111_row3_col0, #T_fc111_row3_col1, #T_fc111_row4_col0, #T_fc111_row4_col1, #T_fc111_row5_col0, #T_fc111_row5_col1, #T_fc111_row6_col0, #T_fc111_row6_col1, #T_fc111_row7_col0, #T_fc111_row7_col1, #T_fc111_row8_col0, #T_fc111_row8_col1, #T_fc111_row9_col0, #T_fc111_row9_col1 {\n",
" white-space: pre-wrap;\n",
"}\n",
"</style>\n",
"<table id=\"T_fc111\">\n",
" <thead>\n",
" <tr>\n",
" <th id=\"T_fc111_level0_col0\" class=\"col_heading level0 col0\" >Question</th>\n",
" <th id=\"T_fc111_level0_col1\" class=\"col_heading level0 col1\" >Answer</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td id=\"T_fc111_row0_col0\" class=\"data row0 col0\" >What year was the Banská Akadémia founded?</td>\n",
" <td id=\"T_fc111_row0_col1\" class=\"data row0 col1\" >1735</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_fc111_row1_col0\" class=\"data row1 col0\" >What is another speed that can also be reported by the camera?</td>\n",
" <td id=\"T_fc111_row1_col1\" class=\"data row1 col1\" >SOS-based speed</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_fc111_row2_col0\" class=\"data row2 col0\" >Where were the use of advanced materials and techniques on display in Sumer?</td>\n",
" <td id=\"T_fc111_row2_col1\" class=\"data row2 col1\" >Sumerian temples and palaces</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_fc111_row3_col0\" class=\"data row3 col0\" >Who is elected every even numbered year?</td>\n",
" <td id=\"T_fc111_row3_col1\" class=\"data row3 col1\" >mayor</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_fc111_row4_col0\" class=\"data row4 col0\" >What was the purpose of top secret ICBM committee?</td>\n",
" <td id=\"T_fc111_row4_col1\" class=\"data row4 col1\" >decide on the feasibility of building an ICBM large enough to carry a thermonuclear weapon</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_fc111_row5_col0\" class=\"data row5 col0\" >What conferences became a requirement after Vatican II?</td>\n",
" <td id=\"T_fc111_row5_col1\" class=\"data row5 col1\" >National Bishop Conferences</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_fc111_row6_col0\" class=\"data row6 col0\" >Who does M fight with?</td>\n",
" <td id=\"T_fc111_row6_col1\" class=\"data row6 col1\" >C</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_fc111_row7_col0\" class=\"data row7 col0\" >How many species of fungi have been found on Antarctica?</td>\n",
" <td id=\"T_fc111_row7_col1\" class=\"data row7 col1\" >1150</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_fc111_row8_col0\" class=\"data row8 col0\" >After losing the battle of Guilford Courthouse, Cornawallis moved his troops where?</td>\n",
" <td id=\"T_fc111_row8_col1\" class=\"data row8 col1\" >Virginia coastline</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_fc111_row9_col0\" class=\"data row9 col0\" >What is the Olympic Torch made from?</td>\n",
" <td id=\"T_fc111_row9_col1\" class=\"data row9 col1\" >aluminum.</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n"
],
"text/plain": [
"<pandas.io.formats.style.Styler at 0x3afc43c80>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"np.random.seed(42)\n",
"arr =np.array(data.question_answer_pairs)\n",
"n_samples = 10\n",
"indices = np.random.choice(len(arr), n_samples, replace=False)\n",
"random_sample = arr[indices]\n",
"# Display the questions and answers in the random sample as a dataframe\n",
"dfSample = pd.DataFrame(random_sample, columns=[\"Question\", \"Answer\"])\n",
"display_text_df(dfSample)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create the agent to be evaluated"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"from agent import get_agent\n",
"agent = get_agent()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Run the agent on the random sample of questions"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "4bce5a5c2449435dbd058ed938db2a91",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/10 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from gradio import ChatMessage\n",
"from transformers.agents import agent_types\n",
"from tqdm.notebook import tqdm\n",
"import logging\n",
"\n",
"answers_ref, answers_pred = [], [] \n",
"\n",
"# Suppress logging from the agent, which can be quite verbose\n",
"agent.logger.setLevel(logging.CRITICAL)\n",
"\n",
"for question, answer in tqdm(random_sample):\n",
" class Output:\n",
" output: agent_types.AgentType | str = None\n",
"\n",
" prompt = question\n",
" answers_ref.append(answer)\n",
" final_answer = agent.run(prompt, stream=False, reset=True)\n",
" answers_pred.append(final_answer)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Use semantic similarity to evaluate the agent's answers against the reference answers\n",
"\n",
"* One flaw of this approach is that it does not take into account the existence of multiple acceptable answers.\n",
"* It also does not benefit from having the context of the question. "
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"from semscore import EmbeddingModelWrapper\n",
"from statistics import mean\n",
"\n",
"answers_ref = [str(answer) for answer in answers_ref]\n",
"answers_pred = [str(answer) for answer in answers_pred]\n",
"\n",
"em = EmbeddingModelWrapper()\n",
"similarities = em.get_similarities(\n",
" em.get_embeddings( answers_pred ),\n",
" em.get_embeddings( answers_ref ),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style type=\"text/css\">\n",
"#T_67704 th {\n",
" text-align: left;\n",
"}\n",
"#T_67704 td {\n",
" text-align: left;\n",
"}\n",
"#T_67704_row0_col0, #T_67704_row0_col1, #T_67704_row0_col2, #T_67704_row0_col3, #T_67704_row1_col0, #T_67704_row1_col1, #T_67704_row1_col2, #T_67704_row1_col3, #T_67704_row2_col0, #T_67704_row2_col1, #T_67704_row2_col2, #T_67704_row2_col3, #T_67704_row3_col0, #T_67704_row3_col1, #T_67704_row3_col2, #T_67704_row3_col3, #T_67704_row4_col0, #T_67704_row4_col1, #T_67704_row4_col2, #T_67704_row4_col3, #T_67704_row5_col0, #T_67704_row5_col1, #T_67704_row5_col2, #T_67704_row5_col3, #T_67704_row6_col0, #T_67704_row6_col1, #T_67704_row6_col2, #T_67704_row6_col3, #T_67704_row7_col0, #T_67704_row7_col1, #T_67704_row7_col2, #T_67704_row7_col3, #T_67704_row8_col0, #T_67704_row8_col1, #T_67704_row8_col2, #T_67704_row8_col3, #T_67704_row9_col0, #T_67704_row9_col1, #T_67704_row9_col2, #T_67704_row9_col3 {\n",
" white-space: pre-wrap;\n",
"}\n",
"</style>\n",
"<table id=\"T_67704\">\n",
" <thead>\n",
" <tr>\n",
" <th id=\"T_67704_level0_col0\" class=\"col_heading level0 col0\" >Question</th>\n",
" <th id=\"T_67704_level0_col1\" class=\"col_heading level0 col1\" >Reference Answer</th>\n",
" <th id=\"T_67704_level0_col2\" class=\"col_heading level0 col2\" >Predicted Answer</th>\n",
" <th id=\"T_67704_level0_col3\" class=\"col_heading level0 col3\" >Similarity</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td id=\"T_67704_row0_col0\" class=\"data row0 col0\" >What year was the Banská Akadémia founded?</td>\n",
" <td id=\"T_67704_row0_col1\" class=\"data row0 col1\" >1735</td>\n",
" <td id=\"T_67704_row0_col2\" class=\"data row0 col2\" >1735</td>\n",
" <td id=\"T_67704_row0_col3\" class=\"data row0 col3\" >1.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_67704_row1_col0\" class=\"data row1 col0\" >What is another speed that can also be reported by the camera?</td>\n",
" <td id=\"T_67704_row1_col1\" class=\"data row1 col1\" >SOS-based speed</td>\n",
" <td id=\"T_67704_row1_col2\" class=\"data row1 col2\" >Average speed</td>\n",
" <td id=\"T_67704_row1_col3\" class=\"data row1 col3\" >0.433297</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_67704_row2_col0\" class=\"data row2 col0\" >Where were the use of advanced materials and techniques on display in Sumer?</td>\n",
" <td id=\"T_67704_row2_col1\" class=\"data row2 col1\" >Sumerian temples and palaces</td>\n",
" <td id=\"T_67704_row2_col2\" class=\"data row2 col2\" >Based on the information provided, it appears that the Sumerians developed and displayed advanced materials and techniques such as metrology, writing, and astronomy throughout their city-states. The specific locations where these advanced materials and techniques were on display are not explicitly mentioned.\n",
"\n",
"However, considering the context of the question, I would argue that the city-states of Sumer itself is the most relevant answer. The city-states of Sumer were the hub of Sumerian civilization, culture, and innovation, and it was likely there that these advanced materials and techniques were developed, displayed, and showcased.\n",
"\n",
"Therefore, my final answer to the user request is:\n",
"\n",
"The city-states of Sumer</td>\n",
" <td id=\"T_67704_row2_col3\" class=\"data row2 col3\" >0.545807</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_67704_row3_col0\" class=\"data row3 col0\" >Who is elected every even numbered year?</td>\n",
" <td id=\"T_67704_row3_col1\" class=\"data row3 col1\" >mayor</td>\n",
" <td id=\"T_67704_row3_col2\" class=\"data row3 col2\" >mayor</td>\n",
" <td id=\"T_67704_row3_col3\" class=\"data row3 col3\" >1.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_67704_row4_col0\" class=\"data row4 col0\" >What was the purpose of top secret ICBM committee?</td>\n",
" <td id=\"T_67704_row4_col1\" class=\"data row4 col1\" >decide on the feasibility of building an ICBM large enough to carry a thermonuclear weapon</td>\n",
" <td id=\"T_67704_row4_col2\" class=\"data row4 col2\" >decide on the feasibility of building an ICBM large enough to carry a thermonuclear weapon</td>\n",
" <td id=\"T_67704_row4_col3\" class=\"data row4 col3\" >1.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_67704_row5_col0\" class=\"data row5 col0\" >What conferences became a requirement after Vatican II?</td>\n",
" <td id=\"T_67704_row5_col1\" class=\"data row5 col1\" >National Bishop Conferences</td>\n",
" <td id=\"T_67704_row5_col2\" class=\"data row5 col2\" >['National Bishop Conferences']</td>\n",
" <td id=\"T_67704_row5_col3\" class=\"data row5 col3\" >0.937632</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_67704_row6_col0\" class=\"data row6 col0\" >Who does M fight with?</td>\n",
" <td id=\"T_67704_row6_col1\" class=\"data row6 col1\" >C</td>\n",
" <td id=\"T_67704_row6_col2\" class=\"data row6 col2\" >C</td>\n",
" <td id=\"T_67704_row6_col3\" class=\"data row6 col3\" >1.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_67704_row7_col0\" class=\"data row7 col0\" >How many species of fungi have been found on Antarctica?</td>\n",
" <td id=\"T_67704_row7_col1\" class=\"data row7 col1\" >1150</td>\n",
" <td id=\"T_67704_row7_col2\" class=\"data row7 col2\" >Based on the output from the `squad_retriever` tool, I can see that there are two documents in the SQuAD dataset that answer the question \"How many species of fungi have been found on Antarctica?\".\n",
"\n",
"The first document states that about 1150 species of fungi have been recorded from Antarctica. The second document does not provide a different answer to this question.\n",
"\n",
"Therefore, my final answer is:\n",
"\n",
"There are approximately 1150 species of fungi that have been found on Antarctica.</td>\n",
" <td id=\"T_67704_row7_col3\" class=\"data row7 col3\" >-0.020657</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_67704_row8_col0\" class=\"data row8 col0\" >After losing the battle of Guilford Courthouse, Cornawallis moved his troops where?</td>\n",
" <td id=\"T_67704_row8_col1\" class=\"data row8 col1\" >Virginia coastline</td>\n",
" <td id=\"T_67704_row8_col2\" class=\"data row8 col2\" >The Virginia coastline</td>\n",
" <td id=\"T_67704_row8_col3\" class=\"data row8 col3\" >0.948570</td>\n",
" </tr>\n",
" <tr>\n",
" <td id=\"T_67704_row9_col0\" class=\"data row9 col0\" >What is the Olympic Torch made from?</td>\n",
" <td id=\"T_67704_row9_col1\" class=\"data row9 col1\" >aluminum.</td>\n",
" <td id=\"T_67704_row9_col2\" class=\"data row9 col2\" >aluminum</td>\n",
" <td id=\"T_67704_row9_col3\" class=\"data row9 col3\" >0.973508</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n"
],
"text/plain": [
"<pandas.io.formats.style.Styler at 0x3b0db7320>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Mean similarity: 0.78\n"
]
}
],
"source": [
"import pandas as pd\n",
"questions = [question for question, _ in random_sample]\n",
"dfAnswers = pd.DataFrame(list(zip(questions, answers_ref, answers_pred)), columns=[\"Question\", \"Reference Answer\", \"Predicted Answer\"])\n",
"dfAnswers[\"Similarity\"] = similarities\n",
"display(dfAnswers.style.set_properties(**{'white-space': 'pre-wrap'}).set_table_styles(\n",
" [{'selector': 'th', 'props': [('text-align', 'left')]},\n",
" {'selector': 'td', 'props': [('text-align', 'left')]}\n",
" ]\n",
").hide())\n",
"print(f\"Mean similarity: {round(mean(similarities), 2)}\")\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "aai520",
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|