Spaces:
Sleeping
Sleeping
Created using Colaboratory
Browse files- notebooks/02-Basic_RAG.ipynb +940 -891
notebooks/02-Basic_RAG.ipynb
CHANGED
@@ -1,736 +1,232 @@
|
|
1 |
{
|
2 |
-
"
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
"source": [
|
10 |
-
"<a href=\"https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/02-Basic_RAG.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
|
11 |
-
]
|
12 |
-
},
|
13 |
-
{
|
14 |
-
"cell_type": "markdown",
|
15 |
-
"metadata": {
|
16 |
-
"id": "4Tw3tvMs6R-Y"
|
17 |
-
},
|
18 |
-
"source": [
|
19 |
-
"# Install Packages and Setup Variables"
|
20 |
-
]
|
21 |
-
},
|
22 |
-
{
|
23 |
-
"cell_type": "code",
|
24 |
-
"execution_count": 20,
|
25 |
-
"metadata": {
|
26 |
-
"colab": {
|
27 |
-
"base_uri": "https://localhost:8080/"
|
28 |
-
},
|
29 |
-
"id": "HaB4G9zr0BYm",
|
30 |
-
"outputId": "30ab3a13-8e8f-467b-e6c7-362d466a0dd7"
|
31 |
-
},
|
32 |
-
"outputs": [],
|
33 |
-
"source": [
|
34 |
-
"!pip install -q openai==1.6.0 cohere==4.39 tiktoken==0.5.2 pandas==2.2.0 scikit-learn==1.4.1.post1"
|
35 |
-
]
|
36 |
-
},
|
37 |
-
{
|
38 |
-
"cell_type": "code",
|
39 |
-
"execution_count": 2,
|
40 |
-
"metadata": {
|
41 |
-
"id": "MYvUA6CF2Le6"
|
42 |
-
},
|
43 |
-
"outputs": [],
|
44 |
-
"source": [
|
45 |
-
"import os\n",
|
46 |
-
"\n",
|
47 |
-
"# Set the \"OPENAI_API_KEY\" in the Python environment. Will be used by OpenAI client later.\n",
|
48 |
-
"os.environ[\"OPENAI_API_KEY\"] = \"<YOUR_OPENAI_KEY>\""
|
49 |
-
]
|
50 |
-
},
|
51 |
-
{
|
52 |
-
"cell_type": "code",
|
53 |
-
"execution_count": 3,
|
54 |
-
"metadata": {
|
55 |
-
"id": "0ViVXXIqXBai"
|
56 |
-
},
|
57 |
-
"outputs": [],
|
58 |
-
"source": [
|
59 |
-
"# False: Generate the embedding for the dataset. (Associated cost with using OpenAI endpoint)\n",
|
60 |
-
"# True: Load the dataset that already has the embedding vectors.\n",
|
61 |
-
"load_embedding = False"
|
62 |
-
]
|
63 |
-
},
|
64 |
-
{
|
65 |
-
"cell_type": "markdown",
|
66 |
-
"metadata": {
|
67 |
-
"id": "D8Nzx-cN_bDz"
|
68 |
-
},
|
69 |
-
"source": [
|
70 |
-
"# Load Dataset"
|
71 |
-
]
|
72 |
},
|
73 |
-
{
|
74 |
-
"
|
75 |
-
"
|
76 |
-
"id": "5JpI7GiZ--Gw"
|
77 |
-
},
|
78 |
-
"source": [
|
79 |
-
"## Download Dataset (JSON)"
|
80 |
-
]
|
81 |
},
|
82 |
-
{
|
83 |
-
"
|
84 |
-
"metadata": {
|
85 |
-
"id": "NT68BDYt-GkG"
|
86 |
-
},
|
87 |
-
"source": [
|
88 |
-
"The dataset includes several articles from the TowardsAI blog, which provide an in-depth explanation of the LLaMA2 model."
|
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 |
-
"cell_type": "markdown",
|
122 |
-
"metadata": {
|
123 |
-
"id": "oYDd03Qn_clh"
|
124 |
-
},
|
125 |
-
"source": [
|
126 |
-
"## Read File"
|
127 |
-
]
|
128 |
-
},
|
129 |
-
{
|
130 |
-
"cell_type": "code",
|
131 |
-
"execution_count": 10,
|
132 |
-
"metadata": {
|
133 |
-
"colab": {
|
134 |
-
"base_uri": "https://localhost:8080/"
|
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 |
-
"len( data['chunks'] )"
|
160 |
-
]
|
161 |
-
},
|
162 |
-
{
|
163 |
-
"cell_type": "code",
|
164 |
-
"execution_count": 13,
|
165 |
-
"metadata": {
|
166 |
-
"colab": {
|
167 |
-
"base_uri": "https://localhost:8080/"
|
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 |
-
" return None"
|
239 |
-
]
|
240 |
-
},
|
241 |
-
{
|
242 |
-
"cell_type": "code",
|
243 |
-
"execution_count": 16,
|
244 |
-
"metadata": {
|
245 |
-
"colab": {
|
246 |
-
"base_uri": "https://localhost:8080/",
|
247 |
-
"height": 67,
|
248 |
-
"referenced_widgets": [
|
249 |
-
"5a7a3fb5cad44cedb743b29ae3d7642b",
|
250 |
-
"d00352e335b344adb4066268059420b5",
|
251 |
-
"ffb90a464cb64dae9d6a1d323dfe4724",
|
252 |
-
"56816b5e94c94591b1da8d8f36f496b3",
|
253 |
-
"7251b3615601497581d998607d620fa9",
|
254 |
-
"ec2a733c559e47f89380d62dd7c606bf",
|
255 |
-
"7970407a245e43558e098c521935f5ba",
|
256 |
-
"6d9a5776d24d459bb76ea2f665f5663a",
|
257 |
-
"7134f2d58a9a493bb5de794d275729a5",
|
258 |
-
"8f30969794454a3aa775f5f1fdcbf720",
|
259 |
-
"a98e772e74464e0391399aedf6a18e7d"
|
260 |
-
]
|
261 |
-
},
|
262 |
-
"id": "qC6aeFr3Rmi2",
|
263 |
-
"outputId": "02208934-78fe-4a77-e375-aed39da6b678"
|
264 |
-
},
|
265 |
-
"outputs": [
|
266 |
-
{
|
267 |
-
"name": "stdout",
|
268 |
-
"output_type": "stream",
|
269 |
-
"text": [
|
270 |
-
"Generating embeddings...\n"
|
271 |
-
]
|
272 |
-
},
|
273 |
-
{
|
274 |
-
"name": "stderr",
|
275 |
-
"output_type": "stream",
|
276 |
-
"text": [
|
277 |
-
"22it [00:04, 4.99it/s]\n"
|
278 |
-
]
|
279 |
-
}
|
280 |
-
],
|
281 |
-
"source": [
|
282 |
-
"from tqdm import tqdm\n",
|
283 |
-
"\n",
|
284 |
-
"# Generate embedding\n",
|
285 |
-
"if not load_embedding:\n",
|
286 |
-
" print(\"Generating embeddings...\")\n",
|
287 |
-
" for index, row in tqdm( df.iterrows() ):\n",
|
288 |
-
" row['embedding'] = get_embedding( row['text'] )\n",
|
289 |
-
"\n",
|
290 |
-
"# Or, load the embedding from the file.\n",
|
291 |
-
"else:\n",
|
292 |
-
" print(\"Loaded the embedding file.\")\n",
|
293 |
-
" with open('./mini-dataset_with_embedding.json', 'r') as file:\n",
|
294 |
-
" data = json.load(file)\n",
|
295 |
-
" df = pd.DataFrame(data)"
|
296 |
-
]
|
297 |
-
},
|
298 |
-
{
|
299 |
-
"cell_type": "markdown",
|
300 |
-
"metadata": {
|
301 |
-
"id": "E_qrXwImXrXJ"
|
302 |
-
},
|
303 |
-
"source": [
|
304 |
-
"# User Question"
|
305 |
-
]
|
306 |
-
},
|
307 |
-
{
|
308 |
-
"cell_type": "code",
|
309 |
-
"execution_count": 17,
|
310 |
-
"metadata": {
|
311 |
-
"colab": {
|
312 |
-
"base_uri": "https://localhost:8080/"
|
313 |
-
},
|
314 |
-
"id": "xGTa7cqCX97q",
|
315 |
-
"outputId": "6935f145-1858-4959-833e-db2da1d49342"
|
316 |
-
},
|
317 |
-
"outputs": [
|
318 |
-
{
|
319 |
-
"data": {
|
320 |
-
"text/plain": [
|
321 |
-
"1536"
|
322 |
-
]
|
323 |
-
},
|
324 |
-
"execution_count": 17,
|
325 |
-
"metadata": {},
|
326 |
-
"output_type": "execute_result"
|
327 |
-
}
|
328 |
-
],
|
329 |
-
"source": [
|
330 |
-
"# Define the user question, and convert it to embedding.\n",
|
331 |
-
"QUESTION = \"How many parameters LLaMA2 model has?\"\n",
|
332 |
-
"QUESTION_emb = get_embedding( QUESTION )\n",
|
333 |
-
"\n",
|
334 |
-
"len( QUESTION_emb )"
|
335 |
-
]
|
336 |
-
},
|
337 |
-
{
|
338 |
-
"cell_type": "markdown",
|
339 |
-
"metadata": {
|
340 |
-
"id": "BXNzNWrJYWhU"
|
341 |
-
},
|
342 |
-
"source": [
|
343 |
-
"# Test Cosine Similarity"
|
344 |
-
]
|
345 |
-
},
|
346 |
-
{
|
347 |
-
"cell_type": "markdown",
|
348 |
-
"metadata": {
|
349 |
-
"id": "Vxaq-FgLIhIj"
|
350 |
-
},
|
351 |
-
"source": [
|
352 |
-
"Calculating the similarity of embedding representations can help us to find pieces of text that are close to each other. In the following sample you see how the Cosine Similarity metric can identify which sentence could be a possible answer for the given user question. Obviously, the unrelated answer will score lower."
|
353 |
-
]
|
354 |
-
},
|
355 |
-
{
|
356 |
-
"cell_type": "code",
|
357 |
-
"execution_count": 18,
|
358 |
-
"metadata": {
|
359 |
-
"id": "LqDWcPd4b-ZI"
|
360 |
-
},
|
361 |
-
"outputs": [],
|
362 |
-
"source": [
|
363 |
-
"BAD_SOURCE_emb = get_embedding( \"The sky is blue.\" )\n",
|
364 |
-
"GOOD_SOURCE_emb = get_embedding( \"LLaMA2 model has a total of 2B parameters.\" )"
|
365 |
-
]
|
366 |
-
},
|
367 |
-
{
|
368 |
-
"cell_type": "code",
|
369 |
-
"execution_count": 21,
|
370 |
-
"metadata": {
|
371 |
-
"colab": {
|
372 |
-
"base_uri": "https://localhost:8080/"
|
373 |
-
},
|
374 |
-
"id": "OI00eN86YZKB",
|
375 |
-
"outputId": "3c75fdaa-098e-467c-daa8-41e87dda0738"
|
376 |
-
},
|
377 |
-
"outputs": [
|
378 |
-
{
|
379 |
-
"name": "stdout",
|
380 |
-
"output_type": "stream",
|
381 |
-
"text": [
|
382 |
-
"> Bad Response Score: [[0.69954666]]\n",
|
383 |
-
"> Good Response Score: [[0.93135959]]\n"
|
384 |
-
]
|
385 |
-
}
|
386 |
-
],
|
387 |
-
"source": [
|
388 |
-
"from sklearn.metrics.pairwise import cosine_similarity\n",
|
389 |
-
"\n",
|
390 |
-
"# A sample that how a good piece of text can achieve high similarity score compared\n",
|
391 |
-
"# to a completely unrelated text.\n",
|
392 |
-
"print(\"> Bad Response Score:\", cosine_similarity([QUESTION_emb], [BAD_SOURCE_emb]) )\n",
|
393 |
-
"print(\"> Good Response Score:\", cosine_similarity([QUESTION_emb], [GOOD_SOURCE_emb]) )"
|
394 |
-
]
|
395 |
-
},
|
396 |
-
{
|
397 |
-
"cell_type": "markdown",
|
398 |
-
"metadata": {
|
399 |
-
"id": "kdJlEtaaJC4I"
|
400 |
-
},
|
401 |
-
"source": [
|
402 |
-
"# Calculate Cosine Similarities"
|
403 |
-
]
|
404 |
-
},
|
405 |
-
{
|
406 |
-
"cell_type": "code",
|
407 |
-
"execution_count": 22,
|
408 |
-
"metadata": {
|
409 |
-
"colab": {
|
410 |
-
"base_uri": "https://localhost:8080/"
|
411 |
-
},
|
412 |
-
"id": "PNPN7OAXemmH",
|
413 |
-
"outputId": "5b76bf4f-bd0c-4023-9144-a9e251b843c9"
|
414 |
-
},
|
415 |
-
"outputs": [
|
416 |
-
{
|
417 |
-
"name": "stdout",
|
418 |
-
"output_type": "stream",
|
419 |
-
"text": [
|
420 |
-
"[[0.81723369 0.86596983 0.80878332 0.76617099 0.83996096 0.79028881\n",
|
421 |
-
" 0.78438889 0.80187419 0.82465877 0.81997573 0.84173789 0.75347142\n",
|
422 |
-
" 0.82343033 0.70476467 0.656007 0.73980692 0.76207973 0.76291356\n",
|
423 |
-
" 0.79877322 0.73906994 0.78277211 0.75805319]]\n"
|
424 |
-
]
|
425 |
-
}
|
426 |
-
],
|
427 |
-
"source": [
|
428 |
-
"# The similarity between the questions and each part of the essay.\n",
|
429 |
-
"cosine_similarities = cosine_similarity( [QUESTION_emb], df['embedding'].tolist() )\n",
|
430 |
-
"\n",
|
431 |
-
"print( cosine_similarities )"
|
432 |
-
]
|
433 |
-
},
|
434 |
-
{
|
435 |
-
"cell_type": "code",
|
436 |
-
"execution_count": 23,
|
437 |
-
"metadata": {
|
438 |
-
"colab": {
|
439 |
-
"base_uri": "https://localhost:8080/"
|
440 |
-
},
|
441 |
-
"id": "1-XI1_7mhlw4",
|
442 |
-
"outputId": "c91bd5bc-c547-4760-d821-9f13fd564392"
|
443 |
-
},
|
444 |
-
"outputs": [
|
445 |
-
{
|
446 |
-
"name": "stdout",
|
447 |
-
"output_type": "stream",
|
448 |
-
"text": [
|
449 |
-
"[ 1 10 4]\n"
|
450 |
-
]
|
451 |
-
}
|
452 |
-
],
|
453 |
-
"source": [
|
454 |
-
"import numpy as np\n",
|
455 |
-
"\n",
|
456 |
-
"number_of_chunks_to_retrieve = 3\n",
|
457 |
-
"\n",
|
458 |
-
"# Sort the scores\n",
|
459 |
-
"highest_index = np.argmax( cosine_similarities )\n",
|
460 |
-
"\n",
|
461 |
-
"# Pick the N highest scored chunks\n",
|
462 |
-
"indices = np.argsort(cosine_similarities[0])[::-1][:number_of_chunks_to_retrieve]\n",
|
463 |
-
"print( indices )"
|
464 |
-
]
|
465 |
-
},
|
466 |
-
{
|
467 |
-
"cell_type": "code",
|
468 |
-
"execution_count": 24,
|
469 |
-
"metadata": {
|
470 |
-
"colab": {
|
471 |
-
"base_uri": "https://localhost:8080/"
|
472 |
-
},
|
473 |
-
"id": "JPmhCb9kfB0w",
|
474 |
-
"outputId": "ee0f9bc2-3353-4ae8-b292-2356169b0d68"
|
475 |
-
},
|
476 |
-
"outputs": [
|
477 |
-
{
|
478 |
-
"name": "stdout",
|
479 |
-
"output_type": "stream",
|
480 |
-
"text": [
|
481 |
-
"> Chunk 1\n",
|
482 |
-
"II. Llama 2 Model Flavors: Llama 2 is available in four different model sizes: 7 billion, 13 billion, 34 billion, and 70 billion parameters. While 7B, 13B, and 70B have already been released, the 34B model is still awaited. The pretrained variant, trained on a whopping 2 trillion tokens, boasts a context window of 4096 tokens, twice the size of its predecessor Llama 1. Meta also released a Llama 2 fine-tuned model for chat applications that was trained on over 1 million human annotations. Such extensive training comes at a cost, with the 70B model taking a staggering 1720320 GPU hours to train. The context windowβs length determines the amount of content the model can process at once, making Llama 2 a powerful language model in terms of scale and efficiency.\n",
|
483 |
-
"----\n",
|
484 |
-
"> Chunk 2\n",
|
485 |
-
"New Llama-2 model: In mid-July, Meta released its new family of pre-trained and finetuned models called Llama-2, with an open source and commercial character to facilitate its use and expansion. The base model was released with a chat version and sizes 7B, 13B, and 70B. Together with the models, the corresponding papers were published describing their characteristics and relevant points of the learning process, which provide very interesting information on the subject. An updated version of Llama 1, trained on a new mix of publicly available data. The pretraining corpus size was increased by 40%, the modelβs context length was doubled, and grouped-query attention was adopted. Variants with 7B, 13B, and 70B parameters are released, along with 34B variants reported in the paper but not released.[1]\n",
|
486 |
-
"----\n",
|
487 |
-
"> Chunk 3\n",
|
488 |
-
"IV. Helpfulness Comparison: Llama 2 Outperforms Competitors: Llama 2 emerges as a strong contender in the open-source language model arena, outperforming its competitors in most categories. The 70B parameter model outperforms all other open-source models, while the 7B and 34B models outshine Falcon in all categories and MPT in all categories except coding. Source: Meta Llama 2 paper. Despite being smaller, Llam a2βs performance rivals that of Chat GPT 3.5, a significantly larger closed-source model. While GPT 4 and PalM-2-L, with their larger size, outperform Llama 2, this is expected due to their capacity for handling complex language tasks. Llama 2βs impressive ability to compete with larger models highlights its efficiency and potential in the market. Source: Meta Llama 2 paper. However, Llama 2 does face challenges in coding and math problems, where models like Chat GPT 4 excel, given their significantly larger size. Chat GPT 4 performed significantly better than Llama 2 for coding (HumanEval benchmark)and math problem tasks (GSM8k benchmark). Open-source AI technologies, like Llama 2, continue to advance, offering strong competition to closed-source models.\n",
|
489 |
-
"----\n"
|
490 |
-
]
|
491 |
-
}
|
492 |
-
],
|
493 |
-
"source": [
|
494 |
-
"# Look at the highest scored retrieved pieces of text\n",
|
495 |
-
"for idx, item in enumerate( df.text[indices] ):\n",
|
496 |
-
" print(f\"> Chunk {idx+1}\")\n",
|
497 |
-
" print(item)\n",
|
498 |
-
" print(\"----\")"
|
499 |
-
]
|
500 |
-
},
|
501 |
-
{
|
502 |
-
"cell_type": "markdown",
|
503 |
-
"metadata": {
|
504 |
-
"id": "7uvQACqAkHg4"
|
505 |
-
},
|
506 |
-
"source": [
|
507 |
-
"# Augment the Prompt"
|
508 |
-
]
|
509 |
-
},
|
510 |
-
{
|
511 |
-
"cell_type": "code",
|
512 |
-
"execution_count": 25,
|
513 |
-
"metadata": {
|
514 |
-
"id": "MXRdzta5kJ3V"
|
515 |
-
},
|
516 |
-
"outputs": [],
|
517 |
-
"source": [
|
518 |
-
"# Use the OpenAI API to answer the questions based on the retrieved pieces of text.\n",
|
519 |
-
"try:\n",
|
520 |
-
" # Formulating the system prompt and condition the model to answer only AI-related questions.\n",
|
521 |
-
" system_prompt = (\n",
|
522 |
-
" \"You are an assistant and expert in answering questions from a chunks of content. \"\n",
|
523 |
-
" \"Only answer AI-related question, else say that you cannot answer this question.\"\n",
|
524 |
-
" )\n",
|
525 |
-
"\n",
|
526 |
-
" # Create a user prompt with the user's question\n",
|
527 |
-
" prompt = (\n",
|
528 |
-
" \"Read the following informations that might contain the context you require to answer the question. You can use the informations starting from the <START_OF_CONTEXT> tag and end with the <END_OF_CONTEXT> tag. Here is the content:\\n\\n<START_OF_CONTEXT>\\n{}\\n<END_OF_CONTEXT>\\n\\n\"\n",
|
529 |
-
" \"Please provide an informative and accurate answer to the following question based on the avaiable context. Be concise and take your time. \\nQuestion: {}\\nAnswer:\"\n",
|
530 |
-
" )\n",
|
531 |
-
" # Add the retrieved pieces of text to the prompt.\n",
|
532 |
-
" prompt = prompt.format( \"\".join( df.text[indices] ), QUESTION )\n",
|
533 |
-
"\n",
|
534 |
-
" # Call the OpenAI API\n",
|
535 |
-
" response = client.chat.completions.create(\n",
|
536 |
-
" model='gpt-3.5-turbo-0125',\n",
|
537 |
-
" temperature=0.0,\n",
|
538 |
-
" messages=[\n",
|
539 |
-
" {\"role\": \"system\", \"content\": system_prompt},\n",
|
540 |
-
" {\"role\": \"user\", \"content\": prompt}\n",
|
541 |
-
" ]\n",
|
542 |
-
" )\n",
|
543 |
-
"\n",
|
544 |
-
" # Return the AI's response\n",
|
545 |
-
" res = response.choices[0].message.content.strip()\n",
|
546 |
-
"\n",
|
547 |
-
"except Exception as e:\n",
|
548 |
-
" print( f\"An error occurred: {e}\" )"
|
549 |
-
]
|
550 |
-
},
|
551 |
-
{
|
552 |
-
"cell_type": "code",
|
553 |
-
"execution_count": 26,
|
554 |
-
"metadata": {
|
555 |
-
"colab": {
|
556 |
-
"base_uri": "https://localhost:8080/"
|
557 |
-
},
|
558 |
-
"id": "9tBvJ8oMucha",
|
559 |
-
"outputId": "f473068b-0edc-424f-bb11-f7cfea76dc81"
|
560 |
-
},
|
561 |
-
"outputs": [
|
562 |
-
{
|
563 |
-
"name": "stdout",
|
564 |
-
"output_type": "stream",
|
565 |
-
"text": [
|
566 |
-
"The Llama 2 model has four different model sizes: 7 billion, 13 billion, 34 billion, and 70 billion parameters.\n"
|
567 |
-
]
|
568 |
-
}
|
569 |
-
],
|
570 |
-
"source": [
|
571 |
-
"print( res )"
|
572 |
-
]
|
573 |
-
},
|
574 |
-
{
|
575 |
-
"cell_type": "markdown",
|
576 |
-
"metadata": {
|
577 |
-
"id": "pW-BNCAC2JzE"
|
578 |
-
},
|
579 |
-
"source": [
|
580 |
-
"# Without Augmentation"
|
581 |
-
]
|
582 |
-
},
|
583 |
-
{
|
584 |
-
"cell_type": "markdown",
|
585 |
-
"metadata": {
|
586 |
-
"id": "tr5zXEGIMwJu"
|
587 |
-
},
|
588 |
-
"source": [
|
589 |
-
"Test the OpenAI API to answer the same question without the addition of retrieved documents. Basically, the LLM will use its knowledge to answer the question."
|
590 |
-
]
|
591 |
-
},
|
592 |
-
{
|
593 |
-
"cell_type": "code",
|
594 |
-
"execution_count": 33,
|
595 |
-
"metadata": {
|
596 |
-
"id": "RuyXjzZyuecE"
|
597 |
-
},
|
598 |
-
"outputs": [],
|
599 |
-
"source": [
|
600 |
-
"# Formulating the system prompt\n",
|
601 |
-
"system_prompt = (\n",
|
602 |
-
" \"You are an assistant and expert in answering questions.\"\n",
|
603 |
-
")\n",
|
604 |
-
"\n",
|
605 |
-
"# Combining the system prompt with the user's question\n",
|
606 |
-
"prompt = (\n",
|
607 |
-
" \"Be concise and take your time to answer the following question. \\nQuestion: {}\\nAnswer:\"\n",
|
608 |
-
")\n",
|
609 |
-
"prompt = prompt.format( QUESTION )\n",
|
610 |
-
"\n",
|
611 |
-
"# Call the OpenAI API\n",
|
612 |
-
"response = client.chat.completions.create(\n",
|
613 |
-
" model='gpt-3.5-turbo-0125',\n",
|
614 |
-
" temperature=.9,\n",
|
615 |
-
" messages=[\n",
|
616 |
-
" {\"role\": \"system\", \"content\": system_prompt},\n",
|
617 |
-
" {\"role\": \"user\", \"content\": prompt}\n",
|
618 |
-
" ]\n",
|
619 |
-
")\n",
|
620 |
-
"\n",
|
621 |
-
"# Return the AI's response\n",
|
622 |
-
"res = response.choices[0].message.content.strip()"
|
623 |
-
]
|
624 |
-
},
|
625 |
-
{
|
626 |
-
"cell_type": "code",
|
627 |
-
"execution_count": 34,
|
628 |
-
"metadata": {
|
629 |
-
"colab": {
|
630 |
-
"base_uri": "https://localhost:8080/"
|
631 |
},
|
632 |
-
"
|
633 |
-
|
634 |
-
|
635 |
-
|
636 |
-
{
|
637 |
-
"name": "stdout",
|
638 |
-
"output_type": "stream",
|
639 |
-
"text": [
|
640 |
-
"The LLaMA2 model has a total of X parameters.\n"
|
641 |
-
]
|
642 |
-
}
|
643 |
-
],
|
644 |
-
"source": [
|
645 |
-
"print( res )"
|
646 |
-
]
|
647 |
-
},
|
648 |
-
{
|
649 |
-
"cell_type": "code",
|
650 |
-
"execution_count": null,
|
651 |
-
"metadata": {},
|
652 |
-
"outputs": [],
|
653 |
-
"source": []
|
654 |
-
},
|
655 |
-
{
|
656 |
-
"cell_type": "code",
|
657 |
-
"execution_count": null,
|
658 |
-
"metadata": {},
|
659 |
-
"outputs": [],
|
660 |
-
"source": []
|
661 |
-
}
|
662 |
-
],
|
663 |
-
"metadata": {
|
664 |
-
"colab": {
|
665 |
-
"authorship_tag": "ABX9TyMXMMAHStqNGMvJEAKWLtU7",
|
666 |
-
"include_colab_link": true,
|
667 |
-
"provenance": []
|
668 |
-
},
|
669 |
-
"kernelspec": {
|
670 |
-
"display_name": "Python 3",
|
671 |
-
"name": "python3"
|
672 |
-
},
|
673 |
-
"language_info": {
|
674 |
-
"codemirror_mode": {
|
675 |
-
"name": "ipython",
|
676 |
-
"version": 3
|
677 |
-
},
|
678 |
-
"file_extension": ".py",
|
679 |
-
"mimetype": "text/x-python",
|
680 |
-
"name": "python",
|
681 |
-
"nbconvert_exporter": "python",
|
682 |
-
"pygments_lexer": "ipython3",
|
683 |
-
"version": "3.11.8"
|
684 |
-
},
|
685 |
-
"widgets": {
|
686 |
-
"application/vnd.jupyter.widget-state+json": {
|
687 |
-
"56816b5e94c94591b1da8d8f36f496b3": {
|
688 |
-
"model_module": "@jupyter-widgets/controls",
|
689 |
-
"model_module_version": "1.5.0",
|
690 |
-
"model_name": "HTMLModel",
|
691 |
"state": {
|
692 |
-
"
|
693 |
-
"
|
694 |
-
"
|
695 |
-
"_model_name": "HTMLModel",
|
696 |
"_view_count": null,
|
697 |
-
"_view_module": "@jupyter-widgets/
|
698 |
-
"_view_module_version": "1.
|
699 |
-
"_view_name": "
|
700 |
-
"
|
701 |
-
"
|
702 |
-
"
|
703 |
-
"
|
704 |
-
"
|
705 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
706 |
}
|
707 |
},
|
708 |
-
"
|
709 |
"model_module": "@jupyter-widgets/controls",
|
|
|
710 |
"model_module_version": "1.5.0",
|
711 |
-
"model_name": "HBoxModel",
|
712 |
"state": {
|
713 |
-
"_dom_classes": [],
|
714 |
"_model_module": "@jupyter-widgets/controls",
|
715 |
"_model_module_version": "1.5.0",
|
716 |
-
"_model_name": "
|
717 |
"_view_count": null,
|
718 |
-
"_view_module": "@jupyter-widgets/
|
719 |
-
"_view_module_version": "1.
|
720 |
-
"_view_name": "
|
721 |
-
"
|
722 |
-
"children": [
|
723 |
-
"IPY_MODEL_d00352e335b344adb4066268059420b5",
|
724 |
-
"IPY_MODEL_ffb90a464cb64dae9d6a1d323dfe4724",
|
725 |
-
"IPY_MODEL_56816b5e94c94591b1da8d8f36f496b3"
|
726 |
-
],
|
727 |
-
"layout": "IPY_MODEL_7251b3615601497581d998607d620fa9"
|
728 |
}
|
729 |
},
|
730 |
-
"
|
731 |
"model_module": "@jupyter-widgets/base",
|
732 |
-
"model_module_version": "1.2.0",
|
733 |
"model_name": "LayoutModel",
|
|
|
734 |
"state": {
|
735 |
"_model_module": "@jupyter-widgets/base",
|
736 |
"_model_module_version": "1.2.0",
|
@@ -779,10 +275,10 @@
|
|
779 |
"width": "20px"
|
780 |
}
|
781 |
},
|
782 |
-
"
|
783 |
"model_module": "@jupyter-widgets/controls",
|
784 |
-
"model_module_version": "1.5.0",
|
785 |
"model_name": "ProgressStyleModel",
|
|
|
786 |
"state": {
|
787 |
"_model_module": "@jupyter-widgets/controls",
|
788 |
"_model_module_version": "1.5.0",
|
@@ -795,10 +291,10 @@
|
|
795 |
"description_width": ""
|
796 |
}
|
797 |
},
|
798 |
-
"
|
799 |
"model_module": "@jupyter-widgets/base",
|
800 |
-
"model_module_version": "1.2.0",
|
801 |
"model_name": "LayoutModel",
|
|
|
802 |
"state": {
|
803 |
"_model_module": "@jupyter-widgets/base",
|
804 |
"_model_module_version": "1.2.0",
|
@@ -847,188 +343,741 @@
|
|
847 |
"width": null
|
848 |
}
|
849 |
},
|
850 |
-
"
|
851 |
-
"model_module": "@jupyter-widgets/controls",
|
852 |
-
"
|
853 |
-
"
|
854 |
-
"state": {
|
855 |
-
"_model_module": "@jupyter-widgets/controls",
|
856 |
-
"_model_module_version": "1.5.0",
|
857 |
-
"_model_name": "DescriptionStyleModel",
|
858 |
-
"_view_count": null,
|
859 |
-
"_view_module": "@jupyter-widgets/base",
|
860 |
-
"_view_module_version": "1.2.0",
|
861 |
-
"_view_name": "StyleView",
|
862 |
-
"description_width": ""
|
863 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
864 |
},
|
865 |
-
|
866 |
-
"
|
867 |
-
"
|
868 |
-
|
869 |
-
|
870 |
-
|
871 |
-
"
|
872 |
-
|
873 |
-
|
874 |
-
|
875 |
-
|
876 |
-
|
877 |
-
|
878 |
-
|
879 |
-
|
880 |
-
|
881 |
-
|
882 |
-
|
883 |
-
|
884 |
-
|
885 |
-
|
886 |
-
|
887 |
-
|
888 |
-
|
889 |
-
|
890 |
-
|
891 |
-
|
892 |
-
|
893 |
-
|
894 |
-
|
895 |
-
|
896 |
-
|
897 |
-
|
898 |
-
|
899 |
-
|
900 |
-
|
901 |
-
|
902 |
-
|
903 |
-
|
904 |
-
|
905 |
-
|
906 |
-
|
907 |
-
|
908 |
-
|
909 |
-
|
910 |
-
|
911 |
-
|
912 |
-
|
913 |
-
"visibility": null,
|
914 |
-
"width": null
|
915 |
-
}
|
916 |
},
|
917 |
-
"
|
918 |
-
|
919 |
-
|
920 |
-
|
921 |
-
|
922 |
-
|
923 |
-
|
924 |
-
|
925 |
-
"
|
926 |
-
|
927 |
-
|
928 |
-
|
929 |
-
|
930 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
931 |
},
|
932 |
-
"
|
933 |
-
|
934 |
-
|
935 |
-
|
936 |
-
|
937 |
-
|
938 |
-
|
939 |
-
|
940 |
-
|
941 |
-
"
|
942 |
-
"
|
943 |
-
|
944 |
-
|
945 |
-
|
946 |
-
|
947 |
-
|
948 |
-
|
949 |
-
|
950 |
-
|
951 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
952 |
},
|
953 |
-
"
|
954 |
-
|
955 |
-
|
956 |
-
|
957 |
-
|
958 |
-
|
959 |
-
|
960 |
-
|
961 |
-
|
962 |
-
"
|
963 |
-
|
964 |
-
|
965 |
-
|
966 |
-
|
967 |
-
|
968 |
-
|
969 |
-
|
970 |
-
|
971 |
-
|
972 |
-
|
973 |
-
|
974 |
-
|
975 |
-
|
976 |
-
|
977 |
-
|
978 |
-
|
979 |
-
"grid_row": null,
|
980 |
-
"grid_template_areas": null,
|
981 |
-
"grid_template_columns": null,
|
982 |
-
"grid_template_rows": null,
|
983 |
-
"height": null,
|
984 |
-
"justify_content": null,
|
985 |
-
"justify_items": null,
|
986 |
-
"left": null,
|
987 |
-
"margin": null,
|
988 |
-
"max_height": null,
|
989 |
-
"max_width": null,
|
990 |
-
"min_height": null,
|
991 |
-
"min_width": null,
|
992 |
-
"object_fit": null,
|
993 |
-
"object_position": null,
|
994 |
-
"order": null,
|
995 |
-
"overflow": null,
|
996 |
-
"overflow_x": null,
|
997 |
-
"overflow_y": null,
|
998 |
-
"padding": null,
|
999 |
-
"right": null,
|
1000 |
-
"top": null,
|
1001 |
-
"visibility": null,
|
1002 |
-
"width": null
|
1003 |
-
}
|
1004 |
},
|
1005 |
-
"
|
1006 |
-
|
1007 |
-
|
1008 |
-
|
1009 |
-
|
1010 |
-
|
1011 |
-
|
1012 |
-
|
1013 |
-
|
1014 |
-
"
|
1015 |
-
"
|
1016 |
-
"
|
1017 |
-
"
|
1018 |
-
"
|
1019 |
-
"
|
1020 |
-
"
|
1021 |
-
"
|
1022 |
-
"
|
1023 |
-
|
1024 |
-
|
1025 |
-
|
1026 |
-
|
1027 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1028 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1029 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1030 |
}
|
1031 |
-
|
1032 |
-
|
1033 |
-
"nbformat_minor": 0
|
1034 |
-
}
|
|
|
1 |
{
|
2 |
+
"nbformat": 4,
|
3 |
+
"nbformat_minor": 0,
|
4 |
+
"metadata": {
|
5 |
+
"colab": {
|
6 |
+
"provenance": [],
|
7 |
+
"authorship_tag": "ABX9TyMeVXuX4AISIHdAML8DITii",
|
8 |
+
"include_colab_link": true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
},
|
10 |
+
"kernelspec": {
|
11 |
+
"name": "python3",
|
12 |
+
"display_name": "Python 3"
|
|
|
|
|
|
|
|
|
|
|
13 |
},
|
14 |
+
"language_info": {
|
15 |
+
"name": "python"
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
},
|
17 |
+
"widgets": {
|
18 |
+
"application/vnd.jupyter.widget-state+json": {
|
19 |
+
"46a91770024e4802acd3e64e9bc46f32": {
|
20 |
+
"model_module": "@jupyter-widgets/controls",
|
21 |
+
"model_name": "HBoxModel",
|
22 |
+
"model_module_version": "1.5.0",
|
23 |
+
"state": {
|
24 |
+
"_dom_classes": [],
|
25 |
+
"_model_module": "@jupyter-widgets/controls",
|
26 |
+
"_model_module_version": "1.5.0",
|
27 |
+
"_model_name": "HBoxModel",
|
28 |
+
"_view_count": null,
|
29 |
+
"_view_module": "@jupyter-widgets/controls",
|
30 |
+
"_view_module_version": "1.5.0",
|
31 |
+
"_view_name": "HBoxView",
|
32 |
+
"box_style": "",
|
33 |
+
"children": [
|
34 |
+
"IPY_MODEL_613898a418d64df3b18d35083f0bb36d",
|
35 |
+
"IPY_MODEL_9f9427eb6a644166906bb321f13eaf48",
|
36 |
+
"IPY_MODEL_a4a232c5b5e1493897e9acdd25b8efd4"
|
37 |
+
],
|
38 |
+
"layout": "IPY_MODEL_b2e91819e1c94f28b7bbad66918cb797"
|
39 |
+
}
|
40 |
},
|
41 |
+
"613898a418d64df3b18d35083f0bb36d": {
|
42 |
+
"model_module": "@jupyter-widgets/controls",
|
43 |
+
"model_name": "HTMLModel",
|
44 |
+
"model_module_version": "1.5.0",
|
45 |
+
"state": {
|
46 |
+
"_dom_classes": [],
|
47 |
+
"_model_module": "@jupyter-widgets/controls",
|
48 |
+
"_model_module_version": "1.5.0",
|
49 |
+
"_model_name": "HTMLModel",
|
50 |
+
"_view_count": null,
|
51 |
+
"_view_module": "@jupyter-widgets/controls",
|
52 |
+
"_view_module_version": "1.5.0",
|
53 |
+
"_view_name": "HTMLView",
|
54 |
+
"description": "",
|
55 |
+
"description_tooltip": null,
|
56 |
+
"layout": "IPY_MODEL_010cbcb0f1364576b15f792f4d11f605",
|
57 |
+
"placeholder": "β",
|
58 |
+
"style": "IPY_MODEL_f51d5da0f39e4c1885357d3d4c9964d9",
|
59 |
+
"value": ""
|
60 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
},
|
62 |
+
"9f9427eb6a644166906bb321f13eaf48": {
|
63 |
+
"model_module": "@jupyter-widgets/controls",
|
64 |
+
"model_name": "FloatProgressModel",
|
65 |
+
"model_module_version": "1.5.0",
|
66 |
+
"state": {
|
67 |
+
"_dom_classes": [],
|
68 |
+
"_model_module": "@jupyter-widgets/controls",
|
69 |
+
"_model_module_version": "1.5.0",
|
70 |
+
"_model_name": "FloatProgressModel",
|
71 |
+
"_view_count": null,
|
72 |
+
"_view_module": "@jupyter-widgets/controls",
|
73 |
+
"_view_module_version": "1.5.0",
|
74 |
+
"_view_name": "ProgressView",
|
75 |
+
"bar_style": "success",
|
76 |
+
"description": "",
|
77 |
+
"description_tooltip": null,
|
78 |
+
"layout": "IPY_MODEL_c4ceff5437e0470089c161e21488d2a7",
|
79 |
+
"max": 1,
|
80 |
+
"min": 0,
|
81 |
+
"orientation": "horizontal",
|
82 |
+
"style": "IPY_MODEL_6aafd52b0e3e4e0183b1666ad1e8a448",
|
83 |
+
"value": 1
|
84 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
},
|
86 |
+
"a4a232c5b5e1493897e9acdd25b8efd4": {
|
87 |
+
"model_module": "@jupyter-widgets/controls",
|
88 |
+
"model_name": "HTMLModel",
|
89 |
+
"model_module_version": "1.5.0",
|
90 |
+
"state": {
|
91 |
+
"_dom_classes": [],
|
92 |
+
"_model_module": "@jupyter-widgets/controls",
|
93 |
+
"_model_module_version": "1.5.0",
|
94 |
+
"_model_name": "HTMLModel",
|
95 |
+
"_view_count": null,
|
96 |
+
"_view_module": "@jupyter-widgets/controls",
|
97 |
+
"_view_module_version": "1.5.0",
|
98 |
+
"_view_name": "HTMLView",
|
99 |
+
"description": "",
|
100 |
+
"description_tooltip": null,
|
101 |
+
"layout": "IPY_MODEL_80137fc11d4b4e518d8c8957ca5461b1",
|
102 |
+
"placeholder": "β",
|
103 |
+
"style": "IPY_MODEL_c4236d507b354bff830620a8bde32191",
|
104 |
+
"value": "β174/?β[00:31<00:00,ββ6.30it/s]"
|
105 |
+
}
|
106 |
},
|
107 |
+
"b2e91819e1c94f28b7bbad66918cb797": {
|
108 |
+
"model_module": "@jupyter-widgets/base",
|
109 |
+
"model_name": "LayoutModel",
|
110 |
+
"model_module_version": "1.2.0",
|
111 |
+
"state": {
|
112 |
+
"_model_module": "@jupyter-widgets/base",
|
113 |
+
"_model_module_version": "1.2.0",
|
114 |
+
"_model_name": "LayoutModel",
|
115 |
+
"_view_count": null,
|
116 |
+
"_view_module": "@jupyter-widgets/base",
|
117 |
+
"_view_module_version": "1.2.0",
|
118 |
+
"_view_name": "LayoutView",
|
119 |
+
"align_content": null,
|
120 |
+
"align_items": null,
|
121 |
+
"align_self": null,
|
122 |
+
"border": null,
|
123 |
+
"bottom": null,
|
124 |
+
"display": null,
|
125 |
+
"flex": null,
|
126 |
+
"flex_flow": null,
|
127 |
+
"grid_area": null,
|
128 |
+
"grid_auto_columns": null,
|
129 |
+
"grid_auto_flow": null,
|
130 |
+
"grid_auto_rows": null,
|
131 |
+
"grid_column": null,
|
132 |
+
"grid_gap": null,
|
133 |
+
"grid_row": null,
|
134 |
+
"grid_template_areas": null,
|
135 |
+
"grid_template_columns": null,
|
136 |
+
"grid_template_rows": null,
|
137 |
+
"height": null,
|
138 |
+
"justify_content": null,
|
139 |
+
"justify_items": null,
|
140 |
+
"left": null,
|
141 |
+
"margin": null,
|
142 |
+
"max_height": null,
|
143 |
+
"max_width": null,
|
144 |
+
"min_height": null,
|
145 |
+
"min_width": null,
|
146 |
+
"object_fit": null,
|
147 |
+
"object_position": null,
|
148 |
+
"order": null,
|
149 |
+
"overflow": null,
|
150 |
+
"overflow_x": null,
|
151 |
+
"overflow_y": null,
|
152 |
+
"padding": null,
|
153 |
+
"right": null,
|
154 |
+
"top": null,
|
155 |
+
"visibility": null,
|
156 |
+
"width": null
|
157 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
},
|
159 |
+
"010cbcb0f1364576b15f792f4d11f605": {
|
160 |
+
"model_module": "@jupyter-widgets/base",
|
161 |
+
"model_name": "LayoutModel",
|
162 |
+
"model_module_version": "1.2.0",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
"state": {
|
164 |
+
"_model_module": "@jupyter-widgets/base",
|
165 |
+
"_model_module_version": "1.2.0",
|
166 |
+
"_model_name": "LayoutModel",
|
|
|
167 |
"_view_count": null,
|
168 |
+
"_view_module": "@jupyter-widgets/base",
|
169 |
+
"_view_module_version": "1.2.0",
|
170 |
+
"_view_name": "LayoutView",
|
171 |
+
"align_content": null,
|
172 |
+
"align_items": null,
|
173 |
+
"align_self": null,
|
174 |
+
"border": null,
|
175 |
+
"bottom": null,
|
176 |
+
"display": null,
|
177 |
+
"flex": null,
|
178 |
+
"flex_flow": null,
|
179 |
+
"grid_area": null,
|
180 |
+
"grid_auto_columns": null,
|
181 |
+
"grid_auto_flow": null,
|
182 |
+
"grid_auto_rows": null,
|
183 |
+
"grid_column": null,
|
184 |
+
"grid_gap": null,
|
185 |
+
"grid_row": null,
|
186 |
+
"grid_template_areas": null,
|
187 |
+
"grid_template_columns": null,
|
188 |
+
"grid_template_rows": null,
|
189 |
+
"height": null,
|
190 |
+
"justify_content": null,
|
191 |
+
"justify_items": null,
|
192 |
+
"left": null,
|
193 |
+
"margin": null,
|
194 |
+
"max_height": null,
|
195 |
+
"max_width": null,
|
196 |
+
"min_height": null,
|
197 |
+
"min_width": null,
|
198 |
+
"object_fit": null,
|
199 |
+
"object_position": null,
|
200 |
+
"order": null,
|
201 |
+
"overflow": null,
|
202 |
+
"overflow_x": null,
|
203 |
+
"overflow_y": null,
|
204 |
+
"padding": null,
|
205 |
+
"right": null,
|
206 |
+
"top": null,
|
207 |
+
"visibility": null,
|
208 |
+
"width": null
|
209 |
}
|
210 |
},
|
211 |
+
"f51d5da0f39e4c1885357d3d4c9964d9": {
|
212 |
"model_module": "@jupyter-widgets/controls",
|
213 |
+
"model_name": "DescriptionStyleModel",
|
214 |
"model_module_version": "1.5.0",
|
|
|
215 |
"state": {
|
|
|
216 |
"_model_module": "@jupyter-widgets/controls",
|
217 |
"_model_module_version": "1.5.0",
|
218 |
+
"_model_name": "DescriptionStyleModel",
|
219 |
"_view_count": null,
|
220 |
+
"_view_module": "@jupyter-widgets/base",
|
221 |
+
"_view_module_version": "1.2.0",
|
222 |
+
"_view_name": "StyleView",
|
223 |
+
"description_width": ""
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
}
|
225 |
},
|
226 |
+
"c4ceff5437e0470089c161e21488d2a7": {
|
227 |
"model_module": "@jupyter-widgets/base",
|
|
|
228 |
"model_name": "LayoutModel",
|
229 |
+
"model_module_version": "1.2.0",
|
230 |
"state": {
|
231 |
"_model_module": "@jupyter-widgets/base",
|
232 |
"_model_module_version": "1.2.0",
|
|
|
275 |
"width": "20px"
|
276 |
}
|
277 |
},
|
278 |
+
"6aafd52b0e3e4e0183b1666ad1e8a448": {
|
279 |
"model_module": "@jupyter-widgets/controls",
|
|
|
280 |
"model_name": "ProgressStyleModel",
|
281 |
+
"model_module_version": "1.5.0",
|
282 |
"state": {
|
283 |
"_model_module": "@jupyter-widgets/controls",
|
284 |
"_model_module_version": "1.5.0",
|
|
|
291 |
"description_width": ""
|
292 |
}
|
293 |
},
|
294 |
+
"80137fc11d4b4e518d8c8957ca5461b1": {
|
295 |
"model_module": "@jupyter-widgets/base",
|
|
|
296 |
"model_name": "LayoutModel",
|
297 |
+
"model_module_version": "1.2.0",
|
298 |
"state": {
|
299 |
"_model_module": "@jupyter-widgets/base",
|
300 |
"_model_module_version": "1.2.0",
|
|
|
343 |
"width": null
|
344 |
}
|
345 |
},
|
346 |
+
"c4236d507b354bff830620a8bde32191": {
|
347 |
+
"model_module": "@jupyter-widgets/controls",
|
348 |
+
"model_name": "DescriptionStyleModel",
|
349 |
+
"model_module_version": "1.5.0",
|
350 |
+
"state": {
|
351 |
+
"_model_module": "@jupyter-widgets/controls",
|
352 |
+
"_model_module_version": "1.5.0",
|
353 |
+
"_model_name": "DescriptionStyleModel",
|
354 |
+
"_view_count": null,
|
355 |
+
"_view_module": "@jupyter-widgets/base",
|
356 |
+
"_view_module_version": "1.2.0",
|
357 |
+
"_view_name": "StyleView",
|
358 |
+
"description_width": ""
|
359 |
+
}
|
360 |
+
}
|
361 |
+
}
|
362 |
+
}
|
363 |
+
},
|
364 |
+
"cells": [
|
365 |
+
{
|
366 |
+
"cell_type": "markdown",
|
367 |
+
"metadata": {
|
368 |
+
"id": "view-in-github",
|
369 |
+
"colab_type": "text"
|
370 |
+
},
|
371 |
+
"source": [
|
372 |
+
"<a href=\"https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/02-Basic_RAG.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
|
373 |
+
]
|
374 |
+
},
|
375 |
+
{
|
376 |
+
"cell_type": "markdown",
|
377 |
+
"source": [
|
378 |
+
"# Install Packages and Setup Variables"
|
379 |
+
],
|
380 |
+
"metadata": {
|
381 |
+
"id": "4Tw3tvMs6R-Y"
|
382 |
+
}
|
383 |
+
},
|
384 |
+
{
|
385 |
+
"cell_type": "code",
|
386 |
+
"execution_count": 1,
|
387 |
+
"metadata": {
|
388 |
+
"colab": {
|
389 |
+
"base_uri": "https://localhost:8080/"
|
390 |
+
},
|
391 |
+
"id": "HaB4G9zr0BYm",
|
392 |
+
"outputId": "2a76e676-6fae-44df-ae8c-e4869bfbbc2d"
|
393 |
+
},
|
394 |
+
"outputs": [
|
395 |
+
{
|
396 |
+
"output_type": "stream",
|
397 |
+
"name": "stdout",
|
398 |
+
"text": [
|
399 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m225.4/225.4 kB\u001b[0m \u001b[31m2.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
400 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m51.7/51.7 kB\u001b[0m \u001b[31m3.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
401 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m2.0/2.0 MB\u001b[0m \u001b[31m17.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
402 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m6.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
403 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m3.1/3.1 MB\u001b[0m \u001b[31m17.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
404 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m77.8/77.8 kB\u001b[0m \u001b[31m3.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
405 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m5.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
406 |
+
"\u001b[?25h"
|
407 |
+
]
|
408 |
+
}
|
409 |
+
],
|
410 |
+
"source": [
|
411 |
+
"!pip install -q openai==1.6.0 cohere==4.39 tiktoken==0.5.2"
|
412 |
+
]
|
413 |
+
},
|
414 |
+
{
|
415 |
+
"cell_type": "code",
|
416 |
+
"source": [
|
417 |
+
"import os\n",
|
418 |
+
"\n",
|
419 |
+
"# Set the \"OPENAI_API_KEY\" in the Python environment. Will be used by OpenAI client later.\n",
|
420 |
+
"os.environ[\"OPENAI_API_KEY\"] = \"<YOUR_OPENAI_KEY>\""
|
421 |
+
],
|
422 |
+
"metadata": {
|
423 |
+
"id": "MYvUA6CF2Le6"
|
424 |
+
},
|
425 |
+
"execution_count": 2,
|
426 |
+
"outputs": []
|
427 |
+
},
|
428 |
+
{
|
429 |
+
"cell_type": "code",
|
430 |
+
"source": [
|
431 |
+
"# False: Generate the embedding for the dataset. (Associated cost with using OpenAI endpoint)\n",
|
432 |
+
"# True: Load the dataset that already has the embedding vectors.\n",
|
433 |
+
"load_embedding = False"
|
434 |
+
],
|
435 |
+
"metadata": {
|
436 |
+
"id": "0ViVXXIqXBai"
|
437 |
+
},
|
438 |
+
"execution_count": 3,
|
439 |
+
"outputs": []
|
440 |
+
},
|
441 |
+
{
|
442 |
+
"cell_type": "markdown",
|
443 |
+
"source": [
|
444 |
+
"# Load Dataset"
|
445 |
+
],
|
446 |
+
"metadata": {
|
447 |
+
"id": "D8Nzx-cN_bDz"
|
448 |
+
}
|
449 |
+
},
|
450 |
+
{
|
451 |
+
"cell_type": "markdown",
|
452 |
+
"source": [
|
453 |
+
"## Download Dataset (JSON)"
|
454 |
+
],
|
455 |
+
"metadata": {
|
456 |
+
"id": "5JpI7GiZ--Gw"
|
457 |
+
}
|
458 |
+
},
|
459 |
+
{
|
460 |
+
"cell_type": "markdown",
|
461 |
+
"source": [
|
462 |
+
"The dataset includes several articles from the TowardsAI blog, which provide an in-depth explanation of the LLaMA2 model."
|
463 |
+
],
|
464 |
+
"metadata": {
|
465 |
+
"id": "NT68BDYt-GkG"
|
466 |
+
}
|
467 |
+
},
|
468 |
+
{
|
469 |
+
"cell_type": "code",
|
470 |
+
"source": [
|
471 |
+
"!wget https://raw.githubusercontent.com/AlaFalaki/tutorial_notebooks/main/data/mini-llama-articles.csv\n",
|
472 |
+
"!wget https://raw.githubusercontent.com/AlaFalaki/tutorial_notebooks/main/data/mini-llama-articles-with_embeddings.csv"
|
473 |
+
],
|
474 |
+
"metadata": {
|
475 |
+
"colab": {
|
476 |
+
"base_uri": "https://localhost:8080/"
|
477 |
+
},
|
478 |
+
"id": "p6NEJT9S2OoH",
|
479 |
+
"outputId": "fd3aa19c-a644-4635-9838-2c20526c4da2"
|
480 |
+
},
|
481 |
+
"execution_count": 4,
|
482 |
+
"outputs": [
|
483 |
+
{
|
484 |
+
"output_type": "stream",
|
485 |
+
"name": "stdout",
|
486 |
+
"text": [
|
487 |
+
"--2024-03-20 16:18:39-- https://raw.githubusercontent.com/AlaFalaki/tutorial_notebooks/main/data/mini-llama-articles.csv\n",
|
488 |
+
"Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ...\n",
|
489 |
+
"Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected.\n",
|
490 |
+
"HTTP request sent, awaiting response... 200 OK\n",
|
491 |
+
"Length: 173646 (170K) [text/plain]\n",
|
492 |
+
"Saving to: βmini-llama-articles.csvβ\n",
|
493 |
+
"\n",
|
494 |
+
"\rmini-llama-articles 0%[ ] 0 --.-KB/s \rmini-llama-articles 100%[===================>] 169.58K --.-KB/s in 0.02s \n",
|
495 |
+
"\n",
|
496 |
+
"2024-03-20 16:18:40 (6.91 MB/s) - βmini-llama-articles.csvβ saved [173646/173646]\n",
|
497 |
+
"\n",
|
498 |
+
"--2024-03-20 16:18:40-- https://raw.githubusercontent.com/AlaFalaki/tutorial_notebooks/main/data/mini-llama-articles-with_embeddings.csv\n",
|
499 |
+
"Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ...\n",
|
500 |
+
"Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected.\n",
|
501 |
+
"HTTP request sent, awaiting response... 200 OK\n",
|
502 |
+
"Length: 11868176 (11M) [text/plain]\n",
|
503 |
+
"Saving to: βmini-llama-articles-with_embeddings.csvβ\n",
|
504 |
+
"\n",
|
505 |
+
"mini-llama-articles 100%[===================>] 11.32M --.-KB/s in 0.1s \n",
|
506 |
+
"\n",
|
507 |
+
"2024-03-20 16:18:40 (103 MB/s) - βmini-llama-articles-with_embeddings.csvβ saved [11868176/11868176]\n",
|
508 |
+
"\n"
|
509 |
+
]
|
510 |
+
}
|
511 |
+
]
|
512 |
+
},
|
513 |
+
{
|
514 |
+
"cell_type": "markdown",
|
515 |
+
"source": [
|
516 |
+
"## Read File"
|
517 |
+
],
|
518 |
+
"metadata": {
|
519 |
+
"id": "oYDd03Qn_clh"
|
520 |
+
}
|
521 |
+
},
|
522 |
+
{
|
523 |
+
"cell_type": "code",
|
524 |
+
"source": [
|
525 |
+
"# Split the input text into chunks of specified size.\n",
|
526 |
+
"def split_into_chunks(text, chunk_size=1024):\n",
|
527 |
+
" chunks = []\n",
|
528 |
+
" for i in range(0, len(text), chunk_size):\n",
|
529 |
+
" chunks.append( text[i:i+chunk_size] )\n",
|
530 |
+
"\n",
|
531 |
+
" return chunks"
|
532 |
+
],
|
533 |
+
"metadata": {
|
534 |
+
"id": "_bfhs5NMYr4N"
|
535 |
+
},
|
536 |
+
"execution_count": 6,
|
537 |
+
"outputs": []
|
538 |
+
},
|
539 |
+
{
|
540 |
+
"cell_type": "code",
|
541 |
+
"source": [
|
542 |
+
"import csv\n",
|
543 |
+
"\n",
|
544 |
+
"chunks = []\n",
|
545 |
+
"\n",
|
546 |
+
"# Load the file as a CSV\n",
|
547 |
+
"with open(\"./mini-llama-articles.csv\", mode=\"r\", encoding=\"utf-8\") as file:\n",
|
548 |
+
" csv_reader = csv.reader(file)\n",
|
549 |
+
"\n",
|
550 |
+
" for idx, row in enumerate( csv_reader ):\n",
|
551 |
+
" if idx == 0: continue; # Skip header row\n",
|
552 |
+
" chunks.extend( split_into_chunks(row[1]) )"
|
553 |
+
],
|
554 |
+
"metadata": {
|
555 |
+
"id": "UcQ7Ge_XCuXa"
|
556 |
+
},
|
557 |
+
"execution_count": 7,
|
558 |
+
"outputs": []
|
559 |
+
},
|
560 |
+
{
|
561 |
+
"cell_type": "code",
|
562 |
+
"source": [
|
563 |
+
"import pandas as pd\n",
|
564 |
+
"\n",
|
565 |
+
"# Convert the JSON list to a Pandas Dataframe\n",
|
566 |
+
"df = pd.DataFrame(chunks, columns=['chunk'])\n",
|
567 |
+
"\n",
|
568 |
+
"df.keys()"
|
569 |
+
],
|
570 |
+
"metadata": {
|
571 |
+
"colab": {
|
572 |
+
"base_uri": "https://localhost:8080/"
|
573 |
+
},
|
574 |
+
"id": "JKdFSOb0NXjx",
|
575 |
+
"outputId": "ce43c97f-2083-49b5-837d-62cc427fe848"
|
576 |
+
},
|
577 |
+
"execution_count": 8,
|
578 |
+
"outputs": [
|
579 |
+
{
|
580 |
+
"output_type": "execute_result",
|
581 |
+
"data": {
|
582 |
+
"text/plain": [
|
583 |
+
"Index(['chunk'], dtype='object')"
|
584 |
+
]
|
585 |
+
},
|
586 |
+
"metadata": {},
|
587 |
+
"execution_count": 8
|
588 |
+
}
|
589 |
+
]
|
590 |
+
},
|
591 |
+
{
|
592 |
+
"cell_type": "markdown",
|
593 |
+
"source": [
|
594 |
+
"# Generate Embedding"
|
595 |
+
],
|
596 |
+
"metadata": {
|
597 |
+
"id": "21pFDgNdW9rO"
|
598 |
+
}
|
599 |
+
},
|
600 |
+
{
|
601 |
+
"cell_type": "code",
|
602 |
+
"source": [
|
603 |
+
"from openai import OpenAI\n",
|
604 |
+
"\n",
|
605 |
+
"client = OpenAI()\n",
|
606 |
+
"\n",
|
607 |
+
"# Defining a function that converts a text to embedding vector using OpenAI's Ada model.\n",
|
608 |
+
"def get_embedding(text):\n",
|
609 |
+
" try:\n",
|
610 |
+
" # Remove newlines\n",
|
611 |
+
" text = text.replace(\"\\n\", \" \")\n",
|
612 |
+
" res = client.embeddings.create(input = [text], model=\"text-embedding-ada-002\")\n",
|
613 |
+
"\n",
|
614 |
+
" return res.data[0].embedding\n",
|
615 |
+
"\n",
|
616 |
+
" except:\n",
|
617 |
+
" return None"
|
618 |
+
],
|
619 |
+
"metadata": {
|
620 |
+
"id": "AfS9w9eQAKyu"
|
621 |
+
},
|
622 |
+
"execution_count": 9,
|
623 |
+
"outputs": []
|
624 |
+
},
|
625 |
+
{
|
626 |
+
"cell_type": "code",
|
627 |
+
"source": [
|
628 |
+
"from tqdm.notebook import tqdm\n",
|
629 |
+
"import numpy as np\n",
|
630 |
+
"\n",
|
631 |
+
"# Generate embedding\n",
|
632 |
+
"if not load_embedding:\n",
|
633 |
+
" print(\"Generating embeddings...\")\n",
|
634 |
+
" embeddings = []\n",
|
635 |
+
" for index, row in tqdm( df.iterrows() ):\n",
|
636 |
+
" # df.at[index, 'embedding'] = get_embedding( row['chunk'] )\n",
|
637 |
+
" embeddings.append( get_embedding( row['chunk'] ) )\n",
|
638 |
+
"\n",
|
639 |
+
" embeddings_values = pd.Series(embeddings)\n",
|
640 |
+
" df.insert(loc=1, column='embedding', value=embeddings_values)\n",
|
641 |
+
"\n",
|
642 |
+
"# Or, load the embedding from the file.\n",
|
643 |
+
"else:\n",
|
644 |
+
" print(\"Loaded the embedding file.\")\n",
|
645 |
+
" # Load the file as a CSV\n",
|
646 |
+
" df = pd.read_csv('mini-llama-articles-with_embeddings.csv')\n",
|
647 |
+
" # Convert embedding column to an array\n",
|
648 |
+
" df['embedding'] = df['embedding'].apply(lambda x: np.array(eval(x)), 0)"
|
649 |
+
],
|
650 |
+
"metadata": {
|
651 |
+
"colab": {
|
652 |
+
"base_uri": "https://localhost:8080/",
|
653 |
+
"height": 67,
|
654 |
+
"referenced_widgets": [
|
655 |
+
"46a91770024e4802acd3e64e9bc46f32",
|
656 |
+
"613898a418d64df3b18d35083f0bb36d",
|
657 |
+
"9f9427eb6a644166906bb321f13eaf48",
|
658 |
+
"a4a232c5b5e1493897e9acdd25b8efd4",
|
659 |
+
"b2e91819e1c94f28b7bbad66918cb797",
|
660 |
+
"010cbcb0f1364576b15f792f4d11f605",
|
661 |
+
"f51d5da0f39e4c1885357d3d4c9964d9",
|
662 |
+
"c4ceff5437e0470089c161e21488d2a7",
|
663 |
+
"6aafd52b0e3e4e0183b1666ad1e8a448",
|
664 |
+
"80137fc11d4b4e518d8c8957ca5461b1",
|
665 |
+
"c4236d507b354bff830620a8bde32191"
|
666 |
+
]
|
667 |
+
},
|
668 |
+
"id": "qC6aeFr3Rmi2",
|
669 |
+
"outputId": "7f54333f-fcb9-44ce-d4a0-94a9a8d822d5"
|
670 |
+
},
|
671 |
+
"execution_count": 10,
|
672 |
+
"outputs": [
|
673 |
+
{
|
674 |
+
"output_type": "stream",
|
675 |
+
"name": "stdout",
|
676 |
+
"text": [
|
677 |
+
"Generating embeddings...\n"
|
678 |
+
]
|
679 |
},
|
680 |
+
{
|
681 |
+
"output_type": "display_data",
|
682 |
+
"data": {
|
683 |
+
"text/plain": [
|
684 |
+
"0it [00:00, ?it/s]"
|
685 |
+
],
|
686 |
+
"application/vnd.jupyter.widget-view+json": {
|
687 |
+
"version_major": 2,
|
688 |
+
"version_minor": 0,
|
689 |
+
"model_id": "46a91770024e4802acd3e64e9bc46f32"
|
690 |
+
}
|
691 |
+
},
|
692 |
+
"metadata": {}
|
693 |
+
}
|
694 |
+
]
|
695 |
+
},
|
696 |
+
{
|
697 |
+
"cell_type": "code",
|
698 |
+
"source": [
|
699 |
+
"# df.to_csv('mini-llama-articles-with_embeddings.csv')"
|
700 |
+
],
|
701 |
+
"metadata": {
|
702 |
+
"id": "jyX9M_n9o2ve"
|
703 |
+
},
|
704 |
+
"execution_count": 13,
|
705 |
+
"outputs": []
|
706 |
+
},
|
707 |
+
{
|
708 |
+
"cell_type": "markdown",
|
709 |
+
"source": [
|
710 |
+
"# User Question"
|
711 |
+
],
|
712 |
+
"metadata": {
|
713 |
+
"id": "E_qrXwImXrXJ"
|
714 |
+
}
|
715 |
+
},
|
716 |
+
{
|
717 |
+
"cell_type": "code",
|
718 |
+
"source": [
|
719 |
+
"# Define the user question, and convert it to embedding.\n",
|
720 |
+
"QUESTION = \"How many parameters LLaMA2 model has?\"\n",
|
721 |
+
"QUESTION_emb = get_embedding( QUESTION )\n",
|
722 |
+
"\n",
|
723 |
+
"len( QUESTION_emb )"
|
724 |
+
],
|
725 |
+
"metadata": {
|
726 |
+
"colab": {
|
727 |
+
"base_uri": "https://localhost:8080/"
|
|
|
|
|
|
|
728 |
},
|
729 |
+
"id": "xGTa7cqCX97q",
|
730 |
+
"outputId": "6ae836e3-1a65-4447-b732-88758378e9dd"
|
731 |
+
},
|
732 |
+
"execution_count": 15,
|
733 |
+
"outputs": [
|
734 |
+
{
|
735 |
+
"output_type": "execute_result",
|
736 |
+
"data": {
|
737 |
+
"text/plain": [
|
738 |
+
"1536"
|
739 |
+
]
|
740 |
+
},
|
741 |
+
"metadata": {},
|
742 |
+
"execution_count": 15
|
743 |
+
}
|
744 |
+
]
|
745 |
+
},
|
746 |
+
{
|
747 |
+
"cell_type": "markdown",
|
748 |
+
"source": [
|
749 |
+
"# Test Cosine Similarity"
|
750 |
+
],
|
751 |
+
"metadata": {
|
752 |
+
"id": "BXNzNWrJYWhU"
|
753 |
+
}
|
754 |
+
},
|
755 |
+
{
|
756 |
+
"cell_type": "markdown",
|
757 |
+
"source": [
|
758 |
+
"Calculating the similarity of embedding representations can help us to find pieces of text that are close to each other. In the following sample you see how the Cosine Similarity metric can identify which sentence could be a possible answer for the given user question. Obviously, the unrelated answer will score lower."
|
759 |
+
],
|
760 |
+
"metadata": {
|
761 |
+
"id": "Vxaq-FgLIhIj"
|
762 |
+
}
|
763 |
+
},
|
764 |
+
{
|
765 |
+
"cell_type": "code",
|
766 |
+
"source": [
|
767 |
+
"BAD_SOURCE_emb = get_embedding( \"The sky is blue.\" )\n",
|
768 |
+
"GOOD_SOURCE_emb = get_embedding( \"LLaMA2 model has a total of 2B parameters.\" )"
|
769 |
+
],
|
770 |
+
"metadata": {
|
771 |
+
"id": "LqDWcPd4b-ZI"
|
772 |
+
},
|
773 |
+
"execution_count": 16,
|
774 |
+
"outputs": []
|
775 |
+
},
|
776 |
+
{
|
777 |
+
"cell_type": "code",
|
778 |
+
"source": [
|
779 |
+
"from sklearn.metrics.pairwise import cosine_similarity\n",
|
780 |
+
"\n",
|
781 |
+
"# A sample that how a good piece of text can achieve high similarity score compared\n",
|
782 |
+
"# to a completely unrelated text.\n",
|
783 |
+
"print(\"> Bad Response Score:\", cosine_similarity([QUESTION_emb], [BAD_SOURCE_emb]) )\n",
|
784 |
+
"print(\"> Good Response Score:\", cosine_similarity([QUESTION_emb], [GOOD_SOURCE_emb]) )"
|
785 |
+
],
|
786 |
+
"metadata": {
|
787 |
+
"colab": {
|
788 |
+
"base_uri": "https://localhost:8080/"
|
789 |
},
|
790 |
+
"id": "OI00eN86YZKB",
|
791 |
+
"outputId": "0d06c9ea-7de2-48a0-e6d8-3fc6e428914b"
|
792 |
+
},
|
793 |
+
"execution_count": 17,
|
794 |
+
"outputs": [
|
795 |
+
{
|
796 |
+
"output_type": "stream",
|
797 |
+
"name": "stdout",
|
798 |
+
"text": [
|
799 |
+
"> Bad Response Score: [[0.69953438]]\n",
|
800 |
+
"> Good Response Score: [[0.93126147]]\n"
|
801 |
+
]
|
802 |
+
}
|
803 |
+
]
|
804 |
+
},
|
805 |
+
{
|
806 |
+
"cell_type": "markdown",
|
807 |
+
"source": [
|
808 |
+
"# Calculate Cosine Similarities"
|
809 |
+
],
|
810 |
+
"metadata": {
|
811 |
+
"id": "kdJlEtaaJC4I"
|
812 |
+
}
|
813 |
+
},
|
814 |
+
{
|
815 |
+
"cell_type": "code",
|
816 |
+
"source": [
|
817 |
+
"# The similarity between the questions and each part of the essay.\n",
|
818 |
+
"cosine_similarities = cosine_similarity( [QUESTION_emb], df['embedding'].tolist() )\n",
|
819 |
+
"\n",
|
820 |
+
"print( cosine_similarities )"
|
821 |
+
],
|
822 |
+
"metadata": {
|
823 |
+
"colab": {
|
824 |
+
"base_uri": "https://localhost:8080/"
|
825 |
+
},
|
826 |
+
"id": "PNPN7OAXemmH",
|
827 |
+
"outputId": "54beed07-04de-4696-b513-f49a935d6820"
|
828 |
+
},
|
829 |
+
"execution_count": 18,
|
830 |
+
"outputs": [
|
831 |
+
{
|
832 |
+
"output_type": "stream",
|
833 |
+
"name": "stdout",
|
834 |
+
"text": [
|
835 |
+
"[[0.82047387 0.79858187 0.74135248 0.73226232 0.72406104 0.75608299\n",
|
836 |
+
" 0.76808965 0.77621683 0.80498431 0.71399955 0.69822549 0.67532971\n",
|
837 |
+
" 0.72473021 0.73449361 0.69998132 0.73749561 0.68490681 0.75076836\n",
|
838 |
+
" 0.72540663 0.70675593 0.76047822 0.73849418 0.78103858 0.75189435\n",
|
839 |
+
" 0.73619013 0.76962672 0.71289635 0.76996122 0.7827543 0.77959332\n",
|
840 |
+
" 0.82716952 0.77719335 0.80172766 0.76301732 0.78111546 0.75179235\n",
|
841 |
+
" 0.74741505 0.7576328 0.78998865 0.77283347 0.79180172 0.78170323\n",
|
842 |
+
" 0.80264132 0.79923073 0.76146584 0.75199024 0.8341403 0.74460259\n",
|
843 |
+
" 0.76259332 0.73693499 0.78469623 0.81698455 0.8254561 0.77921093\n",
|
844 |
+
" 0.75351863 0.79319721 0.73098248 0.71716001 0.73210099 0.74684248\n",
|
845 |
+
" 0.75760574 0.71070101 0.71507394 0.70847896 0.72395535 0.77801292\n",
|
846 |
+
" 0.75446732 0.75100258 0.7361131 0.78430831 0.74170516 0.71862961\n",
|
847 |
+
" 0.76792911 0.76471996 0.78551313 0.80846857 0.79231644 0.79505895\n",
|
848 |
+
" 0.76910825 0.78341548 0.74952152 0.7849115 0.80407507 0.82641741\n",
|
849 |
+
" 0.77074756 0.7356681 0.77452715 0.76224969 0.79906149 0.84520641\n",
|
850 |
+
" 0.82301383 0.8362749 0.81676624 0.8035085 0.80532594 0.81186134\n",
|
851 |
+
" 0.69082726 0.72587048 0.70070204 0.7155819 0.71758016 0.74945217\n",
|
852 |
+
" 0.72555195 0.7356198 0.73695714 0.75553407 0.77502366 0.71438692\n",
|
853 |
+
" 0.75846916 0.79831901 0.78600515 0.7601161 0.78696534 0.80404804\n",
|
854 |
+
" 0.85209549 0.77037783 0.76985195 0.75062239 0.69339426 0.7108229\n",
|
855 |
+
" 0.72051435 0.75137579 0.71168549 0.72276919 0.77669437 0.7726572\n",
|
856 |
+
" 0.74774188 0.73290677 0.70262553 0.72831247 0.7525444 0.7495277\n",
|
857 |
+
" 0.75188765 0.71491865 0.74460111 0.73599028 0.76314747 0.71318814\n",
|
858 |
+
" 0.70723754 0.73098562 0.72745902 0.76077793 0.72614335 0.72636887\n",
|
859 |
+
" 0.77770561 0.69882456 0.72396024 0.70349095 0.70541201 0.76424393\n",
|
860 |
+
" 0.72785191 0.74371405 0.67802651 0.7353597 0.69916559 0.70605271\n",
|
861 |
+
" 0.71477477 0.71021711 0.77423355 0.70897606 0.74946665 0.70971011\n",
|
862 |
+
" 0.72360056 0.72906996 0.76590153 0.74469991 0.73669136 0.71547661\n",
|
863 |
+
" 0.6958848 0.71459824 0.74863434 0.71430407 0.75165385 0.74221148]]\n"
|
864 |
+
]
|
865 |
+
}
|
866 |
+
]
|
867 |
+
},
|
868 |
+
{
|
869 |
+
"cell_type": "code",
|
870 |
+
"source": [
|
871 |
+
"import numpy as np\n",
|
872 |
+
"\n",
|
873 |
+
"number_of_chunks_to_retrieve = 3\n",
|
874 |
+
"\n",
|
875 |
+
"# Sort the scores\n",
|
876 |
+
"highest_index = np.argmax( cosine_similarities )\n",
|
877 |
+
"\n",
|
878 |
+
"# Pick the N highest scored chunks\n",
|
879 |
+
"indices = np.argsort(cosine_similarities[0])[::-1][:number_of_chunks_to_retrieve]\n",
|
880 |
+
"print( indices )"
|
881 |
+
],
|
882 |
+
"metadata": {
|
883 |
+
"colab": {
|
884 |
+
"base_uri": "https://localhost:8080/"
|
885 |
},
|
886 |
+
"id": "1-XI1_7mhlw4",
|
887 |
+
"outputId": "9598da10-ab61-45e9-e0bb-3e1d7046b657"
|
888 |
+
},
|
889 |
+
"execution_count": 19,
|
890 |
+
"outputs": [
|
891 |
+
{
|
892 |
+
"output_type": "stream",
|
893 |
+
"name": "stdout",
|
894 |
+
"text": [
|
895 |
+
"[114 89 91]\n"
|
896 |
+
]
|
897 |
+
}
|
898 |
+
]
|
899 |
+
},
|
900 |
+
{
|
901 |
+
"cell_type": "code",
|
902 |
+
"source": [
|
903 |
+
"# Look at the highest scored retrieved pieces of text\n",
|
904 |
+
"for idx, item in enumerate( df.chunk[indices] ):\n",
|
905 |
+
" print(f\"> Chunk {idx+1}\")\n",
|
906 |
+
" print(item)\n",
|
907 |
+
" print(\"----\")"
|
908 |
+
],
|
909 |
+
"metadata": {
|
910 |
+
"colab": {
|
911 |
+
"base_uri": "https://localhost:8080/"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
912 |
},
|
913 |
+
"id": "JPmhCb9kfB0w",
|
914 |
+
"outputId": "5089b207-a65a-4856-c065-56b3b9bbba72"
|
915 |
+
},
|
916 |
+
"execution_count": 20,
|
917 |
+
"outputs": [
|
918 |
+
{
|
919 |
+
"output_type": "stream",
|
920 |
+
"name": "stdout",
|
921 |
+
"text": [
|
922 |
+
"> Chunk 1\n",
|
923 |
+
"by Meta that ventures into both the AI and academic spaces. The model aims to help researchers, scientists, and engineers advance their work in exploring AI applications. It will be released under a non-commercial license to prevent misuse, and access will be granted to academic researchers, individuals, and organizations affiliated with the government, civil society, academia, and industry research facilities on a selective case-by-case basis. The sharing of codes and weights allows other researchers to test new approaches in LLMs. The LLaMA models have a range of 7 billion to 65 billion parameters. LLaMA-65B can be compared to DeepMind's Chinchilla and Google's PaLM. Publicly available unlabeled data was used to train these models, and training smaller foundational models require less computing power and resources. LLaMA 65B and 33B have been trained on 1.4 trillion tokens in 20 different languages, and according to the Facebook Artificial Intelligence Research (FAIR) team, the model's performance varies ac\n",
|
924 |
+
"----\n",
|
925 |
+
"> Chunk 2\n",
|
926 |
+
"I. Llama 2: Revolutionizing Commercial Use Unlike its predecessor Llama 1, which was limited to research use, Llama 2 represents a major advancement as an open-source commercial model. Businesses can now integrate Llama 2 into products to create AI-powered applications. Availability on Azure and AWS facilitates fine-tuning and adoption. However, restrictions apply to prevent exploitation. Companies with over 700 million active daily users cannot use Llama 2. Additionally, its output cannot be used to improve other language models. II. Llama 2 Model Flavors Llama 2 is available in four different model sizes: 7 billion, 13 billion, 34 billion, and 70 billion parameters. While 7B, 13B, and 70B have already been released, the 34B model is still awaited. The pretrained variant, trained on a whopping 2 trillion tokens, boasts a context window of 4096 tokens, twice the size of its predecessor Llama 1. Meta also released a Llama 2 fine-tuned model for chat applications that was trained on over 1 million human annota\n",
|
927 |
+
"----\n",
|
928 |
+
"> Chunk 3\n",
|
929 |
+
"vely address a diverse range of questions. This limitation could hinder the model's practical applicability and user experience. Thus, achieving an optimum balance that allows the model to be both helpful and safe is of utmost importance. To strike the right balance between helpfulness and safety, Meta employed two reward models - one for helpfulness and another for safety - to optimize the model's responses. The 34B parameter model has reported higher safety violations than other variants, possibly contributing to the delay in its release. IV. Helpfulness Comparison: Llama 2 Outperforms Competitors Llama 2 emerges as a strong contender in the open-source language model arena, outperforming its competitors in most categories. The 70B parameter model outperforms all other open-source models, while the 7B and 34B models outshine Falcon in all categories and MPT in all categories except coding. Despite being smaller, Llam a2's performance rivals that of Chat GPT 3.5, a significantly larger closed-source model. \n",
|
930 |
+
"----\n"
|
931 |
+
]
|
932 |
+
}
|
933 |
+
]
|
934 |
+
},
|
935 |
+
{
|
936 |
+
"cell_type": "markdown",
|
937 |
+
"source": [
|
938 |
+
"# Augment the Prompt"
|
939 |
+
],
|
940 |
+
"metadata": {
|
941 |
+
"id": "7uvQACqAkHg4"
|
942 |
+
}
|
943 |
+
},
|
944 |
+
{
|
945 |
+
"cell_type": "code",
|
946 |
+
"source": [
|
947 |
+
"# Use the OpenAI API to answer the questions based on the retrieved pieces of text.\n",
|
948 |
+
"try:\n",
|
949 |
+
" # Formulating the system prompt and condition the model to answer only AI-related questions.\n",
|
950 |
+
" system_prompt = (\n",
|
951 |
+
" \"You are an assistant and expert in answering questions from a chunks of content. \"\n",
|
952 |
+
" \"Only answer AI-related question, else say that you cannot answer this question.\"\n",
|
953 |
+
" )\n",
|
954 |
+
"\n",
|
955 |
+
" # Create a user prompt with the user's question\n",
|
956 |
+
" prompt = (\n",
|
957 |
+
" \"Read the following informations that might contain the context you require to answer the question. You can use the informations starting from the <START_OF_CONTEXT> tag and end with the <END_OF_CONTEXT> tag. Here is the content:\\n\\n<START_OF_CONTEXT>\\n{}\\n<END_OF_CONTEXT>\\n\\n\"\n",
|
958 |
+
" \"Please provide an informative and accurate answer to the following question based on the avaiable context. Be concise and take your time. \\nQuestion: {}\\nAnswer:\"\n",
|
959 |
+
" )\n",
|
960 |
+
" # Add the retrieved pieces of text to the prompt.\n",
|
961 |
+
" prompt = prompt.format( \"\".join( df.chunk[indices] ), QUESTION )\n",
|
962 |
+
"\n",
|
963 |
+
" # Call the OpenAI API\n",
|
964 |
+
" response = client.chat.completions.create(\n",
|
965 |
+
" model='gpt-3.5-turbo-16k',\n",
|
966 |
+
" temperature=0.0,\n",
|
967 |
+
" messages=[\n",
|
968 |
+
" {\"role\": \"system\", \"content\": system_prompt},\n",
|
969 |
+
" {\"role\": \"user\", \"content\": prompt}\n",
|
970 |
+
" ]\n",
|
971 |
+
" )\n",
|
972 |
+
"\n",
|
973 |
+
" # Return the AI's response\n",
|
974 |
+
" res = response.choices[0].message.content.strip()\n",
|
975 |
+
"\n",
|
976 |
+
"except Exception as e:\n",
|
977 |
+
" print( f\"An error occurred: {e}\" )"
|
978 |
+
],
|
979 |
+
"metadata": {
|
980 |
+
"id": "MXRdzta5kJ3V"
|
981 |
+
},
|
982 |
+
"execution_count": 21,
|
983 |
+
"outputs": []
|
984 |
+
},
|
985 |
+
{
|
986 |
+
"cell_type": "code",
|
987 |
+
"source": [
|
988 |
+
"print( res )"
|
989 |
+
],
|
990 |
+
"metadata": {
|
991 |
+
"colab": {
|
992 |
+
"base_uri": "https://localhost:8080/"
|
993 |
+
},
|
994 |
+
"id": "9tBvJ8oMucha",
|
995 |
+
"outputId": "418c0220-c2ee-43cf-a9bc-0ea755f7a04e"
|
996 |
+
},
|
997 |
+
"execution_count": 22,
|
998 |
+
"outputs": [
|
999 |
+
{
|
1000 |
+
"output_type": "stream",
|
1001 |
+
"name": "stdout",
|
1002 |
+
"text": [
|
1003 |
+
"The LLaMA2 model has four different sizes: 7 billion, 13 billion, 34 billion, and 70 billion parameters.\n"
|
1004 |
+
]
|
1005 |
}
|
1006 |
+
]
|
1007 |
+
},
|
1008 |
+
{
|
1009 |
+
"cell_type": "markdown",
|
1010 |
+
"source": [
|
1011 |
+
"# Without Augmentation"
|
1012 |
+
],
|
1013 |
+
"metadata": {
|
1014 |
+
"id": "pW-BNCAC2JzE"
|
1015 |
}
|
1016 |
+
},
|
1017 |
+
{
|
1018 |
+
"cell_type": "markdown",
|
1019 |
+
"source": [
|
1020 |
+
"Test the OpenAI API to answer the same question without the addition of retrieved documents. Basically, the LLM will use its knowledge to answer the question."
|
1021 |
+
],
|
1022 |
+
"metadata": {
|
1023 |
+
"id": "tr5zXEGIMwJu"
|
1024 |
+
}
|
1025 |
+
},
|
1026 |
+
{
|
1027 |
+
"cell_type": "code",
|
1028 |
+
"source": [
|
1029 |
+
"# Formulating the system prompt\n",
|
1030 |
+
"system_prompt = (\n",
|
1031 |
+
" \"You are an assistant and expert in answering questions.\"\n",
|
1032 |
+
")\n",
|
1033 |
+
"\n",
|
1034 |
+
"# Combining the system prompt with the user's question\n",
|
1035 |
+
"prompt = (\n",
|
1036 |
+
" \"Be concise and take your time to answer the following question. \\nQuestion: {}\\nAnswer:\"\n",
|
1037 |
+
")\n",
|
1038 |
+
"prompt = prompt.format( QUESTION )\n",
|
1039 |
+
"\n",
|
1040 |
+
"# Call the OpenAI API\n",
|
1041 |
+
"response = client.chat.completions.create(\n",
|
1042 |
+
" model='gpt-3.5-turbo-16k',\n",
|
1043 |
+
" temperature=.9,\n",
|
1044 |
+
" messages=[\n",
|
1045 |
+
" {\"role\": \"system\", \"content\": system_prompt},\n",
|
1046 |
+
" {\"role\": \"user\", \"content\": prompt}\n",
|
1047 |
+
" ]\n",
|
1048 |
+
")\n",
|
1049 |
+
"\n",
|
1050 |
+
"# Return the AI's response\n",
|
1051 |
+
"res = response.choices[0].message.content.strip()"
|
1052 |
+
],
|
1053 |
+
"metadata": {
|
1054 |
+
"id": "RuyXjzZyuecE"
|
1055 |
+
},
|
1056 |
+
"execution_count": null,
|
1057 |
+
"outputs": []
|
1058 |
+
},
|
1059 |
+
{
|
1060 |
+
"cell_type": "code",
|
1061 |
+
"source": [
|
1062 |
+
"print( res )"
|
1063 |
+
],
|
1064 |
+
"metadata": {
|
1065 |
+
"colab": {
|
1066 |
+
"base_uri": "https://localhost:8080/"
|
1067 |
+
},
|
1068 |
+
"id": "YAy34tPTzGbh",
|
1069 |
+
"outputId": "54041329-dd5f-4cdd-db38-f1440ae77181"
|
1070 |
+
},
|
1071 |
+
"execution_count": null,
|
1072 |
+
"outputs": [
|
1073 |
+
{
|
1074 |
+
"output_type": "stream",
|
1075 |
+
"name": "stdout",
|
1076 |
+
"text": [
|
1077 |
+
"The LLaMA2 model has a total of [insert number] parameters.\n"
|
1078 |
+
]
|
1079 |
+
}
|
1080 |
+
]
|
1081 |
}
|
1082 |
+
]
|
1083 |
+
}
|
|
|
|