LangMemLangMem 0.0.30 · LangGraph 1.2 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
18 small wins to finish your pathNext lesson

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.

Example
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:

Example
for item in store.search(("faq",), query="I want my money back", limit=1):
    print(f"{item.score:.3f} {item.key}")
Example
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.

Try it yourself
  • Add fields=["text"] to the index and store a value with another field.
  • Search with limit=1 for "courier".
  • Store an item with index=False in put and search for it.

Every expert started right here.