Khush12295 commited on
Commit
1039d16
·
1 Parent(s): 8714101

Upload week2assignment1.ipynb

Browse files
Files changed (1) hide show
  1. week2assignment1.ipynb +1090 -0
week2assignment1.ipynb ADDED
@@ -0,0 +1,1090 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": [],
7
+ "machine_shape": "hm",
8
+ "gpuType": "V100"
9
+ },
10
+ "kernelspec": {
11
+ "name": "python3",
12
+ "display_name": "Python 3"
13
+ },
14
+ "language_info": {
15
+ "name": "python"
16
+ },
17
+ "accelerator": "GPU"
18
+ },
19
+ "cells": [
20
+ {
21
+ "cell_type": "code",
22
+ "execution_count": 1,
23
+ "metadata": {
24
+ "id": "kSiMkjMcQQwI"
25
+ },
26
+ "outputs": [],
27
+ "source": [
28
+ "!pip install openai langchain python-dotenv -q\n"
29
+ ]
30
+ },
31
+ {
32
+ "cell_type": "code",
33
+ "source": [
34
+ "!echo openai_api_key=\"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\" > .env\n"
35
+ ],
36
+ "metadata": {
37
+ "id": "9XJ9mhg5Qbxp"
38
+ },
39
+ "execution_count": 2,
40
+ "outputs": []
41
+ },
42
+ {
43
+ "cell_type": "code",
44
+ "source": [
45
+ "\n",
46
+ "import os\n",
47
+ "import openai\n",
48
+ "from dotenv import load_dotenv\n",
49
+ "\n",
50
+ "load_dotenv(\".env\")\n",
51
+ "\n",
52
+ "openai.api_key = os.environ.get(\"openai_api_key\")"
53
+ ],
54
+ "metadata": {
55
+ "id": "hhKlgHGDQobM"
56
+ },
57
+ "execution_count": 3,
58
+ "outputs": []
59
+ },
60
+ {
61
+ "cell_type": "code",
62
+ "source": [
63
+ "\n",
64
+ "from IPython.display import display, Markdown\n",
65
+ "\n",
66
+ "def disp_markdown(text: str) -> None:\n",
67
+ " display(Markdown(text))\n",
68
+ ""
69
+ ],
70
+ "metadata": {
71
+ "id": "K-fuGz7BQrSQ"
72
+ },
73
+ "execution_count": 4,
74
+ "outputs": []
75
+ },
76
+ {
77
+ "cell_type": "code",
78
+ "source": [
79
+ "\n",
80
+ "from langchain.chat_models import ChatOpenAI\n",
81
+ "from langchain.schema import HumanMessage\n",
82
+ "\n",
83
+ "chat_model = ChatOpenAI(model_name=\"gpt-3.5-turbo\", openai_api_key=os.environ.get(\"openai_api_key\"))\n",
84
+ "\n",
85
+ ""
86
+ ],
87
+ "metadata": {
88
+ "id": "JiARD_5fQtbs"
89
+ },
90
+ "execution_count": 5,
91
+ "outputs": []
92
+ },
93
+ {
94
+ "cell_type": "code",
95
+ "source": [
96
+ "from langchain.schema import (\n",
97
+ " AIMessage,\n",
98
+ " HumanMessage,\n",
99
+ " SystemMessage\n",
100
+ ")\n",
101
+ "\n",
102
+ "# The SystemMessage is associated with the system role\n",
103
+ "system_message = SystemMessage(content=\"You are a food critic.\")\n",
104
+ "\n",
105
+ "# The HumanMessage is associated with the user role\n",
106
+ "user_message = HumanMessage(content=\"Do you think Kraft Dinner constitues fine dining?\")\n",
107
+ "\n",
108
+ "# The AIMessage is associated with the assistant role\n",
109
+ "assistant_message = AIMessage(content=\"Egads! No, it most certainly does not!\")\n",
110
+ ""
111
+ ],
112
+ "metadata": {
113
+ "id": "EZ80ev8pQw75"
114
+ },
115
+ "execution_count": 6,
116
+ "outputs": []
117
+ },
118
+ {
119
+ "cell_type": "code",
120
+ "source": [
121
+ "second_user_message = HumanMessage(content=\"What about Red Lobster, surely that is fine dining!\")\n",
122
+ "\n",
123
+ "# create the list of prompts\n",
124
+ "list_of_prompts = [\n",
125
+ " system_message,\n",
126
+ " user_message,\n",
127
+ " assistant_message,\n",
128
+ " second_user_message\n",
129
+ "]\n",
130
+ "\n",
131
+ "# we can just call our chat_model on the list of prompts!\n",
132
+ "chat_model(list_of_prompts)"
133
+ ],
134
+ "metadata": {
135
+ "colab": {
136
+ "base_uri": "https://localhost:8080/"
137
+ },
138
+ "id": "tqTZtKvwRmVe",
139
+ "outputId": "b5d3f356-aa1e-458c-83f2-cb82d45dad34"
140
+ },
141
+ "execution_count": 7,
142
+ "outputs": [
143
+ {
144
+ "output_type": "execute_result",
145
+ "data": {
146
+ "text/plain": [
147
+ "AIMessage(content=\"Well, Red Lobster falls under the category of casual dining rather than fine dining. While they may serve seafood dishes that are enjoyable for many, the overall dining experience and the quality of ingredients may not meet the standards of fine dining. Fine dining typically involves meticulously prepared dishes using premium ingredients, expertly crafted flavors, attentive service, and an elegant atmosphere. However, if you're looking for a more laid-back seafood experience, Red Lobster can be a satisfactory choice.\", additional_kwargs={}, example=False)"
148
+ ]
149
+ },
150
+ "metadata": {},
151
+ "execution_count": 7
152
+ }
153
+ ]
154
+ },
155
+ {
156
+ "cell_type": "code",
157
+ "source": [
158
+ "from langchain.prompts.chat import (\n",
159
+ " ChatPromptTemplate,\n",
160
+ " SystemMessagePromptTemplate,\n",
161
+ " HumanMessagePromptTemplate\n",
162
+ ")\n",
163
+ "\n",
164
+ "# we can signify variables we want access to by wrapping them in {}\n",
165
+ "system_prompt_template = \"You are an expert in {SUBJECT}, and you're currently feeling {MOOD}\"\n",
166
+ "system_prompt_template = SystemMessagePromptTemplate.from_template(system_prompt_template)\n",
167
+ "\n",
168
+ "user_prompt_template = \"{CONTENT}\"\n",
169
+ "user_prompt_template = HumanMessagePromptTemplate.from_template(user_prompt_template)\n",
170
+ "\n",
171
+ "# put them together into a ChatPromptTemplate\n",
172
+ "chat_prompt = ChatPromptTemplate.from_messages([system_prompt_template, user_prompt_template])"
173
+ ],
174
+ "metadata": {
175
+ "id": "eSv9kdAZRo_n"
176
+ },
177
+ "execution_count": 8,
178
+ "outputs": []
179
+ },
180
+ {
181
+ "cell_type": "code",
182
+ "source": [
183
+ "formatted_chat_prompt = chat_prompt.format_prompt(SUBJECT=\"cheeses\", MOOD=\"quite tired\", CONTENT=\"Hi, what are the finest cheeses?\").to_messages()\n",
184
+ "\n",
185
+ "disp_markdown(chat_model(formatted_chat_prompt).content)"
186
+ ],
187
+ "metadata": {
188
+ "colab": {
189
+ "base_uri": "https://localhost:8080/",
190
+ "height": 231
191
+ },
192
+ "id": "mLxt8XdTRtH-",
193
+ "outputId": "924d5606-39b1-4de4-cbcd-6441e81afeef"
194
+ },
195
+ "execution_count": 9,
196
+ "outputs": [
197
+ {
198
+ "output_type": "display_data",
199
+ "data": {
200
+ "text/plain": [
201
+ "<IPython.core.display.Markdown object>"
202
+ ],
203
+ "text/markdown": "Hello! As an expert in cheeses, I can confidently say that there are many exquisite cheeses out there. However, since I'm feeling quite tired at the moment, I'll provide you with a list of some widely acclaimed favorites:\n\n1. Parmigiano Reggiano: A hard, aged Italian cheese with a rich, nutty flavor.\n2. Gruyère: A Swiss cheese with a smooth texture and a slightly sweet, nutty taste.\n3. Roquefort: A creamy blue cheese from France, known for its tangy, sharp flavor.\n4. Comté: A French cheese with a complex flavor profile, ranging from fruity to nutty.\n5. Manchego: A Spanish cheese made from sheep's milk, characterized by its buttery texture and slightly salty taste.\n6. Brie: A soft, creamy French cheese with a mild, buttery flavor.\n7. Cheddar: A versatile cheese with a range of flavors, from mild to extra sharp, originating from England.\n\nOf course, this is just a small selection, and there are countless other exceptional cheeses to explore from various regions around the world."
204
+ },
205
+ "metadata": {}
206
+ }
207
+ ]
208
+ },
209
+ {
210
+ "cell_type": "code",
211
+ "source": [
212
+ "from langchain.chains import LLMChain\n",
213
+ "\n",
214
+ "chain = LLMChain(llm=chat_model, prompt=chat_prompt)\n",
215
+ "\n",
216
+ "disp_markdown(chain.run(SUBJECT=\"classic cars\", MOOD=\"angry\", CONTENT=\"Is the 67 Chevrolet Impala a good vehicle?\"))"
217
+ ],
218
+ "metadata": {
219
+ "colab": {
220
+ "base_uri": "https://localhost:8080/",
221
+ "height": 81
222
+ },
223
+ "id": "4kAlhPv3Rv76",
224
+ "outputId": "94200c2c-5874-471b-9b1f-7417001e472b"
225
+ },
226
+ "execution_count": 10,
227
+ "outputs": [
228
+ {
229
+ "output_type": "display_data",
230
+ "data": {
231
+ "text/plain": [
232
+ "<IPython.core.display.Markdown object>"
233
+ ],
234
+ "text/markdown": "No, the 67 Chevrolet Impala is not a good vehicle. It is an exceptional vehicle. It is a classic American car that is highly sought after by collectors and enthusiasts. With its iconic design, powerful engine options, and smooth performance, the 67 Impala is considered one of the greatest cars of its era. It's a timeless classic that continues to captivate car lovers to this day. So, to answer your question, yes, the 67 Chevrolet Impala is an outstanding vehicle."
235
+ },
236
+ "metadata": {}
237
+ }
238
+ ]
239
+ },
240
+ {
241
+ "cell_type": "code",
242
+ "source": [
243
+ "!wget https://erki.lap.ee/failid/raamatud/guide1.txt\n"
244
+ ],
245
+ "metadata": {
246
+ "colab": {
247
+ "base_uri": "https://localhost:8080/"
248
+ },
249
+ "id": "9IWQL3viR0en",
250
+ "outputId": "37fd8eb2-da9a-4ae4-9910-84e528dc71fb"
251
+ },
252
+ "execution_count": 11,
253
+ "outputs": [
254
+ {
255
+ "output_type": "stream",
256
+ "name": "stdout",
257
+ "text": [
258
+ "--2023-09-06 07:28:21-- https://erki.lap.ee/failid/raamatud/guide1.txt\n",
259
+ "Resolving erki.lap.ee (erki.lap.ee)... 185.158.177.102\n",
260
+ "Connecting to erki.lap.ee (erki.lap.ee)|185.158.177.102|:443... connected.\n",
261
+ "HTTP request sent, awaiting response... 200 OK\n",
262
+ "Length: 291862 (285K) [text/plain]\n",
263
+ "Saving to: ‘guide1.txt.1’\n",
264
+ "\n",
265
+ "guide1.txt.1 100%[===================>] 285.02K 314KB/s in 0.9s \n",
266
+ "\n",
267
+ "2023-09-06 07:28:23 (314 KB/s) - ‘guide1.txt.1’ saved [291862/291862]\n",
268
+ "\n"
269
+ ]
270
+ }
271
+ ]
272
+ },
273
+ {
274
+ "cell_type": "code",
275
+ "source": [
276
+ "with open(\"guide1.txt\") as f:\n",
277
+ " hitchhikersguide = f.read()"
278
+ ],
279
+ "metadata": {
280
+ "id": "tcl__e2TR2_T"
281
+ },
282
+ "execution_count": 12,
283
+ "outputs": []
284
+ },
285
+ {
286
+ "cell_type": "code",
287
+ "source": [
288
+ "\n",
289
+ "from langchain.text_splitter import CharacterTextSplitter\n",
290
+ "\n",
291
+ "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0, separator = \"\\n\")\n",
292
+ "texts = text_splitter.split_text(hitchhikersguide)"
293
+ ],
294
+ "metadata": {
295
+ "id": "Nu1e3ijRR57o"
296
+ },
297
+ "execution_count": 13,
298
+ "outputs": []
299
+ },
300
+ {
301
+ "cell_type": "code",
302
+ "source": [
303
+ "from langchain.embeddings.openai import OpenAIEmbeddings\n",
304
+ "\n",
305
+ "os.environ[\"OPENAI_API_KEY\"] = openai.api_key\n",
306
+ "\n",
307
+ "embeddings = OpenAIEmbeddings()"
308
+ ],
309
+ "metadata": {
310
+ "id": "rnIGR0sfR7-D"
311
+ },
312
+ "execution_count": 14,
313
+ "outputs": []
314
+ },
315
+ {
316
+ "cell_type": "code",
317
+ "source": [
318
+ "# !pip install chromadb==0.3.22 tiktoken -q\n"
319
+ ],
320
+ "metadata": {
321
+ "id": "R7Vl5A1CR-Or"
322
+ },
323
+ "execution_count": 15,
324
+ "outputs": []
325
+ },
326
+ {
327
+ "cell_type": "code",
328
+ "source": [
329
+ "# !pip install chromadb -U"
330
+ ],
331
+ "metadata": {
332
+ "id": "PTS3HYM6SVGr"
333
+ },
334
+ "execution_count": 16,
335
+ "outputs": []
336
+ },
337
+ {
338
+ "cell_type": "code",
339
+ "source": [
340
+ "!pip install pydantic -q\n",
341
+ "import chromadb\n"
342
+ ],
343
+ "metadata": {
344
+ "id": "fdEtdt3aSAjx"
345
+ },
346
+ "execution_count": 17,
347
+ "outputs": []
348
+ },
349
+ {
350
+ "cell_type": "code",
351
+ "source": [
352
+ "from langchain.vectorstores.chroma import Chroma\n",
353
+ "docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{\"source\": str(i)} for i in range(len(texts))]).as_retriever()\n"
354
+ ],
355
+ "metadata": {
356
+ "id": "qpGmit-pSJ-5"
357
+ },
358
+ "execution_count": 18,
359
+ "outputs": []
360
+ },
361
+ {
362
+ "cell_type": "code",
363
+ "source": [
364
+ "\n",
365
+ "query = \"What makes towels important?\"\n",
366
+ "docs = docsearch.get_relevant_documents(query)"
367
+ ],
368
+ "metadata": {
369
+ "id": "pWyC02vRXPX8"
370
+ },
371
+ "execution_count": 19,
372
+ "outputs": []
373
+ },
374
+ {
375
+ "cell_type": "code",
376
+ "source": [
377
+ "docs[0]\n"
378
+ ],
379
+ "metadata": {
380
+ "colab": {
381
+ "base_uri": "https://localhost:8080/"
382
+ },
383
+ "id": "RUmBJ8byZX0D",
384
+ "outputId": "6d8d56fa-6413-4a4e-c048-f97abb935343"
385
+ },
386
+ "execution_count": 20,
387
+ "outputs": [
388
+ {
389
+ "output_type": "execute_result",
390
+ "data": {
391
+ "text/plain": [
392
+ "Document(page_content=\"value - you can wrap it around you for warmth as you bound across\\nthe cold moons of Jaglan Beta; you can lie on it on the brilliant\\nmarble-sanded beaches of Santraginus V, inhaling the heady sea\\nvapours; you can sleep under it beneath the stars which shine so\\nredly on the desert world of Kakrafoon; use it to sail a mini\\nraft down the slow heavy river Moth; wet it for use in hand-to-\\nhand-combat; wrap it round your head to ward off noxious fumes or\\nto avoid the gaze of the Ravenous Bugblatter Beast of Traal (a\\nmindboggingly stupid animal, it assumes that if you can't see it,\\nit can't see you - daft as a bush, but very ravenous); you can\\nwave your towel in emergencies as a distress signal, and of\\ncourse dry yourself off with it if it still seems to be clean\\nenough.\\n \\nMore importantly, a towel has immense psychological value. For\\nsome reason, if a strag (strag: non-hitch hiker) discovers that a\\nhitch hiker has his towel with him, he will automatically assume\", metadata={'source': '36'})"
393
+ ]
394
+ },
395
+ "metadata": {},
396
+ "execution_count": 20
397
+ }
398
+ ]
399
+ },
400
+ {
401
+ "cell_type": "code",
402
+ "source": [
403
+ "\n",
404
+ "from langchain.chains.question_answering import load_qa_chain\n",
405
+ "from langchain.llms import OpenAI\n",
406
+ "\n",
407
+ "chain = load_qa_chain(OpenAI(temperature=0), chain_type=\"stuff\")\n",
408
+ "query = \"What makes towels important?\"\n",
409
+ "chain.run(input_documents=docs, question=query)"
410
+ ],
411
+ "metadata": {
412
+ "colab": {
413
+ "base_uri": "https://localhost:8080/",
414
+ "height": 105
415
+ },
416
+ "id": "EGIj3R5WZZ75",
417
+ "outputId": "bb8749c7-39fd-4645-c5a8-6cdd72f5e540"
418
+ },
419
+ "execution_count": 21,
420
+ "outputs": [
421
+ {
422
+ "output_type": "execute_result",
423
+ "data": {
424
+ "text/plain": [
425
+ "' Towels have immense psychological value. For some reason, if a strag discovers that a hitch hiker has his towel with him, he will automatically assume that he is also in possession of a toothbrush, face flannel, soap, tin of biscuits, flask, compass, map, ball of string, gnat spray, wet weather gear, space suit etc., etc. Furthermore, the strag will then happily lend the hitch hiker any of these or a dozen other items that the hitch hiker might accidentally have \"lost\". What the strag will think is that any man who can hitch the length and breadth of the galaxy, rough it, slum it, struggle against terrible odds, win through, and still knows where his towel is is clearly a man to be reckoned with.'"
426
+ ],
427
+ "application/vnd.google.colaboratory.intrinsic+json": {
428
+ "type": "string"
429
+ }
430
+ },
431
+ "metadata": {},
432
+ "execution_count": 21
433
+ }
434
+ ]
435
+ },
436
+ {
437
+ "cell_type": "markdown",
438
+ "source": [
439
+ "# Assignment 2\n"
440
+ ],
441
+ "metadata": {
442
+ "id": "SXqvD_BZZkXl"
443
+ }
444
+ },
445
+ {
446
+ "cell_type": "code",
447
+ "source": [
448
+ "!git clone https://github.com/TheMITTech/shakespeare\n"
449
+ ],
450
+ "metadata": {
451
+ "colab": {
452
+ "base_uri": "https://localhost:8080/"
453
+ },
454
+ "id": "toFapMZDZcgc",
455
+ "outputId": "c1d69e7e-a4d7-4dcd-c10b-4335ff94573e"
456
+ },
457
+ "execution_count": 22,
458
+ "outputs": [
459
+ {
460
+ "output_type": "stream",
461
+ "name": "stdout",
462
+ "text": [
463
+ "Cloning into 'shakespeare'...\n",
464
+ "remote: Enumerating objects: 2187, done.\u001b[K\n",
465
+ "remote: Total 2187 (delta 0), reused 0 (delta 0), pack-reused 2187\u001b[K\n",
466
+ "Receiving objects: 100% (2187/2187), 5.95 MiB | 12.43 MiB/s, done.\n",
467
+ "Resolving deltas: 100% (1135/1135), done.\n"
468
+ ]
469
+ }
470
+ ]
471
+ },
472
+ {
473
+ "cell_type": "code",
474
+ "source": [
475
+ "from glob import glob\n",
476
+ "\n",
477
+ "files = glob(\"./shakespeare/**/*.html\")"
478
+ ],
479
+ "metadata": {
480
+ "id": "lox62mX5Zra2"
481
+ },
482
+ "execution_count": 23,
483
+ "outputs": []
484
+ },
485
+ {
486
+ "cell_type": "code",
487
+ "source": [
488
+ "import shutil\n",
489
+ "import os\n",
490
+ "\n",
491
+ "os.mkdir('./data')\n",
492
+ "destination_folder = './data/'\n",
493
+ "\n",
494
+ "for html_file in files:\n",
495
+ " shutil.move(html_file, destination_folder + html_file.split(\"/\")[-1])"
496
+ ],
497
+ "metadata": {
498
+ "id": "yKcjYtcPZt09"
499
+ },
500
+ "execution_count": 24,
501
+ "outputs": []
502
+ },
503
+ {
504
+ "cell_type": "code",
505
+ "source": [
506
+ "!pip install beautifulsoup4 -q\n"
507
+ ],
508
+ "metadata": {
509
+ "id": "_WdeAObmZvnH"
510
+ },
511
+ "execution_count": 25,
512
+ "outputs": []
513
+ },
514
+ {
515
+ "cell_type": "code",
516
+ "source": [
517
+ "\n",
518
+ "from langchain.document_loaders import BSHTMLLoader, DirectoryLoader\n",
519
+ "\n",
520
+ "bshtml_dir_loader = DirectoryLoader('./data/', loader_cls=BSHTMLLoader)\n",
521
+ "\n",
522
+ "data = bshtml_dir_loader.load()"
523
+ ],
524
+ "metadata": {
525
+ "id": "HN0IGrSBZxuz"
526
+ },
527
+ "execution_count": 26,
528
+ "outputs": []
529
+ },
530
+ {
531
+ "cell_type": "code",
532
+ "source": [
533
+ "\n",
534
+ "from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
535
+ "\n",
536
+ "text_splitter = RecursiveCharacterTextSplitter(\n",
537
+ " chunk_size = 1000,\n",
538
+ " chunk_overlap = 20,\n",
539
+ " length_function = len,\n",
540
+ ")\n",
541
+ "\n",
542
+ "documents = text_splitter.split_documents(data)"
543
+ ],
544
+ "metadata": {
545
+ "id": "7Yc45R9qZz7Y"
546
+ },
547
+ "execution_count": 27,
548
+ "outputs": []
549
+ },
550
+ {
551
+ "cell_type": "code",
552
+ "source": [
553
+ "persist_directory = \"vector_db\"\n",
554
+ "\n",
555
+ "vectordb = Chroma.from_documents(documents=documents, embedding=embeddings, persist_directory=persist_directory)"
556
+ ],
557
+ "metadata": {
558
+ "id": "_79heZP0Z1qh"
559
+ },
560
+ "execution_count": 28,
561
+ "outputs": []
562
+ },
563
+ {
564
+ "cell_type": "code",
565
+ "source": [
566
+ "\n",
567
+ "vectordb.persist()\n",
568
+ "vectordb = None\n",
569
+ ""
570
+ ],
571
+ "metadata": {
572
+ "id": "EgVo1xv3Z7bc"
573
+ },
574
+ "execution_count": 29,
575
+ "outputs": []
576
+ },
577
+ {
578
+ "cell_type": "code",
579
+ "source": [
580
+ "vectordb = Chroma(persist_directory=persist_directory, embedding_function=embeddings)\n"
581
+ ],
582
+ "metadata": {
583
+ "id": "2zwY5jKcaQ0l"
584
+ },
585
+ "execution_count": 30,
586
+ "outputs": []
587
+ },
588
+ {
589
+ "cell_type": "code",
590
+ "source": [
591
+ "llm = ChatOpenAI(temperature=0, model=\"gpt-4\")"
592
+ ],
593
+ "metadata": {
594
+ "id": "MR7vYxC5aTXs"
595
+ },
596
+ "execution_count": 31,
597
+ "outputs": []
598
+ },
599
+ {
600
+ "cell_type": "code",
601
+ "source": [
602
+ "doc_retriever = vectordb.as_retriever()\n"
603
+ ],
604
+ "metadata": {
605
+ "id": "lBR8WMu3aVmC"
606
+ },
607
+ "execution_count": 32,
608
+ "outputs": []
609
+ },
610
+ {
611
+ "cell_type": "code",
612
+ "source": [
613
+ "from langchain.chains import RetrievalQA\n",
614
+ "\n",
615
+ "shakespeare_qa = RetrievalQA.from_chain_type(llm=llm, chain_type=\"stuff\", retriever=doc_retriever)"
616
+ ],
617
+ "metadata": {
618
+ "id": "cgDanDpxaXvT"
619
+ },
620
+ "execution_count": 33,
621
+ "outputs": []
622
+ },
623
+ {
624
+ "cell_type": "code",
625
+ "source": [
626
+ "shakespeare_qa.run(\"Who is Hamlet'?\")\n"
627
+ ],
628
+ "metadata": {
629
+ "colab": {
630
+ "base_uri": "https://localhost:8080/",
631
+ "height": 53
632
+ },
633
+ "id": "t1hDzituabJ1",
634
+ "outputId": "4ddf766a-3590-48d7-9474-76640624d3b8"
635
+ },
636
+ "execution_count": 35,
637
+ "outputs": [
638
+ {
639
+ "output_type": "execute_result",
640
+ "data": {
641
+ "text/plain": [
642
+ "\"Hamlet is the protagonist of William Shakespeare's play of the same name. He is the Prince of Denmark, son of the deceased King Hamlet and his wife, Queen Gertrude. The play revolves around Hamlet's quest for revenge against his uncle, Claudius, who murdered his father and took the throne.\""
643
+ ],
644
+ "application/vnd.google.colaboratory.intrinsic+json": {
645
+ "type": "string"
646
+ }
647
+ },
648
+ "metadata": {},
649
+ "execution_count": 35
650
+ }
651
+ ]
652
+ },
653
+ {
654
+ "cell_type": "code",
655
+ "source": [
656
+ "!pip install google-search-results -q\n"
657
+ ],
658
+ "metadata": {
659
+ "colab": {
660
+ "base_uri": "https://localhost:8080/"
661
+ },
662
+ "id": "4Y0m_ZV7ae8Z",
663
+ "outputId": "0b5c474c-9339-4475-84b6-763dd2f64fd9"
664
+ },
665
+ "execution_count": 36,
666
+ "outputs": [
667
+ {
668
+ "output_type": "stream",
669
+ "name": "stdout",
670
+ "text": [
671
+ " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
672
+ " Building wheel for google-search-results (setup.py) ... \u001b[?25l\u001b[?25hdone\n"
673
+ ]
674
+ }
675
+ ]
676
+ },
677
+ {
678
+ "cell_type": "code",
679
+ "source": [
680
+ "os.environ[\"SERPAPI_API_KEY\"] = \"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\"\n"
681
+ ],
682
+ "metadata": {
683
+ "id": "PdrUgZ1-al1F"
684
+ },
685
+ "execution_count": 37,
686
+ "outputs": []
687
+ },
688
+ {
689
+ "cell_type": "code",
690
+ "source": [
691
+ "from langchain.utilities import SerpAPIWrapper\n",
692
+ "\n",
693
+ "search = SerpAPIWrapper()\n"
694
+ ],
695
+ "metadata": {
696
+ "id": "BFy70uDgasi_"
697
+ },
698
+ "execution_count": 38,
699
+ "outputs": []
700
+ },
701
+ {
702
+ "cell_type": "code",
703
+ "source": [
704
+ "\n",
705
+ "from langchain.agents import initialize_agent, Tool\n",
706
+ "from langchain.agents import AgentType\n",
707
+ "from langchain.tools import BaseTool\n",
708
+ "from langchain.llms import OpenAI\n",
709
+ "from langchain import LLMMathChain, SerpAPIWrapper\n",
710
+ "\n",
711
+ "tools = [\n",
712
+ " Tool(\n",
713
+ " name = \"Shakespeare QA System\",\n",
714
+ " func=shakespeare_qa.run,\n",
715
+ " description=\"useful for when you need to answer questions about Shakespeare's works. Input should be a fully formed question.\"\n",
716
+ " ),\n",
717
+ " Tool(\n",
718
+ " name = \"SERP API Search\",\n",
719
+ " func=search.run,\n",
720
+ " description=\"useful for when you need to answer questions about ruff (a python linter). Input should be a fully formed question.\"\n",
721
+ " ),\n",
722
+ "]"
723
+ ],
724
+ "metadata": {
725
+ "id": "YHM00wTravlH"
726
+ },
727
+ "execution_count": 39,
728
+ "outputs": []
729
+ },
730
+ {
731
+ "cell_type": "code",
732
+ "source": [
733
+ "agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)\n"
734
+ ],
735
+ "metadata": {
736
+ "id": "fFL_9d_Jax3m"
737
+ },
738
+ "execution_count": 40,
739
+ "outputs": []
740
+ },
741
+ {
742
+ "cell_type": "code",
743
+ "source": [
744
+ "agent.run(\"What is Hamlet and more importantly who is hamlet?\")\n"
745
+ ],
746
+ "metadata": {
747
+ "colab": {
748
+ "base_uri": "https://localhost:8080/",
749
+ "height": 281
750
+ },
751
+ "id": "z55Hrtpfaz-W",
752
+ "outputId": "55dc8582-a18e-47dd-b39d-962c11b29eda"
753
+ },
754
+ "execution_count": 43,
755
+ "outputs": [
756
+ {
757
+ "output_type": "stream",
758
+ "name": "stdout",
759
+ "text": [
760
+ "\n",
761
+ "\n",
762
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
763
+ "\u001b[32;1m\u001b[1;3mThis question is about Shakespeare's work, so I should use the Shakespeare QA System.\n",
764
+ "Action: Shakespeare QA System\n",
765
+ "Action Input: What is Hamlet and who is Hamlet in Shakespeare's work?\u001b[0m\n",
766
+ "Observation: \u001b[36;1m\u001b[1;3m\"Hamlet\" is a tragedy written by William Shakespeare. It is one of his most popular works. The character Hamlet is the protagonist of the play. He is the Prince of Denmark, son of the recently deceased King Hamlet, and nephew of King Claudius, his father's brother and successor. The play follows Hamlet's quest for revenge against Claudius, who murdered his father.\u001b[0m\n",
767
+ "Thought:\u001b[32;1m\u001b[1;3mI now know the final answer\n",
768
+ "Final Answer: \"Hamlet\" is a tragedy written by William Shakespeare and is one of his most popular works. The character Hamlet is the protagonist of the play. He is the Prince of Denmark, son of the recently deceased King Hamlet, and nephew of King Claudius, his father's brother and successor. The play follows Hamlet's quest for revenge against Claudius, who murdered his father.\u001b[0m\n",
769
+ "\n",
770
+ "\u001b[1m> Finished chain.\u001b[0m\n"
771
+ ]
772
+ },
773
+ {
774
+ "output_type": "execute_result",
775
+ "data": {
776
+ "text/plain": [
777
+ "'\"Hamlet\" is a tragedy written by William Shakespeare and is one of his most popular works. The character Hamlet is the protagonist of the play. He is the Prince of Denmark, son of the recently deceased King Hamlet, and nephew of King Claudius, his father\\'s brother and successor. The play follows Hamlet\\'s quest for revenge against Claudius, who murdered his father.'"
778
+ ],
779
+ "application/vnd.google.colaboratory.intrinsic+json": {
780
+ "type": "string"
781
+ }
782
+ },
783
+ "metadata": {},
784
+ "execution_count": 43
785
+ }
786
+ ]
787
+ },
788
+ {
789
+ "cell_type": "code",
790
+ "source": [
791
+ "from langchain.memory import ConversationBufferMemory, ReadOnlySharedMemory\n",
792
+ "\n",
793
+ "memory = ConversationBufferMemory(memory_key=\"chat_history\")\n",
794
+ "readonlymemory = ReadOnlySharedMemory(memory=memory)"
795
+ ],
796
+ "metadata": {
797
+ "id": "jhS8Lr8la3s5"
798
+ },
799
+ "execution_count": 44,
800
+ "outputs": []
801
+ },
802
+ {
803
+ "cell_type": "code",
804
+ "source": [
805
+ "shakespeare_qa = RetrievalQA.from_chain_type(llm=llm, chain_type=\"stuff\", retriever=doc_retriever, memory=readonlymemory)\n"
806
+ ],
807
+ "metadata": {
808
+ "id": "R73CmCEua943"
809
+ },
810
+ "execution_count": 45,
811
+ "outputs": []
812
+ },
813
+ {
814
+ "cell_type": "code",
815
+ "source": [
816
+ "tools = [\n",
817
+ " Tool(\n",
818
+ " name = \"Shakespeare QA System\",\n",
819
+ " func=shakespeare_qa.run,\n",
820
+ " description=\"useful for when you need to answer questions about Shakespeare's works. Input should be a fully formed question.\"\n",
821
+ " ),\n",
822
+ " Tool(\n",
823
+ " name = \"SERP API Search\",\n",
824
+ " func=search.run,\n",
825
+ " description=\"useful for when you need to answer questions about ruff (a python linter). Input should be a fully formed question.\"\n",
826
+ " ),\n",
827
+ "]\n",
828
+ ""
829
+ ],
830
+ "metadata": {
831
+ "id": "0esXXlrFbG0l"
832
+ },
833
+ "execution_count": 46,
834
+ "outputs": []
835
+ },
836
+ {
837
+ "cell_type": "code",
838
+ "source": [
839
+ "from langchain.agents import ZeroShotAgent, Tool, AgentExecutor\n",
840
+ "\n",
841
+ "prefix = \"\"\"Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:\"\"\"\n",
842
+ "suffix = \"\"\"Begin!\"\n",
843
+ "\n",
844
+ "{chat_history}\n",
845
+ "Question: {input}\n",
846
+ "{agent_scratchpad}\"\"\"\n",
847
+ "\n",
848
+ "prompt = ZeroShotAgent.create_prompt(\n",
849
+ " tools,\n",
850
+ " prefix=prefix,\n",
851
+ " suffix=suffix,\n",
852
+ " input_variables=[\"input\", \"chat_history\", \"agent_scratchpad\"]\n",
853
+ ")"
854
+ ],
855
+ "metadata": {
856
+ "id": "fH2hoPmAbJia"
857
+ },
858
+ "execution_count": 47,
859
+ "outputs": []
860
+ },
861
+ {
862
+ "cell_type": "code",
863
+ "source": [
864
+ "from langchain import OpenAI, LLMChain, PromptTemplate\n",
865
+ "\n",
866
+ "llm_chain = LLMChain(llm=llm, prompt=prompt)\n"
867
+ ],
868
+ "metadata": {
869
+ "id": "2ykKpyDdbMRy"
870
+ },
871
+ "execution_count": 48,
872
+ "outputs": []
873
+ },
874
+ {
875
+ "cell_type": "code",
876
+ "source": [
877
+ "agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)\n",
878
+ "agent_chain = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True, memory=memory)"
879
+ ],
880
+ "metadata": {
881
+ "id": "LV-t8-ombOf8"
882
+ },
883
+ "execution_count": 49,
884
+ "outputs": []
885
+ },
886
+ {
887
+ "cell_type": "code",
888
+ "source": [
889
+ "agent_chain.run(input=\"Who is Hamlet and What is Hamlet?\")\n"
890
+ ],
891
+ "metadata": {
892
+ "colab": {
893
+ "base_uri": "https://localhost:8080/",
894
+ "height": 368
895
+ },
896
+ "id": "MzUkosUKbRFK",
897
+ "outputId": "c81917ee-c677-4ecf-a3b7-e431bd4885c3"
898
+ },
899
+ "execution_count": 50,
900
+ "outputs": [
901
+ {
902
+ "output_type": "stream",
903
+ "name": "stdout",
904
+ "text": [
905
+ "\n",
906
+ "\n",
907
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
908
+ "\u001b[32;1m\u001b[1;3mThought: The question is about Shakespeare's work, Hamlet. I should use the Shakespeare QA System to answer this question.\n",
909
+ "Action: Shakespeare QA System\n",
910
+ "Action Input: Who is Hamlet in Shakespeare's works?\u001b[0m\n",
911
+ "Observation: \u001b[36;1m\u001b[1;3mHamlet is the protagonist of William Shakespeare's tragedy of the same name, \"Hamlet\". He is the Prince of Denmark, son of the deceased King Hamlet and Queen Gertrude. The play revolves around his struggle to avenge his father's murder.\u001b[0m\n",
912
+ "Thought:\u001b[32;1m\u001b[1;3mI have the answer to the first part of the question. Now I need to answer the second part of the question: What is Hamlet?\n",
913
+ "Action: Shakespeare QA System\n",
914
+ "Action Input: What is the play Hamlet about?\u001b[0m\n",
915
+ "Observation: \u001b[36;1m\u001b[1;3m\"Hamlet\" is a tragedy written by William Shakespeare. It's about Prince Hamlet's revenge on his uncle Claudius for murdering King Hamlet, Claudius's brother and Prince Hamlet's father, and then succeeding to the throne and taking Gertrude, the old king's widow and Prince Hamlet's mother, as his wife. The play explores themes of revenge, treachery, moral corruption, and the consequences of indecision.\u001b[0m\n",
916
+ "Thought:\u001b[32;1m\u001b[1;3mI now know the final answer.\n",
917
+ "Final Answer: Hamlet is the protagonist of William Shakespeare's tragedy, \"Hamlet\". He is the Prince of Denmark, son of the deceased King Hamlet and Queen Gertrude. The play, \"Hamlet\", is about Prince Hamlet's revenge on his uncle Claudius for murdering King Hamlet, Claudius's brother and Prince Hamlet's father, and then succeeding to the throne and taking Gertrude, the old king's widow and Prince Hamlet's mother, as his wife. The play explores themes of revenge, treachery, moral corruption, and the consequences of indecision.\u001b[0m\n",
918
+ "\n",
919
+ "\u001b[1m> Finished chain.\u001b[0m\n"
920
+ ]
921
+ },
922
+ {
923
+ "output_type": "execute_result",
924
+ "data": {
925
+ "text/plain": [
926
+ "'Hamlet is the protagonist of William Shakespeare\\'s tragedy, \"Hamlet\". He is the Prince of Denmark, son of the deceased King Hamlet and Queen Gertrude. The play, \"Hamlet\", is about Prince Hamlet\\'s revenge on his uncle Claudius for murdering King Hamlet, Claudius\\'s brother and Prince Hamlet\\'s father, and then succeeding to the throne and taking Gertrude, the old king\\'s widow and Prince Hamlet\\'s mother, as his wife. The play explores themes of revenge, treachery, moral corruption, and the consequences of indecision.'"
927
+ ],
928
+ "application/vnd.google.colaboratory.intrinsic+json": {
929
+ "type": "string"
930
+ }
931
+ },
932
+ "metadata": {},
933
+ "execution_count": 50
934
+ }
935
+ ]
936
+ },
937
+ {
938
+ "cell_type": "code",
939
+ "source": [
940
+ "agent_chain.run(input=\"What age was he in the play?\")\n"
941
+ ],
942
+ "metadata": {
943
+ "colab": {
944
+ "base_uri": "https://localhost:8080/",
945
+ "height": 226
946
+ },
947
+ "id": "-lnPJdu-bU-v",
948
+ "outputId": "eea5356d-2ecf-475e-dfd7-86f71d122721"
949
+ },
950
+ "execution_count": 51,
951
+ "outputs": [
952
+ {
953
+ "output_type": "stream",
954
+ "name": "stdout",
955
+ "text": [
956
+ "\n",
957
+ "\n",
958
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
959
+ "\u001b[32;1m\u001b[1;3mThought: This question is about a character in a Shakespeare play, so I should use the Shakespeare QA System.\n",
960
+ "Action: Shakespeare QA System\n",
961
+ "Action Input: How old is Hamlet in the play \"Hamlet\"?\u001b[0m\n",
962
+ "Observation: \u001b[36;1m\u001b[1;3mThe text provided does not specify Hamlet's age in the play \"Hamlet\".\u001b[0m\n",
963
+ "Thought:\u001b[32;1m\u001b[1;3mI now know the final answer.\n",
964
+ "Final Answer: The text of the play \"Hamlet\" does not specify Hamlet's exact age.\u001b[0m\n",
965
+ "\n",
966
+ "\u001b[1m> Finished chain.\u001b[0m\n"
967
+ ]
968
+ },
969
+ {
970
+ "output_type": "execute_result",
971
+ "data": {
972
+ "text/plain": [
973
+ "'The text of the play \"Hamlet\" does not specify Hamlet\\'s exact age.'"
974
+ ],
975
+ "application/vnd.google.colaboratory.intrinsic+json": {
976
+ "type": "string"
977
+ }
978
+ },
979
+ "metadata": {},
980
+ "execution_count": 51
981
+ }
982
+ ]
983
+ },
984
+ {
985
+ "cell_type": "code",
986
+ "source": [
987
+ "agent_chain.run(input=\"Did he live through the play?\")\n"
988
+ ],
989
+ "metadata": {
990
+ "colab": {
991
+ "base_uri": "https://localhost:8080/",
992
+ "height": 226
993
+ },
994
+ "id": "qcJ8wtwSboPR",
995
+ "outputId": "b7ad1a79-854f-4a1d-99e3-fe584de9353d"
996
+ },
997
+ "execution_count": 52,
998
+ "outputs": [
999
+ {
1000
+ "output_type": "stream",
1001
+ "name": "stdout",
1002
+ "text": [
1003
+ "\n",
1004
+ "\n",
1005
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
1006
+ "\u001b[32;1m\u001b[1;3mThought: This question is about the plot of a Shakespeare play.\n",
1007
+ "Action: Shakespeare QA System\n",
1008
+ "Action Input: Does Hamlet survive at the end of the play?\u001b[0m\n",
1009
+ "Observation: \u001b[36;1m\u001b[1;3mNo, Hamlet does not survive at the end of the play. He dies from poison.\u001b[0m\n",
1010
+ "Thought:\u001b[32;1m\u001b[1;3mI now know the final answer.\n",
1011
+ "Final Answer: No, Hamlet does not live through the play. He dies from poison.\u001b[0m\n",
1012
+ "\n",
1013
+ "\u001b[1m> Finished chain.\u001b[0m\n"
1014
+ ]
1015
+ },
1016
+ {
1017
+ "output_type": "execute_result",
1018
+ "data": {
1019
+ "text/plain": [
1020
+ "'No, Hamlet does not live through the play. He dies from poison.'"
1021
+ ],
1022
+ "application/vnd.google.colaboratory.intrinsic+json": {
1023
+ "type": "string"
1024
+ }
1025
+ },
1026
+ "metadata": {},
1027
+ "execution_count": 52
1028
+ }
1029
+ ]
1030
+ },
1031
+ {
1032
+ "cell_type": "code",
1033
+ "source": [
1034
+ "agent_chain.run(input=\"What age did you think he was if you approximate without directly reading it from the play? You make the inference on his acts and ages of people in his life\")\n"
1035
+ ],
1036
+ "metadata": {
1037
+ "colab": {
1038
+ "base_uri": "https://localhost:8080/",
1039
+ "height": 246
1040
+ },
1041
+ "id": "z1H1MLN-bswW",
1042
+ "outputId": "c0b673c8-bbf2-4ae3-8851-2a80e99f5a75"
1043
+ },
1044
+ "execution_count": 54,
1045
+ "outputs": [
1046
+ {
1047
+ "output_type": "stream",
1048
+ "name": "stdout",
1049
+ "text": [
1050
+ "\n",
1051
+ "\n",
1052
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
1053
+ "\u001b[32;1m\u001b[1;3mThought: This question requires knowledge about the character Hamlet from Shakespeare's play. I should use the Shakespeare QA System to find an answer.\n",
1054
+ "Action: Shakespeare QA System\n",
1055
+ "Action Input: What is the approximate age of Hamlet based on his actions and the ages of people in his life in the play?\u001b[0m\n",
1056
+ "Observation: \u001b[36;1m\u001b[1;3mThe text provided does not give specific information about Hamlet's age or the ages of people in his life. However, in the full play of Hamlet by William Shakespeare, it is suggested that Hamlet is around 30 years old.\u001b[0m\n",
1057
+ "Thought:\u001b[32;1m\u001b[1;3mI now know the final answer.\n",
1058
+ "Final Answer: Based on his actions and the ages of people in his life in the play, it is suggested that Hamlet is around 30 years old.\u001b[0m\n",
1059
+ "\n",
1060
+ "\u001b[1m> Finished chain.\u001b[0m\n"
1061
+ ]
1062
+ },
1063
+ {
1064
+ "output_type": "execute_result",
1065
+ "data": {
1066
+ "text/plain": [
1067
+ "'Based on his actions and the ages of people in his life in the play, it is suggested that Hamlet is around 30 years old.'"
1068
+ ],
1069
+ "application/vnd.google.colaboratory.intrinsic+json": {
1070
+ "type": "string"
1071
+ }
1072
+ },
1073
+ "metadata": {},
1074
+ "execution_count": 54
1075
+ }
1076
+ ]
1077
+ },
1078
+ {
1079
+ "cell_type": "code",
1080
+ "source": [
1081
+ "huggingface-cli login\n"
1082
+ ],
1083
+ "metadata": {
1084
+ "id": "n9Wdhpxpbz2l"
1085
+ },
1086
+ "execution_count": null,
1087
+ "outputs": []
1088
+ }
1089
+ ]
1090
+ }