from_texts(
cls,
texts: List[str],
embedding: Embeddings,
metadatas: Optional| 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: None |
config | Optional[RedisConfig] | Default: None |
keys | Optional[List[str]] | Default: None |
return_keys | bool | Default: False |
**kwargs | Any | Default: {} |
Create a RedisVectorStore from a list of texts.
Example:
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.Optional list of metadata dicts associated with the texts.
Optional RedisConfig object. If not provided, one will be created
from kwargs.
Optional list of keys to associate with the documents.
Whether to return the keys of the added documents.
Additional keyword arguments to pass to RedisConfig if config is
not provided.
Common kwargs include:
index_name: Name of the Redis index to create.redis_url: URL of the Redis instance to connect to.distance_metric: Distance metric to use for similarity search.
'COSINE'.indexing_algorithm: Indexing algorithm to use.
'FLAT'.