From 50626a10ee5b4cf2b7f7f0463e0edbb8233945d0 Mon Sep 17 00:00:00 2001 From: Harrison Chase Date: Wed, 22 Mar 2023 19:57:56 -0700 Subject: [PATCH] Hx23840 feat/add redisearch vectorstore (#1909) Co-authored-by: Peter Co-authored-by: Peter Shi <42536066+hx23840@users.noreply.github.com> --- .../indexes/vectorstore_examples/redis.ipynb | 65 ++++++++----------- langchain/vectorstores/redis.py | 33 +++++++++- 2 files changed, 58 insertions(+), 40 deletions(-) diff --git a/docs/modules/indexes/vectorstore_examples/redis.ipynb b/docs/modules/indexes/vectorstore_examples/redis.ipynb index 75bad037f3..385ba0b7b6 100644 --- a/docs/modules/indexes/vectorstore_examples/redis.ipynb +++ b/docs/modules/indexes/vectorstore_examples/redis.ipynb @@ -8,10 +8,7 @@ "This notebook shows how to use functionality related to the Redis database." ], "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%% md\n" - } + "collapsed": false } }, { @@ -24,10 +21,7 @@ "from langchain.vectorstores.redis import Redis" ], "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } + "collapsed": false } }, { @@ -44,10 +38,7 @@ "embeddings = OpenAIEmbeddings()" ], "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } + "collapsed": false } }, { @@ -55,13 +46,10 @@ "execution_count": 4, "outputs": [], "source": [ - "rds = Redis.from_documents(docs, embeddings,redis_url=\"redis://localhost:6379\")" + "rds = Redis.from_documents(docs, embeddings, redis_url=\"redis://localhost:6379\", index_name='link')" ], "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } + "collapsed": false } }, { @@ -81,10 +69,14 @@ "rds.index_name" ], "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } + "collapsed": false + } + }, + { + "cell_type": "markdown", + "source": [], + "metadata": { + "collapsed": false } }, { @@ -115,10 +107,7 @@ "print(results[0].page_content)" ], "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } + "collapsed": false } }, { @@ -137,10 +126,7 @@ "print(rds.add_texts([\"Ankush went to Princeton\"]))" ], "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } + "collapsed": false } }, { @@ -161,22 +147,23 @@ "print(results[0].page_content)" ], "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } + "collapsed": false } }, { "cell_type": "code", "execution_count": null, "outputs": [], - "source": [], + "source": [ + "#Query\n", + "rds = Redis.from_existing_index(embeddings, redis_url=\"redis://localhost:6379\", index_name='link')\n", + "\n", + "query = \"What did the president say about Ketanji Brown Jackson\"\n", + "results = rds.similarity_search(query)\n", + "print(results[0].page_content)" + ], "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } + "collapsed": false } } ], @@ -201,4 +188,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} \ No newline at end of file +} diff --git a/langchain/vectorstores/redis.py b/langchain/vectorstores/redis.py index d851c69ca4..1729ebff8c 100644 --- a/langchain/vectorstores/redis.py +++ b/langchain/vectorstores/redis.py @@ -250,7 +250,6 @@ class Redis(VectorStore): client = redis.from_url(url=redis_url, **kwargs) except ValueError as e: raise ValueError(f"Your redis connected error: {e}") - # Check if index exists try: client.ft(index_name).dropindex(delete_documents) @@ -259,3 +258,35 @@ class Redis(VectorStore): except: # noqa # Index not exist return False + + @classmethod + def from_existing_index( + cls, + embedding: Embeddings, + index_name: str, + **kwargs: Any, + ) -> Redis: + redis_url = get_from_dict_or_env(kwargs, "redis_url", "REDIS_URL") + try: + import redis + except ImportError: + raise ValueError( + "Could not import redis python package. " + "Please install it with `pip install redis`." + ) + try: + # We need to first remove redis_url from kwargs, + # otherwise passing it to Redis will result in an error. + kwargs.pop("redis_url") + client = redis.from_url(url=redis_url, **kwargs) + except ValueError as e: + raise ValueError(f"Your redis connected error: {e}") + + # check if redis add redisearch module + if not _check_redis_module_exist(client, "search"): + raise ValueError( + "Could not use redis directly, you need to add search module" + "Please refer [RediSearch](https://redis.io/docs/stack/search/quick_start/)" # noqa + ) + + return cls(redis_url, index_name, embedding.embed_query)