a0c8464e58
Build Package / build (ubuntu-latest) (push) Failing after 1s
CodeQL / Analyze (python) (push) Failing after 1s
Core Typecheck / core-typecheck (push) Failing after 1s
Linting / lint (push) Failing after 1s
llama-dev tests / test-llama-dev (push) Failing after 1s
Publish Sub-Package to PyPI if Needed / publish_subpackage_if_needed (push) Has been skipped
Sync Docs to Developer Hub / sync-docs (push) Failing after 0s
Build Package / build (windows-latest) (push) Has been cancelled
374 lines
12 KiB
Plaintext
374 lines
12 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"id": "38b4bd73",
|
|
"metadata": {},
|
|
"source": [
|
|
"<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/vector_stores/ZepIndexDemo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"id": "2f0a7faf6e43d4c1",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Zep Vector Store\n",
|
|
"\n",
|
|
"## A long-term memory store for LLM applications\n",
|
|
"\n",
|
|
"This notebook demonstrates how to use the Zep Vector Store with LlamaIndex.\n",
|
|
"\n",
|
|
"## About Zep\n",
|
|
"\n",
|
|
"Zep makes it easy for developers to add relevant documents, chat history memory & rich user data to their LLM app's prompts.\n",
|
|
"\n",
|
|
"## Note\n",
|
|
"\n",
|
|
"Zep can automatically embed your documents. The LlamaIndex implementation of the Zep Vector Store utilizes LlamaIndex's embedders to do so.\n",
|
|
"\n",
|
|
"## Getting Started\n",
|
|
"\n",
|
|
"**Quick Start Guide:** https://docs.getzep.com/deployment/quickstart/\n",
|
|
"**GitHub:** https://github.com/getzep/zep\n"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"id": "bca41fe0",
|
|
"metadata": {},
|
|
"source": [
|
|
"If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "aa418deb",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"%pip install llama-index-vector-stores-zep"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "7b4a2b47",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"!pip install llama-index"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "1bb0627e",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# !pip install zep-python"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "b6547bd146426a4a",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import logging\n",
|
|
"import sys\n",
|
|
"from uuid import uuid4\n",
|
|
"\n",
|
|
"logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n",
|
|
"logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))\n",
|
|
"\n",
|
|
"import os\n",
|
|
"import openai\n",
|
|
"from dotenv import load_dotenv\n",
|
|
"\n",
|
|
"load_dotenv()\n",
|
|
"\n",
|
|
"# os.environ[\"OPENAI_API_KEY\"] = \"sk-...\"\n",
|
|
"openai.api_key = os.environ[\"OPENAI_API_KEY\"]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "e69fa431d972ca88",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"INFO:numexpr.utils:NumExpr defaulting to 8 threads.\n",
|
|
"NumExpr defaulting to 8 threads.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from llama_index.core import VectorStoreIndex, SimpleDirectoryReader\n",
|
|
"from llama_index.vector_stores.zep import ZepVectorStore"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"id": "0a797bf8",
|
|
"metadata": {},
|
|
"source": [
|
|
"Download Data"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "31416430",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"!mkdir -p 'data/paul_graham/'\n",
|
|
"!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "43aeb87a167a31e4",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# load documents\n",
|
|
"documents = SimpleDirectoryReader(\"../data/paul_graham/\").load_data()"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"id": "5285bd54baf41918",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Create a Zep Vector Store and Index\n",
|
|
"\n",
|
|
"You can use an existing Zep Collection, or create a new one.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "c8d4cea738a5e867",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"INFO:httpx:HTTP Request: GET http://localhost:8000/healthz \"HTTP/1.1 200 OK\"\n",
|
|
"HTTP Request: GET http://localhost:8000/healthz \"HTTP/1.1 200 OK\"\n",
|
|
"INFO:httpx:HTTP Request: GET http://localhost:8000/api/v1/collection/grahamfbf0c456a2ad46c2887a707ccc7bb5df \"HTTP/1.1 404 Not Found\"\n",
|
|
"HTTP Request: GET http://localhost:8000/api/v1/collection/grahamfbf0c456a2ad46c2887a707ccc7bb5df \"HTTP/1.1 404 Not Found\"\n",
|
|
"INFO:llama_index.vector_stores.zep:Collection grahamfbf0c456a2ad46c2887a707ccc7bb5df does not exist, will try creating one with dimensions=1536\n",
|
|
"Collection grahamfbf0c456a2ad46c2887a707ccc7bb5df does not exist, will try creating one with dimensions=1536\n",
|
|
"INFO:httpx:HTTP Request: POST http://localhost:8000/api/v1/collection/grahamfbf0c456a2ad46c2887a707ccc7bb5df \"HTTP/1.1 200 OK\"\n",
|
|
"HTTP Request: POST http://localhost:8000/api/v1/collection/grahamfbf0c456a2ad46c2887a707ccc7bb5df \"HTTP/1.1 200 OK\"\n",
|
|
"INFO:httpx:HTTP Request: GET http://localhost:8000/api/v1/collection/grahamfbf0c456a2ad46c2887a707ccc7bb5df \"HTTP/1.1 200 OK\"\n",
|
|
"HTTP Request: GET http://localhost:8000/api/v1/collection/grahamfbf0c456a2ad46c2887a707ccc7bb5df \"HTTP/1.1 200 OK\"\n",
|
|
"INFO:httpx:HTTP Request: POST http://localhost:8000/api/v1/collection/grahamfbf0c456a2ad46c2887a707ccc7bb5df/document \"HTTP/1.1 200 OK\"\n",
|
|
"HTTP Request: POST http://localhost:8000/api/v1/collection/grahamfbf0c456a2ad46c2887a707ccc7bb5df/document \"HTTP/1.1 200 OK\"\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from llama_index.core import StorageContext\n",
|
|
"\n",
|
|
"zep_api_url = \"http://localhost:8000\"\n",
|
|
"collection_name = f\"graham{uuid4().hex}\"\n",
|
|
"\n",
|
|
"vector_store = ZepVectorStore(\n",
|
|
" api_url=zep_api_url,\n",
|
|
" collection_name=collection_name,\n",
|
|
" embedding_dimensions=1536,\n",
|
|
")\n",
|
|
"\n",
|
|
"storage_context = StorageContext.from_defaults(vector_store=vector_store)\n",
|
|
"\n",
|
|
"index = VectorStoreIndex.from_documents(\n",
|
|
" documents, storage_context=storage_context\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "c72041211dcbdedc",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"INFO:httpx:HTTP Request: POST http://localhost:8000/api/v1/collection/grahamfbf0c456a2ad46c2887a707ccc7bb5df/search?limit=2 \"HTTP/1.1 200 OK\"\n",
|
|
"HTTP Request: POST http://localhost:8000/api/v1/collection/grahamfbf0c456a2ad46c2887a707ccc7bb5df/search?limit=2 \"HTTP/1.1 200 OK\"\n",
|
|
"The author worked on writing and programming outside of school before college. They wrote short stories and tried writing programs on an IBM 1401 computer using an early version of Fortran. They later got a microcomputer and started programming more extensively, writing simple games, a program to predict rocket heights, and a word processor. They initially planned to study philosophy in college but switched to AI. They also started publishing essays online and realized the potential of the web as a medium for publishing.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"query_engine = index.as_query_engine()\n",
|
|
"response = query_engine.query(\"What did the author do growing up?\")\n",
|
|
"\n",
|
|
"print(str(response))"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"id": "e0732f58bfe12bfd",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Querying with Metadata filters\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "356e0ec08af5c035",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from llama_index.core.schema import TextNode\n",
|
|
"\n",
|
|
"nodes = [\n",
|
|
" TextNode(\n",
|
|
" text=\"The Shawshank Redemption\",\n",
|
|
" metadata={\n",
|
|
" \"author\": \"Stephen King\",\n",
|
|
" \"theme\": \"Friendship\",\n",
|
|
" },\n",
|
|
" ),\n",
|
|
" TextNode(\n",
|
|
" text=\"The Godfather\",\n",
|
|
" metadata={\n",
|
|
" \"director\": \"Francis Ford Coppola\",\n",
|
|
" \"theme\": \"Mafia\",\n",
|
|
" },\n",
|
|
" ),\n",
|
|
" TextNode(\n",
|
|
" text=\"Inception\",\n",
|
|
" metadata={\n",
|
|
" \"director\": \"Christopher Nolan\",\n",
|
|
" },\n",
|
|
" ),\n",
|
|
"]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "18afce2b836a8dd",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"INFO:httpx:HTTP Request: GET http://localhost:8000/healthz \"HTTP/1.1 200 OK\"\n",
|
|
"HTTP Request: GET http://localhost:8000/healthz \"HTTP/1.1 200 OK\"\n",
|
|
"INFO:httpx:HTTP Request: GET http://localhost:8000/api/v1/collection/movies40ffd4f8a68c4822ae1680bb752c07e1 \"HTTP/1.1 404 Not Found\"\n",
|
|
"HTTP Request: GET http://localhost:8000/api/v1/collection/movies40ffd4f8a68c4822ae1680bb752c07e1 \"HTTP/1.1 404 Not Found\"\n",
|
|
"INFO:llama_index.vector_stores.zep:Collection movies40ffd4f8a68c4822ae1680bb752c07e1 does not exist, will try creating one with dimensions=1536\n",
|
|
"Collection movies40ffd4f8a68c4822ae1680bb752c07e1 does not exist, will try creating one with dimensions=1536\n",
|
|
"INFO:httpx:HTTP Request: POST http://localhost:8000/api/v1/collection/movies40ffd4f8a68c4822ae1680bb752c07e1 \"HTTP/1.1 200 OK\"\n",
|
|
"HTTP Request: POST http://localhost:8000/api/v1/collection/movies40ffd4f8a68c4822ae1680bb752c07e1 \"HTTP/1.1 200 OK\"\n",
|
|
"INFO:httpx:HTTP Request: GET http://localhost:8000/api/v1/collection/movies40ffd4f8a68c4822ae1680bb752c07e1 \"HTTP/1.1 200 OK\"\n",
|
|
"HTTP Request: GET http://localhost:8000/api/v1/collection/movies40ffd4f8a68c4822ae1680bb752c07e1 \"HTTP/1.1 200 OK\"\n",
|
|
"INFO:httpx:HTTP Request: POST http://localhost:8000/api/v1/collection/movies40ffd4f8a68c4822ae1680bb752c07e1/document \"HTTP/1.1 200 OK\"\n",
|
|
"HTTP Request: POST http://localhost:8000/api/v1/collection/movies40ffd4f8a68c4822ae1680bb752c07e1/document \"HTTP/1.1 200 OK\"\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"collection_name = f\"movies{uuid4().hex}\"\n",
|
|
"\n",
|
|
"vector_store = ZepVectorStore(\n",
|
|
" api_url=zep_api_url,\n",
|
|
" collection_name=collection_name,\n",
|
|
" embedding_dimensions=1536,\n",
|
|
")\n",
|
|
"\n",
|
|
"storage_context = StorageContext.from_defaults(vector_store=vector_store)\n",
|
|
"index = VectorStoreIndex(nodes, storage_context=storage_context)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "32138298211dddee",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from llama_index.core.vector_stores import ExactMatchFilter, MetadataFilters\n",
|
|
"\n",
|
|
"filters = MetadataFilters(\n",
|
|
" filters=[ExactMatchFilter(key=\"theme\", value=\"Mafia\")]\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "4fa27e386c42f215",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"INFO:httpx:HTTP Request: POST http://localhost:8000/api/v1/collection/movies40ffd4f8a68c4822ae1680bb752c07e1/search?limit=2 \"HTTP/1.1 200 OK\"\n",
|
|
"HTTP Request: POST http://localhost:8000/api/v1/collection/movies40ffd4f8a68c4822ae1680bb752c07e1/search?limit=2 \"HTTP/1.1 200 OK\"\n",
|
|
"\n",
|
|
" Node ID: 2b5ad50a-8ec0-40fa-b401-6e6b7ac3d304\n",
|
|
"Text: The Godfather\n",
|
|
"Score: 0.8841066656525941\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"retriever = index.as_retriever(filters=filters)\n",
|
|
"result = retriever.retrieve(\"What is inception about?\")\n",
|
|
"\n",
|
|
"for r in result:\n",
|
|
" print(\"\\n\", r.node)\n",
|
|
" print(\"Score:\", r.score)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|