mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-05 21:12:48 +00:00
Add redis self-query support (#10199)
This commit is contained in:
@@ -10,9 +10,9 @@
|
||||
"\n",
|
||||
"## What is Redis?\n",
|
||||
"\n",
|
||||
"Most developers from a web services background are probably familiar with Redis. At it's core, Redis is an open-source key-value store that can be used as a cache, message broker, and database. Developers choice Redis because it is fast, has a large ecosystem of client libraries, and has been deployed by major enterprises for years.\n",
|
||||
"Most developers from a web services background are probably familiar with Redis. At it's core, Redis is an open-source key-value store that can be used as a cache, message broker, and database. Developers choose Redis because it is fast, has a large ecosystem of client libraries, and has been deployed by major enterprises for years.\n",
|
||||
"\n",
|
||||
"In addition to the traditional uses of Redis. Redis also provides capabilities built directly into Redis. These capabilities include the Search and Query capability that allows users to create secondary index structures within Redis. This allows Redis to be a Vector Database, at the speed of a cache. \n",
|
||||
"On top of these traditional use cases, Redis provides additional capabilities like the Search and Query capability that allows users to create secondary index structures within Redis. This allows Redis to be a Vector Database, at the speed of a cache. \n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Redis as a Vector Database\n",
|
||||
@@ -123,7 +123,7 @@
|
||||
"source": [
|
||||
"## Install Redis Python Client\n",
|
||||
"\n",
|
||||
"Redis-py is the officially supported client by Redis. Recently released is the RedisVL client which is purpose built for the Vector Database use cases. Both can be installed with pip."
|
||||
"Redis-py is the officially supported client by Redis. Recently released is the RedisVL client which is purpose-built for the Vector Database use cases. Both can be installed with pip."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -153,9 +153,17 @@
|
||||
"import os\n",
|
||||
"import getpass\n",
|
||||
"\n",
|
||||
"os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n",
|
||||
"\n",
|
||||
"os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.embeddings import OpenAIEmbeddings\n",
|
||||
"\n",
|
||||
"embeddings = OpenAIEmbeddings()"
|
||||
]
|
||||
},
|
||||
@@ -215,6 +223,12 @@
|
||||
"source": [
|
||||
"## Initializing Redis\n",
|
||||
"\n",
|
||||
"To locally deploy Redis, run:\n",
|
||||
"```console\n",
|
||||
"docker run -d -p 6379:6379 -p 8001:8001 redis/redis-stack:latest\n",
|
||||
"```\n",
|
||||
"If things are running correctly you should see a nice Redis UI at http://localhost:8001. See the [Deployment Options](#deployment-options) section above for other ways to deploy.\n",
|
||||
"\n",
|
||||
"The Redis VectorStore instance can be initialized in a number of ways. There are multiple class methods that can be used to initialize a Redis VectorStore instance.\n",
|
||||
"\n",
|
||||
"- ``Redis.__init__`` - Initialize directly\n",
|
||||
@@ -223,7 +237,7 @@
|
||||
"- ``Redis.from_texts_return_keys`` - Initialize from a list of texts (optionally with metadata) and return the keys\n",
|
||||
"- ``Redis.from_existing_index`` - Initialize from an existing Redis index\n",
|
||||
"\n",
|
||||
"Below we will use the ``Redis.from_documents`` method."
|
||||
"Below we will use the ``Redis.from_texts`` method."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -234,28 +248,12 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.vectorstores.redis import Redis"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"If you're not interested in the keys of your entries you can also create your redis instance from the documents."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.docstore.document import Document\n",
|
||||
"from langchain.vectorstores.redis import Redis\n",
|
||||
"\n",
|
||||
"documents = [Document(page_content=t, metadata=m) for t, m in zip(texts, metadata)]\n",
|
||||
"rds = Redis.from_documents(\n",
|
||||
" documents,\n",
|
||||
"rds = Redis.from_texts(\n",
|
||||
" texts,\n",
|
||||
" embeddings,\n",
|
||||
" metadatas=metadats,\n",
|
||||
" redis_url=\"redis://localhost:6379\",\n",
|
||||
" index_name=\"users\"\n",
|
||||
")"
|
||||
@@ -454,7 +452,7 @@
|
||||
"results = rds.similarity_search(\"foo\", k=3)\n",
|
||||
"meta = results[1].metadata\n",
|
||||
"print(\"Key of the document in Redis: \", meta.pop(\"id\"))\n",
|
||||
"print(\"Metadata of the document: \", meta)\n"
|
||||
"print(\"Metadata of the document: \", meta)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1229,7 +1227,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.13"
|
||||
"version": "3.11.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
Reference in New Issue
Block a user