langchain_community.vectorstores.momento_vector_index
.MomentoVectorIndex¶
- class langchain_community.vectorstores.momento_vector_index.MomentoVectorIndex(embedding: Embeddings, client: PreviewVectorIndexClient, index_name: str = 'default', distance_strategy: DistanceStrategy = DistanceStrategy.COSINE, text_field: str = 'text', ensure_index_exists: bool = True, **kwargs: Any)[source]¶
`Momento Vector Index`(MVI)向量存储。
Momento Vector Index 是一个无服务器向量索引,可用于存储和搜索向量。要使用它,您应该已安装``momento`` python包。
- 示例:
from langchain_community.embeddings import OpenAIEmbeddings from langchain_community.vectorstores import MomentoVectorIndex from momento import ( CredentialProvider, PreviewVectorIndexClient, VectorIndexConfigurations, ) vectorstore = MomentoVectorIndex( embedding=OpenAIEmbeddings(), client=PreviewVectorIndexClient( VectorIndexConfigurations.Default.latest(), credential_provider=CredentialProvider.from_environment_variable( "MOMENTO_API_KEY" ), ), index_name="my-index", )
初始化由Momento Vector Index支持的Vector Store。
- 参数:
embedding (Embeddings): 要使用的嵌入函数。 configuration (VectorIndexConfiguration): 用于初始化Vector Index的配置。 credential_provider (CredentialProvider): 用于验证Vector Index的凭据提供程序。 index_name (str, optional): 存储文档的索引名称。默认为”default”。 distance_strategy (DistanceStrategy, optional): 要使用的距离策略。如果选择DistanceStrategy.EUCLIDEAN_DISTANCE,Momento将使用平方欧氏距离。默认为DistanceStrategy.COSINE。 text_field (str, optional): 存储原始文本的元数据字段名称。默认为”text”。 ensure_index_exists (bool, optional): 在向其添加文档之前是否确保索引存在。默认为True。
Attributes
embeddings
如果可用,访问查询嵌入对象。
Methods
__init__
(embedding, client[, index_name, ...])初始化由Momento Vector Index支持的Vector Store。
aadd_documents
(documents, **kwargs)运行更多的文档通过嵌入并添加到向量存储中。
aadd_texts
(texts[, metadatas])运行更多的文本通过嵌入,并添加到向量存储中。
add_documents
(documents, **kwargs)运行更多的文档通过嵌入并添加到向量存储中。
add_texts
(texts[, metadatas])运行更多的文本通过嵌入并添加到向量存储中。
adelete
([ids])根据向量ID或其他条件进行删除。
afrom_documents
(documents, embedding, **kwargs)返回从文档和嵌入初始化的VectorStore。
afrom_texts
(texts, embedding[, metadatas])返回从文本和嵌入初始化的VectorStore。
amax_marginal_relevance_search
(query[, k, ...])返回使用最大边际相关性选择的文档。
返回使用最大边际相关性选择的文档。
as_retriever
(**kwargs)返回从此VectorStore初始化的VectorStoreRetriever。
asearch
(query, search_type, **kwargs)返回使用指定搜索类型的查询最相似的文档。
asimilarity_search
(query[, k])返回与查询最相似的文档。
asimilarity_search_by_vector
(embedding[, k])返回与嵌入向量最相似的文档。
返回文档和相关性分数在范围[0, 1]内,异步执行。
asimilarity_search_with_score
(*args, **kwargs)使用异步方式运行相似性搜索与距离。
delete
([ids])根据向量ID删除。
from_documents
(documents, embedding, **kwargs)返回从文档和嵌入初始化的VectorStore。
from_texts
(texts, embedding[, metadatas])返回从文本和嵌入初始化的向量存储。
max_marginal_relevance_search
(query[, k, ...])返回使用最大边际相关性选择的文档。
返回使用最大边际相关性选择的文档。
search
(query, search_type, **kwargs)返回使用指定搜索类型的查询最相似的文档。
similarity_search
(query[, k])搜索与查询字符串相似的文档。
similarity_search_by_vector
(embedding[, k])搜索与查询向量相似的文档。
返回文档和相关性得分在[0, 1]范围内。
similarity_search_with_score
(query[, k])搜索与查询字符串相似的文档。
similarity_search_with_score_by_vector
(embedding)搜索与查询向量相似的文档。
- Parameters
embedding (Embeddings) –
client (PreviewVectorIndexClient) –
index_name (str) –
distance_strategy (DistanceStrategy) –
text_field (str) –
ensure_index_exists (bool) –
kwargs (Any) –
- __init__(embedding: Embeddings, client: PreviewVectorIndexClient, index_name: str = 'default', distance_strategy: DistanceStrategy = DistanceStrategy.COSINE, text_field: str = 'text', ensure_index_exists: bool = True, **kwargs: Any)[source]¶
初始化由Momento Vector Index支持的Vector Store。
- 参数:
embedding (Embeddings): 要使用的嵌入函数。 configuration (VectorIndexConfiguration): 用于初始化Vector Index的配置。 credential_provider (CredentialProvider): 用于验证Vector Index的凭据提供程序。 index_name (str, optional): 存储文档的索引名称。默认为”default”。 distance_strategy (DistanceStrategy, optional): 要使用的距离策略。如果选择DistanceStrategy.EUCLIDEAN_DISTANCE,Momento将使用平方欧氏距离。默认为DistanceStrategy.COSINE。 text_field (str, optional): 存储原始文本的元数据字段名称。默认为”text”。 ensure_index_exists (bool, optional): 在向其添加文档之前是否确保索引存在。默认为True。
- Parameters
embedding (Embeddings) –
client (PreviewVectorIndexClient) –
index_name (str) –
distance_strategy (DistanceStrategy) –
text_field (str) –
ensure_index_exists (bool) –
kwargs (Any) –
- async aadd_documents(documents: List[Document], **kwargs: Any) List[str] ¶
运行更多的文档通过嵌入并添加到向量存储中。
- 参数:
documents (List[Document]): 要添加到向量存储中的文档。
- 返回:
List[str]: 添加的文本的ID列表。
- Parameters
documents (List[Document]) –
kwargs (Any) –
- Return type
List[str]
- async aadd_texts(texts: Iterable[str], metadatas: Optional[List[dict]] = None, **kwargs: Any) List[str] ¶
运行更多的文本通过嵌入,并添加到向量存储中。
- Parameters
texts (Iterable[str]) –
metadatas (Optional[List[dict]]) –
kwargs (Any) –
- Return type
List[str]
- add_documents(documents: List[Document], **kwargs: Any) List[str] ¶
运行更多的文档通过嵌入并添加到向量存储中。
- 参数:
documents (List[Document]): 要添加到向量存储中的文档。
- 返回:
List[str]: 添加的文本的ID列表。
- Parameters
documents (List[Document]) –
kwargs (Any) –
- Return type
List[str]
- add_texts(texts: Iterable[str], metadatas: Optional[List[dict]] = None, **kwargs: Any) List[str] [source]¶
运行更多的文本通过嵌入并添加到向量存储中。
- 参数:
texts (Iterable[str]): 要添加到向量存储中的字符串的可迭代对象。 metadatas (Optional[List[dict]]): 与文本相关联的元数据的可选列表。 kwargs (Any): 其他可选参数。具体包括: - ids (List[str], optional): 用于文本的id列表。
默认为None,此时将生成uuid。
- 返回:
List[str]: 将文本添加到向量存储中后的id列表。
- Parameters
texts (Iterable[str]) –
metadatas (Optional[List[dict]]) –
kwargs (Any) –
- Return type
List[str]
- async adelete(ids: Optional[List[str]] = None, **kwargs: Any) Optional[bool] ¶
根据向量ID或其他条件进行删除。
- 参数:
ids:要删除的ID列表。 **kwargs:子类可能使用的其他关键字参数。
- 返回:
Optional[bool]:如果删除成功则为True,否则为False,如果未实现则为None。
- Parameters
ids (Optional[List[str]]) –
kwargs (Any) –
- Return type
Optional[bool]
- async classmethod afrom_documents(documents: List[Document], embedding: Embeddings, **kwargs: Any) VST ¶
返回从文档和嵌入初始化的VectorStore。
- Parameters
documents (List[Document]) –
embedding (Embeddings) –
kwargs (Any) –
- Return type
VST
- async classmethod afrom_texts(texts: List[str], embedding: Embeddings, metadatas: Optional[List[dict]] = None, **kwargs: Any) VST ¶
返回从文本和嵌入初始化的VectorStore。
- Parameters
texts (List[str]) –
embedding (Embeddings) –
metadatas (Optional[List[dict]]) –
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] ¶
返回使用最大边际相关性选择的文档。
最大边际相关性优化了与查询的相似性和所选文档之间的多样性。
- 参数:
query:要查找类似文档的文本。 k:要返回的文档数量。默认为4。 fetch_k:要获取以传递给MMR算法的文档数量。 lambda_mult:0到1之间的数字,确定结果之间多样性的程度,0对应最大多样性,1对应最小多样性。默认为0.5。
- 返回:
由最大边际相关性选择的文档列表。
- Parameters
query (str) –
k (int) –
fetch_k (int) –
lambda_mult (float) –
kwargs (Any) –
- Return type
List[Document]
- async amax_marginal_relevance_search_by_vector(embedding: List[float], k: int = 4, fetch_k: int = 20, lambda_mult: float = 0.5, **kwargs: Any) List[Document] ¶
返回使用最大边际相关性选择的文档。
- Parameters
embedding (List[float]) –
k (int) –
fetch_k (int) –
lambda_mult (float) –
kwargs (Any) –
- Return type
List[Document]
- as_retriever(**kwargs: Any) VectorStoreRetriever ¶
返回从此VectorStore初始化的VectorStoreRetriever。
- 参数:
- search_type(可选[str]):定义Retriever应执行的搜索类型。
可以是”similarity”(默认值)、”mmr”或”similarity_score_threshold”。
- search_kwargs(可选[Dict]):传递给搜索函数的关键字参数。可以包括:
k:要返回的文档数量(默认值:4) score_threshold:用于similarity_score_threshold的最小相关性阈值 fetch_k:传递给MMR算法的文档数量(默认值:20) lambda_mult:MMR返回结果的多样性;1表示最小多样性,0表示最大多样性(默认值:0.5) filter:按文档元数据筛选
- 返回:
VectorStoreRetriever:VectorStore的Retriever类。
示例:
# 检索更多具有更高多样性的文档 # 如果数据集中有许多相似的文档,则很有用 docsearch.as_retriever( search_type="mmr", search_kwargs={'k': 6, 'lambda_mult': 0.25} ) # 为MMR算法考虑更多文档 # 但只返回前5个 docsearch.as_retriever( search_type="mmr", search_kwargs={'k': 5, 'fetch_k': 50} ) # 仅检索具有特定阈值以上相关性分数的文档 docsearch.as_retriever( search_type="similarity_score_threshold", search_kwargs={'score_threshold': 0.8} ) # 仅从数据集中获取最相似的单个文档 docsearch.as_retriever(search_kwargs={'k': 1}) # 使用筛选器仅从特定论文中检索文档 docsearch.as_retriever( search_kwargs={'filter': {'paper_title':'GPT-4 Technical Report'}} )
- Parameters
kwargs (Any) –
- Return type
- async asearch(query: str, search_type: str, **kwargs: Any) List[Document] ¶
返回使用指定搜索类型的查询最相似的文档。
- Parameters
query (str) –
search_type (str) –
kwargs (Any) –
- Return type
List[Document]
- async asimilarity_search(query: str, k: int = 4, **kwargs: Any) List[Document] ¶
返回与查询最相似的文档。
- Parameters
query (str) –
k (int) –
kwargs (Any) –
- Return type
List[Document]
- async asimilarity_search_by_vector(embedding: List[float], k: int = 4, **kwargs: Any) List[Document] ¶
返回与嵌入向量最相似的文档。
- Parameters
embedding (List[float]) –
k (int) –
kwargs (Any) –
- Return type
List[Document]
- async asimilarity_search_with_relevance_scores(query: str, k: int = 4, **kwargs: Any) List[Tuple[Document, float]] ¶
返回文档和相关性分数在范围[0, 1]内,异步执行。
0表示不相似,1表示最相似。
- 参数:
query:输入文本 k:要返回的文档数量。默认为4。 **kwargs:要传递给相似性搜索的kwargs。应包括:
score_threshold:可选,介于0到1之间的浮点值,用于过滤检索到的文档集
- 返回:
元组列表(doc,相似度分数)的列表
- Parameters
query (str) –
k (int) –
kwargs (Any) –
- Return type
List[Tuple[Document, float]]
- async asimilarity_search_with_score(*args: Any, **kwargs: Any) List[Tuple[Document, float]] ¶
使用异步方式运行相似性搜索与距离。
- Parameters
args (Any) –
kwargs (Any) –
- Return type
List[Tuple[Document, float]]
- delete(ids: Optional[List[str]] = None, **kwargs: Any) Optional[bool] [source]¶
根据向量ID删除。
- 参数:
ids(List[str]):要删除的ID列表。 kwargs(Any):其他可选参数(未使用)
- 返回:
Optional[bool]:如果删除成功则为True,否则为False,如果未实现则为None。
- Parameters
ids (Optional[List[str]]) –
kwargs (Any) –
- Return type
Optional[bool]
- classmethod from_documents(documents: List[Document], embedding: Embeddings, **kwargs: Any) VST ¶
返回从文档和嵌入初始化的VectorStore。
- Parameters
documents (List[Document]) –
embedding (Embeddings) –
kwargs (Any) –
- Return type
VST
- classmethod from_texts(texts: List[str], embedding: Embeddings, metadatas: Optional[List[dict]] = None, **kwargs: Any) VST [source]¶
返回从文本和嵌入初始化的向量存储。
- 参数:
cls(Type[VST]):用于初始化向量存储的向量存储类。 texts(List[str]):用于初始化向量存储的文本。 embedding(Embeddings):要使用的嵌入函数。 metadatas(Optional[List[dict],可选):与文本相关联的元数据。默认为None。 kwargs(Any):向量存储特定参数。以下参数将被转发到向量存储构造函数并且是必需的: - index_name(str,可选):存储文档的索引名称。默认为”default”。 - text_field(str,可选):存储原始文本的元数据字段名称。默认为”text”。 - distance_strategy(DistanceStrategy,可选):要使用的距离策略。默认为DistanceStrategy.COSINE。如果选择DistanceStrategy.EUCLIDEAN_DISTANCE,Momento将使用平方欧氏距离。 - ensure_index_exists(bool,可选):在向其添加文档之前是否确保索引存在。默认为True。 此外,您可以传入客户端或API密钥 - client(PreviewVectorIndexClient):要使用的Momento向量索引客户端。 - api_key(Optional[str]):用于初始化向量索引的配置。默认为None。如果为None,则配置将从环境变量`MOMENTO_API_KEY`初始化。
- 返回:
VST:从文本和嵌入初始化的Momento向量索引向量存储。
- Parameters
texts (List[str]) –
embedding (Embeddings) –
metadatas (Optional[List[dict]]) –
kwargs (Any) –
- Return type
VST
- max_marginal_relevance_search(query: str, k: int = 4, fetch_k: int = 20, lambda_mult: float = 0.5, **kwargs: Any) List[Document] [source]¶
返回使用最大边际相关性选择的文档。
最大边际相关性优化了与查询的相似性和所选文档之间的多样性。
- 参数:
query:要查找相似文档的文本。 k:要返回的文档数量。默认为4。 fetch_k:要获取以传递给MMR算法的文档数量。 lambda_mult:0到1之间的数字,确定结果之间多样性的程度,其中0对应最大多样性,1对应最小多样性。默认为0.5。
- 返回:
由最大边际相关性选择的文档列表。
- Parameters
query (str) –
k (int) –
fetch_k (int) –
lambda_mult (float) –
kwargs (Any) –
- 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, **kwargs: Any) List[Document] [source]¶
返回使用最大边际相关性选择的文档。
最大边际相关性优化了与查询的相似性和所选文档之间的多样性。
- 参数:
embedding:要查找相似文档的嵌入。 k:要返回的文档数量。默认为4。 fetch_k:要获取并传递给MMR算法的文档数量。 lambda_mult:0到1之间的数字,确定结果之间多样性的程度,
0对应最大多样性,1对应最小多样性。 默认为0.5。
- 返回:
由最大边际相关性选择的文档列表。
- Parameters
embedding (List[float]) –
k (int) –
fetch_k (int) –
lambda_mult (float) –
kwargs (Any) –
- Return type
List[Document]
- search(query: str, search_type: str, **kwargs: Any) List[Document] ¶
返回使用指定搜索类型的查询最相似的文档。
- Parameters
query (str) –
search_type (str) –
kwargs (Any) –
- Return type
List[Document]
- similarity_search(query: str, k: int = 4, **kwargs: Any) List[Document] [source]¶
搜索与查询字符串相似的文档。
- 参数:
query(str):要搜索的查询字符串。 k(int,可选):要返回的结果数量。默认为4。
- 返回:
List[Document]:与查询相似的文档列表。
- Parameters
query (str) –
k (int) –
kwargs (Any) –
- Return type
List[Document]
- similarity_search_by_vector(embedding: List[float], k: int = 4, **kwargs: Any) List[Document] [source]¶
搜索与查询向量相似的文档。
- 参数:
embedding (List[float]): 要搜索的查询向量。 k (int, optional): 要返回的结果数量。默认为4。
- 返回:
List[Document]: 与查询相似的文档列表。
- Parameters
embedding (List[float]) –
k (int) –
kwargs (Any) –
- Return type
List[Document]
- similarity_search_with_relevance_scores(query: str, k: int = 4, **kwargs: Any) List[Tuple[Document, float]] ¶
返回文档和相关性得分在[0, 1]范围内。
0表示不相似,1表示最相似。
- 参数:
query:输入文本 k:要返回的文档数量。默认为4。 **kwargs:要传递给相似性搜索的kwargs。应包括:
score_threshold:可选,介于0到1之间的浮点值,用于过滤检索到的文档集
- 返回:
元组列表(doc,相似性得分)
- Parameters
query (str) –
k (int) –
kwargs (Any) –
- Return type
List[Tuple[Document, float]]
- similarity_search_with_score(query: str, k: int = 4, **kwargs: Any) List[Tuple[Document, float]] [source]¶
搜索与查询字符串相似的文档。
- 参数:
query (str): 要搜索的查询字符串。 k (int, optional): 要返回的结果数量。默认为4。 kwargs (Any): 向量存储特定的搜索参数。以下参数将被转发给Momento向量索引: - top_k (int, optional): 要返回的结果数量。
- 返回:
List[Tuple[Document, float]]: 一个元组列表,形式为(Document, score)。
- Parameters
query (str) –
k (int) –
kwargs (Any) –
- Return type
List[Tuple[Document, float]]
- similarity_search_with_score_by_vector(embedding: List[float], k: int = 4, **kwargs: Any) List[Tuple[Document, float]] [source]¶
搜索与查询向量相似的文档。
- 参数:
embedding(List[float]):要搜索的查询向量。 k(int,可选):要返回的结果数量。默认为4。 kwargs(Any):向量存储特定的搜索参数。以下参数将被转发到Momento向量索引: - top_k(int,可选):要返回的结果数量。
- 返回:
List[Tuple[Document, float]]:形式为(Document,score)的元组列表。
- Parameters
embedding (List[float]) –
k (int) –
kwargs (Any) –
- Return type
List[Tuple[Document, float]]