Spaces:
Runtime error
Runtime error
File size: 3,005 Bytes
4d50781 fa2543e 4d50781 37050e9 4d50781 fa2543e 4d50781 fa2543e 4d50781 fa2543e 4d50781 fa2543e 4d50781 a7abe3e 89578da a7abe3e 4d50781 fa2543e 4d50781 a7abe3e 4d50781 fa2543e 4d50781 2410f34 4d50781 0189767 4d50781 0189767 fa2543e 4d50781 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from chatbot_multiagent import submitUserMessage\n",
"import random\n",
"from math import inf\n",
"from time import time\n",
"\n",
"def QA_sample_test(quesion_test:list[str], num_samples:int=inf):\n",
" stt = time()\n",
" # Ensure there are at least 5 lines in the list\n",
" num_samples = min(10, len(quesion_test))\n",
" sample_quesion = random.sample(quesion_test, num_samples)\n",
" \n",
" result = []\n",
" for quesion in sample_quesion:\n",
" try:\n",
" answer = submitUserMessage(quesion)\n",
" result.append({'quesion': quesion, 'answer': answer}) \n",
" except Exception as e:\n",
" result.append({'quesion': quesion, 'error': e}) \n",
" \n",
" exet = time() - stt\n",
" exet_rept = f\"average execution time: {exet/num_samples}sec.\"\n",
" print(exet_rept)\n",
" return result, exet_rept"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"average execution time: 25.32185878753662sec.\n"
]
}
],
"source": [
"with open('./testsets/user_question_testsets.txt', 'r') as file:\n",
" quesion_test = file.readlines() \n",
"\n",
"results, exet_rept = QA_sample_test(quesion_test, num_samples=10)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Results saved to testsets/QA_smaple.txt\n"
]
}
],
"source": [
"file_path = 'testsets/QA_smaple.txt'\n",
"\n",
"# Open the file in write mode\n",
"with open(file_path, 'w') as file:\n",
" # Iterate over each dictionary in the list\n",
" for result in results:\n",
" # Iterate over each key-value pair in the dictionary\n",
" for key, value in result.items():\n",
" # Write each key-value pair to the file\n",
" file.write(f\"{key}: \\n\\t{str(value).strip()}\\n\")\n",
" # Add a blank line between dictionaries\n",
" file.write(\"\\n\\n\")\n",
" file.write(\"-\"*200+\"\\n\")\n",
" \n",
" file.write(exet_rept)\n",
"\n",
"print(f\"Results saved to {file_path}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|