Query Zep Collections for LLM Data
LlamaIndex reader that queries an existing Zep long-term memory collection.
Why it matters
Integrate your LLM applications with Zep, a long-term memory store. This asset allows you to easily retrieve relevant documents and chat history from Zep collections to enhance your LLM prompts.
Outcomes
What it gets done
Connect to a Zep API endpoint.
Query Zep collections by text or embeddings.
Retrieve top-k relevant documents from Zep.
Load data from Zep Document Collections.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/li-reader-readers-zep | bash Overview
Zep Reader
The Zep Reader queries an existing Zep Document Collection by text or embedding, returning the top matching chunks for use in a LlamaIndex or LLM prompt, backed by Zep's long-term memory store. Use it when you have a populated, embedded Zep collection and need to query it for relevant results. It requires a running Zep instance.
What it does
The Zep Reader returns texts matching a text query or embeddings, retrieved from a Zep Document Collection. Zep is a long-term memory store for LLM applications, making it easy to add relevant documents, chat history memory, and rich user data to an LLM app's prompts. You initialize ZepReader with a Zep API URL and, optionally, an API key, then use it to load data from a collection.
When to use - and when NOT to
Use it when you already have a populated Zep Document Collection and need to query it, by text or embedding, for the top matching results to feed into a LlamaIndex or LLM prompt. It reads from an existing collection rather than creating one, so it is not usable without a running Zep instance and a collection that already has embedded documents.
Inputs and outputs
Install with:
pip install llama-index-readers-zep
An end-to-end flow: create a Zep collection, chunk a document and add it, wait for Zep's async embedder to finish, then query with ZepReader:
from zep_python import ZepClient
from zep_python.document import Document as ZepDocument
from llama_index.readers.zep import ZepReader
zep_api_url = "http://localhost:8000"
client = ZepClient(base_url=zep_api_url, api_key="optional_api_key")
collection = client.document.add_collection(
name=collection_name,
description="Babbage's Calculating Engine",
metadata={"foo": "bar"},
embedding_dimensions=1536,
is_auto_embedded=True,
)
### chunk your document, convert to ZepDocument, then add it:
uuids = collection.add_documents(zep_docs)
### wait until c.status == "ready", then query:
query = "Was Babbage awarded a medal?"
reader = ZepReader(api_url=zep_api_url, api_key="optional_api_key")
results = reader.load_data(
collection_name=collection_name, query=query, top_k=3
)
Creating a collection takes a name, an optional description and metadata, embedding_dimensions (must match your configured embedding model), and is_auto_embedded (defaults to True, using Zep's built-in embedder). Once documents are added and embedded, checkable via collection.status == "ready", ZepReader.load_data takes a collection_name, a query, and a top_k count of results to return.
Who it's for
Developers building LlamaIndex pipelines that need long-term memory or document retrieval backed by Zep, querying a collection for the most relevant chunks to a question.
Source README
Zep Reader
pip install llama-index-readers-zep
The Zep Reader returns a set of texts corresponding to a text query or embeddings retrieved from a Zep Collection.
The Reader is initialized with a Zep API URL and optionally an API key. The Reader can then be used to load data
from a Zep Document Collection.
About Zep
Zep is a long-term memory store for LLM applications. Zep makes it simple to add relevant documents, chat history memory
and rich user data to your LLM app's prompts.
For more information about Zep and the Zep Quick Start Guide, see the Zep documentation.
Usage
Here's an end-to-end example usage of the ZepReader. First, we create a Zep Collection, chunk a document,
and add it to the collection.
We then wait for Zep's async embedder to embed the document chunks. Finally, we query the collection and print the
results.
import time
from uuid import uuid4
from llama_index.core.node_parser import SimpleNodeParser
from llama_index.core import Document
from zep_python import ZepClient
from zep_python.document import Document as ZepDocument
from llama_index.readers.zep import ZepReader
### Create a Zep collection
zep_api_url = "http://localhost:8000" # replace with your Zep API URL
collection_name = f"babbage{uuid4().hex}"
file = "babbages_calculating_engine.txt"
print(f"Creating collection {collection_name}")
client = ZepClient(base_url=zep_api_url, api_key="optional_api_key")
collection = client.document.add_collection(
name=collection_name, # required
description="Babbage's Calculating Engine", # optional
metadata={"foo": "bar"}, # optional metadata
embedding_dimensions=1536, # this must match the model you've configured in Zep
is_auto_embedded=True, # use Zep's built-in embedder. Defaults to True
)
node_parser = SimpleNodeParser.from_defaults(chunk_size=250, chunk_overlap=20)
with open(file) as f:
raw_text = f.read()
print("Splitting text into chunks and adding them to the Zep vector store.")
docs = node_parser.get_nodes_from_documents(
[Document(text=raw_text)], show_progress=True
)
### Convert nodes to ZepDocument
zep_docs = [ZepDocument(content=d.get_content()) for d in docs]
uuids = collection.add_documents(zep_docs)
print(f"Added {len(uuids)} documents to collection {collection_name}")
print("Waiting for documents to be embedded")
while True:
c = client.document.get_collection(collection_name)
print(
"Embedding status: "
f"{c.document_embedded_count}/{c.document_count} documents embedded"
)
time.sleep(1)
if c.status == "ready":
break
query = "Was Babbage awarded a medal?"
### Using the ZepReader to load data from Zep
reader = ZepReader(api_url=zep_api_url, api_key="optional_api_key")
results = reader.load_data(
collection_name=collection_name, query=query, top_k=3
)
print("\n\n".join([r.text for r in results]))
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.