How AI Language Models Work · Building with Models
A long document is cut into overlapping chunks, and only the chunk that matches the question goes into the prompt.
# Long documents are cut into chunks, so retrieval can find just the part that answers.
doc = "our shop opens at 9 am . returns are free for 30 days . we ship to india and nepal . gift cards never expire"
words = doc.split(" ")
size, overlap = 8, 2
chunks = []
for start in range(0, len(words), size - overlap):
chunks.append(" ".join(words[start:start + size]))
question = "do gift cards expire"
scores = [len(set(question.split(" ")) & set(c.split(" "))) for c in chunks]
best = chunks[scores.index(max(scores))]
print(best)chunks is created and points to a new list: [].start changes from 0 to 6.start changes from 6 to 12.chunks points to.start changes from 12 to 18.start changes from 18 to 24.for start in range(0, len(words), size - overlap).best is created with the value 'and nepal . gift cards never expire'.