Tony20100/langchain_full huggingface dataset:
Return VectorStore initialized from documents and embeddings.
from_texts(texts, embedding[, metadatas, ...])
This is a user friendly interface that:
max_marginal_relevance_search(query[, k, ...])
Return docs selected using the maximal marginal relevance.
max_marginal_relevance_search_by_vector(...)
Return docs selected using the maximal marginal relevance.
search(query, search_type, **kwargs)
Return docs most similar to query using specified search type.
similarity_search(query[, k, metadata_keys, ...])
Return aerospike documents most similar to query.
similarity_search_by_vector(embedding[, k, ...])
Return docs most similar to embedding vector.
similarity_search_by_vector_with_score(embedding)
Return aerospike documents most similar to embedding, along with scores.
similarity_search_with_relevance_scores(query)
Return docs and relevance scores in the range [0, 1].
similarity_search_with_score(query[, k, ...])
Return aerospike documents most similar to query, along with scores.
__init__(client: Client, embedding: Union[Embeddings, Callable], namespace: str, index_name: Optional[str] = None, vector_key: str = '_vector', text_key: str = '_text', id_key: str = '_id', set_name: Optional[str] = None, distance_strategy: Optional[Union[DistanceStrategy, VectorDistanceMetric]] = DistanceStrategy.EUCLIDEAN_DISTANCE)[source]¶
Initialize with Aerospike client.
Parameters
client (Client) – Aerospike client.
embedding (Union[Embeddings, Callable]) – Embeddings object or Callable (deprecated) to embed text.
namespace (str) – Namespace to use for storing vectors. This should match
namespace (str) – Namespace to use for storing vectors. This should match
index_name (Optional[str]) – Name of the index previously created in Aerospike. This
vector_key (str) – Key to use for vector in metadata. This should match the
key used during index creation.
text_key (str) – Key to use for text in metadata.
id_key (str) – Key to use for id in metadata.
set_name (Optional[str]) – Default set name to use for storing vectors.
distance_strategy (Optional[Union[DistanceStrategy, VectorDistanceMetric]]) – Distance strategy to use for similarity search
This should match the distance strategy used during index creation.
async aadd_documents(documents: List[Document], **kwargs: Any) → List[str]¶
Run more documents through the embeddings and add to the vectorstore.
Parameters
documents (List[Document]) – Documents to add to the vectorstore.
kwargs (Any) – 
Returns
List of IDs of the added texts.
Return type
List[str]
async aadd_texts(texts: Iterable[str], metadatas: Optional[List[dict]] = None, **kwargs: Any) → List[str]¶
Run more texts through the embeddings and add to the vectorstore.
Parameters
texts (Iterable[str]) – Iterable of strings to add to the vectorstore.
metadatas (Optional[List[dict]]) – Optional list of metadatas associated with the texts.
**kwargs (Any) – vectorstore specific parameters.
Returns
List of ids from adding the texts into the vectorstore.
Return type
List[str]
add_documents(documents: List[Document], **kwargs: Any) → List[str]¶
Run more documents through the embeddings and add to the vectorstore.
Parameters
documents (List[Document]) – Documents to add to the vectorstore.
Parameters
documents (List[Document]) – Documents to add to the vectorstore.
kwargs (Any) – 
Returns
List of IDs of the added texts.
Return type
List[str]
add_texts(texts: Iterable[str], metadatas: Optional[List[dict]] = None, ids: Optional[List[str]] = None, set_name: Optional[str] = None, embedding_chunk_size: int = 1000, index_name: Optional[str] = None, wait_for_index: bool = True, **kwargs: Any) → List[str][source]¶
Run more texts through the embeddings and add to the vectorstore.
Parameters
texts (Iterable[str]) – Iterable of strings to add to the vectorstore.
metadatas (Optional[List[dict]]) – Optional list of metadatas associated with the texts.
ids (Optional[List[str]]) – Optional list of ids to associate with the texts.
set_name (Optional[str]) – Optional aerospike set name to add the texts to.
batch_size – Batch size to use when adding the texts to the vectorstore.
embedding_chunk_size (int) – Chunk size to use when embedding the texts.
index_name (Optional[str]) – Optional aerospike index name used for waiting for index
completion. If not provided, the default index_name will be used.
wait_for_index (bool) – If True, wait for the all the texts to be indexed
before returning. Requires index_name to be provided. Defaults
to True.
**kwargs (Any) – Additional keyword arguments to pass to the client upsert call.
Returns
List of ids from adding the texts into the vectorstore.
Return type
List[str]
async adelete(ids: Optional[List[str]] = None, **kwargs: Any) → Optional[bool]¶
Delete by vector ID or other criteria.
Parameters
Delete by vector ID or other criteria.
Parameters
ids (Optional[List[str]]) – List of ids to delete.
**kwargs (Any) – Other keyword arguments that subclasses might use.
Returns
True if deletion is successful,
False otherwise, None if not implemented.
Return type
Optional[bool]
async classmethod afrom_documents(documents: List[Document], embedding: Embeddings, **kwargs: Any) → VST¶
Return VectorStore initialized from documents and embeddings.
Parameters
documents (List[Document]) – List of Documents to add to the vectorstore.
embedding (Embeddings) – Embedding function to use.
kwargs (Any) – 
Return type
VST
async classmethod afrom_texts(texts: List[str], embedding: Embeddings, metadatas: Optional[List[dict]] = None, **kwargs: Any) → VST¶
Return VectorStore initialized from texts and embeddings.
Parameters
texts (List[str]) – Texts to add to the vectorstore.
metadatas (Optional[List[dict]]) – Optional list of metadatas associated with the texts.
embedding (Embeddings) – Embedding function to use.
kwargs (Any) – 
Return type
VST
async amax_marginal_relevance_search(query: str, k: int = 4, fetch_k: int = 20, lambda_mult: float = 0.5, **kwargs: Any) → List[Document]¶
Return docs selected using the maximal marginal relevance.
Maximal marginal relevance optimizes for similarity to query AND diversity
among selected documents.
Parameters
query (str) – Text to look up documents similar to.
k (int) – Number of Documents to return. Defaults to 4.
fetch_k (int) – Number of Documents to fetch to pass to MMR algorithm.
score_threshold: Optional, a floating point value between 0 to 1 to
filter the resulting set of retrieved docs
Returns
List of Tuples of (doc, similarity_score)
Return type
List[Tuple[Document, float]]
async asimilarity_search_with_score(*args: Any, **kwargs: Any) → List[Tuple[Document, float]]¶
Run similarity search with distance.
Returns
List of Tuples of (doc, similarity_score)
Parameters
args (Any) – 
kwargs (Any) – 
Return type
List[Tuple[Document, float]]
static convert_distance_strategy(distance_strategy: Union[VectorDistanceMetric, DistanceStrategy]) → DistanceStrategy[source]¶
Convert Aerospikes distance strategy to langchains DistanceStrategy
enum. This is a convenience method to allow users to pass in the same
distance metric used to create the index.
Parameters
distance_strategy (Union[VectorDistanceMetric, DistanceStrategy]) – 
Return type
DistanceStrategy
delete(ids: Optional[List[str]] = None, set_name: Optional[str] = None, **kwargs: Any) → Optional[bool][source]¶
Delete by vector ID or other criteria.
Parameters
ids (Optional[List[str]]) – List of ids to delete.
**kwargs (Any) – Other keyword arguments to pass to client delete call.
set_name (Optional[str]) – 
**kwargs – 
Returns
True if deletion is successful,
False otherwise, None if not implemented.
Return type
Optional[bool]
classmethod from_documents(documents: List[Document], embedding: Embeddings, **kwargs: Any) → VST¶
Return VectorStore initialized from documents and embeddings.
Parameters
documents (List[Document]) – List of Documents to add to the vectorstore.
embedding (Embeddings) – Embedding function to use.
embedding (Embeddings) – Embedding function to use.
kwargs (Any) – 
Return type
VST
classmethod from_texts(texts: List[str], embedding: Embeddings, metadatas: Optional[List[dict]] = None, client: Client = None, namespace: str = 'test', index_name: Optional[str] = None, ids: Optional[List[str]] = None, embeddings_chunk_size: int = 1000, client_kwargs: Optional[dict] = None, **kwargs: Any) → Aerospike[source]¶
This is a user friendly interface that:
Embeds text.
Converts the texts into documents.
Adds the documents to a provided Aerospike index
This is intended to be a quick way to get started.
Example
from langchain_community.vectorstores import Aerospike
from langchain_openai import OpenAIEmbeddings
from aerospike_vector_search import Client, HostPort
client = Client(seeds=HostPort(host="localhost", port=5000))
aerospike = Aerospike.from_texts(
    ["foo", "bar", "baz"],
    embedder,
    client,
    "namespace",
    index_name="index",
    vector_key="vector",
    distance_strategy=MODEL_DISTANCE_CALC,
)
Parameters
texts (List[str]) – 
embedding (Embeddings) – 
metadatas (Optional[List[dict]]) – 
client (Client) – 
namespace (str) – 
index_name (Optional[str]) – 
ids (Optional[List[str]]) – 
embeddings_chunk_size (int) – 
client_kwargs (Optional[dict]) – 
kwargs (Any) – 
Return type
Aerospike
kwargs (Any) – 
Return type
Aerospike
max_marginal_relevance_search(query: str, k: int = 4, fetch_k: int = 20, lambda_mult: float = 0.5, metadata_keys: Optional[List[str]] = None, index_name: Optional[str] = None, **kwargs: Any) → List[Document][source]¶
Return docs selected using the maximal marginal relevance.
Maximal marginal relevance optimizes for similarity to query AND diversity
among selected documents.
Parameters
query (str) – Text to look up documents similar to.
k (int) – Number of Documents to return. Defaults to 4.
fetch_k (int) – Number of Documents to fetch to pass to MMR algorithm.
lambda_mult (float) – Number between 0 and 1 that determines the degree
of diversity among the results with 0 corresponding
to maximum diversity and 1 to minimum diversity.
Defaults to 0.5.
index_name (Optional[str]) – Name of the index to search.
metadata_keys (Optional[List[str]]) – 
kwargs (Any) – 
Returns
List of Documents selected by maximal marginal relevance.
Return type
List[Document]
max_marginal_relevance_search_by_vector(embedding: List[float], k: int = 4, fetch_k: int = 20, lambda_mult: float = 0.5, metadata_keys: Optional[List[str]] = None, index_name: Optional[str] = None, **kwargs: Any) → List[Document][source]¶
Return docs selected using the maximal marginal relevance.
Maximal marginal relevance optimizes for similarity to query AND diversity
among selected documents.
Parameters
embedding (List[float]) – Embedding to look up documents similar to.
k (int) – Number of Documents to return. Defaults to 4.
k (int) – Number of Documents to return. Defaults to 4.
fetch_k (int) – Number of Documents to fetch to pass to MMR algorithm.
lambda_mult (float) – Number between 0 and 1 that determines the degree of
diversity among the results with 0 corresponding to maximum
diversity and 1 to minimum diversity. Defaults to 0.5.
metadata_keys (Optional[List[str]]) – List of metadata keys to return with the documents.
If None, all metadata keys will be returned. Defaults to None.
index_name (Optional[str]) – Optional name of the index to search. Overrides the
default index_name.
kwargs (Any) – 
Returns
List of Documents selected by maximal marginal relevance.
Return type
List[Document]
search(query: str, search_type: str, **kwargs: Any) → List[Document]¶
Return docs most similar to query using specified search type.
Parameters
query (str) – Input text
search_type (str) – Type of search to perform. Can be “similarity”,
“mmr”, or “similarity_score_threshold”.
**kwargs (Any) – Arguments to pass to the search method.
Return type
List[Document]
similarity_search(query: str, k: int = 4, metadata_keys: Optional[List[str]] = None, index_name: Optional[str] = None, **kwargs: Any) → List[Document][source]¶
Return aerospike documents most similar to query.
Parameters
query (str) – Text to look up documents similar to.
k (int) – Number of Documents to return. Defaults to 4.
metadata_keys (Optional[List[str]]) – List of metadata keys to return with the documents.
If None, all metadata keys will be returned. Defaults to None.
If None, all metadata keys will be returned. Defaults to None.
index_name (Optional[str]) – Optional name of the index to search. Overrides the
default index_name.
kwargs (Any) – 
Returns
List of Documents most similar to the query and score for each
Return type
List[Document]
similarity_search_by_vector(embedding: List[float], k: int = 4, metadata_keys: Optional[List[str]] = None, index_name: Optional[str] = None, **kwargs: Any) → List[Document][source]¶
Return docs most similar to embedding vector.
Parameters
embedding (List[float]) – Embedding to look up documents similar to.
k (int) – Number of Documents to return. Defaults to 4.
metadata_keys (Optional[List[str]]) – List of metadata keys to return with the documents.
If None, all metadata keys will be returned. Defaults to None.
index_name (Optional[str]) – Name of the index to search. Overrides the default
index_name.
kwargs (Any) – Additional keyword arguments to pass to the search method.
Returns
List of Documents most similar to the query vector.
Return type
List[Document]
similarity_search_by_vector_with_score(embedding: List[float], k: int = 4, metadata_keys: Optional[List[str]] = None, index_name: Optional[str] = None, **kwargs: Any) → List[Tuple[Document, float]][source]¶
Return aerospike documents most similar to embedding, along with scores.
Parameters
embedding (List[float]) – Embedding to look up documents similar to.
k (int) – Number of Documents to return. Defaults to 4.
metadata_keys (Optional[List[str]]) – List of metadata keys to return with the documents.
If None, all metadata keys will be returned. Defaults to None.
If None, all metadata keys will be returned. Defaults to None.
index_name (Optional[str]) – Name of the index to search. Overrides the default
index_name.
kwargs (Any) – Additional keyword arguments to pass to the client
vector_search method.
Returns
List of Documents most similar to the query and associated scores.
Return type
List[Tuple[Document, float]]
similarity_search_with_relevance_scores(query: str, k: int = 4, **kwargs: Any) → List[Tuple[Document, float]]¶
Return docs and relevance scores in the range [0, 1].
0 is dissimilar, 1 is most similar.
Parameters
query (str) – Input text.
k (int) – Number of Documents to return. Defaults to 4.
**kwargs (Any) – kwargs to be passed to similarity search. Should include:
score_threshold: Optional, a floating point value between 0 to 1 to
filter the resulting set of retrieved docs
Returns
List of Tuples of (doc, similarity_score)
Return type
List[Tuple[Document, float]]
similarity_search_with_score(query: str, k: int = 4, metadata_keys: Optional[List[str]] = None, index_name: Optional[str] = None, **kwargs: Any) → List[Tuple[Document, float]][source]¶
Return aerospike documents most similar to query, along with scores.
Parameters
query (str) – Text to look up documents similar to.
k (int) – Number of Documents to return. Defaults to 4.
metadata_keys (Optional[List[str]]) – List of metadata keys to return with the documents.
If None, all metadata keys will be returned. Defaults to None.
index_name (Optional[str]) – Name of the index to search. Overrides the default
index_name.
langchain_community.vectorstores.redis.base.Redis¶
class langchain_community.vectorstores.redis.base.Redis(redis_url: str, index_name: str, embedding: Embeddings, index_schema: Optional[Union[Dict[str, List[Dict[str, str]]], str, PathLike]] = None, vector_schema: Optional[Dict[str, Union[str, int]]] = None, relevance_score_fn: Optional[Callable[[float], float]] = None, key_prefix: Optional[str] = None, **kwargs: Any)[source]¶
Redis vector database.
To use, you should have the redis python package installed
and have a running Redis Enterprise or Redis-Stack server
For production use cases, it is recommended to use Redis Enterprise
as the scaling, performance, stability and availability is much
better than Redis-Stack.
For testing and prototyping, however, this is not required.
Redis-Stack is available as a docker container the full vector
search API available.
# to run redis stack in docker locally
docker run -d -p 6379:6379 -p 8001:8001 redis/redis-stack:latest
Once running, you can connect to the redis server with the following url schemas:
- redis://<host>:<port> # simple connection
- redis://<username>:<password>@<host>:<port> # connection with authentication
- rediss://<host>:<port> # connection with SSL
- rediss://<username>:<password>@<host>:<port> # connection with SSL and auth
Examples:
The following examples show various ways to use the Redis VectorStore with
LangChain.
For all the following examples assume we have the following imports:
from langchain_community.vectorstores import Redis
from langchain_community.embeddings import OpenAIEmbeddings
Initialize, create index, and load Documentsfrom langchain_community.vectorstores import Redis
)
Advanced examples:
Custom vector schema can be supplied to change the way that
Redis creates the underlying vector schema. This is useful
for production use cases where you want to optimize the
vector schema for your use case. ex. using HNSW instead of
FLAT (knn) which is the default
vector_schema = {
    "algorithm": "HNSW"
}
rds = Redis.from_texts(
    texts, # a list of strings
    metadata, # a list of metadata dicts
    embeddings, # an Embeddings object
    vector_schema=vector_schema,
    redis_url="redis://localhost:6379",
)
Custom index schema can be supplied to change the way that the
metadata is indexed. This is useful for you would like to use the
hybrid querying (filtering) capability of Redis.
By default, this implementation will automatically generate the index
schema according to the following rules:
All strings are indexed as text fields
All numbers are indexed as numeric fields
All lists of strings are indexed as tag fields (joined bylangchain_community.vectorstores.redis.constants.REDIS_TAG_SEPARATOR)
All None values are not indexed but still stored in Redis these arenot retrievable through the interface here, but the raw Redis client
can be used to retrieve them.
All other types are not indexed
To override these rules, you can pass in a custom index schema like the following
tag:
    - name: credit_score
