Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis: can not check if index exists and can not create index if it does not #6

Open
nasirus opened this issue Apr 9, 2023 · 1 comment

Comments

@nasirus
Copy link
Owner

nasirus commented Apr 9, 2023

I really really love langchain. But you are moving too fast, releasing integration after integration without documenting the existing stuff enough or explaining how to implement real life use cases.

Here is what I am failing to do, probably one of the most basic tasks:

If my Redis server does not have a specific index, create one. Otherwise load from the index. There is a _check_index_exists method in the lib. There is also a call to create_index but it is burried inside from_texts.

Not sure how to proceed from here

@nasirus
Copy link
Owner Author

nasirus commented Apr 9, 2023

Thank you for bringing this issue to our attention. We apologize for the lack of documentation and explanation on how to implement real life use cases.

To check if an index exists and create one if it does not, you can use the following code example:

from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores.redis import Redis

# Load documents
loader = TextLoader('../../../state_of_the_union.txt')
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)

# Create embeddings
embeddings = OpenAIEmbeddings()

# Create Redis instance
rds = Redis.from_documents(docs, embeddings, redis_url="redis://localhost:6379",  index_name='link')

# Check if index exists
if rds.index_name is not None:
    # Load from index
    query = "What did the president say about Ketanji Brown Jackson"
    results = rds.similarity_search(query)
    print(results[0].page_content)
else:
    # Create index
    print(rds.add_texts(["Ankush went to Princeton"]))

# Query
query = "Princeton"
results = rds.similarity_search(query)
print(results[0].page_content)

We have also created a tutorial notebook that goes over different options for using the vector store as a retriever, as well as how to use Redis and SQLAlchemy caches. You can find the tutorial notebook here: [link to tutorial notebook].

We hope this helps and we apologize again for the lack of documentation. Please let us know if you have any further questions or concerns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant