langchain_community.vectorstores.upstash
.UpstashVectorStore¶
- class langchain_community.vectorstores.upstash.UpstashVectorStore(text_key: str = 'text', index: Optional[Index] = None, async_index: Optional[AsyncIndex] = None, index_url: Optional[str] = None, index_token: Optional[str] = None, embedding: Optional[Union[Embeddings, bool]] = None, *, namespace: str = '')[source]¶
Upstash Vector vector store
To use, the
upstash-vector
python package must be installed.Also an Upstash Vector index is required. First create a new Upstash Vector index and copy the index_url and index_token variables. Then either pass them through the constructor or set the environment variables UPSTASH_VECTOR_REST_URL and UPSTASH_VECTOR_REST_TOKEN.
Example
from langchain_openai import OpenAIEmbeddings from langchain_community.vectorstores import UpstashVectorStore embeddings = OpenAIEmbeddings(model="text-embedding-3-large") vectorstore = UpstashVectorStore( embedding=embeddings, index_url="...", index_token="..." ) # or import os os.environ["UPSTASH_VECTOR_REST_URL"] = "..." os.environ["UPSTASH_VECTOR_REST_TOKEN"] = "..." vectorstore = UpstashVectorStore( embedding=embeddings )
Constructor for UpstashVectorStore.
If index or index_url and index_token are not provided, the constructor will attempt to create an index using the environment variables UPSTASH_VECTOR_REST_URL`and `UPSTASH_VECTOR_REST_TOKEN.
- Parameters
text_key (str) – Key to store the text in metadata.
index (Optional[Index]) – UpstashVector Index object.
async_index (Optional[AsyncIndex]) – UpstashVector AsyncIndex object, provide only if async
needed (functions are) –
index_url (Optional[str]) – URL of the UpstashVector index.
index_token (Optional[str]) – Token of the UpstashVector index.
embedding (Optional[Union[Embeddings, bool]]) – Embeddings object or a boolean. When false, no embedding is applied. If true, Upstash embeddings are used. When Upstash embeddings are used, text is sent directly to Upstash and embedding is applied there instead of embedding in Langchain.
namespace (str) – Namespace to use from the index.
Example
from langchain_community.vectorstores.upstash import UpstashVectorStore from langchain_community.embeddings.openai import OpenAIEmbeddings embeddings = OpenAIEmbeddings() vectorstore = UpstashVectorStore( embedding=embeddings, index_url="...", index_token="...", namespace="..." ) # With an existing index from upstash_vector import Index index = Index(url="...", token="...") vectorstore = UpstashVectorStore( embedding=embeddings, index=index, namespace="..." )
Attributes
embeddings
Access the query embedding object if available.
Methods
__init__
([text_key, index, async_index, ...])Constructor for UpstashVectorStore.
aadd_documents
(documents[, ids, batch_size, ...])Get the embeddings for the documents and add them to the vectorstore.
aadd_texts
(texts[, metadatas, ids, ...])Get the embeddings for the texts and add them to the vectorstore.
add_documents
(documents[, ids, batch_size, ...])Get the embeddings for the documents and add them to the vectorstore.
add_texts
(texts[, metadatas, ids, ...])Get the embeddings for the texts and add them to the vectorstore.
adelete
([ids, delete_all, batch_size, namespace])Delete by vector IDs
afrom_documents
(documents, embedding, **kwargs)Async return VectorStore initialized from documents and embeddings.
afrom_texts
(texts, embedding[, metadatas, ...])Create a new UpstashVectorStore from a list of texts.
aget_by_ids
(ids, /)Async get documents by their IDs.
ainfo
()Get statistics about the index.
amax_marginal_relevance_search
(query[, k, ...])Return docs selected using the maximal marginal relevance.
Return docs selected using the maximal marginal relevance.
as_retriever
(**kwargs)Return VectorStoreRetriever initialized from this VectorStore.
asearch
(query, search_type, **kwargs)Async return docs most similar to query using a specified search type.
asimilarity_search
(query[, k, filter, namespace])Return documents most similar to query.
asimilarity_search_by_vector
(embedding[, k, ...])Return documents closest to the given embedding.
Return texts whose embedding is closest to the given embedding
Async return docs and relevance scores in the range [0, 1].
asimilarity_search_with_score
(query[, k, ...])Retrieve texts most similar to query and convert the result to Document objects.
astreaming_upsert
(items, /, batch_size, **kwargs)aupsert
(items, /, **kwargs)delete
([ids, delete_all, batch_size, namespace])Delete by vector IDs
from_documents
(documents, embedding, **kwargs)Return VectorStore initialized from documents and embeddings.
from_texts
(texts, embedding[, metadatas, ...])Create a new UpstashVectorStore from a list of texts.
get_by_ids
(ids, /)Get documents by their IDs.
info
()Get statistics about the index.
max_marginal_relevance_search
(query[, k, ...])Return docs selected using the maximal marginal relevance.
Return docs selected using the maximal marginal relevance.
search
(query, search_type, **kwargs)Return docs most similar to query using a specified search type.
similarity_search
(query[, k, filter, namespace])Return documents most similar to query.
similarity_search_by_vector
(embedding[, k, ...])Return documents closest to the given embedding.
similarity_search_by_vector_with_score
(embedding)Return texts whose embedding is closest to the given embedding
Return docs and relevance scores in the range [0, 1].
similarity_search_with_score
(query[, k, ...])Retrieve texts most similar to query and convert the result to Document objects.
streaming_upsert
(items, /, batch_size, **kwargs)upsert
(items, /, **kwargs)- __init__(text_key: str = 'text', index: Optional[Index] = None, async_index: Optional[AsyncIndex] = None, index_url: Optional[str] = None, index_token: Optional[str] = None, embedding: Optional[Union[Embeddings, bool]] = None, *, namespace: str = '')[source]¶
Constructor for UpstashVectorStore.
If index or index_url and index_token are not provided, the constructor will attempt to create an index using the environment variables UPSTASH_VECTOR_REST_URL`and `UPSTASH_VECTOR_REST_TOKEN.
- Parameters
text_key (str) – Key to store the text in metadata.
index (Optional[Index]) – UpstashVector Index object.
async_index (Optional[AsyncIndex]) – UpstashVector AsyncIndex object, provide only if async
needed (functions are) –
index_url (Optional[str]) – URL of the UpstashVector index.
index_token (Optional[str]) – Token of the UpstashVector index.
embedding (Optional[Union[Embeddings, bool]]) – Embeddings object or a boolean. When false, no embedding is applied. If true, Upstash embeddings are used. When Upstash embeddings are used, text is sent directly to Upstash and embedding is applied there instead of embedding in Langchain.
namespace (str) – Namespace to use from the index.
Example
from langchain_community.vectorstores.upstash import UpstashVectorStore from langchain_community.embeddings.openai import OpenAIEmbeddings embeddings = OpenAIEmbeddings() vectorstore = UpstashVectorStore( embedding=embeddings, index_url="...", index_token="...", namespace="..." ) # With an existing index from upstash_vector import Index index = Index(url="...", token="...") vectorstore = UpstashVectorStore( embedding=embeddings, index=index, namespace="..." )
- async aadd_documents(documents: Iterable[Document], ids: Optional[List[str]] = None, batch_size: int = 32, embedding_chunk_size: int = 1000, *, namespace: Optional[str] = None, **kwargs: Any) List[str] [source]¶
Get the embeddings for the documents and add them to the vectorstore.
Documents are sent to the embeddings object in batches of size embedding_chunk_size. The embeddings are then upserted into the vectorstore in batches of size batch_size.
- Parameters
documents (Iterable[Document]) – Iterable of Documents to add to the vectorstore.
batch_size (int) – Batch size to use when upserting the embeddings.
request. (Upstash supports at max 1000 vectors per) –
embedding_batch_size – Chunk size to use when embedding the texts.
namespace (Optional[str]) – Namespace to use from the index.
ids (Optional[List[str]]) –
embedding_chunk_size (int) –
kwargs (Any) –
- Returns
List of ids from adding the texts into the vectorstore.
- Return type
List[str]
- async aadd_texts(texts: Iterable[str], metadatas: Optional[List[dict]] = None, ids: Optional[List[str]] = None, batch_size: int = 32, embedding_chunk_size: int = 1000, *, namespace: Optional[str] = None, **kwargs: Any) List[str] [source]¶
Get the embeddings for the texts and add them to the vectorstore.
Texts are sent to the embeddings object in batches of size embedding_chunk_size. The embeddings are then upserted into the vectorstore in batches of size batch_size.
- 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.
batch_size (int) – Batch size to use when upserting the embeddings.
request. (Upstash supports at max 1000 vectors per) –
embedding_batch_size – Chunk size to use when embedding the texts.
namespace (Optional[str]) – Namespace to use from the index.
embedding_chunk_size (int) –
kwargs (Any) –
- Returns
List of ids from adding the texts into the vectorstore.
- Return type
List[str]
- add_documents(documents: List[Document], ids: Optional[List[str]] = None, batch_size: int = 32, embedding_chunk_size: int = 1000, *, namespace: Optional[str] = None, **kwargs: Any) List[str] [source]¶
Get the embeddings for the documents and add them to the vectorstore.
Documents are sent to the embeddings object in batches of size embedding_chunk_size. The embeddings are then upserted into the vectorstore in batches of size batch_size.
- Parameters
documents (List[Document]) – Iterable of Documents to add to the vectorstore.
batch_size (int) – Batch size to use when upserting the embeddings.
request. (Upstash supports at max 1000 vectors per) –
embedding_batch_size – Chunk size to use when embedding the texts.
namespace (Optional[str]) – Namespace to use from the index.
ids (Optional[List[str]]) –
embedding_chunk_size (int) –
kwargs (Any) –
- Returns
List of ids from adding the texts into the vectorstore.
- Return type
List[str]
- add_texts(texts: Iterable[str], metadatas: Optional[List[dict]] = None, ids: Optional[List[str]] = None, batch_size: int = 32, embedding_chunk_size: int = 1000, *, namespace: Optional[str] = None, **kwargs: Any) List[str] [source]¶
Get the embeddings for the texts and add them to the vectorstore.
Texts are sent to the embeddings object in batches of size embedding_chunk_size. The embeddings are then upserted into the vectorstore in batches of size batch_size.
- 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.
batch_size (int) – Batch size to use when upserting the embeddings.
request. (Upstash supports at max 1000 vectors per) –
embedding_batch_size – Chunk size to use when embedding the texts.
namespace (Optional[str]) – Namespace to use from the index.
embedding_chunk_size (int) –
kwargs (Any) –
- Returns
List of ids from adding the texts into the vectorstore.
- Return type
List[str]
- async adelete(ids: Optional[List[str]] = None, delete_all: Optional[bool] = None, batch_size: Optional[int] = 1000, *, namespace: Optional[str] = None, **kwargs: Any) None [source]¶
Delete by vector IDs
- Parameters
ids (Optional[List[str]]) – 要删除的 ID 列表。
delete_all (Optional[bool]) – 删除索引中的所有向量。
batch_size (Optional[int]) – 删除嵌入时使用的批次大小。
namespace (Optional[str]) – Namespace to use from the index.
request. (Upstash 支持每个请求最多 1000 次删除。) –
kwargs (Any) –
- Return type
None
- async classmethod afrom_documents(documents: List[Document], embedding: Embeddings, **kwargs: Any) VST ¶
Async return VectorStore initialized from documents and embeddings.
- Parameters
documents (List[Document]) – 要添加到向量存储的文档列表。
embedding (Embeddings) – 要使用的嵌入函数。
kwargs (Any) – 额外的关键字参数。
- Returns
从文档和嵌入初始化的 VectorStore。
- Return type
- async classmethod afrom_texts(texts: List[str], embedding: Embeddings, metadatas: Optional[List[dict]] = None, ids: Optional[List[str]] = None, embedding_chunk_size: int = 1000, batch_size: int = 32, text_key: str = 'text', index: Optional[Index] = None, async_index: Optional[AsyncIndex] = None, index_url: Optional[str] = None, index_token: Optional[str] = None, *, namespace: str = '', **kwargs: Any) UpstashVectorStore [source]¶
Create a new UpstashVectorStore from a list of texts.
Example
- Parameters
texts (List[str]) –
embedding (Embeddings) –
metadatas (Optional[List[dict]]) –
ids (Optional[List[str]]) –
embedding_chunk_size (int) –
batch_size (int) –
text_key (str) –
index (Optional[Index]) –
async_index (Optional[AsyncIndex]) –
index_url (Optional[str]) –
index_token (Optional[str]) –
namespace (str) –
kwargs (Any) –
- Return type
- async aget_by_ids(ids: Sequence[str], /) List[Document] ¶
Async get documents by their IDs.
返回的文档应将 ID 字段设置为向量存储中文档的 ID。
如果某些 ID 未找到或存在重复 ID,则返回的文档可能少于请求的文档。
用户不应假设返回文档的顺序与输入 ID 的顺序匹配。相反,用户应依赖返回文档的 ID 字段。
如果某些 ID 没有找到文档,此方法**不应**引发异常。
- Parameters
ids (Sequence[str]) – 要检索的 ID 列表。
- Returns
文档列表。
- Return type
List[Document]
0.2.11 版本中新增。
- async ainfo() InfoResult [source]¶
Get statistics about the index.
- Returns
向量总数
等待索引的向量总数
索引在磁盘上的总大小(字节)
索引的维度计数
为索引选择的相似度函数
- Return type
InfoResult
- async amax_marginal_relevance_search(query: str, k: int = 4, fetch_k: int = 20, lambda_mult: float = 0.5, filter: Optional[str] = None, *, namespace: Optional[str] = None, **kwargs: Any) List[Document] [source]¶
Return docs selected using the maximal marginal relevance.
最大边际相关性优化查询的相似性和所选文档之间的多样性。
- Parameters
query (str) – 用于查找相似文档的文本。
k (int) – 要返回的文档数量。默认为 4。
fetch_k (int) – 要获取并传递给 MMR 算法的文档数量。
lambda_mult (float) – 介于 0 和 1 之间的数字,用于确定结果之间多样性的程度,其中 0 对应于最大多样性,1 对应于最小多样性。默认为 0.5。
filter (Optional[str]) – str 格式的可选元数据过滤器
namespace (Optional[str]) – Namespace to use from the index.
kwargs (Any) –
- Returns
通过最大边际相关性选择的文档列表。
- Return type
List[Document]
- async amax_marginal_relevance_search_by_vector(embedding: Union[List[float], str], k: int = 4, fetch_k: int = 20, lambda_mult: float = 0.5, filter: Optional[str] = None, *, namespace: Optional[str] = None, **kwargs: Any) List[Document] [source]¶
Return docs selected using the maximal marginal relevance.
最大边际相关性优化查询的相似性和所选文档之间的多样性。
- Parameters
embedding (Union[List[float], str]) – 用于查找相似文档的嵌入。
k (int) – 要返回的文档数量。默认为 4。
fetch_k (int) – 要获取并传递给 MMR 算法的文档数量。
lambda_mult (float) – 介于 0 和 1 之间的数字,用于确定结果之间多样性的程度,其中 0 对应于最大多样性,1 对应于最小多样性。默认为 0.5。
filter (Optional[str]) – str 格式的可选元数据过滤器
namespace (Optional[str]) – Namespace to use from the index.
kwargs (Any) –
- Returns
通过最大边际相关性选择的文档列表。
- Return type
List[Document]
- as_retriever(**kwargs: Any) VectorStoreRetriever ¶
Return VectorStoreRetriever initialized from this VectorStore.
- Parameters
**kwargs (Any) –
传递给搜索函数的关键字参数。可以包括:search_type (Optional[str]):定义搜索类型,
Retriever 应执行的搜索类型。可以是“similarity”(默认)、“mmr”或“similarity_score_threshold”。
- search_kwargs (Optional[Dict]):传递给
- 搜索函数的关键字参数。可以包括例如
k:要返回的文档数量(默认值:4)score_threshold:相似度分数阈值的最小相关性阈值
对于 similarity_score_threshold
- fetch_k:传递给 MMR 算法的文档数量
(默认值:20)
- lambda_mult:MMR 返回结果的多样性;
1 表示最小多样性,0 表示最大多样性。(默认值:0.5)
filter:按文档元数据过滤
- Returns
VectorStore 的 Retriever 类。
- Return type
示例
# Retrieve more documents with higher diversity # Useful if your dataset has many similar documents docsearch.as_retriever( search_type="mmr", search_kwargs={'k': 6, 'lambda_mult': 0.25} ) # Fetch more documents for the MMR algorithm to consider # But only return the top 5 docsearch.as_retriever( search_type="mmr", search_kwargs={'k': 5, 'fetch_k': 50} ) # Only retrieve documents that have a relevance score # Above a certain threshold docsearch.as_retriever( search_type="similarity_score_threshold", search_kwargs={'score_threshold': 0.8} ) # Only get the single most similar document from the dataset docsearch.as_retriever(search_kwargs={'k': 1}) # Use a filter to only retrieve documents from a specific paper docsearch.as_retriever( search_kwargs={'filter': {'paper_title':'GPT-4 Technical Report'}} )
- async asearch(query: str, search_type: str, **kwargs: Any) List[Document] ¶
Async return docs most similar to query using a specified search type.
- Parameters
query (str) – 输入文本。
search_type (str) – 要执行的搜索类型。可以是“similarity”、“mmr”或“similarity_score_threshold”。
**kwargs (Any) – 传递给搜索方法的参数。
- Returns
与查询最相似的文档列表。
- Raises
ValueError – 如果 search_type 不是 “similarity”、“mmr” 或 “similarity_score_threshold” 之一。
- Return type
List[Document]
- async asimilarity_search(query: str, k: int = 4, filter: Optional[str] = None, *, namespace: Optional[str] = None, **kwargs: Any) List[Document] [source]¶
Return documents most similar to query.
- Parameters
query (str) – 用于查找相似文档的文本。
k (int) – 要返回的文档数量。默认为 4。
filter (Optional[str]) – str 格式的可选元数据过滤器
namespace (Optional[str]) – Namespace to use from the index.
kwargs (Any) –
- Returns
与查询最相似的文档列表
- Return type
List[Document]
- async asimilarity_search_by_vector(embedding: Union[List[float], str], k: int = 4, filter: Optional[str] = None, *, namespace: Optional[str] = None, **kwargs: Any) List[Document] [source]¶
Return documents closest to the given embedding.
- Parameters
embedding (Union[List[float], str]) – 用于查找相似文档的嵌入。
k (int) – 要返回的文档数量。默认为 4。
filter (Optional[str]) – str 格式的可选元数据过滤器
namespace (Optional[str]) – Namespace to use from the index.
kwargs (Any) –
- Returns
与查询最相似的文档列表
- Return type
List[Document]
- async asimilarity_search_by_vector_with_score(embedding: Union[List[float], str], k: int = 4, filter: Optional[str] = None, *, namespace: Optional[str] = None, **kwargs: Any) List[Tuple[Document, float]] [source]¶
Return texts whose embedding is closest to the given embedding
- Parameters
embedding (Union[List[float], str]) –
k (int) –
filter (Optional[str]) –
namespace (Optional[str]) –
kwargs (Any) –
- Return type
List[Tuple[Document, float]]
- async asimilarity_search_with_relevance_scores(query: str, k: int = 4, **kwargs: Any) List[Tuple[Document, float]] ¶
Async return docs and relevance scores in the range [0, 1].
0 表示不相似,1 表示最相似。
- Parameters
query (str) – 输入文本。
k (int) – 要返回的文档数量。默认为 4。
**kwargs (Any) –
传递给相似性搜索的 kwargs。应包含:score_threshold: 可选,一个介于 0 到 1 之间的浮点值,用于
过滤检索到的文档结果集
- Returns
由 (doc, similarity_score) 元组组成的列表
- Return type
List[Tuple[Document, float]]
- async asimilarity_search_with_score(query: str, k: int = 4, filter: Optional[str] = None, *, namespace: Optional[str] = None, **kwargs: Any) List[Tuple[Document, float]] [source]¶
Retrieve texts most similar to query and convert the result to Document objects.
- Parameters
query (str) – 用于查找相似文档的文本。
k (int) – 要返回的文档数量。默认为 4。
filter (Optional[str]) – str 格式的可选元数据过滤器
namespace (Optional[str]) – Namespace to use from the index.
kwargs (Any) –
- Returns
最相似于查询的文档列表以及每个文档的分数
- Return type
List[Tuple[Document, float]]
- astreaming_upsert(items: AsyncIterable[Document], /, batch_size: int, **kwargs: Any) AsyncIterator[UpsertResponse] ¶
Beta
添加于 0.2.11 版本。API 可能会发生变化。
以流式方式 Upsert 文档。 streaming_upsert 的异步版本。
- Parameters
items (AsyncIterable[Document]) – 要添加到向量存储的文档的可迭代对象。
batch_size (int) – 每次 upsert 的批量大小。
kwargs (Any) – 额外的关键字参数。 kwargs 应该只包含所有文档通用的参数。(例如,索引超时、重试策略等)kwargs 不应包含 ids 以避免语义不明确。相反,ID 应该作为 Document 对象的一部分提供。
- Yields
UpsertResponse – 一个响应对象,其中包含已成功添加到或更新到向量存储的 ID 列表,以及未能添加或更新的 ID 列表。
- Return type
AsyncIterator[UpsertResponse]
0.2.11 版本中新增。
- async aupsert(items: Sequence[Document], /, **kwargs: Any) UpsertResponse ¶
Beta
添加于 0.2.11 版本。API 可能会发生变化。
在向量存储中添加或更新文档。 upsert 的异步版本。
如果提供了 Document 对象的 ID 字段,upsert 功能应使用该字段。如果未提供 ID,则 upsert 方法可以自由地为文档生成 ID。
当指定了 ID 并且文档已存在于向量存储中时,upsert 方法应使用新数据更新文档。如果文档不存在,则 upsert 方法应将文档添加到向量存储中。
- Parameters
items (Sequence[Document]) – 要添加到向量存储的文档序列。
kwargs (Any) – 额外的关键字参数。
- Returns
一个响应对象,其中包含已成功添加到或更新到向量存储的 ID 列表,以及未能添加或更新的 ID 列表。
- Return type
0.2.11 版本中新增。
- delete(ids: Optional[List[str]] = None, delete_all: Optional[bool] = None, batch_size: Optional[int] = 1000, *, namespace: Optional[str] = None, **kwargs: Any) None [source]¶
Delete by vector IDs
- Parameters
ids (Optional[List[str]]) – 要删除的 ID 列表。
delete_all (Optional[bool]) – 删除索引中的所有向量。
batch_size (Optional[int]) – 删除嵌入时使用的批次大小。
namespace (Optional[str]) – Namespace to use from the index.
request. (Upstash 支持每个请求最多 1000 次删除。) –
kwargs (Any) –
- Return type
None
- classmethod from_documents(documents: List[Document], embedding: Embeddings, **kwargs: Any) VST ¶
Return VectorStore initialized from documents and embeddings.
- Parameters
documents (List[Document]) – 要添加到向量存储的文档列表。
embedding (Embeddings) – 要使用的嵌入函数。
kwargs (Any) – 额外的关键字参数。
- Returns
从文档和嵌入初始化的 VectorStore。
- Return type
- classmethod from_texts(texts: List[str], embedding: Embeddings, metadatas: Optional[List[dict]] = None, ids: Optional[List[str]] = None, embedding_chunk_size: int = 1000, batch_size: int = 32, text_key: str = 'text', index: Optional[Index] = None, async_index: Optional[AsyncIndex] = None, index_url: Optional[str] = None, index_token: Optional[str] = None, *, namespace: str = '', **kwargs: Any) UpstashVectorStore [source]¶
Create a new UpstashVectorStore from a list of texts.
Example
- Parameters
texts (List[str]) –
embedding (Embeddings) –
metadatas (Optional[List[dict]]) –
ids (Optional[List[str]]) –
embedding_chunk_size (int) –
batch_size (int) –
text_key (str) –
index (Optional[Index]) –
async_index (Optional[AsyncIndex]) –
index_url (Optional[str]) –
index_token (Optional[str]) –
namespace (str) –
kwargs (Any) –
- Return type
- get_by_ids(ids: Sequence[str], /) List[Document] ¶
Get documents by their IDs.
返回的文档应将 ID 字段设置为向量存储中文档的 ID。
如果某些 ID 未找到或存在重复 ID,则返回的文档可能少于请求的文档。
用户不应假设返回文档的顺序与输入 ID 的顺序匹配。相反,用户应依赖返回文档的 ID 字段。
如果某些 ID 没有找到文档,此方法**不应**引发异常。
- Parameters
ids (Sequence[str]) – 要检索的 ID 列表。
- Returns
文档列表。
- Return type
List[Document]
0.2.11 版本中新增。
- info() InfoResult [source]¶
Get statistics about the index.
- Returns
向量总数
等待索引的向量总数
索引在磁盘上的总大小(字节)
索引的维度计数
为索引选择的相似度函数
- Return type
InfoResult
- max_marginal_relevance_search(query: str, k: int = 4, fetch_k: int = 20, lambda_mult: float = 0.5, filter: Optional[str] = None, *, namespace: Optional[str] = None, **kwargs: Any) List[Document] [source]¶
Return docs selected using the maximal marginal relevance.
最大边际相关性优化查询的相似性和所选文档之间的多样性。
- Parameters
query (str) – 用于查找相似文档的文本。
k (int) – 要返回的文档数量。默认为 4。
fetch_k (int) – 要获取并传递给 MMR 算法的文档数量。
lambda_mult (float) – 介于 0 和 1 之间的数字,用于确定结果之间多样性的程度,其中 0 对应于最大多样性,1 对应于最小多样性。默认为 0.5。
filter (Optional[str]) – str 格式的可选元数据过滤器
namespace (Optional[str]) – Namespace to use from the index.
kwargs (Any) –
- Returns
通过最大边际相关性选择的文档列表。
- Return type
List[Document]
- max_marginal_relevance_search_by_vector(embedding: Union[List[float], str], k: int = 4, fetch_k: int = 20, lambda_mult: float = 0.5, filter: Optional[str] = None, *, namespace: Optional[str] = None, **kwargs: Any) List[Document] [source]¶
Return docs selected using the maximal marginal relevance.
最大边际相关性优化查询的相似性和所选文档之间的多样性。
- Parameters
embedding (Union[List[float], str]) – 用于查找相似文档的嵌入。
k (int) – 要返回的文档数量。默认为 4。
fetch_k (int) – 要获取并传递给 MMR 算法的文档数量。
lambda_mult (float) – 介于 0 和 1 之间的数字,用于确定结果之间多样性的程度,其中 0 对应于最大多样性,1 对应于最小多样性。默认为 0.5。
filter (Optional[str]) – str 格式的可选元数据过滤器
namespace (Optional[str]) – Namespace to use from the index.
kwargs (Any) –
- Returns
通过最大边际相关性选择的文档列表。
- Return type
List[Document]
- search(query: str, search_type: str, **kwargs: Any) List[Document] ¶
Return docs most similar to query using a specified search type.
- Parameters
query (str) – 输入文本
search_type (str) – 要执行的搜索类型。可以是“similarity”、“mmr”或“similarity_score_threshold”。
**kwargs (Any) – 传递给搜索方法的参数。
- Returns
与查询最相似的文档列表。
- Raises
ValueError – 如果 search_type 不是 “similarity”、“mmr” 或 “similarity_score_threshold” 之一。
- Return type
List[Document]
- similarity_search(query: str, k: int = 4, filter: Optional[str] = None, *, namespace: Optional[str] = None, **kwargs: Any) List[Document] [source]¶
Return documents most similar to query.
- Parameters
query (str) – 用于查找相似文档的文本。
k (int) – 要返回的文档数量。默认为 4。
filter (Optional[str]) – str 格式的可选元数据过滤器
namespace (Optional[str]) – Namespace to use from the index.
kwargs (Any) –
- Returns
最相似于查询的文档列表以及每个文档的分数
- Return type
List[Document]
- similarity_search_by_vector(embedding: Union[List[float], str], k: int = 4, filter: Optional[str] = None, *, namespace: Optional[str] = None, **kwargs: Any) List[Document] [source]¶
Return documents closest to the given embedding.
- Parameters
embedding (Union[List[float], str]) – 用于查找相似文档的嵌入。
k (int) – 要返回的文档数量。默认为 4。
filter (Optional[str]) – str 格式的可选元数据过滤器
namespace (Optional[str]) – Namespace to use from the index.
kwargs (Any) –
- Returns
与查询最相似的文档列表
- Return type
List[Document]
- similarity_search_by_vector_with_score(embedding: Union[List[float], str], k: int = 4, filter: Optional[str] = None, *, namespace: Optional[str] = None, **kwargs: Any) List[Tuple[Document, float]] [source]¶
Return texts whose embedding is closest to the given embedding
- Parameters
embedding (Union[List[float], str]) –
k (int) –
filter (Optional[str]) –
namespace (Optional[str]) –
kwargs (Any) –
- 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 表示不相似,1 表示最相似。
- Parameters
query (str) – 输入文本。
k (int) – 要返回的文档数量。默认为 4。
**kwargs (Any) –
传递给相似性搜索的 kwargs。应包含:score_threshold: 可选,一个介于 0 到 1 之间的浮点值,用于
过滤检索到的文档结果集。
- Returns
由 (doc, similarity_score) 元组组成的列表。
- Return type
List[Tuple[Document, float]]
- similarity_search_with_score(query: str, k: int = 4, filter: Optional[str] = None, *, namespace: Optional[str] = None, **kwargs: Any) List[Tuple[Document, float]] [source]¶
Retrieve texts most similar to query and convert the result to Document objects.
- Parameters
query (str) – 用于查找相似文档的文本。
k (int) – 要返回的文档数量。默认为 4。
filter (Optional[str]) – str 格式的可选元数据过滤器
namespace (Optional[str]) – Namespace to use from the index.
kwargs (Any) –
- Returns
最相似于查询的文档列表以及每个文档的分数
- Return type
List[Tuple[Document, float]]
- streaming_upsert(items: Iterable[Document], /, batch_size: int, **kwargs: Any) Iterator[UpsertResponse] ¶
Beta
添加于 0.2.11 版本。API 可能会发生变化。
以流式方式更新文档。
- Parameters
items (Iterable[Document]) – 要添加到向量存储的可迭代文档。
batch_size (int) – 每次 upsert 的批量大小。
kwargs (Any) – 附加关键字参数。 kwargs 应该只包含所有文档通用的参数。(例如,索引超时、重试策略等。)kwargs 不应包含 id 以避免语义模糊。相反,ID 应该作为 Document 对象的一部分提供。
- Yields
UpsertResponse – 一个响应对象,其中包含已成功添加到或更新到向量存储的 ID 列表,以及未能添加或更新的 ID 列表。
- Return type
Iterator[UpsertResponse]
0.2.11 版本中新增。
- upsert(items: Sequence[Document], /, **kwargs: Any) UpsertResponse ¶
Beta
添加于 0.2.11 版本。API 可能会发生变化。
在向量存储中添加或更新文档。
如果提供了 Document 对象的 ID 字段,upsert 功能应使用该字段。如果未提供 ID,则 upsert 方法可以自由地为文档生成 ID。
当指定了 ID 并且文档已存在于向量存储中时,upsert 方法应使用新数据更新文档。如果文档不存在,则 upsert 方法应将文档添加到向量存储中。
- Parameters
items (Sequence[Document]) – 要添加到向量存储的文档序列。
kwargs (Any) – 额外的关键字参数。
- Returns
一个响应对象,其中包含已成功添加到或更新到向量存储的 ID 列表,以及未能添加或更新的 ID 列表。
- Return type
0.2.11 版本中新增。