File size: 32,827 Bytes
e8b9514 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\Admin\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"import google.generativeai as genai\n",
"import arxiv_bot_utils as utils\n",
"import os\n",
"from getpass import getpass\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"models/gemini-1.0-pro\n",
"models/gemini-1.0-pro-001\n",
"models/gemini-1.0-pro-latest\n",
"models/gemini-1.0-pro-vision-latest\n",
"models/gemini-1.5-pro-latest\n",
"models/gemini-pro\n",
"models/gemini-pro-vision\n"
]
}
],
"source": [
"os.environ['GEMINI_API_KEY'] = getpass(\"Input your API key: \")\n",
"\n",
"gemini_api_key = os.getenv(\"GEMINI_API_KEY\")\n",
"if not gemini_api_key:\n",
" raise ValueError(\n",
" \"Gemini API Key not provided. Please provide GEMINI_API_KEY as an environment variable\"\n",
" )\n",
"genai.configure(api_key=gemini_api_key)\n",
"for m in genai.list_models():\n",
" if 'generateContent' in m.supported_generation_methods:\n",
" print(m.name)\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"config = genai.GenerationConfig(max_output_tokens=2048,\n",
" temperature=0.7)\n",
"safety_settings = [\n",
" {\n",
" \"category\": \"HARM_CATEGORY_DANGEROUS\",\n",
" \"threshold\": \"BLOCK_NONE\",\n",
" },\n",
" {\n",
" \"category\": \"HARM_CATEGORY_HARASSMENT\",\n",
" \"threshold\": \"BLOCK_NONE\",\n",
" },\n",
" {\n",
" \"category\": \"HARM_CATEGORY_HATE_SPEECH\",\n",
" \"threshold\": \"BLOCK_NONE\",\n",
" },\n",
" {\n",
" \"category\": \"HARM_CATEGORY_SEXUALLY_EXPLICIT\",\n",
" \"threshold\": \"BLOCK_NONE\",\n",
" },\n",
" {\n",
" \"category\": \"HARM_CATEGORY_DANGEROUS_CONTENT\",\n",
" \"threshold\": \"BLOCK_NONE\",\n",
" },\n",
"]\n",
"model = genai.GenerativeModel(\"gemini-pro\",\n",
" generation_config=config,\n",
" safety_settings=safety_settings)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"def extract_keyword_prompt(query):\n",
" \"\"\"A prompt that return a JSON block as arguments for querying database\"\"\"\n",
"\n",
" prompt = (\n",
" \"\"\"[INST] SYSTEM: You are an assistant that choose only one action below based on guest question.\n",
" 1. If the guest question is asking for a single specific document or article with explicit title, you need to respond the information in JSON format with 2 keys \"title\", \"author\" if found any above. The authors are separated with the word 'and'. \n",
" 2. If the guest question is asking for relevant informations about a topic, you need to respond the information in JSON format with 2 keys \"keywords\", \"description\", include a list of keywords represent the main academic topic, \\\n",
" and a description about the main topic. You may paraphrase the keywords to add more. \\\n",
" 3. If the guest is not asking for any informations or documents, you need to respond with a polite answer in JSON format with 1 key \"answer\".\n",
" QUESTION: '{query}'\n",
" [/INST]\n",
" ANSWER: \n",
" \"\"\"\n",
" ).format(query=query)\n",
"\n",
" return prompt\n",
"\n",
"def make_answer_prompt(input, contexts):\n",
" \"\"\"A prompt that return the final answer, based on the queried context\"\"\"\n",
"\n",
" prompt = (\n",
" \"\"\"[INST] You are a library assistant that help to search articles and documents based on user's question.\n",
" From guest's question, you have found some records and documents that may help. Now you need to answer the guest with the information found.\n",
" If no information found in the database, you may generate some other recommendation related to user's question using your own knowledge. Each article or paper must have a link to the pdf download page.\n",
" You should answer in a conversational form politely.\n",
" QUESTION: '{input}'\n",
" INFORMATION: '{contexts}'\n",
" [/INST]\n",
" ANSWER:\n",
" \"\"\"\n",
" ).format(input=input, contexts=contexts)\n",
"\n",
" return prompt"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"def response(args):\n",
" \"\"\"Create response context, based on input arguments\"\"\"\n",
" keys = list(dict.keys(args))\n",
" if \"answer\" in keys:\n",
" return args['answer'], None # trả lời trực tiếp\n",
" \n",
" if \"keywords\" in keys:\n",
" # perform query\n",
" query_texts = args[\"description\"]\n",
" keywords = args[\"keywords\"]\n",
" results = utils.db.query_relevant(keywords=keywords, query_texts=query_texts)\n",
" # print(results)\n",
" ids = results['metadatas'][0]\n",
" if len(ids) == 0:\n",
" # go crawl some\n",
" new_records = utils.crawl_arxiv(keyword_list=keywords, max_results=10)\n",
" print(\"Got new records: \",len(new_records))\n",
" if type(new_records) == str:\n",
" return \"Error occured, information not found\", new_records\n",
" utils.db.add(new_records)\n",
" utils.sqldb.add(new_records)\n",
" results = utils.db.query_relevant(keywords=keywords, query_texts=query_texts)\n",
" ids = results['metadatas'][0]\n",
" print(\"Re-queried on chromadb, results: \",ids)\n",
" paper_id = [id['paper_id'] for id in ids]\n",
" paper_info = utils.sqldb.query_id(paper_id)\n",
" print(paper_info)\n",
" records = [] # get title (2), author (3), link (6)\n",
" result_string = \"\"\n",
" if paper_info:\n",
" for i in range(len(paper_info)):\n",
" result_string += \"Title: {}, Author: {}, Link: {}\".format(paper_info[i][2],paper_info[i][3],paper_info[i][6])\n",
" records.append([paper_info[i][2],paper_info[i][3],paper_info[i][6]])\n",
" return result_string, records\n",
" else:\n",
" return \"Information not found\", \"Information not found\"\n",
" # invoke llm and return result\n",
"\n",
" if \"title\" in keys:\n",
" title = args['title']\n",
" authors = utils.authors_str_to_list(args['author'])\n",
" paper_info = utils.sqldb.query(title = title,author = authors)\n",
" # if query not found then go crawl brh\n",
" # print(paper_info)\n",
"\n",
" if len(paper_info) == 0:\n",
" new_records = utils.crawl_exact_paper(title=title,author=authors)\n",
" print(\"Got new records: \",len(new_records))\n",
" if type(new_records) == str:\n",
" # print(new_records)\n",
" return \"Error occured, information not found\", \"Information not found\"\n",
" utils.db.add(new_records)\n",
" utils.sqldb.add(new_records)\n",
" paper_info = utils.sqldb.query(title = title,author = authors)\n",
" print(\"Re-queried on chromadb, results: \",paper_info)\n",
" # -------------------------------------\n",
" records = [] # get title (2), author (3), link (6)\n",
" result_string = \"\"\n",
" for i in range(len(paper_info)):\n",
" result_string += \"Title: {}, Author: {}, Link: {}\".format(paper_info[i][2],paper_info[i][3],paper_info[i][6])\n",
" records.append([paper_info[i][2],paper_info[i][3],paper_info[i][6]])\n",
" # process results:\n",
" if len(result_string) == 0:\n",
" return \"Information not found\", \"Information not found\"\n",
" return result_string, records\n",
" # invoke llm and return result"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"def full_chain_single_question(input_prompt):\n",
" try:\n",
" first_prompt = extract_keyword_prompt(input_prompt)\n",
" temp_answer = model.generate_content(first_prompt).text\n",
"\n",
" args = json.loads(utils.trimming(temp_answer))\n",
" contexts, results = response(args)\n",
" if not results:\n",
" print(contexts)\n",
" else:\n",
" output_prompt = make_answer_prompt(input_prompt,contexts)\n",
" answer = model.generate_content(output_prompt).text\n",
" return temp_answer, answer\n",
" except Exception as e:\n",
" print(e)\n",
" return temp_answer, \"Error occured: \" + str(e)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('1903.04824v1', 'computer science', 'Proceedings of the Fifth International Conference on Cloud and Robotics (ICCR2018)', ' Huaxi, Zhang, Jacques Malenfan', '2019-03-12', '2019-03-12', 'http://arxiv.org/pdf/1903.04824v1'), ('1709.07597v1', 'economics', 'Inverse Reinforcement Learning with Conditional Choice Probabilities', 'Mohit Sharma, Kris M. Kitani, Joachim Groege', '2017-09-22', '2017-09-22', 'http://arxiv.org/pdf/1709.07597v1')]\n",
"Sure, here are some key papers on model predictive control for nonlinear systems:\n",
"\n",
"* **Nonlinear Model Predictive Control: A Survey** by Garcia, P.D., Prett, D.M., and Morari, M. (1989)\n",
"* **Model Predictive Control for Nonlinear Systems** by Camacho, E.F. and Bordons, C. (1999)\n",
"* **Nonlinear Model Predictive Control** by Rawlings, J.B. and Mayne, D.Q. (2009)\n",
"\n",
"As for recent reviews on the application of control theory to robotics, here are a few:\n",
"\n",
"* **Control of Robot Manipulators** by Spong, M.W., Hutchinson, S., and Vidyasagar, M. (2006)\n",
"* **Robotics: Modelling, Planning and Control** by Siciliano, B., Sciavicco, L., Villani, L., and Oriolo, G. (2010)\n",
"* **Control of Robot Arms** by Featherstone, R. (2014)\n",
"\n",
"I hope this information is helpful. Please let me know if you have any other questions.\n"
]
}
],
"source": [
"# test response, second step\n",
"input_prompt = \"Can you suggest some key papers on model predictive control for nonlinear systems, and are there any recent reviews on the application of control theory to robotics?\"\n",
"args = \"{\\n \\\"keywords\\\": [\\\"Model predictive control\\\", \\\"Nonlinear systems\\\", \\\"Robotics\\\", \\\"Control theory\\\"],\\n \\\"description\\\": \\\"Model predictive control (MPC) is a control algorithm that uses a model of the system to predict future behavior and optimize the control inputs. MPC is particularly well-suited for nonlinear systems, as it can handle the complex dynamics of these systems. In recent years, MPC has been increasingly applied to robotics, as it can improve the performance and safety of robotic systems. Control theory is a branch of mathematics that deals with the analysis and design of control systems. Control theory has been applied to a wide range of problems in robotics, including motion planning, trajectory tracking, and force control.\\\"\\n}\"\n",
"args = json.loads(args)\n",
"contexts, results = response(args)\n",
"if not results:\n",
" # direct answer\n",
" print(contexts)\n",
"else:\n",
" output_prompt = make_answer_prompt(input_prompt,contexts)\n",
" answer = model.generate_content(output_prompt).text\n",
" print(answer)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'desired': 'Natural Language Processing (Computer Science)', 'question': 'What are some recent papers on deep learning architectures for text classification, and can you recommend any surveys or reviews on the topic?'}\n",
"0\n",
"[('1904.08067v5', 'computer science', 'Text Classification Algorithms: A Survey', 'Kamran Kowsari, Kiana Jafari Meimandi, Mojtaba Heidarysafa, Sanjana Mendu, Laura E. Barnes, Donald E. Brow', '2020-05-20', '2019-04-17', 'http://arxiv.org/pdf/1904.08067v5'), ('1910.11470v1', 'computer science', 'A Survey on Recent Advances in Named Entity Recognition from Deep Learning models', 'Vikas Yadav, Steven Bethar', '2019-10-25', '2019-10-25', 'http://arxiv.org/pdf/1910.11470v1'), ('1710.04288v1', 'electrical engineering and system science', 'Audio Concept Classification with Hierarchical Deep Neural Networks', 'Mirco Ravanelli, Benjamin Elizalde, Karl Ni, Gerald Friedlan', '2017-10-11', '2017-10-11', 'http://arxiv.org/pdf/1710.04288v1')]\n",
"1\n",
"[('1601.04187v1', 'computer science', 'Conversion of Artificial Recurrent Neural Networks to Spiking Neural Networks for Low-power Neuromorphic Hardware', 'Peter U. Diehl, Guido Zarrella, Andrew Cassidy, Bruno U. Pedroni, Emre Neftc', '2016-01-16', '2016-01-16', 'http://arxiv.org/pdf/1601.04187v1'), ('1801.01093v3', 'economics', 'Comparing the Forecasting Performances of Linear Models for Electricity Prices with High RES Penetration', 'Angelica Gianfreda, Francesco Ravazzolo, Luca Rossin', '2019-11-12', '2018-01-03', 'http://arxiv.org/pdf/1801.01093v3'), ('2302.11093v1', 'electrical engineering and system science', 'Use Cases for Time-Frequency Image Representations and Deep Learning Techniques for Improved Signal Classification', 'Mehmet Parla', '2023-02-22', '2023-02-22', 'http://arxiv.org/pdf/2302.11093v1')]\n",
"2\n",
"[('1505.07907v4', 'economics', 'Linking Economic Complexity, Institutions and Income Inequality', 'D. Hartmann, M. R. Guevara, C. Jara-Figueroa, M. Aristaran, C. A. Hidalg', '2017-01-04', '2015-05-29', 'http://arxiv.org/pdf/1505.07907v4'), ('2201.09806v1', 'economics', 'Infinite Growth: A Curse or a Blessing?', 'Gennady Shkliarevsk', '2022-01-24', '2022-01-24', 'http://arxiv.org/pdf/2201.09806v1'), ('1910.11780v1', 'economics', 'Inequality in Turkey: Looking Beyond Growth', 'Bayram Cakir, Ipek Ergu', '2019-10-25', '2019-10-25', 'http://arxiv.org/pdf/1910.11780v1')]\n",
"3\n",
"[('1607.06583v2', 'computer science', \"Classification of Alzheimer's Disease Structural MRI Data by Deep Learning Convolutional Neural Networks\", 'Saman Sarraf, Ghassem Tofigh', '2017-05-19', '2016-07-22', 'http://arxiv.org/pdf/1607.06583v2'), ('1804.02527v1', 'computer science', 'Visual Analytics for Explainable Deep Learning', 'Jaegul Choo, Shixia Li', '2018-04-07', '2018-04-07', 'http://arxiv.org/pdf/1804.02527v1'), ('2208.03143v1', 'computer science', 'Deep Learning and Health Informatics for Smart Monitoring and Diagnosis', 'Amin Gasm', '2022-08-05', '2022-08-05', 'http://arxiv.org/pdf/2208.03143v1')]\n",
"4\n",
"Got new records: 10\n",
"Re-queried on chromadb, results: []\n",
"None\n",
"5\n",
"[('2308.16539v2', 'computer science', 'On a Connection between Differential Games, Optimal Control, and Energy-based Models for Multi-Agent Interactions', 'Christopher Diehl, Tobias Klosek, Martin Krüger, Nils Murzyn, Torsten Bertra', '2023-10-16', '2023-08-31', 'http://arxiv.org/pdf/2308.16539v2'), ('2008.13221v1', 'computer science', 'Human-in-the-Loop Methods for Data-Driven and Reinforcement Learning Systems', 'Vinicius G. Goeck', '2020-08-30', '2020-08-30', 'http://arxiv.org/pdf/2008.13221v1')]\n",
"6\n",
"Expecting ',' delimiter: line 1 column 498 (char 497)\n",
"7\n",
"[('2101.00793v2', 'computer science', 'A Framework for Fast Scalable BNN Inference using Googlenet and Transfer Learning', 'Karthik ', '2021-01-05', '2021-01-04', 'http://arxiv.org/pdf/2101.00793v2')]\n",
"8\n",
"[('1803.04840v1', 'electrical engineering and system science', 'Resource aware design of a deep convolutional-recurrent neural network for speech recognition through audio-visual sensor fusion', 'Matthijs Van keirsbilck, Bert Moons, Marian Verhels', '2018-03-13', '2018-03-13', 'http://arxiv.org/pdf/1803.04840v1'), ('1004.3275v1', 'electrical engineering and system science', 'C Implementation & comparison of companding & silence audio compression techniques', 'Kruti Dangarwala, Jigar Sha', '2010-04-19', '2010-04-19', 'http://arxiv.org/pdf/1004.3275v1'), ('2108.05684v2', 'electrical engineering and system science', 'RW-Resnet: A Novel Speech Anti-Spoofing Model Using Raw Waveform', 'Youxuan Ma, Zongze Ren, Shugong X', '2021-08-13', '2021-08-12', 'http://arxiv.org/pdf/2108.05684v2')]\n",
"500 An internal error has occurred. Please retry or report in https://developers.generativeai.google/guide/troubleshooting\n",
"9\n",
"500 An internal error has occurred. Please retry or report in https://developers.generativeai.google/guide/troubleshooting\n"
]
},
{
"ename": "UnboundLocalError",
"evalue": "local variable 'temp_answer' referenced before assignment",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mInternalServerError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[9], line 4\u001b[0m, in \u001b[0;36mfull_chain_single_question\u001b[1;34m(input_prompt)\u001b[0m\n\u001b[0;32m 3\u001b[0m first_prompt \u001b[38;5;241m=\u001b[39m extract_keyword_prompt(input_prompt)\n\u001b[1;32m----> 4\u001b[0m temp_answer \u001b[38;5;241m=\u001b[39m \u001b[43mmodel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgenerate_content\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfirst_prompt\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241m.\u001b[39mtext\n\u001b[0;32m 6\u001b[0m args \u001b[38;5;241m=\u001b[39m json\u001b[38;5;241m.\u001b[39mloads(utils\u001b[38;5;241m.\u001b[39mtrimming(temp_answer))\n",
"File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\google\\generativeai\\generative_models.py:262\u001b[0m, in \u001b[0;36mGenerativeModel.generate_content\u001b[1;34m(self, contents, generation_config, safety_settings, stream, tools, tool_config, request_options)\u001b[0m\n\u001b[0;32m 261\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m--> 262\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_client\u001b[38;5;241m.\u001b[39mgenerate_content(\n\u001b[0;32m 263\u001b[0m request,\n\u001b[0;32m 264\u001b[0m \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mrequest_options,\n\u001b[0;32m 265\u001b[0m )\n\u001b[0;32m 266\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m generation_types\u001b[38;5;241m.\u001b[39mGenerateContentResponse\u001b[38;5;241m.\u001b[39mfrom_response(response)\n",
"File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\google\\ai\\generativelanguage_v1beta\\services\\generative_service\\client.py:791\u001b[0m, in \u001b[0;36mGenerativeServiceClient.generate_content\u001b[1;34m(self, request, model, contents, retry, timeout, metadata)\u001b[0m\n\u001b[0;32m 790\u001b[0m \u001b[38;5;66;03m# Send the request.\u001b[39;00m\n\u001b[1;32m--> 791\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[43mrpc\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 792\u001b[0m \u001b[43m \u001b[49m\u001b[43mrequest\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 793\u001b[0m \u001b[43m \u001b[49m\u001b[43mretry\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mretry\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 794\u001b[0m \u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtimeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 795\u001b[0m \u001b[43m \u001b[49m\u001b[43mmetadata\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmetadata\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 796\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 798\u001b[0m \u001b[38;5;66;03m# Done; return the response.\u001b[39;00m\n",
"File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\google\\api_core\\gapic_v1\\method.py:131\u001b[0m, in \u001b[0;36m_GapicCallable.__call__\u001b[1;34m(self, timeout, retry, compression, *args, **kwargs)\u001b[0m\n\u001b[0;32m 129\u001b[0m kwargs[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcompression\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m compression\n\u001b[1;32m--> 131\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m wrapped_func(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
"File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\google\\api_core\\retry\\retry_unary.py:293\u001b[0m, in \u001b[0;36mRetry.__call__.<locals>.retry_wrapped_func\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m 290\u001b[0m sleep_generator \u001b[38;5;241m=\u001b[39m exponential_sleep_generator(\n\u001b[0;32m 291\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_initial, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_maximum, multiplier\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_multiplier\n\u001b[0;32m 292\u001b[0m )\n\u001b[1;32m--> 293\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mretry_target\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 294\u001b[0m \u001b[43m \u001b[49m\u001b[43mtarget\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 295\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_predicate\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 296\u001b[0m \u001b[43m \u001b[49m\u001b[43msleep_generator\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 297\u001b[0m \u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_timeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 298\u001b[0m \u001b[43m \u001b[49m\u001b[43mon_error\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mon_error\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 299\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\google\\api_core\\retry\\retry_unary.py:153\u001b[0m, in \u001b[0;36mretry_target\u001b[1;34m(target, predicate, sleep_generator, timeout, on_error, exception_factory, **kwargs)\u001b[0m\n\u001b[0;32m 151\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m exc:\n\u001b[0;32m 152\u001b[0m \u001b[38;5;66;03m# defer to shared logic for handling errors\u001b[39;00m\n\u001b[1;32m--> 153\u001b[0m \u001b[43m_retry_error_helper\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 154\u001b[0m \u001b[43m \u001b[49m\u001b[43mexc\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 155\u001b[0m \u001b[43m \u001b[49m\u001b[43mdeadline\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 156\u001b[0m \u001b[43m \u001b[49m\u001b[43msleep\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 157\u001b[0m \u001b[43m \u001b[49m\u001b[43merror_list\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 158\u001b[0m \u001b[43m \u001b[49m\u001b[43mpredicate\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 159\u001b[0m \u001b[43m \u001b[49m\u001b[43mon_error\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 160\u001b[0m \u001b[43m \u001b[49m\u001b[43mexception_factory\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 161\u001b[0m \u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 162\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 163\u001b[0m \u001b[38;5;66;03m# if exception not raised, sleep before next attempt\u001b[39;00m\n",
"File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\google\\api_core\\retry\\retry_base.py:212\u001b[0m, in \u001b[0;36m_retry_error_helper\u001b[1;34m(exc, deadline, next_sleep, error_list, predicate_fn, on_error_fn, exc_factory_fn, original_timeout)\u001b[0m\n\u001b[0;32m 207\u001b[0m final_exc, source_exc \u001b[38;5;241m=\u001b[39m exc_factory_fn(\n\u001b[0;32m 208\u001b[0m error_list,\n\u001b[0;32m 209\u001b[0m RetryFailureReason\u001b[38;5;241m.\u001b[39mNON_RETRYABLE_ERROR,\n\u001b[0;32m 210\u001b[0m original_timeout,\n\u001b[0;32m 211\u001b[0m )\n\u001b[1;32m--> 212\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m final_exc \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msource_exc\u001b[39;00m\n\u001b[0;32m 213\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m on_error_fn \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n",
"File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\google\\api_core\\retry\\retry_unary.py:144\u001b[0m, in \u001b[0;36mretry_target\u001b[1;34m(target, predicate, sleep_generator, timeout, on_error, exception_factory, **kwargs)\u001b[0m\n\u001b[0;32m 143\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 144\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[43mtarget\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 145\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m inspect\u001b[38;5;241m.\u001b[39misawaitable(result):\n",
"File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\google\\api_core\\timeout.py:120\u001b[0m, in \u001b[0;36mTimeToDeadlineTimeout.__call__.<locals>.func_with_timeout\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m 118\u001b[0m kwargs[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtimeout\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mmax\u001b[39m(\u001b[38;5;241m0\u001b[39m, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_timeout \u001b[38;5;241m-\u001b[39m time_since_first_attempt)\n\u001b[1;32m--> 120\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m func(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
"File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\google\\api_core\\grpc_helpers.py:78\u001b[0m, in \u001b[0;36m_wrap_unary_errors.<locals>.error_remapped_callable\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m 77\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m grpc\u001b[38;5;241m.\u001b[39mRpcError \u001b[38;5;28;01mas\u001b[39;00m exc:\n\u001b[1;32m---> 78\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exceptions\u001b[38;5;241m.\u001b[39mfrom_grpc_error(exc) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mexc\u001b[39;00m\n",
"\u001b[1;31mInternalServerError\u001b[0m: 500 An internal error has occurred. Please retry or report in https://developers.generativeai.google/guide/troubleshooting",
"\nDuring handling of the above exception, another exception occurred:\n",
"\u001b[1;31mUnboundLocalError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[13], line 8\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i,t \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28menumerate\u001b[39m(data):\n\u001b[0;32m 7\u001b[0m \u001b[38;5;28mprint\u001b[39m(i)\n\u001b[1;32m----> 8\u001b[0m temp_answer, answer \u001b[38;5;241m=\u001b[39m \u001b[43mfull_chain_single_question\u001b[49m\u001b[43m(\u001b[49m\u001b[43mt\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mquestion\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 9\u001b[0m test_log\u001b[38;5;241m.\u001b[39mappend({\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdesired topic\u001b[39m\u001b[38;5;124m'\u001b[39m:t[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdesired\u001b[39m\u001b[38;5;124m'\u001b[39m],\n\u001b[0;32m 10\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mquestion\u001b[39m\u001b[38;5;124m'\u001b[39m:t[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mquestion\u001b[39m\u001b[38;5;124m'\u001b[39m],\n\u001b[0;32m 11\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mfirst answer\u001b[39m\u001b[38;5;124m'\u001b[39m:temp_answer,\n\u001b[0;32m 12\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mfinal answer\u001b[39m\u001b[38;5;124m'\u001b[39m:answer})\n\u001b[0;32m 13\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mopen\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtest_results.json\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mw\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mas\u001b[39;00m outfile:\n",
"Cell \u001b[1;32mIn[9], line 16\u001b[0m, in \u001b[0;36mfull_chain_single_question\u001b[1;34m(input_prompt)\u001b[0m\n\u001b[0;32m 14\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[0;32m 15\u001b[0m \u001b[38;5;28mprint\u001b[39m(e)\n\u001b[1;32m---> 16\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mtemp_answer\u001b[49m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mError occured: \u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m+\u001b[39m \u001b[38;5;28mstr\u001b[39m(e)\n",
"\u001b[1;31mUnboundLocalError\u001b[0m: local variable 'temp_answer' referenced before assignment"
]
}
],
"source": [
"with open(\"test_questions.txt\",\"r\") as infile:\n",
" data = json.load(infile)\n",
"print(data[0])\n",
"\n",
"test_log = []\n",
"for i,t in enumerate(data):\n",
" print(i)\n",
" temp_answer, answer = full_chain_single_question(t['question'])\n",
" test_log.append({'desired topic':t['desired'],\n",
" 'question':t['question'],\n",
" 'first answer':temp_answer,\n",
" 'final answer':answer})\n",
"with open(\"test_results.json\",\"w\") as outfile:\n",
" json.dump(test_log,outfile)"
]
}
],
"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.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|