Create a RedisVectorStore from a list of texts.
from_texts(
cls,
texts: List[str],
embedding: Embeddings,
metadatas: Optional[List[Dict[str, Any]]] = None,
config: Optional[RedisConfig] = None,
keys: Optional[List[str]] = None,
return_keys: bool = False,
**kwargs: Any = {}
) -> RedisVectorStoreExample:
from langchain_redis import RedisVectorStore
from langchain_openai import OpenAIEmbeddings
texts = [
"The quick brown fox jumps over the lazy dog",
"Hello world",
"Machine learning is fascinating"
]
metadatas = [
{"source": "book", "page": 1},
{"source": "greeting", "language": "english"},
{"source": "article", "topic": "AI"}
]
embeddings = OpenAIEmbeddings()
vector_store = RedisVectorStore.from_texts(
texts=texts,
embedding=embeddings,
metadatas=metadatas,
index_name="langchain-demo",
redis_url="redis://localhost:6379",
distance_metric="COSINE"
)
# Now you can use the vector_store for similarity search
results = vector_store.similarity_search("AI and machine learning", k=1)
print(results[0].page_content)
Note:
RedisVectorStore instance and adds the
provided texts to it.metadatas is provided, it must have the same length as texts.keys is provided, it must have the same length as texts.return_keys parameter determines whether the method returns just the
RedisVectorStore instance or a tuple of
(RedisVectorStore, List[str]) where the second element is the list of
keys for the added documents.| Name | Type | Description |
|---|---|---|
texts* | List[str] | List of texts to add to the vector store. |
embedding* | Embeddings | Embedding function to use for encoding the texts. |
metadatas | Optional[List[Dict[str, Any]]] | Default: NoneOptional list of metadata dicts associated with the texts. |
config | Optional[RedisConfig] | Default: NoneOptional |
keys | Optional[List[str]] | Default: NoneOptional list of keys to associate with the documents. |
return_keys | bool | Default: FalseWhether to return the keys of the added documents. |
**kwargs | Any | Default: {}Additional keyword arguments to pass to Common kwargs include:
|