Hx23840 feat/add redisearch vectorstore (#1909)

Co-authored-by: Peter <peter.shi@alephf.com>
Co-authored-by: Peter Shi <42536066+hx23840@users.noreply.github.com>
This commit is contained in:
Harrison Chase 2023-03-22 19:57:56 -07:00 committed by GitHub
parent 6e1b5b8f7e
commit 50626a10ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 40 deletions

View File

@ -8,10 +8,7 @@
"This notebook shows how to use functionality related to the Redis database." "This notebook shows how to use functionality related to the Redis database."
], ],
"metadata": { "metadata": {
"collapsed": false, "collapsed": false
"pycharm": {
"name": "#%% md\n"
}
} }
}, },
{ {
@ -24,10 +21,7 @@
"from langchain.vectorstores.redis import Redis" "from langchain.vectorstores.redis import Redis"
], ],
"metadata": { "metadata": {
"collapsed": false, "collapsed": false
"pycharm": {
"name": "#%%\n"
}
} }
}, },
{ {
@ -44,10 +38,7 @@
"embeddings = OpenAIEmbeddings()" "embeddings = OpenAIEmbeddings()"
], ],
"metadata": { "metadata": {
"collapsed": false, "collapsed": false
"pycharm": {
"name": "#%%\n"
}
} }
}, },
{ {
@ -55,13 +46,10 @@
"execution_count": 4, "execution_count": 4,
"outputs": [], "outputs": [],
"source": [ "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": { "metadata": {
"collapsed": false, "collapsed": false
"pycharm": {
"name": "#%%\n"
}
} }
}, },
{ {
@ -81,10 +69,14 @@
"rds.index_name" "rds.index_name"
], ],
"metadata": { "metadata": {
"collapsed": false, "collapsed": false
"pycharm": { }
"name": "#%%\n" },
} {
"cell_type": "markdown",
"source": [],
"metadata": {
"collapsed": false
} }
}, },
{ {
@ -115,10 +107,7 @@
"print(results[0].page_content)" "print(results[0].page_content)"
], ],
"metadata": { "metadata": {
"collapsed": false, "collapsed": false
"pycharm": {
"name": "#%%\n"
}
} }
}, },
{ {
@ -137,10 +126,7 @@
"print(rds.add_texts([\"Ankush went to Princeton\"]))" "print(rds.add_texts([\"Ankush went to Princeton\"]))"
], ],
"metadata": { "metadata": {
"collapsed": false, "collapsed": false
"pycharm": {
"name": "#%%\n"
}
} }
}, },
{ {
@ -161,22 +147,23 @@
"print(results[0].page_content)" "print(results[0].page_content)"
], ],
"metadata": { "metadata": {
"collapsed": false, "collapsed": false
"pycharm": {
"name": "#%%\n"
}
} }
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"outputs": [], "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": { "metadata": {
"collapsed": false, "collapsed": false
"pycharm": {
"name": "#%%\n"
}
} }
} }
], ],
@ -201,4 +188,4 @@
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 0
} }

View File

@ -250,7 +250,6 @@ class Redis(VectorStore):
client = redis.from_url(url=redis_url, **kwargs) client = redis.from_url(url=redis_url, **kwargs)
except ValueError as e: except ValueError as e:
raise ValueError(f"Your redis connected error: {e}") raise ValueError(f"Your redis connected error: {e}")
# Check if index exists # Check if index exists
try: try:
client.ft(index_name).dropindex(delete_documents) client.ft(index_name).dropindex(delete_documents)
@ -259,3 +258,35 @@ class Redis(VectorStore):
except: # noqa except: # noqa
# Index not exist # Index not exist
return False 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)