Spaces:
Sleeping
Sleeping
lingyit1108
commited on
Commit
·
349fa69
1
Parent(s):
e041d59
added notebooks
Browse files
notebooks/fine-tuning-embedding-model.ipynb
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
6 |
+
"id": "ca2c990f-5215-4ab9-8143-1d79db28edc6",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": []
|
10 |
+
}
|
11 |
+
],
|
12 |
+
"metadata": {
|
13 |
+
"kernelspec": {
|
14 |
+
"display_name": "Python 3 (ipykernel)",
|
15 |
+
"language": "python",
|
16 |
+
"name": "python3"
|
17 |
+
},
|
18 |
+
"language_info": {
|
19 |
+
"codemirror_mode": {
|
20 |
+
"name": "ipython",
|
21 |
+
"version": 3
|
22 |
+
},
|
23 |
+
"file_extension": ".py",
|
24 |
+
"mimetype": "text/x-python",
|
25 |
+
"name": "python",
|
26 |
+
"nbconvert_exporter": "python",
|
27 |
+
"pygments_lexer": "ipython3",
|
28 |
+
"version": "3.9.18"
|
29 |
+
}
|
30 |
+
},
|
31 |
+
"nbformat": 4,
|
32 |
+
"nbformat_minor": 5
|
33 |
+
}
|
notebooks/persisted-embedding-model.ipynb
ADDED
@@ -0,0 +1,679 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
6 |
+
"id": "aaa7e27e-7632-4024-9764-a4404293fd05",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": [
|
10 |
+
"import openai\n",
|
11 |
+
"from llama_index import SimpleDirectoryReader\n",
|
12 |
+
"from llama_index import Document\n",
|
13 |
+
"from llama_index import VectorStoreIndex\n",
|
14 |
+
"from llama_index import ServiceContext\n",
|
15 |
+
"from llama_index.llms import OpenAI\n",
|
16 |
+
"\n",
|
17 |
+
"from llama_index.embeddings import HuggingFaceEmbedding\n",
|
18 |
+
"from llama_index import StorageContext, load_index_from_storage\n",
|
19 |
+
"\n",
|
20 |
+
"import time"
|
21 |
+
]
|
22 |
+
},
|
23 |
+
{
|
24 |
+
"cell_type": "code",
|
25 |
+
"execution_count": null,
|
26 |
+
"id": "8a2abdeb-9ff3-4793-8a14-92d8d06391f9",
|
27 |
+
"metadata": {},
|
28 |
+
"outputs": [],
|
29 |
+
"source": []
|
30 |
+
},
|
31 |
+
{
|
32 |
+
"cell_type": "markdown",
|
33 |
+
"id": "ca5606cf-16ec-4ae1-a057-25fab105d545",
|
34 |
+
"metadata": {},
|
35 |
+
"source": [
|
36 |
+
"### Run Indexing in memory without using vectorstore"
|
37 |
+
]
|
38 |
+
},
|
39 |
+
{
|
40 |
+
"cell_type": "code",
|
41 |
+
"execution_count": null,
|
42 |
+
"id": "a2d79bc4-1c0f-4354-ae6c-fe3697a28c9d",
|
43 |
+
"metadata": {},
|
44 |
+
"outputs": [],
|
45 |
+
"source": [
|
46 |
+
"start_time = time.time()"
|
47 |
+
]
|
48 |
+
},
|
49 |
+
{
|
50 |
+
"cell_type": "code",
|
51 |
+
"execution_count": null,
|
52 |
+
"id": "219bf47e-2df4-46e2-b6c4-b0406820fe8c",
|
53 |
+
"metadata": {},
|
54 |
+
"outputs": [],
|
55 |
+
"source": [
|
56 |
+
"documents = SimpleDirectoryReader(input_files=[\"../raw_documents/HI_Knowledge_Base.pdf\"]).load_data()\n",
|
57 |
+
"document = Document(text=\"\\n\\n\".join([doc.text for doc in documents]))"
|
58 |
+
]
|
59 |
+
},
|
60 |
+
{
|
61 |
+
"cell_type": "code",
|
62 |
+
"execution_count": null,
|
63 |
+
"id": "6e0e2c29-98c5-48c8-9f1c-b41ca0a1e904",
|
64 |
+
"metadata": {},
|
65 |
+
"outputs": [],
|
66 |
+
"source": []
|
67 |
+
},
|
68 |
+
{
|
69 |
+
"cell_type": "code",
|
70 |
+
"execution_count": null,
|
71 |
+
"id": "ad4edb79-f29b-4556-9e69-afa8917568c7",
|
72 |
+
"metadata": {},
|
73 |
+
"outputs": [],
|
74 |
+
"source": [
|
75 |
+
"llm = OpenAI(model=\"gpt-3.5-turbo-1106\", temperature=0.1)"
|
76 |
+
]
|
77 |
+
},
|
78 |
+
{
|
79 |
+
"cell_type": "code",
|
80 |
+
"execution_count": null,
|
81 |
+
"id": "f4911316-bc1b-4e65-a66e-c83e70c6fa00",
|
82 |
+
"metadata": {},
|
83 |
+
"outputs": [],
|
84 |
+
"source": [
|
85 |
+
"embed_model = HuggingFaceEmbedding(model_name=\"BAAI/bge-small-en-v1.5\")"
|
86 |
+
]
|
87 |
+
},
|
88 |
+
{
|
89 |
+
"cell_type": "code",
|
90 |
+
"execution_count": null,
|
91 |
+
"id": "6a441a1f-91d8-49ec-999d-36adbccbfa90",
|
92 |
+
"metadata": {},
|
93 |
+
"outputs": [],
|
94 |
+
"source": []
|
95 |
+
},
|
96 |
+
{
|
97 |
+
"cell_type": "code",
|
98 |
+
"execution_count": null,
|
99 |
+
"id": "942f4406-4b24-4c4e-a803-e0a9a5f31f6c",
|
100 |
+
"metadata": {},
|
101 |
+
"outputs": [],
|
102 |
+
"source": [
|
103 |
+
"# service_context = ServiceContext.from_defaults(llm=llm, embed_model=embed_model)\n",
|
104 |
+
"service_context = ServiceContext.from_defaults(llm=None, embed_model=embed_model)\n",
|
105 |
+
"index = VectorStoreIndex.from_documents([document], service_context=service_context)"
|
106 |
+
]
|
107 |
+
},
|
108 |
+
{
|
109 |
+
"cell_type": "code",
|
110 |
+
"execution_count": null,
|
111 |
+
"id": "630afd70-7aad-4049-950e-096976e4f7fa",
|
112 |
+
"metadata": {},
|
113 |
+
"outputs": [],
|
114 |
+
"source": [
|
115 |
+
"indexing_cost = time.time() - start_time\n",
|
116 |
+
"indexing_cost = indexing_cost / 60\n",
|
117 |
+
"print(f\"indexing time: {indexing_cost:.1f} mins\")"
|
118 |
+
]
|
119 |
+
},
|
120 |
+
{
|
121 |
+
"cell_type": "code",
|
122 |
+
"execution_count": null,
|
123 |
+
"id": "24691a2c-00d9-4121-81f8-8aacbe764ae8",
|
124 |
+
"metadata": {},
|
125 |
+
"outputs": [],
|
126 |
+
"source": []
|
127 |
+
},
|
128 |
+
{
|
129 |
+
"cell_type": "code",
|
130 |
+
"execution_count": null,
|
131 |
+
"id": "36f81a58-4249-43bd-97e2-781b1d08b6d9",
|
132 |
+
"metadata": {},
|
133 |
+
"outputs": [],
|
134 |
+
"source": [
|
135 |
+
"index.storage_context.persist(persist_dir=\"../models/llama_index_json/\")"
|
136 |
+
]
|
137 |
+
},
|
138 |
+
{
|
139 |
+
"cell_type": "code",
|
140 |
+
"execution_count": null,
|
141 |
+
"id": "dca06682-8fa1-4cf2-81a9-4c7d223d2032",
|
142 |
+
"metadata": {},
|
143 |
+
"outputs": [],
|
144 |
+
"source": [
|
145 |
+
"query_engine = index.as_query_engine()"
|
146 |
+
]
|
147 |
+
},
|
148 |
+
{
|
149 |
+
"cell_type": "code",
|
150 |
+
"execution_count": null,
|
151 |
+
"id": "f2553fd3-dc88-4a75-a85c-ab095ae3fba8",
|
152 |
+
"metadata": {},
|
153 |
+
"outputs": [],
|
154 |
+
"source": [
|
155 |
+
"response = query_engine.query(\"What is medishield\")"
|
156 |
+
]
|
157 |
+
},
|
158 |
+
{
|
159 |
+
"cell_type": "code",
|
160 |
+
"execution_count": null,
|
161 |
+
"id": "c8c6f966-6196-4af2-aaac-042e327bb046",
|
162 |
+
"metadata": {},
|
163 |
+
"outputs": [],
|
164 |
+
"source": [
|
165 |
+
"print(str(response))"
|
166 |
+
]
|
167 |
+
},
|
168 |
+
{
|
169 |
+
"cell_type": "code",
|
170 |
+
"execution_count": null,
|
171 |
+
"id": "b9365c5a-fcbb-4581-997b-d23723175129",
|
172 |
+
"metadata": {},
|
173 |
+
"outputs": [],
|
174 |
+
"source": []
|
175 |
+
},
|
176 |
+
{
|
177 |
+
"cell_type": "markdown",
|
178 |
+
"id": "94977d29-431f-4704-a93f-3b96a1e70cd0",
|
179 |
+
"metadata": {},
|
180 |
+
"source": [
|
181 |
+
"### Load index from persisted file without using vectorstore"
|
182 |
+
]
|
183 |
+
},
|
184 |
+
{
|
185 |
+
"cell_type": "code",
|
186 |
+
"execution_count": null,
|
187 |
+
"id": "c5348da7-59e8-45d1-afee-d50ff58549cb",
|
188 |
+
"metadata": {},
|
189 |
+
"outputs": [],
|
190 |
+
"source": [
|
191 |
+
"start_time = time.time()"
|
192 |
+
]
|
193 |
+
},
|
194 |
+
{
|
195 |
+
"cell_type": "code",
|
196 |
+
"execution_count": null,
|
197 |
+
"id": "0d51d2d5-9523-46dd-9bc4-b013bba42735",
|
198 |
+
"metadata": {},
|
199 |
+
"outputs": [],
|
200 |
+
"source": [
|
201 |
+
"embed_model = HuggingFaceEmbedding(model_name=\"BAAI/bge-small-en-v1.5\")"
|
202 |
+
]
|
203 |
+
},
|
204 |
+
{
|
205 |
+
"cell_type": "code",
|
206 |
+
"execution_count": null,
|
207 |
+
"id": "c2a72bfe-8e3e-4b26-8cbe-4e55b1fbab34",
|
208 |
+
"metadata": {},
|
209 |
+
"outputs": [],
|
210 |
+
"source": [
|
211 |
+
"service_context = ServiceContext.from_defaults(llm=None, embed_model=embed_model)"
|
212 |
+
]
|
213 |
+
},
|
214 |
+
{
|
215 |
+
"cell_type": "code",
|
216 |
+
"execution_count": null,
|
217 |
+
"id": "7a460ce0-a3a2-47ec-95bb-472c61d9f23a",
|
218 |
+
"metadata": {},
|
219 |
+
"outputs": [],
|
220 |
+
"source": [
|
221 |
+
"storage_context = StorageContext.from_defaults(persist_dir=\"../models/llama_index_json/\")"
|
222 |
+
]
|
223 |
+
},
|
224 |
+
{
|
225 |
+
"cell_type": "code",
|
226 |
+
"execution_count": null,
|
227 |
+
"id": "1331b3e5-7b3a-4f0f-80f0-f8a6810431af",
|
228 |
+
"metadata": {},
|
229 |
+
"outputs": [],
|
230 |
+
"source": [
|
231 |
+
"index = load_index_from_storage(storage_context=storage_context, service_context=service_context)"
|
232 |
+
]
|
233 |
+
},
|
234 |
+
{
|
235 |
+
"cell_type": "code",
|
236 |
+
"execution_count": null,
|
237 |
+
"id": "1e9b0825-eff8-42cd-b014-0f612b268d42",
|
238 |
+
"metadata": {},
|
239 |
+
"outputs": [],
|
240 |
+
"source": [
|
241 |
+
"load_indexing_cost = time.time() - start_time\n",
|
242 |
+
"load_indexing_cost = load_indexing_cost / 60\n",
|
243 |
+
"print(f\"Load indexing time: {load_indexing_cost:.1f} mins\")"
|
244 |
+
]
|
245 |
+
},
|
246 |
+
{
|
247 |
+
"cell_type": "code",
|
248 |
+
"execution_count": null,
|
249 |
+
"id": "7df3557b-c79f-45ad-9216-8d1502a5bc8e",
|
250 |
+
"metadata": {},
|
251 |
+
"outputs": [],
|
252 |
+
"source": []
|
253 |
+
},
|
254 |
+
{
|
255 |
+
"cell_type": "markdown",
|
256 |
+
"id": "f77c3494-65a6-44fc-ab6e-de64153300f0",
|
257 |
+
"metadata": {},
|
258 |
+
"source": [
|
259 |
+
"### Instantiate query engine"
|
260 |
+
]
|
261 |
+
},
|
262 |
+
{
|
263 |
+
"cell_type": "code",
|
264 |
+
"execution_count": null,
|
265 |
+
"id": "891f6fe1-ef71-44f8-b6a8-03d8abdcadae",
|
266 |
+
"metadata": {},
|
267 |
+
"outputs": [],
|
268 |
+
"source": [
|
269 |
+
"query_engine = index.as_query_engine()"
|
270 |
+
]
|
271 |
+
},
|
272 |
+
{
|
273 |
+
"cell_type": "code",
|
274 |
+
"execution_count": null,
|
275 |
+
"id": "a6ad3fbb-8bb0-4115-88e5-4b272b1ba422",
|
276 |
+
"metadata": {
|
277 |
+
"scrolled": true
|
278 |
+
},
|
279 |
+
"outputs": [],
|
280 |
+
"source": [
|
281 |
+
"response = query_engine.query(\"What is medishield\")\n",
|
282 |
+
"print(str(response))"
|
283 |
+
]
|
284 |
+
},
|
285 |
+
{
|
286 |
+
"cell_type": "code",
|
287 |
+
"execution_count": null,
|
288 |
+
"id": "88fc7e00-64af-4910-a1d0-34a36e110b05",
|
289 |
+
"metadata": {},
|
290 |
+
"outputs": [],
|
291 |
+
"source": []
|
292 |
+
},
|
293 |
+
{
|
294 |
+
"cell_type": "code",
|
295 |
+
"execution_count": null,
|
296 |
+
"id": "34a4ea9f-73d0-4733-847e-89fb4a051294",
|
297 |
+
"metadata": {},
|
298 |
+
"outputs": [],
|
299 |
+
"source": []
|
300 |
+
},
|
301 |
+
{
|
302 |
+
"cell_type": "markdown",
|
303 |
+
"id": "8acae3ed-2953-45a3-aba9-0327b6ae3679",
|
304 |
+
"metadata": {},
|
305 |
+
"source": [
|
306 |
+
"### ChromaDB method - create vectorstore"
|
307 |
+
]
|
308 |
+
},
|
309 |
+
{
|
310 |
+
"cell_type": "code",
|
311 |
+
"execution_count": null,
|
312 |
+
"id": "7de9c591-5a77-4bbe-80f1-4897e15f0b97",
|
313 |
+
"metadata": {},
|
314 |
+
"outputs": [],
|
315 |
+
"source": [
|
316 |
+
"import chromadb\n",
|
317 |
+
"from llama_index import VectorStoreIndex, SimpleDirectoryReader\n",
|
318 |
+
"from llama_index.vector_stores import ChromaVectorStore\n",
|
319 |
+
"from llama_index.storage.storage_context import StorageContext\n",
|
320 |
+
"from llama_index import ServiceContext\n",
|
321 |
+
"from llama_index import Document\n",
|
322 |
+
"\n",
|
323 |
+
"from llama_index.embeddings import HuggingFaceEmbedding\n",
|
324 |
+
"\n",
|
325 |
+
"import time"
|
326 |
+
]
|
327 |
+
},
|
328 |
+
{
|
329 |
+
"cell_type": "code",
|
330 |
+
"execution_count": null,
|
331 |
+
"id": "3e65dff6-77b6-4be8-8857-5cecf3a035bb",
|
332 |
+
"metadata": {},
|
333 |
+
"outputs": [],
|
334 |
+
"source": [
|
335 |
+
"# load some documents\n",
|
336 |
+
"documents = SimpleDirectoryReader(input_files=[\"../raw_documents/HI_Knowledge_Base.pdf\"]).load_data()\n",
|
337 |
+
"document = Document(text=\"\\n\\n\".join([doc.text for doc in documents]))"
|
338 |
+
]
|
339 |
+
},
|
340 |
+
{
|
341 |
+
"cell_type": "code",
|
342 |
+
"execution_count": null,
|
343 |
+
"id": "bd86b3f5-1dfc-4257-bd9c-86d34f02398d",
|
344 |
+
"metadata": {},
|
345 |
+
"outputs": [],
|
346 |
+
"source": [
|
347 |
+
"# initialize client, setting path to save data\n",
|
348 |
+
"db = chromadb.PersistentClient(path=\"../models/chroma_db\")"
|
349 |
+
]
|
350 |
+
},
|
351 |
+
{
|
352 |
+
"cell_type": "code",
|
353 |
+
"execution_count": null,
|
354 |
+
"id": "f568ce7b-bcbf-455c-acf1-6c2cae129fed",
|
355 |
+
"metadata": {},
|
356 |
+
"outputs": [],
|
357 |
+
"source": [
|
358 |
+
"# create collection\n",
|
359 |
+
"chroma_collection = db.get_or_create_collection(\"quickstart\")"
|
360 |
+
]
|
361 |
+
},
|
362 |
+
{
|
363 |
+
"cell_type": "code",
|
364 |
+
"execution_count": null,
|
365 |
+
"id": "ed0b018e-1982-46b2-b1b4-04f5c0ce8672",
|
366 |
+
"metadata": {},
|
367 |
+
"outputs": [],
|
368 |
+
"source": [
|
369 |
+
"# assign chroma as the vector_store to the context\n",
|
370 |
+
"vector_store = ChromaVectorStore(chroma_collection=chroma_collection)"
|
371 |
+
]
|
372 |
+
},
|
373 |
+
{
|
374 |
+
"cell_type": "code",
|
375 |
+
"execution_count": null,
|
376 |
+
"id": "eb5edab2-30db-4bf7-96b5-4005d3161988",
|
377 |
+
"metadata": {},
|
378 |
+
"outputs": [],
|
379 |
+
"source": []
|
380 |
+
},
|
381 |
+
{
|
382 |
+
"cell_type": "code",
|
383 |
+
"execution_count": null,
|
384 |
+
"id": "b4adc76c-b18d-4a3f-8563-127074491ba9",
|
385 |
+
"metadata": {},
|
386 |
+
"outputs": [],
|
387 |
+
"source": [
|
388 |
+
"embed_model = HuggingFaceEmbedding(model_name=\"BAAI/bge-small-en-v1.5\")"
|
389 |
+
]
|
390 |
+
},
|
391 |
+
{
|
392 |
+
"cell_type": "code",
|
393 |
+
"execution_count": null,
|
394 |
+
"id": "5289f0f9-bce2-4a2f-9428-b99906b78622",
|
395 |
+
"metadata": {},
|
396 |
+
"outputs": [],
|
397 |
+
"source": [
|
398 |
+
"service_context = ServiceContext.from_defaults(llm=None, embed_model=embed_model)"
|
399 |
+
]
|
400 |
+
},
|
401 |
+
{
|
402 |
+
"cell_type": "code",
|
403 |
+
"execution_count": null,
|
404 |
+
"id": "dab4c6f3-ef67-4d90-b3d5-e290c5d1b6f4",
|
405 |
+
"metadata": {},
|
406 |
+
"outputs": [],
|
407 |
+
"source": [
|
408 |
+
"storage_context = StorageContext.from_defaults(vector_store=vector_store)"
|
409 |
+
]
|
410 |
+
},
|
411 |
+
{
|
412 |
+
"cell_type": "code",
|
413 |
+
"execution_count": null,
|
414 |
+
"id": "e492ed4a-23a3-47d6-8b50-51fb48b3aa05",
|
415 |
+
"metadata": {},
|
416 |
+
"outputs": [],
|
417 |
+
"source": [
|
418 |
+
"start_time = time.time()"
|
419 |
+
]
|
420 |
+
},
|
421 |
+
{
|
422 |
+
"cell_type": "code",
|
423 |
+
"execution_count": null,
|
424 |
+
"id": "82fc724e-4b03-433d-ada4-d451e13e25e9",
|
425 |
+
"metadata": {},
|
426 |
+
"outputs": [],
|
427 |
+
"source": [
|
428 |
+
"# create your index\n",
|
429 |
+
"index = VectorStoreIndex.from_documents(\n",
|
430 |
+
" documents, service_context=service_context, storage_context=storage_context\n",
|
431 |
+
")"
|
432 |
+
]
|
433 |
+
},
|
434 |
+
{
|
435 |
+
"cell_type": "code",
|
436 |
+
"execution_count": null,
|
437 |
+
"id": "1ff50855-e043-4736-87c7-0ef8c11bbb26",
|
438 |
+
"metadata": {},
|
439 |
+
"outputs": [],
|
440 |
+
"source": [
|
441 |
+
"indexing_cost = time.time() - start_time\n",
|
442 |
+
"indexing_cost = indexing_cost / 60\n",
|
443 |
+
"print(f\"Indexing time: {indexing_cost:.1f} mins\")"
|
444 |
+
]
|
445 |
+
},
|
446 |
+
{
|
447 |
+
"cell_type": "code",
|
448 |
+
"execution_count": null,
|
449 |
+
"id": "ccbfa64b-6e09-40c4-b6bf-18055eaa6735",
|
450 |
+
"metadata": {},
|
451 |
+
"outputs": [],
|
452 |
+
"source": [
|
453 |
+
"# create a query engine and query\n",
|
454 |
+
"query_engine = index.as_query_engine()"
|
455 |
+
]
|
456 |
+
},
|
457 |
+
{
|
458 |
+
"cell_type": "code",
|
459 |
+
"execution_count": null,
|
460 |
+
"id": "3f16351f-7c28-4b8f-9050-3c90a40998c5",
|
461 |
+
"metadata": {},
|
462 |
+
"outputs": [],
|
463 |
+
"source": [
|
464 |
+
"retriever = index.as_retriever()"
|
465 |
+
]
|
466 |
+
},
|
467 |
+
{
|
468 |
+
"cell_type": "code",
|
469 |
+
"execution_count": null,
|
470 |
+
"id": "3d07f753-1643-4c18-b368-6c55f4a7968a",
|
471 |
+
"metadata": {},
|
472 |
+
"outputs": [],
|
473 |
+
"source": [
|
474 |
+
"r_list = retriever.retrieve(\"What is the meaning of life?\")"
|
475 |
+
]
|
476 |
+
},
|
477 |
+
{
|
478 |
+
"cell_type": "code",
|
479 |
+
"execution_count": null,
|
480 |
+
"id": "74b4367a-185f-42cf-9951-48325378adf0",
|
481 |
+
"metadata": {
|
482 |
+
"scrolled": true
|
483 |
+
},
|
484 |
+
"outputs": [],
|
485 |
+
"source": [
|
486 |
+
"r_list[1].to_dict()"
|
487 |
+
]
|
488 |
+
},
|
489 |
+
{
|
490 |
+
"cell_type": "code",
|
491 |
+
"execution_count": null,
|
492 |
+
"id": "0d058fa9-2608-4508-9a6c-dd8ff9387987",
|
493 |
+
"metadata": {},
|
494 |
+
"outputs": [],
|
495 |
+
"source": [
|
496 |
+
"response = query_engine.query(\"What is the meaning of life?\")"
|
497 |
+
]
|
498 |
+
},
|
499 |
+
{
|
500 |
+
"cell_type": "code",
|
501 |
+
"execution_count": null,
|
502 |
+
"id": "ec91bf3e-ce05-4183-9270-e53c1a21ccb4",
|
503 |
+
"metadata": {},
|
504 |
+
"outputs": [],
|
505 |
+
"source": [
|
506 |
+
"print(response)"
|
507 |
+
]
|
508 |
+
},
|
509 |
+
{
|
510 |
+
"cell_type": "code",
|
511 |
+
"execution_count": null,
|
512 |
+
"id": "08fb2be5-3a44-4bb8-a9fc-61d7f03b7a35",
|
513 |
+
"metadata": {},
|
514 |
+
"outputs": [],
|
515 |
+
"source": []
|
516 |
+
},
|
517 |
+
{
|
518 |
+
"cell_type": "markdown",
|
519 |
+
"id": "a7fc01f6-4738-415b-a96b-afd6cf8d789a",
|
520 |
+
"metadata": {},
|
521 |
+
"source": [
|
522 |
+
"### ChromaDB method - load vectorstore"
|
523 |
+
]
|
524 |
+
},
|
525 |
+
{
|
526 |
+
"cell_type": "code",
|
527 |
+
"execution_count": null,
|
528 |
+
"id": "c1a42c35-5f57-423c-8fb7-7d18b3b466b5",
|
529 |
+
"metadata": {},
|
530 |
+
"outputs": [],
|
531 |
+
"source": [
|
532 |
+
"import chromadb\n",
|
533 |
+
"from llama_index import VectorStoreIndex, SimpleDirectoryReader\n",
|
534 |
+
"from llama_index.vector_stores import ChromaVectorStore\n",
|
535 |
+
"from llama_index.storage.storage_context import StorageContext\n",
|
536 |
+
"from llama_index import ServiceContext\n",
|
537 |
+
"from llama_index import Document\n",
|
538 |
+
"\n",
|
539 |
+
"from llama_index.embeddings import HuggingFaceEmbedding\n",
|
540 |
+
"\n",
|
541 |
+
"import time"
|
542 |
+
]
|
543 |
+
},
|
544 |
+
{
|
545 |
+
"cell_type": "code",
|
546 |
+
"execution_count": null,
|
547 |
+
"id": "11ff0889-ef46-4447-ae2e-6fcaaf0733ec",
|
548 |
+
"metadata": {},
|
549 |
+
"outputs": [],
|
550 |
+
"source": [
|
551 |
+
"embed_model = HuggingFaceEmbedding(model_name=\"BAAI/bge-small-en-v1.5\")"
|
552 |
+
]
|
553 |
+
},
|
554 |
+
{
|
555 |
+
"cell_type": "code",
|
556 |
+
"execution_count": null,
|
557 |
+
"id": "ffdbf912-7eb2-429a-a98e-5e3a9e8fe8bd",
|
558 |
+
"metadata": {},
|
559 |
+
"outputs": [],
|
560 |
+
"source": [
|
561 |
+
"service_context = ServiceContext.from_defaults(llm=None, embed_model=embed_model)"
|
562 |
+
]
|
563 |
+
},
|
564 |
+
{
|
565 |
+
"cell_type": "code",
|
566 |
+
"execution_count": null,
|
567 |
+
"id": "b7b302bf-111d-46a0-95e0-a148cb327ad2",
|
568 |
+
"metadata": {},
|
569 |
+
"outputs": [],
|
570 |
+
"source": []
|
571 |
+
},
|
572 |
+
{
|
573 |
+
"cell_type": "code",
|
574 |
+
"execution_count": null,
|
575 |
+
"id": "2159a2b6-494b-41b9-ac54-dd342bfb74ba",
|
576 |
+
"metadata": {},
|
577 |
+
"outputs": [],
|
578 |
+
"source": [
|
579 |
+
"db = chromadb.PersistentClient(path=\"../models/chroma_db\")"
|
580 |
+
]
|
581 |
+
},
|
582 |
+
{
|
583 |
+
"cell_type": "code",
|
584 |
+
"execution_count": null,
|
585 |
+
"id": "1b385644-b46e-4d13-88fa-9f4af39db405",
|
586 |
+
"metadata": {},
|
587 |
+
"outputs": [],
|
588 |
+
"source": [
|
589 |
+
"chroma_collection = db.get_or_create_collection(\"quickstart\")"
|
590 |
+
]
|
591 |
+
},
|
592 |
+
{
|
593 |
+
"cell_type": "code",
|
594 |
+
"execution_count": null,
|
595 |
+
"id": "93cb53d1-6b8c-4b2d-a839-53501c0d54b2",
|
596 |
+
"metadata": {},
|
597 |
+
"outputs": [],
|
598 |
+
"source": [
|
599 |
+
"# assign chroma as the vector_store to the context\n",
|
600 |
+
"vector_store = ChromaVectorStore(chroma_collection=chroma_collection)\n",
|
601 |
+
"storage_context = StorageContext.from_defaults(vector_store=vector_store)"
|
602 |
+
]
|
603 |
+
},
|
604 |
+
{
|
605 |
+
"cell_type": "code",
|
606 |
+
"execution_count": null,
|
607 |
+
"id": "c40d59e1-6d42-41f0-8c9b-70aa026093ae",
|
608 |
+
"metadata": {},
|
609 |
+
"outputs": [],
|
610 |
+
"source": [
|
611 |
+
"# create your index\n",
|
612 |
+
"index = VectorStoreIndex.from_vector_store(\n",
|
613 |
+
" vector_store=vector_store, service_context=service_context, storage_context=storage_context\n",
|
614 |
+
")"
|
615 |
+
]
|
616 |
+
},
|
617 |
+
{
|
618 |
+
"cell_type": "code",
|
619 |
+
"execution_count": null,
|
620 |
+
"id": "78a5c9f6-a63b-40d6-a43f-0cf4bb7b15a6",
|
621 |
+
"metadata": {},
|
622 |
+
"outputs": [],
|
623 |
+
"source": [
|
624 |
+
"# create a query engine\n",
|
625 |
+
"query_engine = index.as_query_engine()"
|
626 |
+
]
|
627 |
+
},
|
628 |
+
{
|
629 |
+
"cell_type": "code",
|
630 |
+
"execution_count": null,
|
631 |
+
"id": "58315ac6-c0bc-424d-b4b0-90123ebc57df",
|
632 |
+
"metadata": {},
|
633 |
+
"outputs": [],
|
634 |
+
"source": []
|
635 |
+
},
|
636 |
+
{
|
637 |
+
"cell_type": "code",
|
638 |
+
"execution_count": null,
|
639 |
+
"id": "7bb7c21a-7461-40c1-87a7-4a1f92f70153",
|
640 |
+
"metadata": {
|
641 |
+
"scrolled": true
|
642 |
+
},
|
643 |
+
"outputs": [],
|
644 |
+
"source": [
|
645 |
+
"response = query_engine.query(\"What is llama2?\")\n",
|
646 |
+
"print(response)"
|
647 |
+
]
|
648 |
+
},
|
649 |
+
{
|
650 |
+
"cell_type": "code",
|
651 |
+
"execution_count": null,
|
652 |
+
"id": "874a39ce-e682-42fa-8085-646bacea6cdb",
|
653 |
+
"metadata": {},
|
654 |
+
"outputs": [],
|
655 |
+
"source": []
|
656 |
+
}
|
657 |
+
],
|
658 |
+
"metadata": {
|
659 |
+
"kernelspec": {
|
660 |
+
"display_name": "Python 3 (ipykernel)",
|
661 |
+
"language": "python",
|
662 |
+
"name": "python3"
|
663 |
+
},
|
664 |
+
"language_info": {
|
665 |
+
"codemirror_mode": {
|
666 |
+
"name": "ipython",
|
667 |
+
"version": 3
|
668 |
+
},
|
669 |
+
"file_extension": ".py",
|
670 |
+
"mimetype": "text/x-python",
|
671 |
+
"name": "python",
|
672 |
+
"nbconvert_exporter": "python",
|
673 |
+
"pygments_lexer": "ipython3",
|
674 |
+
"version": "3.9.18"
|
675 |
+
}
|
676 |
+
},
|
677 |
+
"nbformat": 4,
|
678 |
+
"nbformat_minor": 5
|
679 |
+
}
|