PythiloLesson 3 of 9 · progress kept in this browser

Retrieval

A model only knows what it was trained on. To answer from your own notes, a system first retrieves the passages that fit the question, then gives them to the model with the question. This is retrieval-augmented generation, RAG.

Real systems use learned embeddings. The idea works with the simplest vectors too: count each word.

import re

text = "Cats sleep. Cats purr."
words = re.findall(r"[a-z]+", text.lower())
print(words)
# ['cats', 'sleep', 'cats', 'purr']

Give every word in the documents a position, turn each text into a vector of counts, and rank the documents by cosine similarity to the question. The best match goes into the prompt.

Your task

Write words(text) returning the lowercase words (runs of letters a to z). Write vectorize(text, vocab) returning a float tensor with the count of each vocabulary word in the text, in vocab's order. Write retrieve(question, docs, k=1) that builds the vocabulary from all the documents' words (sorted), and returns the k documents most similar to the question by cosine similarity, best first.

This lesson uses torch, which Python in this browser does not include. Read it here, and run and check it in the Pythilo app on iPhone or iPad.

Next: Sampling