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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"id": "e5270bcc",
|
6 |
+
"metadata": {},
|
7 |
+
"source": [
|
8 |
+
"# Final project of Agents course: An Agent that response the GAIA benchmark\n",
|
9 |
+
"\n",
|
10 |
+
"In this notebook, **we're going to read the GAIA questions from metadata and we'll store in a chroma database to use as retriever**.\n",
|
11 |
+
"\n",
|
12 |
+
"This notebook is part of the <a href=\"https://www.hf.co/learn/agents-course\">Hugging Face Agents Course</a>, a free course from beginner to expert, where you learn to build Agents.\n",
|
13 |
+
"\n",
|
14 |
+
"## What we'll do\n",
|
15 |
+
"\n",
|
16 |
+
"In this notebook, we'll do:\n",
|
17 |
+
"\n",
|
18 |
+
"1. Reading json metadata with GAIA questions\n",
|
19 |
+
"2. Creating chroma vector store\n",
|
20 |
+
"3. Querying the database and converting to a retriever to search using\n",
|
21 |
+
"4. Loading the database from directory\n",
|
22 |
+
"5. Creating supabase vector store\n"
|
23 |
+
]
|
24 |
+
},
|
25 |
+
{
|
26 |
+
"cell_type": "code",
|
27 |
+
"execution_count": 11,
|
28 |
+
"id": "1e1d8e79",
|
29 |
+
"metadata": {},
|
30 |
+
"outputs": [
|
31 |
+
{
|
32 |
+
"data": {
|
33 |
+
"text/plain": [
|
34 |
+
"True"
|
35 |
+
]
|
36 |
+
},
|
37 |
+
"execution_count": 11,
|
38 |
+
"metadata": {},
|
39 |
+
"output_type": "execute_result"
|
40 |
+
}
|
41 |
+
],
|
42 |
+
"source": [
|
43 |
+
"import os\n",
|
44 |
+
"import json\n",
|
45 |
+
"from dotenv import load_dotenv\n",
|
46 |
+
"from langchain_huggingface import HuggingFaceEmbeddings\n",
|
47 |
+
"from langchain_chroma import Chroma\n",
|
48 |
+
"import random\n",
|
49 |
+
"\n",
|
50 |
+
"# from langchain.schema import Document\n",
|
51 |
+
"from langchain_core.documents import Document\n",
|
52 |
+
"from uuid import uuid4\n",
|
53 |
+
"\n",
|
54 |
+
"from langchain_community.vectorstores import SupabaseVectorStore\n",
|
55 |
+
"from supabase.client import Client, create_client\n",
|
56 |
+
"\n",
|
57 |
+
"load_dotenv()"
|
58 |
+
]
|
59 |
+
},
|
60 |
+
{
|
61 |
+
"cell_type": "code",
|
62 |
+
"execution_count": 7,
|
63 |
+
"id": "bf2a78a5",
|
64 |
+
"metadata": {},
|
65 |
+
"outputs": [],
|
66 |
+
"source": [
|
67 |
+
"os.chdir(os.path.abspath(\"..\"))"
|
68 |
+
]
|
69 |
+
},
|
70 |
+
{
|
71 |
+
"cell_type": "markdown",
|
72 |
+
"id": "c0513724",
|
73 |
+
"metadata": {},
|
74 |
+
"source": [
|
75 |
+
"<div style=\"background-color:lightblue; color:black; font-weight:bold\">\n",
|
76 |
+
"\n",
|
77 |
+
"## 1. Reading json metadata\n",
|
78 |
+
"\n",
|
79 |
+
"</div>\n"
|
80 |
+
]
|
81 |
+
},
|
82 |
+
{
|
83 |
+
"cell_type": "code",
|
84 |
+
"execution_count": 8,
|
85 |
+
"id": "c8f343d6",
|
86 |
+
"metadata": {},
|
87 |
+
"outputs": [],
|
88 |
+
"source": [
|
89 |
+
"# Load the metadata.jsonl file\n",
|
90 |
+
"with open(\"data/metadata.jsonl\", \"r\") as jsonl_file:\n",
|
91 |
+
" json_list = list(jsonl_file)\n",
|
92 |
+
"\n",
|
93 |
+
"json_QA = []\n",
|
94 |
+
"for json_str in json_list:\n",
|
95 |
+
" json_data = json.loads(json_str)\n",
|
96 |
+
" json_QA.append(json_data)"
|
97 |
+
]
|
98 |
+
},
|
99 |
+
{
|
100 |
+
"cell_type": "code",
|
101 |
+
"execution_count": 9,
|
102 |
+
"id": "e7191ac5",
|
103 |
+
"metadata": {},
|
104 |
+
"outputs": [
|
105 |
+
{
|
106 |
+
"name": "stdout",
|
107 |
+
"output_type": "stream",
|
108 |
+
"text": [
|
109 |
+
"==================================================\n",
|
110 |
+
"Task ID: 56137764-b4e0-45b8-9c52-1866420c3df5\n",
|
111 |
+
"Question: Which contributor to the version of OpenCV where support was added for the Mask-RCNN model has the same name as a former Chinese head of government when the names are transliterated to the Latin alphabet?\n",
|
112 |
+
"Level: 2\n",
|
113 |
+
"Final Answer: Li Peng\n",
|
114 |
+
"Annotator Metadata: \n",
|
115 |
+
" ├── Steps: \n",
|
116 |
+
" │ ├── 1. Use search engine to search for \"OpenCV change log\".\n",
|
117 |
+
" │ ├── 2. Open the top result from GitHub and search the page for \"Mask-RCNN\".\n",
|
118 |
+
" │ ├── 3. Observe that support for Mask-RCNN model was added in OpenCV version 4.0.0.\n",
|
119 |
+
" │ ├── 4. Expand the two lists of contributors for version 4.0.0.\n",
|
120 |
+
" │ ├── 5. Go to the Wikipedia page for head of government. \n",
|
121 |
+
" │ ├── 6. Scan through and note that for China, the head of government is the premier.\n",
|
122 |
+
" │ ├── 7. Go to the Wikipedia page for premier of the People's Republic of China.\n",
|
123 |
+
" │ ├── 8. Go to the linked page for List of premiers of the People's Republic of China.\n",
|
124 |
+
" │ ├── 9. Compare the list of OpenCV version 4.0.0 contributors' names and the list of premiers of China to find that Li Peng is present in both lists.\n",
|
125 |
+
" ├── Number of steps: 9\n",
|
126 |
+
" ├── How long did this take?: 5 minutes\n",
|
127 |
+
" ├── Tools:\n",
|
128 |
+
" │ ├── 1. Web browser\n",
|
129 |
+
" │ ├── 2. Search engine\n",
|
130 |
+
" └── Number of tools: 2\n",
|
131 |
+
"==================================================\n"
|
132 |
+
]
|
133 |
+
}
|
134 |
+
],
|
135 |
+
"source": [
|
136 |
+
"# randomly select 3 samples\n",
|
137 |
+
"# {\"task_id\": \"c61d22de-5f6c-4958-a7f6-5e9707bd3466\", \"Question\": \"A paper about AI regulation that was originally submitted to arXiv.org in June 2022 shows a figure with three axes, where each axis has a label word at both ends. Which of these words is used to describe a type of society in a Physics and Society article submitted to arXiv.org on August 11, 2016?\", \"Level\": 2, \"Final answer\": \"egalitarian\", \"file_name\": \"\", \"Annotator Metadata\": {\"Steps\": \"1. Go to arxiv.org and navigate to the Advanced Search page.\\n2. Enter \\\"AI regulation\\\" in the search box and select \\\"All fields\\\" from the dropdown.\\n3. Enter 2022-06-01 and 2022-07-01 into the date inputs, select \\\"Submission date (original)\\\", and submit the search.\\n4. Go through the search results to find the article that has a figure with three axes and labels on each end of the axes, titled \\\"Fairness in Agreement With European Values: An Interdisciplinary Perspective on AI Regulation\\\".\\n5. Note the six words used as labels: deontological, egalitarian, localized, standardized, utilitarian, and consequential.\\n6. Go back to arxiv.org\\n7. Find \\\"Physics and Society\\\" and go to the page for the \\\"Physics and Society\\\" category.\\n8. Note that the tag for this category is \\\"physics.soc-ph\\\".\\n9. Go to the Advanced Search page.\\n10. Enter \\\"physics.soc-ph\\\" in the search box and select \\\"All fields\\\" from the dropdown.\\n11. Enter 2016-08-11 and 2016-08-12 into the date inputs, select \\\"Submission date (original)\\\", and submit the search.\\n12. Search for instances of the six words in the results to find the paper titled \\\"Phase transition from egalitarian to hierarchical societies driven by competition between cognitive and social constraints\\\", indicating that \\\"egalitarian\\\" is the correct answer.\", \"Number of steps\": \"12\", \"How long did this take?\": \"8 minutes\", \"Tools\": \"1. Web browser\\n2. Image recognition tools (to identify and parse a figure with three axes)\", \"Number of tools\": \"2\"}}\n",
|
138 |
+
"# random.seed(42)\n",
|
139 |
+
"random_samples = random.sample(json_QA, 1)\n",
|
140 |
+
"for sample in random_samples:\n",
|
141 |
+
" print(\"=\" * 50)\n",
|
142 |
+
" print(f\"Task ID: {sample['task_id']}\")\n",
|
143 |
+
" print(f\"Question: {sample['Question']}\")\n",
|
144 |
+
" print(f\"Level: {sample['Level']}\")\n",
|
145 |
+
" print(f\"Final Answer: {sample['Final answer']}\")\n",
|
146 |
+
" print(\"Annotator Metadata: \")\n",
|
147 |
+
" print(\" ├── Steps: \")\n",
|
148 |
+
" for step in sample[\"Annotator Metadata\"][\"Steps\"].split(\"\\n\"):\n",
|
149 |
+
" print(f\" │ ├── {step}\")\n",
|
150 |
+
" print(f\" ├── Number of steps: {sample['Annotator Metadata']['Number of steps']}\")\n",
|
151 |
+
" print(\n",
|
152 |
+
" f\" ├── How long did this take?: {sample['Annotator Metadata']['How long did this take?']}\"\n",
|
153 |
+
" )\n",
|
154 |
+
" print(\" ├── Tools:\")\n",
|
155 |
+
" for tool in sample[\"Annotator Metadata\"][\"Tools\"].split(\"\\n\"):\n",
|
156 |
+
" print(f\" │ ├── {tool}\")\n",
|
157 |
+
" print(f\" └── Number of tools: {sample['Annotator Metadata']['Number of tools']}\")\n",
|
158 |
+
"print(\"=\" * 50)"
|
159 |
+
]
|
160 |
+
},
|
161 |
+
{
|
162 |
+
"cell_type": "markdown",
|
163 |
+
"id": "81c30287",
|
164 |
+
"metadata": {},
|
165 |
+
"source": [
|
166 |
+
"<div style=\"background-color:lightblue; color:black; font-weight:bold\">\n",
|
167 |
+
"\n",
|
168 |
+
"## 2. Creating chroma vector store\n",
|
169 |
+
"\n",
|
170 |
+
"</div>\n"
|
171 |
+
]
|
172 |
+
},
|
173 |
+
{
|
174 |
+
"cell_type": "code",
|
175 |
+
"execution_count": null,
|
176 |
+
"id": "046462f4",
|
177 |
+
"metadata": {},
|
178 |
+
"outputs": [
|
179 |
+
{
|
180 |
+
"data": {
|
181 |
+
"application/vnd.jupyter.widget-view+json": {
|
182 |
+
"model_id": "87ec1aea91cf4cf799c882f649ef7e8c",
|
183 |
+
"version_major": 2,
|
184 |
+
"version_minor": 0
|
185 |
+
},
|
186 |
+
"text/plain": [
|
187 |
+
"modules.json: 0%| | 0.00/349 [00:00<?, ?B/s]"
|
188 |
+
]
|
189 |
+
},
|
190 |
+
"metadata": {},
|
191 |
+
"output_type": "display_data"
|
192 |
+
},
|
193 |
+
{
|
194 |
+
"name": "stderr",
|
195 |
+
"output_type": "stream",
|
196 |
+
"text": [
|
197 |
+
"c:\\Users\\ee5011\\Anaconda3\\envs\\llm-huggingface-agents-course-env\\Lib\\site-packages\\huggingface_hub\\file_download.py:143: UserWarning: `huggingface_hub` cache-system uses symlinks by default to efficiently store duplicated files but your machine does not support them in C:\\Users\\ee5011\\.cache\\huggingface\\hub\\models--sentence-transformers--all-mpnet-base-v2. Caching files will still work but in a degraded version that might require more space on your disk. This warning can be disabled by setting the `HF_HUB_DISABLE_SYMLINKS_WARNING` environment variable. For more details, see https://huggingface.co/docs/huggingface_hub/how-to-cache#limitations.\n",
|
198 |
+
"To support symlinks on Windows, you either need to activate Developer Mode or to run Python as an administrator. In order to activate developer mode, see this article: https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development\n",
|
199 |
+
" warnings.warn(message)\n"
|
200 |
+
]
|
201 |
+
},
|
202 |
+
{
|
203 |
+
"data": {
|
204 |
+
"application/vnd.jupyter.widget-view+json": {
|
205 |
+
"model_id": "e0df64b800c14d0eaca2214438eaf4e0",
|
206 |
+
"version_major": 2,
|
207 |
+
"version_minor": 0
|
208 |
+
},
|
209 |
+
"text/plain": [
|
210 |
+
"config_sentence_transformers.json: 0%| | 0.00/116 [00:00<?, ?B/s]"
|
211 |
+
]
|
212 |
+
},
|
213 |
+
"metadata": {},
|
214 |
+
"output_type": "display_data"
|
215 |
+
},
|
216 |
+
{
|
217 |
+
"data": {
|
218 |
+
"application/vnd.jupyter.widget-view+json": {
|
219 |
+
"model_id": "fe6e89ddbb5540b4869935bda0ededc0",
|
220 |
+
"version_major": 2,
|
221 |
+
"version_minor": 0
|
222 |
+
},
|
223 |
+
"text/plain": [
|
224 |
+
"README.md: 0%| | 0.00/10.4k [00:00<?, ?B/s]"
|
225 |
+
]
|
226 |
+
},
|
227 |
+
"metadata": {},
|
228 |
+
"output_type": "display_data"
|
229 |
+
},
|
230 |
+
{
|
231 |
+
"data": {
|
232 |
+
"application/vnd.jupyter.widget-view+json": {
|
233 |
+
"model_id": "6ac82a744f0b4d33a64b21346af272d2",
|
234 |
+
"version_major": 2,
|
235 |
+
"version_minor": 0
|
236 |
+
},
|
237 |
+
"text/plain": [
|
238 |
+
"sentence_bert_config.json: 0%| | 0.00/53.0 [00:00<?, ?B/s]"
|
239 |
+
]
|
240 |
+
},
|
241 |
+
"metadata": {},
|
242 |
+
"output_type": "display_data"
|
243 |
+
},
|
244 |
+
{
|
245 |
+
"data": {
|
246 |
+
"application/vnd.jupyter.widget-view+json": {
|
247 |
+
"model_id": "877342a7a662471cb133598c298f6304",
|
248 |
+
"version_major": 2,
|
249 |
+
"version_minor": 0
|
250 |
+
},
|
251 |
+
"text/plain": [
|
252 |
+
"config.json: 0%| | 0.00/571 [00:00<?, ?B/s]"
|
253 |
+
]
|
254 |
+
},
|
255 |
+
"metadata": {},
|
256 |
+
"output_type": "display_data"
|
257 |
+
},
|
258 |
+
{
|
259 |
+
"name": "stderr",
|
260 |
+
"output_type": "stream",
|
261 |
+
"text": [
|
262 |
+
"Xet Storage is enabled for this repo, but the 'hf_xet' package is not installed. Falling back to regular HTTP download. For better performance, install the package with: `pip install huggingface_hub[hf_xet]` or `pip install hf_xet`\n"
|
263 |
+
]
|
264 |
+
},
|
265 |
+
{
|
266 |
+
"data": {
|
267 |
+
"application/vnd.jupyter.widget-view+json": {
|
268 |
+
"model_id": "29fdd3411b504b2ca875d7e0007db775",
|
269 |
+
"version_major": 2,
|
270 |
+
"version_minor": 0
|
271 |
+
},
|
272 |
+
"text/plain": [
|
273 |
+
"model.safetensors: 0%| | 0.00/438M [00:00<?, ?B/s]"
|
274 |
+
]
|
275 |
+
},
|
276 |
+
"metadata": {},
|
277 |
+
"output_type": "display_data"
|
278 |
+
},
|
279 |
+
{
|
280 |
+
"data": {
|
281 |
+
"application/vnd.jupyter.widget-view+json": {
|
282 |
+
"model_id": "102778342b534cddb8b84d3698371c89",
|
283 |
+
"version_major": 2,
|
284 |
+
"version_minor": 0
|
285 |
+
},
|
286 |
+
"text/plain": [
|
287 |
+
"tokenizer_config.json: 0%| | 0.00/363 [00:00<?, ?B/s]"
|
288 |
+
]
|
289 |
+
},
|
290 |
+
"metadata": {},
|
291 |
+
"output_type": "display_data"
|
292 |
+
},
|
293 |
+
{
|
294 |
+
"data": {
|
295 |
+
"application/vnd.jupyter.widget-view+json": {
|
296 |
+
"model_id": "209ab2f278b949e5846abd5b60e42130",
|
297 |
+
"version_major": 2,
|
298 |
+
"version_minor": 0
|
299 |
+
},
|
300 |
+
"text/plain": [
|
301 |
+
"vocab.txt: 0%| | 0.00/232k [00:00<?, ?B/s]"
|
302 |
+
]
|
303 |
+
},
|
304 |
+
"metadata": {},
|
305 |
+
"output_type": "display_data"
|
306 |
+
},
|
307 |
+
{
|
308 |
+
"data": {
|
309 |
+
"application/vnd.jupyter.widget-view+json": {
|
310 |
+
"model_id": "aced6d0bf212458a89c05dde7a22411b",
|
311 |
+
"version_major": 2,
|
312 |
+
"version_minor": 0
|
313 |
+
},
|
314 |
+
"text/plain": [
|
315 |
+
"tokenizer.json: 0%| | 0.00/466k [00:00<?, ?B/s]"
|
316 |
+
]
|
317 |
+
},
|
318 |
+
"metadata": {},
|
319 |
+
"output_type": "display_data"
|
320 |
+
},
|
321 |
+
{
|
322 |
+
"data": {
|
323 |
+
"application/vnd.jupyter.widget-view+json": {
|
324 |
+
"model_id": "105ea3c52b7444f281aa22bddf40d582",
|
325 |
+
"version_major": 2,
|
326 |
+
"version_minor": 0
|
327 |
+
},
|
328 |
+
"text/plain": [
|
329 |
+
"special_tokens_map.json: 0%| | 0.00/239 [00:00<?, ?B/s]"
|
330 |
+
]
|
331 |
+
},
|
332 |
+
"metadata": {},
|
333 |
+
"output_type": "display_data"
|
334 |
+
},
|
335 |
+
{
|
336 |
+
"data": {
|
337 |
+
"application/vnd.jupyter.widget-view+json": {
|
338 |
+
"model_id": "abbfcff9883645de86ea66f61931d31a",
|
339 |
+
"version_major": 2,
|
340 |
+
"version_minor": 0
|
341 |
+
},
|
342 |
+
"text/plain": [
|
343 |
+
"config.json: 0%| | 0.00/190 [00:00<?, ?B/s]"
|
344 |
+
]
|
345 |
+
},
|
346 |
+
"metadata": {},
|
347 |
+
"output_type": "display_data"
|
348 |
+
}
|
349 |
+
],
|
350 |
+
"source": [
|
351 |
+
"# Necessary packages:\n",
|
352 |
+
"# - langchain-chroma>=0.1.2\n",
|
353 |
+
"# - langchain-huggingface\n",
|
354 |
+
"# Source:\n",
|
355 |
+
"# http://python.langchain.com/docs/integrations/vectorstores/chroma/\n",
|
356 |
+
"\n",
|
357 |
+
"embeddings = HuggingFaceEmbeddings(model_name=\"sentence-transformers/all-mpnet-base-v2\")\n",
|
358 |
+
"vector_store = Chroma(\n",
|
359 |
+
" collection_name=\"gaia_dataset\",\n",
|
360 |
+
" embedding_function=embeddings,\n",
|
361 |
+
" persist_directory=\"./data/chroma_langchain_db\", # Where to save data locally, remove if not necessary\n",
|
362 |
+
")"
|
363 |
+
]
|
364 |
+
},
|
365 |
+
{
|
366 |
+
"cell_type": "code",
|
367 |
+
"execution_count": null,
|
368 |
+
"id": "69e1f1b3",
|
369 |
+
"metadata": {},
|
370 |
+
"outputs": [],
|
371 |
+
"source": [
|
372 |
+
"# wrap the metadata.jsonl's questions and answers into a list of document\n",
|
373 |
+
"docs = []\n",
|
374 |
+
"for i, sample in enumerate(json_QA):\n",
|
375 |
+
" content = (\n",
|
376 |
+
" f\"Question : {sample['Question']}\\n\\nFinal answer : {sample['Final answer']}\"\n",
|
377 |
+
" )\n",
|
378 |
+
" doc = Document(\n",
|
379 |
+
" page_content=content,\n",
|
380 |
+
" metadata={\"source\": sample[\"task_id\"]},\n",
|
381 |
+
" id=i,\n",
|
382 |
+
" )\n",
|
383 |
+
" docs.append(doc)\n",
|
384 |
+
"\n",
|
385 |
+
"# upload the documents to the vector database\n",
|
386 |
+
"try:\n",
|
387 |
+
" uuids = [str(uuid4()) for _ in range(len(docs))]\n",
|
388 |
+
" vector_store.add_documents(documents=docs, ids=uuids)\n",
|
389 |
+
"except Exception as exception:\n",
|
390 |
+
" print(\"Error inserting data into Supabase:\", exception)\n",
|
391 |
+
"\n",
|
392 |
+
"# ALTERNATIVE : Save the documents (a list of dict) into a csv file, and manually upload it to Supabase\n",
|
393 |
+
"# import pandas as pd\n",
|
394 |
+
"# df = pd.DataFrame(docs)\n",
|
395 |
+
"# df.to_csv('supabase_docs.csv', index=False)"
|
396 |
+
]
|
397 |
+
},
|
398 |
+
{
|
399 |
+
"cell_type": "markdown",
|
400 |
+
"id": "d1701d7c",
|
401 |
+
"metadata": {},
|
402 |
+
"source": [
|
403 |
+
"<div style=\"background-color:lightblue; color:black; font-weight:bold\">\n",
|
404 |
+
"\n",
|
405 |
+
"## 3. Querying the database and converting to a retriever to search using\n",
|
406 |
+
"\n",
|
407 |
+
"</div>\n"
|
408 |
+
]
|
409 |
+
},
|
410 |
+
{
|
411 |
+
"cell_type": "code",
|
412 |
+
"execution_count": null,
|
413 |
+
"id": "0a7763a1",
|
414 |
+
"metadata": {},
|
415 |
+
"outputs": [
|
416 |
+
{
|
417 |
+
"name": "stdout",
|
418 |
+
"output_type": "stream",
|
419 |
+
"text": [
|
420 |
+
"* Question : How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia.\n",
|
421 |
+
"\n",
|
422 |
+
"Final answer : 3 [{'source': '8e867cd7-cff9-4e6c-867a-ff5ddc2550be'}]\n",
|
423 |
+
"* Question : It is 1999. Before you party like it is 1999, please assist me in settling a bet.\n",
|
424 |
+
"\n",
|
425 |
+
"Fiona Apple and Paula Cole released albums prior to 1999. Of these albums, which didn't receive a letter grade from Robert Christgau? Provide your answer as a comma delimited list of album titles, sorted alphabetically.\n",
|
426 |
+
"\n",
|
427 |
+
"Final answer : Harbinger, Tidal [{'source': 'f46b4380-207e-4434-820b-f32ce04ae2a4'}]\n"
|
428 |
+
]
|
429 |
+
}
|
430 |
+
],
|
431 |
+
"source": [
|
432 |
+
"# Querying directly from the vector database\n",
|
433 |
+
"query = \"How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia.\"\n",
|
434 |
+
"\n",
|
435 |
+
"results = vector_store.similarity_search(\n",
|
436 |
+
" query=query,\n",
|
437 |
+
" k=2,\n",
|
438 |
+
")\n",
|
439 |
+
"for res in results:\n",
|
440 |
+
" print(f\"* {res.page_content} [{res.metadata}]\")"
|
441 |
+
]
|
442 |
+
},
|
443 |
+
{
|
444 |
+
"cell_type": "code",
|
445 |
+
"execution_count": null,
|
446 |
+
"id": "05895e6c",
|
447 |
+
"metadata": {},
|
448 |
+
"outputs": [],
|
449 |
+
"source": [
|
450 |
+
"# Querying using a retriever\n",
|
451 |
+
"# Conver to a retriever\n",
|
452 |
+
"retriever = vector_store.as_retriever(\n",
|
453 |
+
" search_type=\"mmr\", search_kwargs={\"k\": 1, \"fetch_k\": 5}\n",
|
454 |
+
")\n",
|
455 |
+
"results = retriever.invoke(input=query)"
|
456 |
+
]
|
457 |
+
},
|
458 |
+
{
|
459 |
+
"cell_type": "code",
|
460 |
+
"execution_count": 17,
|
461 |
+
"id": "31649cce",
|
462 |
+
"metadata": {},
|
463 |
+
"outputs": [
|
464 |
+
{
|
465 |
+
"data": {
|
466 |
+
"text/plain": [
|
467 |
+
"[Document(id='454c552a-b27f-4eba-ac9a-d37300e515cd', metadata={'source': '8e867cd7-cff9-4e6c-867a-ff5ddc2550be'}, page_content='Question : How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia.\\n\\nFinal answer : 3')]"
|
468 |
+
]
|
469 |
+
},
|
470 |
+
"execution_count": 17,
|
471 |
+
"metadata": {},
|
472 |
+
"output_type": "execute_result"
|
473 |
+
}
|
474 |
+
],
|
475 |
+
"source": [
|
476 |
+
"results"
|
477 |
+
]
|
478 |
+
},
|
479 |
+
{
|
480 |
+
"cell_type": "markdown",
|
481 |
+
"id": "ccfa3c25",
|
482 |
+
"metadata": {},
|
483 |
+
"source": [
|
484 |
+
"<div style=\"background-color:lightblue; color:black; font-weight:bold\">\n",
|
485 |
+
"\n",
|
486 |
+
"## 4. Loading the database from directory\n",
|
487 |
+
"\n",
|
488 |
+
"</div>\n"
|
489 |
+
]
|
490 |
+
},
|
491 |
+
{
|
492 |
+
"cell_type": "code",
|
493 |
+
"execution_count": null,
|
494 |
+
"id": "4715fbd1",
|
495 |
+
"metadata": {},
|
496 |
+
"outputs": [],
|
497 |
+
"source": [
|
498 |
+
"# Set the proxy for the HuggingFaceEmbeddings and SUpabase client\n",
|
499 |
+
"\n",
|
500 |
+
"\n"
|
501 |
+
]
|
502 |
+
},
|
503 |
+
{
|
504 |
+
"cell_type": "code",
|
505 |
+
"execution_count": null,
|
506 |
+
"id": "6f734773",
|
507 |
+
"metadata": {},
|
508 |
+
"outputs": [],
|
509 |
+
"source": [
|
510 |
+
"embeddings = HuggingFaceEmbeddings(model_name=\"sentence-transformers/all-mpnet-base-v2\")\n",
|
511 |
+
"vector_store_loaded = Chroma(\n",
|
512 |
+
" collection_name=\"gaia_dataset\",\n",
|
513 |
+
" embedding_function=embeddings,\n",
|
514 |
+
" persist_directory=\"./data/chroma_langchain_db\", # Where to save data locally, remove if not necessary\n",
|
515 |
+
")"
|
516 |
+
]
|
517 |
+
},
|
518 |
+
{
|
519 |
+
"cell_type": "code",
|
520 |
+
"execution_count": null,
|
521 |
+
"id": "e0fefcdf",
|
522 |
+
"metadata": {},
|
523 |
+
"outputs": [
|
524 |
+
{
|
525 |
+
"name": "stdout",
|
526 |
+
"output_type": "stream",
|
527 |
+
"text": [
|
528 |
+
"* Question : How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia.\n",
|
529 |
+
"\n",
|
530 |
+
"Final answer : 3 [{'source': '8e867cd7-cff9-4e6c-867a-ff5ddc2550be'}]\n",
|
531 |
+
"* Question : It is 1999. Before you party like it is 1999, please assist me in settling a bet.\n",
|
532 |
+
"\n",
|
533 |
+
"Fiona Apple and Paula Cole released albums prior to 1999. Of these albums, which didn't receive a letter grade from Robert Christgau? Provide your answer as a comma delimited list of album titles, sorted alphabetically.\n",
|
534 |
+
"\n",
|
535 |
+
"Final answer : Harbinger, Tidal [{'source': 'f46b4380-207e-4434-820b-f32ce04ae2a4'}]\n"
|
536 |
+
]
|
537 |
+
}
|
538 |
+
],
|
539 |
+
"source": [
|
540 |
+
"query = \"How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia.\"\n",
|
541 |
+
"\n",
|
542 |
+
"results = vector_store_loaded.similarity_search(\n",
|
543 |
+
" query=query,\n",
|
544 |
+
" k=2,\n",
|
545 |
+
")\n",
|
546 |
+
"for res in results:\n",
|
547 |
+
" print(f\"* {res.page_content} [{res.metadata}]\")\n"
|
548 |
+
]
|
549 |
+
},
|
550 |
+
{
|
551 |
+
"cell_type": "code",
|
552 |
+
"execution_count": 9,
|
553 |
+
"id": "9a42742c",
|
554 |
+
"metadata": {},
|
555 |
+
"outputs": [
|
556 |
+
{
|
557 |
+
"data": {
|
558 |
+
"text/plain": [
|
559 |
+
"Document(id='454c552a-b27f-4eba-ac9a-d37300e515cd', metadata={'source': '8e867cd7-cff9-4e6c-867a-ff5ddc2550be'}, page_content='Question : How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia.\\n\\nFinal answer : 3')"
|
560 |
+
]
|
561 |
+
},
|
562 |
+
"execution_count": 9,
|
563 |
+
"metadata": {},
|
564 |
+
"output_type": "execute_result"
|
565 |
+
}
|
566 |
+
],
|
567 |
+
"source": [
|
568 |
+
"results[0]"
|
569 |
+
]
|
570 |
+
},
|
571 |
+
{
|
572 |
+
"cell_type": "markdown",
|
573 |
+
"id": "2749a453",
|
574 |
+
"metadata": {},
|
575 |
+
"source": [
|
576 |
+
"<div style=\"background-color:lightblue; color:black; font-weight:bold\">\n",
|
577 |
+
"\n",
|
578 |
+
"## 5. Creating supabase vector store\n",
|
579 |
+
"\n",
|
580 |
+
"</div>\n"
|
581 |
+
]
|
582 |
+
},
|
583 |
+
{
|
584 |
+
"cell_type": "code",
|
585 |
+
"execution_count": null,
|
586 |
+
"id": "9138e761",
|
587 |
+
"metadata": {},
|
588 |
+
"outputs": [],
|
589 |
+
"source": [
|
590 |
+
"# Set the proxy for the HuggingFaceEmbeddings and SUpabase client\n",
|
591 |
+
"\n",
|
592 |
+
"\n"
|
593 |
+
]
|
594 |
+
},
|
595 |
+
{
|
596 |
+
"cell_type": "code",
|
597 |
+
"execution_count": null,
|
598 |
+
"id": "fb4e2b68",
|
599 |
+
"metadata": {},
|
600 |
+
"outputs": [],
|
601 |
+
"source": [
|
602 |
+
"supabase_url = os.environ.get(\"SUPABASE_URL\")\n",
|
603 |
+
"supabase_key = os.environ.get(\"SUPABASE_SERVICE_KEY\")\n",
|
604 |
+
"supabase: Client = create_client(supabase_url, supabase_key)\n",
|
605 |
+
"\n",
|
606 |
+
"embeddings = HuggingFaceEmbeddings(model_name=\"sentence-transformers/all-mpnet-base-v2\")\n"
|
607 |
+
]
|
608 |
+
},
|
609 |
+
{
|
610 |
+
"cell_type": "code",
|
611 |
+
"execution_count": null,
|
612 |
+
"id": "c49c6926",
|
613 |
+
"metadata": {},
|
614 |
+
"outputs": [
|
615 |
+
{
|
616 |
+
"name": "stdout",
|
617 |
+
"output_type": "stream",
|
618 |
+
"text": [
|
619 |
+
"768\n"
|
620 |
+
]
|
621 |
+
}
|
622 |
+
],
|
623 |
+
"source": [
|
624 |
+
"# Review the length of the vector dimensions\n",
|
625 |
+
"vector = embeddings.embed_query(\"test\")\n",
|
626 |
+
"print(len(vector))\n"
|
627 |
+
]
|
628 |
+
},
|
629 |
+
{
|
630 |
+
"cell_type": "markdown",
|
631 |
+
"id": "74f48c63",
|
632 |
+
"metadata": {},
|
633 |
+
"source": [
|
634 |
+
"<div style=\"color:red;font-weight:bold\">\n",
|
635 |
+
"IMPORTANT: you need to create before run any code with supabase the table:\n",
|
636 |
+
"</div>\n",
|
637 |
+
"\n",
|
638 |
+
"```sql\n",
|
639 |
+
"-- enable the vector extension once if not already enabled\n",
|
640 |
+
"create extension if not exists vector;\n",
|
641 |
+
"\n",
|
642 |
+
"-- create your vectorstore table\n",
|
643 |
+
"create table gaia_dataset (\n",
|
644 |
+
" id uuid primary key default uuid_generate_v4(),\n",
|
645 |
+
" content text,\n",
|
646 |
+
" embedding vector(768),\n",
|
647 |
+
" metadata jsonb\n",
|
648 |
+
");\n",
|
649 |
+
"```\n"
|
650 |
+
]
|
651 |
+
},
|
652 |
+
{
|
653 |
+
"cell_type": "markdown",
|
654 |
+
"id": "fb3608d4",
|
655 |
+
"metadata": {},
|
656 |
+
"source": [
|
657 |
+
"- Option 1) with sql statements\n"
|
658 |
+
]
|
659 |
+
},
|
660 |
+
{
|
661 |
+
"cell_type": "code",
|
662 |
+
"execution_count": 24,
|
663 |
+
"id": "02fc6c68",
|
664 |
+
"metadata": {},
|
665 |
+
"outputs": [
|
666 |
+
{
|
667 |
+
"name": "stdout",
|
668 |
+
"output_type": "stream",
|
669 |
+
"text": [
|
670 |
+
"Number of documents to insert: 165\n"
|
671 |
+
]
|
672 |
+
}
|
673 |
+
],
|
674 |
+
"source": [
|
675 |
+
"# wrap the metadata.jsonl's questions and answers into a list of document\n",
|
676 |
+
"# from langchain.schema import Document\n",
|
677 |
+
"docs = []\n",
|
678 |
+
"for sample in json_QA:\n",
|
679 |
+
" content = (\n",
|
680 |
+
" f\"Question : {sample['Question']}\\n\\nFinal answer : {sample['Final answer']}\"\n",
|
681 |
+
" )\n",
|
682 |
+
" doc = {\n",
|
683 |
+
" \"content\": content,\n",
|
684 |
+
" \"metadata\": { # meatadata的格式必须时source键,否则会报错\n",
|
685 |
+
" \"source\": sample[\"task_id\"]\n",
|
686 |
+
" },\n",
|
687 |
+
" \"embedding\": embeddings.embed_query(content),\n",
|
688 |
+
" }\n",
|
689 |
+
" docs.append(doc)\n",
|
690 |
+
"\n",
|
691 |
+
"print(f\"Number of documents to insert: {len(docs)}\")\n",
|
692 |
+
"# upload the documents to the vector database\n",
|
693 |
+
"try:\n",
|
694 |
+
" response = supabase.table(\"gaia_dataset\").insert(docs).execute()\n",
|
695 |
+
"except Exception as exception:\n",
|
696 |
+
" print(\"Error inserting data into Supabase:\", exception)\n",
|
697 |
+
"\n",
|
698 |
+
"# ALTERNATIVE : Save the documents (a list of dict) into a csv file, and manually upload it to Supabase\n",
|
699 |
+
"# import pandas as pd\n",
|
700 |
+
"# df = pd.DataFrame(docs)\n",
|
701 |
+
"# df.to_csv('supabase_docs.csv', index=False)\n"
|
702 |
+
]
|
703 |
+
},
|
704 |
+
{
|
705 |
+
"cell_type": "markdown",
|
706 |
+
"id": "7ff670b5",
|
707 |
+
"metadata": {},
|
708 |
+
"source": [
|
709 |
+
"- Option 2) with class of langchain\n"
|
710 |
+
]
|
711 |
+
},
|
712 |
+
{
|
713 |
+
"cell_type": "code",
|
714 |
+
"execution_count": null,
|
715 |
+
"id": "8c20877a",
|
716 |
+
"metadata": {},
|
717 |
+
"outputs": [],
|
718 |
+
"source": [
|
719 |
+
"# wrap the metadata.jsonl's questions and answers into a list of document\n",
|
720 |
+
"docs = []\n",
|
721 |
+
"for i, sample in enumerate(json_QA):\n",
|
722 |
+
" content = (\n",
|
723 |
+
" f\"Question : {sample['Question']}\\n\\nFinal answer : {sample['Final answer']}\"\n",
|
724 |
+
" )\n",
|
725 |
+
" doc = Document(\n",
|
726 |
+
" page_content=content,\n",
|
727 |
+
" metadata={\"source\": sample[\"task_id\"]},\n",
|
728 |
+
" id=i,\n",
|
729 |
+
" )\n",
|
730 |
+
" docs.append(doc)\n",
|
731 |
+
"\n",
|
732 |
+
"# Initialize the vector store with Supabase\n",
|
733 |
+
"vectorstore = SupabaseVectorStore(\n",
|
734 |
+
" client=supabase, embedding=embeddings, table_name=\"gaia_dataset\"\n",
|
735 |
+
")\n",
|
736 |
+
"\n",
|
737 |
+
"# upload the documents to the vector database\n",
|
738 |
+
"try:\n",
|
739 |
+
" vector_store = SupabaseVectorStore.from_documents(\n",
|
740 |
+
" docs,\n",
|
741 |
+
" embeddings,\n",
|
742 |
+
" client=supabase,\n",
|
743 |
+
" table_name=\"gaia_dataset\",\n",
|
744 |
+
" query_name=\"match_documents\",\n",
|
745 |
+
" # chunk_size=500,\n",
|
746 |
+
" )\n",
|
747 |
+
"except Exception as exception:\n",
|
748 |
+
" print(\"Error inserting data into Supabase:\", exception)\n"
|
749 |
+
]
|
750 |
+
},
|
751 |
+
{
|
752 |
+
"cell_type": "markdown",
|
753 |
+
"id": "32ea05c4",
|
754 |
+
"metadata": {},
|
755 |
+
"source": [
|
756 |
+
"Let's connect to the table and do some retrieval\n"
|
757 |
+
]
|
758 |
+
},
|
759 |
+
{
|
760 |
+
"cell_type": "markdown",
|
761 |
+
"id": "9aaf97c0",
|
762 |
+
"metadata": {},
|
763 |
+
"source": [
|
764 |
+
"<div style=\"color:red;font-weight:bold\">\n",
|
765 |
+
"IMPORTANT: you also need to create the match_documents function with SQL:\n",
|
766 |
+
"</div>\n",
|
767 |
+
"\n",
|
768 |
+
"```sql\n",
|
769 |
+
"create or replace function match_documents(query_embedding vector(768), match_count int default 5)\n",
|
770 |
+
"returns table (\n",
|
771 |
+
" id uuid,\n",
|
772 |
+
" content text,\n",
|
773 |
+
" embedding vector(768),\n",
|
774 |
+
" metadata jsonb,\n",
|
775 |
+
" similarity float\n",
|
776 |
+
")\n",
|
777 |
+
"language sql\n",
|
778 |
+
"as $$\n",
|
779 |
+
" select\n",
|
780 |
+
" id,\n",
|
781 |
+
" content,\n",
|
782 |
+
" embedding,\n",
|
783 |
+
" metadata,\n",
|
784 |
+
" 1 - (embedding <=> query_embedding) as similarity\n",
|
785 |
+
" from gaia_dataset\n",
|
786 |
+
" order by embedding <=> query_embedding\n",
|
787 |
+
" limit match_count;\n",
|
788 |
+
"$$;\n",
|
789 |
+
"```\n"
|
790 |
+
]
|
791 |
+
},
|
792 |
+
{
|
793 |
+
"cell_type": "code",
|
794 |
+
"execution_count": null,
|
795 |
+
"id": "da2db9cc",
|
796 |
+
"metadata": {},
|
797 |
+
"outputs": [],
|
798 |
+
"source": [
|
799 |
+
"vector_store = SupabaseVectorStore(\n",
|
800 |
+
" embedding=embeddings,\n",
|
801 |
+
" client=supabase,\n",
|
802 |
+
" table_name=\"gaia_dataset\",\n",
|
803 |
+
" query_name=\"match_documents\",\n",
|
804 |
+
")\n"
|
805 |
+
]
|
806 |
+
},
|
807 |
+
{
|
808 |
+
"cell_type": "code",
|
809 |
+
"execution_count": null,
|
810 |
+
"id": "eec1f65f",
|
811 |
+
"metadata": {},
|
812 |
+
"outputs": [],
|
813 |
+
"source": [
|
814 |
+
"query = \"On June 6, 2023, an article by Carolyn Collins Petersen was published in Universe Today. This article mentions a team that produced a paper about their observations, linked at the bottom of the article. Find this paper. Under what NASA award number was the work performed by R. G. Arendt supported by?\"\n",
|
815 |
+
"matched_docs = vector_store.similarity_search(query)\n"
|
816 |
+
]
|
817 |
+
},
|
818 |
+
{
|
819 |
+
"cell_type": "code",
|
820 |
+
"execution_count": 38,
|
821 |
+
"id": "1e09d302",
|
822 |
+
"metadata": {},
|
823 |
+
"outputs": [
|
824 |
+
{
|
825 |
+
"data": {
|
826 |
+
"text/plain": [
|
827 |
+
"[Document(metadata={'source': '840bfca7-4f7b-481a-8794-c560c340185d'}, page_content='Question : On June 6, 2023, an article by Carolyn Collins Petersen was published in Universe Today. This article mentions a team that produced a paper about their observations, linked at the bottom of the article. Find this paper. Under what NASA award number was the work performed by R. G. Arendt supported by?\\n\\nFinal answer : 80GSFC21M0002'),\n",
|
828 |
+
" Document(metadata={'source': 'a7feb290-76bb-4cb7-8800-7edaf7954f2f'}, page_content='Question : How many High Energy Physics - Lattice articles listed in January 2020 on Arxiv had ps versions available?\\n\\nFinal answer : 31'),\n",
|
829 |
+
" Document(metadata={'source': '0e9e85b8-52b9-4de4-b402-5f635ab9631f'}, page_content=\"Question : What is the latest chronological year date written in the image on the webpage found when following the first citation reference link on the latest version of Carl Nebel's Wikipedia page as of August 2023?\\n\\nFinal answer : 1927\"),\n",
|
830 |
+
" Document(metadata={'source': 'bec74516-02fc-48dc-b202-55e78d0e17cf'}, page_content='Question : What is the average number of pre-2020 works on the open researcher and contributor identification pages of the people whose identification is in this file?\\n\\nFinal answer : 26.4')]"
|
831 |
+
]
|
832 |
+
},
|
833 |
+
"execution_count": 38,
|
834 |
+
"metadata": {},
|
835 |
+
"output_type": "execute_result"
|
836 |
+
}
|
837 |
+
],
|
838 |
+
"source": [
|
839 |
+
"matched_docs"
|
840 |
+
]
|
841 |
+
}
|
842 |
+
],
|
843 |
+
"metadata": {
|
844 |
+
"kernelspec": {
|
845 |
+
"display_name": "llm-huggingface-agents-course-env",
|
846 |
+
"language": "python",
|
847 |
+
"name": "python3"
|
848 |
+
},
|
849 |
+
"language_info": {
|
850 |
+
"codemirror_mode": {
|
851 |
+
"name": "ipython",
|
852 |
+
"version": 3
|
853 |
+
},
|
854 |
+
"file_extension": ".py",
|
855 |
+
"mimetype": "text/x-python",
|
856 |
+
"name": "python",
|
857 |
+
"nbconvert_exporter": "python",
|
858 |
+
"pygments_lexer": "ipython3",
|
859 |
+
"version": "3.11.11"
|
860 |
+
}
|
861 |
+
},
|
862 |
+
"nbformat": 4,
|
863 |
+
"nbformat_minor": 5
|
864 |
+
}
|