forked from Data-drone/ANZ_LLM_Bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0.3_Vector_DBs.py
149 lines (94 loc) · 3.04 KB
/
0.3_Vector_DBs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Databricks notebook source
# MAGIC %md
# MAGIC # Exploring Vector DBs
# COMMAND ----------
%pip install faiss-cpu wikipedia
# COMMAND ----------
dbutils.library.restartPython()
# COMMAND ----------
# DBTITLE 1,Setup
# MAGIC %run ./utils
# COMMAND ----------
import faiss
import wikipedia
import os
# COMMAND ----------
# MAGIC %md
# MAGIC # Get some sample data
# COMMAND ----------
# Load Sample Data
result = wikipedia.search("Neural networks")
print(result)
# COMMAND ----------
# get the first article
page = wikipedia.page(result[0])
len(page.content.split())
# COMMAND ----------
# MAGIC %md
# MAGIC # Load Embedding Model
# COMMAND ----------
from transformers import AutoTokenizer
model_id = 'mosaicml/mpt-7b'
model_revision = '72e5f594ce36f9cabfa2a9fd8f58b491eb467ee7'
tokenizer = AutoTokenizer.from_pretrained(model_id, cache_dir=dbfs_tmp_cache)
# COMMAND ----------
# MAGIC %md
# MAGIC # Explore tokenization
# COMMAND ----------
tokenizer.encode('test')
# COMMAND ----------
tokenizer.encode('party')
tokenizer.encode('partying')
# this word exists!!!!
tokenizer.encode('Pneumonoultramicroscopicsilicovolcanoconiosis')
# COMMAND ----------
# lets decode a little and see what the codes mean
medical_encode = tokenizer.encode('Pneumonoultramicroscopicsilicovolcanoconiosis')
tokenizer.decode(medical_encode[0])
# COMMAND ----------
tokenizer.encode('I am happily eating pizza all day long')
# COMMAND ----------
# MAGIC %md
# MAGIC # Sentence Encoders for FAISS
# MAGIC Tokenizers from an LLM and for VectorStores are a bit different
# MAGIC SentenceTransformers from Huggingface is focused on the latter.
# COMMAND ----------
from sentence_transformers import SentenceTransformer
# initialize sentence transformer model
model = SentenceTransformer('bert-base-nli-mean-tokens')
# COMMAND ----------
paragraph_form = page.content.split('\n\n')
len(paragraph_form)
# COMMAND ----------
# MAGIC %md
# MAGIC Tokenizations work best when it receives chunks of the same size
# MAGIC
# COMMAND ----------
sentence_encode = model.encode(paragraph_form)
sentence_encode.shape
# COMMAND ----------
# MAGIC %md
# MAGIC # Lets build out a FAISS index
# COMMAND ----------
index = faiss.IndexFlatL2(sentence_encode.shape[1])
# COMMAND ----------
index.add(sentence_encode)
# COMMAND ----------
# now we can search!
num_results = 3
question = 'Were animals used in neural network development'
query_vector = model.encode([question])
score, index_id = index.search(query_vector, num_results)
# COMMAND ----------
# Retrieve Index id
print(f'index ids retrieved are: {index_id}\n')
for x in index_id[0]:
print(f'Entry: {x}')
print(f'{paragraph_form[x]}\n')
# COMMAND ----------
# MAGIC %md
# MAGIC # Discussion
# MAGIC The main goal in this exercise is the find the best snippets.\
# MAGIC Specifically for Vector embeddings there are many algorithms\
# MAGIC You can look at some of the varieties here: https://github.com/facebookresearch/faiss/wiki/Faiss-indexes\
# MAGIC Generally you are trading off between speed of indexing / retrieval and accuracy.