Store each text with its vector, then return the top k matches for a question. This is what a vector database does.
AI · intermediate · 8 min · 45 steps · free
import math
def embed(text):
words = text.lower().split()
return [words.count(w) for w in ['refund', 'ship', 'open', 'receipt']]
class VectorStore:
def __init__(self):
self.items = []
def add(self, text):
self.items.append((text, embed(text)))
def search(self, query, k):
q = embed(query)
scored = []
for text, vec in self.items:
scored.append((math.sumprod(q, vec), text))
scored.sort(reverse=True)
return [text for score, text in scored[:k]]
store = VectorStore()
store.add('refund within 30 days')
store.add('we ship worldwide')
store.add('refund needs a receipt')
print(store.search('can i get a refund with no receipt', 2))math module, so the program can use the functions it provides.embed(). Its body does not run yet: it runs each time the function is called.VectorStore. Its methods are stored, but none of them run yet.class VectorStore belongs to it.__init__ is stored in VectorStore. It runs only when it is called on an object.add is stored in VectorStore. It runs only when it is called on an object.search is stored in VectorStore. It runs only when it is called on an object.VectorStore is now defined, ready to make objects from.__init__(), with self = {}. It gets its own names.__init__() has finished, so its names are gone. The value it returned, {items: a list}, is stored in store.add(), with self = {items: a list} and text = 'refund within 30 days'. It gets its own names.embed(), with text = 'refund within 30 days'. It gets its own names.words is created and points to a new list: ['refund', 'within', '30', 'days'].[words.count(w) for w in ['refund', 'ship', 'open', 'receipt']].add() has finished, so its names are gone.add(), with self = {items: a list} and text = 'we ship worldwide'. It gets its own names.embed(), with text = 'we ship worldwide'. It gets its own names.words is created and points to a new list: ['we', 'ship', 'worldwide'].[words.count(w) for w in ['refund', 'ship', 'open', 'receipt']].embed() has finished, so its names are gone. a tuple is added to the end of the list object.add() has finished, so its names are gone.add(), with self = {items: a list} and text = 'refund needs a receipt'. It gets its own names.embed(), with text = 'refund needs a receipt'. It gets its own names.words is created and points to a new list: ['refund', 'needs', 'a', 'receipt'].[words.count(w) for w in ['refund', 'ship', 'open', 'receipt']].embed() has finished, so its names are gone. a tuple is added to the end of the list object.add() has finished, so its names are gone.search(), with self = {items: a list}, query = 'can i get a refund with no receipt' and k = 2. It gets its own names.embed(), with text = 'can i get a refund with no receipt'. It gets its own names.words is created and points to a new list: ['can', 'i', 'get', 'a', 'refund', 'with', 'no', 'receipt'].[words.count(w) for w in ['refund', 'ship', 'open', 'receipt']].scored is created and points to a new list: [].text is created with the value 'refund within 30 days'. vec is created and points to a new list: [1, 0, 0, 0].text changes from 'refund within 30 days' to 'we ship worldwide'. vec now points to a different list: [0, 1, 0, 0].scored points to.text changes from 'we ship worldwide' to 'refund needs a receipt'. vec now points to a different list: [1, 0, 0, 1].scored points to.for text, vec in self.items.[text for score, text in scored[:k]].Answer: How many chunks come back
Answer: O(n)
← Chunking with Overlap · Keyword vs Meaning: Hybrid Search →