forked from Archives/langchain
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:
parent
6e1b5b8f7e
commit
50626a10ee
@ -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
|
||||
}
|
||||
}
|
||||
],
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user