Semantic search with an embedder
Give the store an embedding function and search takes a query: items come back ordered by how similar their text is to it, with a score.
store.put(("faq",), "refunds", {"text": "Refunds take five working days after we receive the return."})
store.put(("faq",), "delivery", {"text": "Parcels are delivered within three days by courier."})
store.put(("faq",), "password", {"text": "Reset your password from the login page."})
for item in store.search(("faq",), query="how long until my refund arrives?"):
print(f"{item.score:.3f} {item.key}")index={"dims": 64, "embed": embed} tells the store to embed every value it saves, as a 64-number vector, and to embed queries the same way. score is the cosine similarity. The refunds answer came first because it shares a word with the query, refund; the others share none, so they score 0.
The stand-in's embed, at the end of memory_model.py, hashes each word to one of 64 slots. Texts with the same words get similar vectors. A real embedding model also places "money back" near "refund", which this one cannot:
for item in store.search(("faq",), query="I want my money back", limit=1):
print(f"{item.score:.3f} {item.key}")store = InMemoryStore(index={"dims": 1536, "embed": "openai:text-embedding-3-small"})With a real embedding model, the index names it, and dims must match the model's vector size.
- Add
fields=["text"]to the index and store a value with another field. - Search with
limit=1for "courier". - Store an item with
index=Falseinputand search for it.
Every expert started right here.