text:
    - name: user
    - name: job
Typically, the credit_score field would be a text field since it’s a string,
however, we can override this behavior by specifying the field type as shown with
the yaml config (can also be a dictionary) above and the code below.
rds = Redis.from_texts(
    texts, # a list of strings
Return docs selected using the maximal marginal relevance.
as_retriever(**kwargs)
Return VectorStoreRetriever initialized from this VectorStore.
asearch(query, search_type, **kwargs)
Return docs most similar to query using specified search type.
asimilarity_search(query[, k])
Return docs most similar to query.
asimilarity_search_by_vector(embedding[, k])
Return docs most similar to embedding vector.
asimilarity_search_with_relevance_scores(query)
Return docs and relevance scores in the range [0, 1].
asimilarity_search_with_score(*args, **kwargs)
Run similarity search with distance.
delete([ids])
Delete a Redis entry.
drop_index(index_name, delete_documents, ...)
Drop a Redis search index.
from_documents(documents, embedding, **kwargs)
Return VectorStore initialized from documents and embeddings.
from_existing_index(embedding, index_name, ...)
Connect to an existing Redis index.
from_texts(texts, embedding[, metadatas, ...])
Create a Redis vectorstore from a list of texts.
from_texts_return_keys(texts, embedding[, ...])
Create a Redis vectorstore from raw documents.
max_marginal_relevance_search(query[, k, ...])
Return docs selected using the maximal marginal relevance.
max_marginal_relevance_search_by_vector(...)
Return docs selected using the maximal marginal relevance.
search(query, search_type, **kwargs)
Return docs most similar to query using specified search type.
similarity_search(query[, k, filter, ...])
Run similarity search
similarity_search_by_vector(embedding[, k, ...])
Run similarity search between a query vector and the indexed vectors.
Run similarity search between a query vector and the indexed vectors.
similarity_search_limit_score(query[, k, ...])
[Deprecated] Returns the most similar indexed documents to the query text within the score_threshold range.
similarity_search_with_relevance_scores(query)
Return docs and relevance scores in the range [0, 1].
similarity_search_with_score(query[, k, ...])
Run similarity search with vector distance.
write_schema(path)
Write the schema to a yaml file.
Parameters
redis_url (str) – 
index_name (str) – 
embedding (Embeddings) – 
index_schema (Optional[Union[Dict[str, ListOfDict], str, os.PathLike]]) – 
vector_schema (Optional[Dict[str, Union[str, int]]]) – 
relevance_score_fn (Optional[Callable[[float], float]]) – 
key_prefix (Optional[str]) – 
kwargs (Any) – 
__init__(redis_url: str, index_name: str, embedding: Embeddings, index_schema: Optional[Union[Dict[str, List[Dict[str, str]]], str, PathLike]] = None, vector_schema: Optional[Dict[str, Union[str, int]]] = None, relevance_score_fn: Optional[Callable[[float], float]] = None, key_prefix: Optional[str] = None, **kwargs: Any)[source]¶
Initialize Redis vector store with necessary components.
Parameters
redis_url (str) – 
index_name (str) – 
embedding (Embeddings) – 
index_schema (Optional[Union[Dict[str, List[Dict[str, str]]], str, PathLike]]) – 
vector_schema (Optional[Dict[str, Union[str, int]]]) – 
relevance_score_fn (Optional[Callable[[float], float]]) –
relevance_score_fn (Optional[Callable[[float], float]]) – 
key_prefix (Optional[str]) – 
kwargs (Any) – 
async aadd_documents(documents: List[Document], **kwargs: Any) → List[str]¶
Run more documents through the embeddings and add to the vectorstore.
Parameters
documents (List[Document]) – Documents to add to the vectorstore.
kwargs (Any) – 
Returns
List of IDs of the added texts.
Return type
List[str]
async aadd_texts(texts: Iterable[str], metadatas: Optional[List[dict]] = None, **kwargs: Any) → List[str]¶
Run more texts through the embeddings and add to the vectorstore.
Parameters
texts (Iterable[str]) – Iterable of strings to add to the vectorstore.
metadatas (Optional[List[dict]]) – Optional list of metadatas associated with the texts.
**kwargs (Any) – vectorstore specific parameters.
Returns
List of ids from adding the texts into the vectorstore.
Return type
List[str]
add_documents(documents: List[Document], **kwargs: Any) → List[str]¶
Run more documents through the embeddings and add to the vectorstore.
Parameters
documents (List[Document]) – Documents to add to the vectorstore.
kwargs (Any) – 
Returns
List of IDs of the added texts.
Return type
List[str]
add_texts(texts: Iterable[str], metadatas: Optional[List[dict]] = None, embeddings: Optional[List[List[float]]] = None, batch_size: int = 1000, clean_metadata: bool = True, **kwargs: Any) → List[str][source]¶
Add more texts to the vectorstore.
Parameters
texts (Iterable[str]) – Iterable of strings/text to add to the vectorstore.