Omar Solano
commited on
Commit
Β·
9e9355f
1
Parent(s):
006c06a
switch from vertex to gemini api
Browse files
notebooks/03-RAG_with_LlamaIndex.ipynb
CHANGED
@@ -1,5 +1,12 @@
|
|
1 |
{
|
2 |
"cells": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
{
|
4 |
"cell_type": "markdown",
|
5 |
"metadata": {
|
@@ -19,55 +26,14 @@
|
|
19 |
"id": "BeuFJKlj9jKz",
|
20 |
"outputId": "6419987a-aa8c-49f8-de20-42aa9d7528c3"
|
21 |
},
|
22 |
-
"outputs": [
|
23 |
-
{
|
24 |
-
"name": "stdout",
|
25 |
-
"output_type": "stream",
|
26 |
-
"text": [
|
27 |
-
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m327.4/327.4 kB\u001b[0m \u001b[31m3.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
28 |
-
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m15.4/15.4 MB\u001b[0m \u001b[31m24.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
29 |
-
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m2.0/2.0 MB\u001b[0m \u001b[31m19.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
30 |
-
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m3.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
31 |
-
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m38.3/38.3 MB\u001b[0m \u001b[31m10.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
32 |
-
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m130.8/130.8 kB\u001b[0m \u001b[31m8.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
33 |
-
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m1.1/1.1 MB\u001b[0m \u001b[31m41.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
34 |
-
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m8.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
35 |
-
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m4.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
36 |
-
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m141.9/141.9 kB\u001b[0m \u001b[31m12.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
37 |
-
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m290.4/290.4 kB\u001b[0m \u001b[31m21.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
38 |
-
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m49.2/49.2 kB\u001b[0m \u001b[31m212.8 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
39 |
-
"\u001b[?25h\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
|
40 |
-
"cudf-cu12 24.4.1 requires pyarrow<15.0.0a0,>=14.0.1, but you have pyarrow 15.0.2 which is incompatible.\u001b[0m\u001b[31m\n",
|
41 |
-
"\u001b[0m"
|
42 |
-
]
|
43 |
-
}
|
44 |
-
],
|
45 |
-
"source": [
|
46 |
-
"!pip install -q llama-index==0.10.49 openai==1.35.3 llama-index-llms-vertex==0.2.0 google-cloud-aiplatform==1.56.0"
|
47 |
-
]
|
48 |
-
},
|
49 |
-
{
|
50 |
-
"cell_type": "code",
|
51 |
-
"execution_count": 2,
|
52 |
-
"metadata": {
|
53 |
-
"id": "XuzgSNqcABpV"
|
54 |
-
},
|
55 |
"outputs": [],
|
56 |
"source": [
|
57 |
-
"
|
58 |
-
"from google.colab import auth\n",
|
59 |
-
"\n",
|
60 |
-
"# Authenticate\n",
|
61 |
-
"auth.authenticate_user()\n",
|
62 |
-
"\n",
|
63 |
-
"# Initialize Vertex AI by setting your project id\n",
|
64 |
-
"PROJECT_ID = \"[your-project-id]\"\n",
|
65 |
-
"aiplatform.init(project=PROJECT_ID, location='us-central1')"
|
66 |
]
|
67 |
},
|
68 |
{
|
69 |
"cell_type": "code",
|
70 |
-
"execution_count":
|
71 |
"metadata": {
|
72 |
"id": "CWholrWlt2OQ"
|
73 |
},
|
@@ -75,8 +41,11 @@
|
|
75 |
"source": [
|
76 |
"import os\n",
|
77 |
"\n",
|
78 |
-
"# Set
|
79 |
-
"os.environ[\"OPENAI_API_KEY\"] = \"<YOUR_OPENAI_KEY>\""
|
|
|
|
|
|
|
80 |
]
|
81 |
},
|
82 |
{
|
@@ -108,7 +77,7 @@
|
|
108 |
},
|
109 |
{
|
110 |
"cell_type": "code",
|
111 |
-
"execution_count":
|
112 |
"metadata": {
|
113 |
"colab": {
|
114 |
"base_uri": "https://localhost:8080/"
|
@@ -123,7 +92,7 @@
|
|
123 |
"text": [
|
124 |
" % Total % Received % Xferd Average Speed Time Time Time Current\n",
|
125 |
" Dload Upload Total Spent Left Speed\n",
|
126 |
-
"100 169k 100 169k 0 0
|
127 |
]
|
128 |
}
|
129 |
],
|
@@ -142,7 +111,7 @@
|
|
142 |
},
|
143 |
{
|
144 |
"cell_type": "code",
|
145 |
-
"execution_count":
|
146 |
"metadata": {
|
147 |
"colab": {
|
148 |
"base_uri": "https://localhost:8080/"
|
@@ -187,7 +156,7 @@
|
|
187 |
},
|
188 |
{
|
189 |
"cell_type": "code",
|
190 |
-
"execution_count":
|
191 |
"metadata": {
|
192 |
"id": "iXrr5-tnEfm9"
|
193 |
},
|
@@ -201,7 +170,7 @@
|
|
201 |
},
|
202 |
{
|
203 |
"cell_type": "code",
|
204 |
-
"execution_count":
|
205 |
"metadata": {
|
206 |
"colab": {
|
207 |
"base_uri": "https://localhost:8080/",
|
@@ -236,32 +205,14 @@
|
|
236 |
},
|
237 |
"outputs": [
|
238 |
{
|
239 |
-
"
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
"
|
246 |
-
|
247 |
-
]
|
248 |
-
},
|
249 |
-
"metadata": {},
|
250 |
-
"output_type": "display_data"
|
251 |
-
},
|
252 |
-
{
|
253 |
-
"data": {
|
254 |
-
"application/vnd.jupyter.widget-view+json": {
|
255 |
-
"model_id": "27fd17bf0eaa49868321cf2d31a5a0a1",
|
256 |
-
"version_major": 2,
|
257 |
-
"version_minor": 0
|
258 |
-
},
|
259 |
-
"text/plain": [
|
260 |
-
"Generating embeddings: 0%| | 0/56 [00:00<?, ?it/s]"
|
261 |
-
]
|
262 |
-
},
|
263 |
-
"metadata": {},
|
264 |
-
"output_type": "display_data"
|
265 |
}
|
266 |
],
|
267 |
"source": [
|
@@ -290,7 +241,7 @@
|
|
290 |
},
|
291 |
{
|
292 |
"cell_type": "code",
|
293 |
-
"execution_count":
|
294 |
"metadata": {
|
295 |
"id": "bUaNH97dEfh9"
|
296 |
},
|
@@ -299,16 +250,16 @@
|
|
299 |
"# Define a query engine that is responsible for retrieving related pieces of text,\n",
|
300 |
"# and using a LLM to formulate the final answer.\n",
|
301 |
"\n",
|
302 |
-
"from llama_index.llms.
|
303 |
"\n",
|
304 |
-
"llm =
|
305 |
"\n",
|
306 |
"query_engine = index.as_query_engine(llm=llm)"
|
307 |
]
|
308 |
},
|
309 |
{
|
310 |
"cell_type": "code",
|
311 |
-
"execution_count":
|
312 |
"metadata": {
|
313 |
"colab": {
|
314 |
"base_uri": "https://localhost:8080/"
|
@@ -321,7 +272,7 @@
|
|
321 |
"name": "stdout",
|
322 |
"output_type": "stream",
|
323 |
"text": [
|
324 |
-
"LLaMA 2
|
325 |
"\n"
|
326 |
]
|
327 |
}
|
@@ -335,7 +286,7 @@
|
|
335 |
},
|
336 |
{
|
337 |
"cell_type": "code",
|
338 |
-
"execution_count":
|
339 |
"metadata": {
|
340 |
"colab": {
|
341 |
"base_uri": "https://localhost:8080/"
|
@@ -348,7 +299,7 @@
|
|
348 |
"name": "stdout",
|
349 |
"output_type": "stream",
|
350 |
"text": [
|
351 |
-
"The context does not provide
|
352 |
"\n"
|
353 |
]
|
354 |
}
|
@@ -359,15 +310,6 @@
|
|
359 |
")\n",
|
360 |
"print(response)"
|
361 |
]
|
362 |
-
},
|
363 |
-
{
|
364 |
-
"cell_type": "code",
|
365 |
-
"execution_count": null,
|
366 |
-
"metadata": {
|
367 |
-
"id": "oZt_sG86RwZ3"
|
368 |
-
},
|
369 |
-
"outputs": [],
|
370 |
-
"source": []
|
371 |
}
|
372 |
],
|
373 |
"metadata": {
|
@@ -379,7 +321,15 @@
|
|
379 |
"name": "python3"
|
380 |
},
|
381 |
"language_info": {
|
|
|
|
|
|
|
|
|
|
|
|
|
382 |
"name": "python",
|
|
|
|
|
383 |
"version": "3.12.3"
|
384 |
},
|
385 |
"widgets": {
|
|
|
1 |
{
|
2 |
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {},
|
6 |
+
"source": [
|
7 |
+
"<a href=\"https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/03-RAG_with_LlamaIndex.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
|
8 |
+
]
|
9 |
+
},
|
10 |
{
|
11 |
"cell_type": "markdown",
|
12 |
"metadata": {
|
|
|
26 |
"id": "BeuFJKlj9jKz",
|
27 |
"outputId": "6419987a-aa8c-49f8-de20-42aa9d7528c3"
|
28 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
"outputs": [],
|
30 |
"source": [
|
31 |
+
"!pip install -q llama-index==0.10.49 llama-index-llms-gemini==0.1.11 openai==1.35.3 google-generativeai==0.5.4"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
]
|
33 |
},
|
34 |
{
|
35 |
"cell_type": "code",
|
36 |
+
"execution_count": 2,
|
37 |
"metadata": {
|
38 |
"id": "CWholrWlt2OQ"
|
39 |
},
|
|
|
41 |
"source": [
|
42 |
"import os\n",
|
43 |
"\n",
|
44 |
+
"# Set your \"OPENAI_API_KEY\" environment variable\n",
|
45 |
+
"os.environ[\"OPENAI_API_KEY\"] = \"<YOUR_OPENAI_KEY>\"\n",
|
46 |
+
"\n",
|
47 |
+
"# Get your GOOGLE_API_KEY from https://aistudio.google.com/app/apikey\n",
|
48 |
+
"os.environ[\"GOOGLE_API_KEY\"] = \"<YOUR_GOOGLE_KEY>\""
|
49 |
]
|
50 |
},
|
51 |
{
|
|
|
77 |
},
|
78 |
{
|
79 |
"cell_type": "code",
|
80 |
+
"execution_count": 3,
|
81 |
"metadata": {
|
82 |
"colab": {
|
83 |
"base_uri": "https://localhost:8080/"
|
|
|
92 |
"text": [
|
93 |
" % Total % Received % Xferd Average Speed Time Time Time Current\n",
|
94 |
" Dload Upload Total Spent Left Speed\n",
|
95 |
+
"100 169k 100 169k 0 0 1817k 0 --:--:-- --:--:-- --:--:-- 1823k\n"
|
96 |
]
|
97 |
}
|
98 |
],
|
|
|
111 |
},
|
112 |
{
|
113 |
"cell_type": "code",
|
114 |
+
"execution_count": 4,
|
115 |
"metadata": {
|
116 |
"colab": {
|
117 |
"base_uri": "https://localhost:8080/"
|
|
|
156 |
},
|
157 |
{
|
158 |
"cell_type": "code",
|
159 |
+
"execution_count": 5,
|
160 |
"metadata": {
|
161 |
"id": "iXrr5-tnEfm9"
|
162 |
},
|
|
|
170 |
},
|
171 |
{
|
172 |
"cell_type": "code",
|
173 |
+
"execution_count": 6,
|
174 |
"metadata": {
|
175 |
"colab": {
|
176 |
"base_uri": "https://localhost:8080/",
|
|
|
205 |
},
|
206 |
"outputs": [
|
207 |
{
|
208 |
+
"name": "stderr",
|
209 |
+
"output_type": "stream",
|
210 |
+
"text": [
|
211 |
+
"/Users/omar/Documents/ai_repos/ai-tutor-rag-system/env/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
212 |
+
" from .autonotebook import tqdm as notebook_tqdm\n",
|
213 |
+
"Parsing nodes: 100%|ββββββββββ| 14/14 [00:00<00:00, 252.38it/s]\n",
|
214 |
+
"Generating embeddings: 100%|ββββββββββ| 56/56 [00:01<00:00, 41.05it/s]\n"
|
215 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
}
|
217 |
],
|
218 |
"source": [
|
|
|
241 |
},
|
242 |
{
|
243 |
"cell_type": "code",
|
244 |
+
"execution_count": 7,
|
245 |
"metadata": {
|
246 |
"id": "bUaNH97dEfh9"
|
247 |
},
|
|
|
250 |
"# Define a query engine that is responsible for retrieving related pieces of text,\n",
|
251 |
"# and using a LLM to formulate the final answer.\n",
|
252 |
"\n",
|
253 |
+
"from llama_index.llms.gemini import Gemini\n",
|
254 |
"\n",
|
255 |
+
"llm = Gemini(model=\"models/gemini-1.5-flash\", temperature=1, max_tokens=512)\n",
|
256 |
"\n",
|
257 |
"query_engine = index.as_query_engine(llm=llm)"
|
258 |
]
|
259 |
},
|
260 |
{
|
261 |
"cell_type": "code",
|
262 |
+
"execution_count": 8,
|
263 |
"metadata": {
|
264 |
"colab": {
|
265 |
"base_uri": "https://localhost:8080/"
|
|
|
272 |
"name": "stdout",
|
273 |
"output_type": "stream",
|
274 |
"text": [
|
275 |
+
"LLaMA 2 is available in four different sizes: 7 billion, 13 billion, 34 billion, and 70 billion parameters. \n",
|
276 |
"\n"
|
277 |
]
|
278 |
}
|
|
|
286 |
},
|
287 |
{
|
288 |
"cell_type": "code",
|
289 |
+
"execution_count": 9,
|
290 |
"metadata": {
|
291 |
"colab": {
|
292 |
"base_uri": "https://localhost:8080/"
|
|
|
299 |
"name": "stdout",
|
300 |
"output_type": "stream",
|
301 |
"text": [
|
302 |
+
"The context does not provide information about the release date of Llama 3. \n",
|
303 |
"\n"
|
304 |
]
|
305 |
}
|
|
|
310 |
")\n",
|
311 |
"print(response)"
|
312 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
313 |
}
|
314 |
],
|
315 |
"metadata": {
|
|
|
321 |
"name": "python3"
|
322 |
},
|
323 |
"language_info": {
|
324 |
+
"codemirror_mode": {
|
325 |
+
"name": "ipython",
|
326 |
+
"version": 3
|
327 |
+
},
|
328 |
+
"file_extension": ".py",
|
329 |
+
"mimetype": "text/x-python",
|
330 |
"name": "python",
|
331 |
+
"nbconvert_exporter": "python",
|
332 |
+
"pygments_lexer": "ipython3",
|
333 |
"version": "3.12.3"
|
334 |
},
|
335 |
"widgets": {
|