langchain_community.vectorstores.supabase
.SupabaseVectorStore¶
- class langchain_community.vectorstores.supabase.SupabaseVectorStore(client: supabase.client.Client, embedding: Embeddings, table_name: str, chunk_size: int = 500, query_name: Union[str, None] = None)[source]¶
Supabase Postgres 向量存储。
它假设您已安装 pgvector 扩展和一个 match_documents (或类似) 函数。更多详情请参阅:https://integrations.langchain.ac.cn/vectorstores?integration_name=SupabaseVectorStore
您可以实现自己的 match_documents 函数,以便根据您自己的授权或业务逻辑将搜索空间限制为文档的子集。
请注意,Supabase Python 客户端尚不支持异步操作。
如果您想使用 max_marginal_relevance_search,请查看以下关于修改 match_documents 函数以返回匹配嵌入的说明。
示例 (Examples)
from langchain_community.embeddings.openai import OpenAIEmbeddings from langchain_core.documents import Document from langchain_community.vectorstores import SupabaseVectorStore from supabase.client import create_client docs = [ Document(page_content="foo", metadata={"id": 1}), ] embeddings = OpenAIEmbeddings() supabase_client = create_client("my_supabase_url", "my_supabase_key") vector_store = SupabaseVectorStore.from_documents( docs, embeddings, client=supabase_client, table_name="documents", query_name="match_documents", chunk_size=500, )
从现有表加载 (To load from an existing table)
from langchain_community.embeddings.openai import OpenAIEmbeddings from langchain_community.vectorstores import SupabaseVectorStore from supabase.client import create_client embeddings = OpenAIEmbeddings() supabase_client = create_client("my_supabase_url", "my_supabase_key") vector_store = SupabaseVectorStore( client=supabase_client, embedding=embeddings, table_name="documents", query_name="match_documents", )
使用 supabase 客户端初始化。(Initialize with supabase client.)
属性 (Attributes)
embeddings
如果可用,访问查询嵌入对象。(Access the query embedding object if available.)
方法 (Methods)
__init__
(client, embedding, table_name[, ...])使用 supabase 客户端初始化。(Initialize with supabase client.)
aadd_documents
(documents, **kwargs)异步运行更多文档通过嵌入并添加到向量存储。(Async run more documents through the embeddings and add to the vectorstore.)
aadd_texts
(texts[, metadatas])异步运行更多文本通过嵌入并添加到向量存储。(Async run more texts through the embeddings and add to the vectorstore.)
add_documents
(documents, **kwargs)在向量存储中添加或更新文档。(Add or update documents in the vectorstore.)
add_texts
(texts[, metadatas, ids])运行更多文本通过嵌入并添加到向量存储。(Run more texts through the embeddings and add to the vectorstore.)
add_vectors
(vectors, documents, ids)adelete
([ids])通过向量 ID 或其他条件异步删除。(Async delete by vector ID or other criteria.)
afrom_documents
(documents, embedding, **kwargs)异步返回从文档和嵌入初始化的 VectorStore。(Async return VectorStore initialized from documents and embeddings.)
afrom_texts
(texts, embedding[, metadatas])异步返回从文本和嵌入初始化的 VectorStore。(Async return VectorStore initialized from texts and embeddings.)
aget_by_ids
(ids, /)异步通过 ID 获取文档。(Async get documents by their IDs.)
amax_marginal_relevance_search
(query[, k, ...])异步返回使用最大边际相关性选择的文档。(Async return docs selected using the maximal marginal relevance.)
异步返回使用最大边际相关性选择的文档。(Async return docs selected using the maximal marginal relevance.)
as_retriever
(**kwargs)返回从该 VectorStore 初始化的 VectorStoreRetriever。(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])异步返回与查询最相似的文档。(Async return docs most similar to query.)
asimilarity_search_by_vector
(embedding[, k])异步返回与嵌入向量最相似的文档。(Async return docs most similar to embedding vector.)
异步返回文档和相关性评分,范围为 [0, 1]。(Async return docs and relevance scores in the range [0, 1].)
asimilarity_search_with_score
(*args, **kwargs)异步运行带距离的相似性搜索。(Async run similarity search with distance.)
astreaming_upsert
(items, /, batch_size, **kwargs)aupsert
(items, /, **kwargs)delete
([ids])通过向量 ID 删除。(Delete by vector IDs.)
from_documents
(documents, embedding, **kwargs)返回从文档和嵌入初始化的 VectorStore。(Return VectorStore initialized from documents and embeddings.)
from_texts
(texts, embedding[, metadatas, ...])返回从文本和嵌入初始化的 VectorStore。(Return VectorStore initialized from texts and embeddings.)
get_by_ids
(ids, /)通过 ID 获取文档。(Get documents by their IDs.)
match_args
(query, filter)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])返回与查询最相似的文档。(Return docs most similar to query.)
similarity_search_by_vector
(embedding[, k, ...])返回与嵌入向量最相似的文档。(Return docs most similar to embedding vector.)
返回文档和相关性评分,范围为 [0, 1]。(Return docs and relevance scores in the range [0, 1].)
similarity_search_with_score
(*args, **kwargs)运行带距离的相似性搜索。(Run similarity search with distance.)
streaming_upsert
(items, /, batch_size, **kwargs)upsert
(items, /, **kwargs)- 参数 (Parameters)
client (supabase.client.Client) –
embedding (Embeddings) –
table_name (str) –
chunk_size (int) –
query_name (Union[str, None]) –
- __init__(client: supabase.client.Client, embedding: Embeddings, table_name: str, chunk_size: int = 500, query_name: Union[str, None] = None) None [source]¶
使用 supabase 客户端初始化。(Initialize with supabase client.)
- 参数 (Parameters)
client (supabase.client.Client) –
embedding (Embeddings) –
table_name (str) –
chunk_size (int) –
query_name (Union[str, None]) –
- 返回类型 (Return type)
None
- async aadd_documents(documents: List[Document], **kwargs: Any) List[str] ¶
异步运行更多文档通过嵌入并添加到向量存储。(Async run more documents through the embeddings and add to the vectorstore.)
- 参数 (Parameters)
documents (List[Document]) – 要添加到向量存储的文档。(Documents to add to the vectorstore.)
kwargs (Any) – 额外的关键字参数。(Additional keyword arguments.)
- 返回值 (Returns)
添加的文本的 ID 列表。(List of IDs of the added texts.)
- Raises
ValueError – 如果 ID 数量与文档数量不匹配。(If the number of IDs does not match the number of documents.)
- 返回类型 (Return type)
List[str]
- async aadd_texts(texts: Iterable[str], metadatas: Optional[List[dict]] = None, **kwargs: Any) List[str] ¶
异步运行更多文本通过嵌入并添加到向量存储。(Async 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]]) – 与文本关联的可选元数据列表。默认为 None。(Optional list of metadatas associated with the texts. Default is None.)
**kwargs (Any) – 向量存储特定参数。(vectorstore specific parameters.)
- 返回值 (Returns)
从将文本添加到向量存储中获取的 ID 列表。(List of ids from adding the texts into the vectorstore.)
- Raises
ValueError – 如果元数据的数量与文本的数量不匹配。(If the number of metadatas does not match the number of texts.)
ValueError – 如果 ID 的数量与文本的数量不匹配。(If the number of ids does not match the number of texts.)
- 返回类型 (Return type)
List[str]
- add_documents(documents: List[Document], **kwargs: Any) List[str] ¶
在向量存储中添加或更新文档。(Add or update documents in the vectorstore.)
- 参数 (Parameters)
documents (List[Document]) – 要添加到向量存储的文档。(Documents to add to the vectorstore.)
kwargs (Any) – 额外的关键字参数。如果 kwargs 包含 ids 并且 documents 包含 ids,则 kwargs 中的 ids 将优先。(Additional keyword arguments. if kwargs contains ids and documents contain ids, the ids in the kwargs will receive precedence.)
- 返回值 (Returns)
添加的文本的 ID 列表。(List of IDs of the added texts.)
- Raises
ValueError – 如果 ID 的数量与文档的数量不匹配。(If the number of ids does not match the number of documents.)
- 返回类型 (Return type)
List[str]
- add_texts(texts: Iterable[str], metadatas: Optional[List[Dict[Any, Any]]] = None, ids: Optional[List[str]] = None, **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[Any, Any]]]) – 与文本关联的可选元数据列表。(Optional list of metadatas associated with the texts.)
**kwargs (Any) – 向量存储特定参数。kwargs 之一应该是 ids,它是与文本关联的 id 列表。(vectorstore specific parameters. One of the kwargs should be ids which is a list of ids associated with the texts.)
ids (Optional[List[str]]) –
**kwargs –
- 返回值 (Returns)
从将文本添加到向量存储中获取的 ID 列表。(List of ids from adding the texts into the vectorstore.)
- Raises
ValueError – 如果元数据的数量与文本的数量不匹配。(If the number of metadatas does not match the number of texts.)
ValueError – 如果 ID 的数量与文本的数量不匹配。(If the number of ids does not match the number of texts.)
- 返回类型 (Return type)
List[str]
- add_vectors(vectors: List[List[float]], documents: List[Document], ids: List[str]) List[str] [source]¶
- 参数 (Parameters)
vectors (List[List[float]]) –
documents (List[Document]) –
ids (List[str]) –
- 返回类型 (Return type)
List[str]
- async adelete(ids: Optional[List[str]] = None, **kwargs: Any) Optional[bool] ¶
通过向量 ID 或其他条件异步删除。(Async delete by vector ID or other criteria.)
- 参数 (Parameters)
ids (Optional[List[str]]) – 要删除的 id 列表。如果为 None,则删除全部。默认为 None。(List of ids to delete. If None, delete all. Default is None.)
**kwargs (Any) – 子类可能使用的其他关键字参数。(Other keyword arguments that subclasses might use.)
- 返回值 (Returns)
如果删除成功,则为 True,否则为 False,如果未实现,则为 None。(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 ¶
异步返回从文档和嵌入初始化的 VectorStore。(Async return VectorStore initialized from documents and embeddings.)
- 参数 (Parameters)
documents (List[Document]) – 要添加到向量存储的文档列表。
embedding (Embeddings) – 要使用的嵌入函数。
kwargs (Any) – 额外的关键字参数。(Additional keyword arguments.)
- 返回值 (Returns)
从文档和嵌入初始化的 VectorStore。
- 返回类型 (Return type)
- async classmethod afrom_texts(texts: List[str], embedding: Embeddings, metadatas: Optional[List[dict]] = None, **kwargs: Any) VST ¶
异步返回从文本和嵌入初始化的 VectorStore。(Async return VectorStore initialized from texts and embeddings.)
- 参数 (Parameters)
texts (List[str]) – 要添加到向量存储的文本列表。
embedding (Embeddings) – 要使用的嵌入函数。
metadatas (Optional[List[dict]]) – 与文本关联的可选元数据列表。默认为 None。(Optional list of metadatas associated with the texts. Default is None.)
kwargs (Any) – 额外的关键字参数。(Additional keyword arguments.)
- 返回值 (Returns)
从文本和嵌入初始化的 VectorStore。
- 返回类型 (Return type)
- async aget_by_ids(ids: Sequence[str], /) List[Document] ¶
异步通过 ID 获取文档。(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 amax_marginal_relevance_search(query: str, k: int = 4, fetch_k: int = 20, lambda_mult: float = 0.5, **kwargs: Any) List[Document] ¶
异步返回使用最大边际相关性选择的文档。(Async return docs selected using the maximal marginal relevance.)
最大边际相关性优化了与查询的相似性以及所选文档之间的多样性。
- 参数 (Parameters)
query (str) – 用于查找相似文档的文本。
k (int) – 要返回的文档数量。默认为 4。
fetch_k (int) – 要获取并传递给 MMR 算法的文档数量。默认为 20。
lambda_mult (float) – 介于 0 和 1 之间的数字,用于确定结果之间多样性的程度,其中 0 对应于最大多样性,1 对应于最小多样性。默认为 0.5。
kwargs (Any) –
- 返回值 (Returns)
通过最大边际相关性选择的文档列表。
- 返回类型 (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] ¶
异步返回使用最大边际相关性选择的文档。(Async return docs selected using the maximal marginal relevance.)
最大边际相关性优化了与查询的相似性以及所选文档之间的多样性。
- 参数 (Parameters)
embedding (List[float]) – 用于查找相似文档的嵌入向量。
k (int) – 要返回的文档数量。默认为 4。
fetch_k (int) – 要获取并传递给 MMR 算法的文档数量。默认为 20。
lambda_mult (float) – 介于 0 和 1 之间的数字,用于确定结果之间多样性的程度,其中 0 对应于最大多样性,1 对应于最小多样性。默认为 0.5。
**kwargs (Any) – 传递给搜索方法的参数。
- 返回值 (Returns)
通过最大边际相关性选择的文档列表。
- 返回类型 (Return type)
List[Document]
- as_retriever(**kwargs: Any) VectorStoreRetriever ¶
返回从该 VectorStore 初始化的 VectorStoreRetriever。(Return VectorStoreRetriever initialized from this VectorStore.)
- 参数 (Parameters)
**kwargs (Any) –
传递给搜索函数的关键字参数。可以包括: search_type (Optional[str]): 定义 Retriever 应执行的搜索类型。可以是 “similarity”(默认)、“mmr” 或 “similarity_score_threshold”。
the Retriever should perform. Can be “similarity” (default), “mmr”, or “similarity_score_threshold”.
- search_kwargs (Optional[Dict]): 传递给
- search function. Can include things like
k: 要返回的文档数量 (默认值: 4) score_threshold: 相似度分数阈值的最小相关性阈值
for similarity_score_threshold
- fetch_k: 要传递给 MMR 算法的文档数量
(Default: 20)
- lambda_mult: MMR 返回结果的多样性;
1 代表最小多样性,0 代表最大多样性。(默认值: 0.5)
filter: 按文档元数据筛选
- 返回值 (Returns)
VectorStore 的 Retriever 类。
- 返回类型 (Return type)
示例 (Examples)
# 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, **kwargs: Any) List[Document] ¶
异步返回与查询最相似的文档。(Async return docs most similar to query.)
- 参数 (Parameters)
query (str) – 输入文本。
k (int) – 要返回的文档数量。默认为 4。
**kwargs (Any) – 传递给搜索方法的参数。
- 返回值 (Returns)
与查询最相似的文档列表。
- 返回类型 (Return type)
List[Document]
- async asimilarity_search_by_vector(embedding: List[float], k: int = 4, **kwargs: Any) List[Document] ¶
异步返回与嵌入向量最相似的文档。(Async return docs most similar to embedding vector.)
- 参数 (Parameters)
embedding (List[float]) – 用于查找相似文档的嵌入向量。
k (int) – 要返回的文档数量。默认为 4。
**kwargs (Any) – 传递给搜索方法的参数。
- 返回值 (Returns)
与查询向量最相似的文档列表。
- 返回类型 (Return type)
List[Document]
- async asimilarity_search_with_relevance_scores(query: str, k: int = 4, **kwargs: Any) List[Tuple[Document, float]] ¶
异步返回文档和相关性评分,范围为 [0, 1]。(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 之间的浮点值,用于
filter the resulting set of retrieved docs
- 返回值 (Returns)
文档和相似度分数元组的列表
- 返回类型 (Return type)
List[Tuple[Document, float]]
- async asimilarity_search_with_score(*args: Any, **kwargs: Any) List[Tuple[Document, float]] ¶
异步运行带距离的相似性搜索。(Async run similarity search with distance.)
- 参数 (Parameters)
*args (Any) – 传递给搜索方法的参数。
**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 不应包含 ID 以避免语义不明确。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) – 额外的关键字参数。(Additional keyword arguments.)
- 返回值 (Returns)
响应对象,其中包含已成功添加到向量存储或在向量存储中更新的 ID 列表,以及未能添加或更新的 ID 列表。
- 返回类型 (Return type)
0.2.11 版本新增。
- delete(ids: Optional[List[str]] = None, **kwargs: Any) None [source]¶
通过向量 ID 删除。(Delete by vector IDs.)
- 参数 (Parameters)
ids (Optional[List[str]]) – 要删除的 ID 列表。
kwargs (Any) –
- 返回类型 (Return type)
None
- classmethod from_documents(documents: List[Document], embedding: Embeddings, **kwargs: Any) VST ¶
返回从文档和嵌入初始化的 VectorStore。(Return VectorStore initialized from documents and embeddings.)
- 参数 (Parameters)
documents (List[Document]) – 要添加到向量存储的文档列表。
embedding (Embeddings) – 要使用的嵌入函数。
kwargs (Any) – 额外的关键字参数。(Additional keyword arguments.)
- 返回值 (Returns)
从文档和嵌入初始化的 VectorStore。
- 返回类型 (Return type)
- classmethod from_texts(texts: List[str], embedding: Embeddings, metadatas: Optional[List[dict]] = None, client: Optional[supabase.client.Client] = None, table_name: Optional[str] = 'documents', query_name: Union[str, None] = 'match_documents', chunk_size: int = 500, ids: Optional[List[str]] = None, **kwargs: Any) SupabaseVectorStore [source]¶
返回从文本和嵌入初始化的 VectorStore。(Return VectorStore initialized from texts and embeddings.)
- 参数 (Parameters)
texts (List[str]) –
embedding (Embeddings) –
metadatas (Optional[List[dict]]) –
client (Optional[supabase.client.Client]) –
table_name (Optional[str]) –
query_name (Union[str, None]) –
chunk_size (int) –
ids (Optional[List[str]]) –
kwargs (Any) –
- 返回类型 (Return type)
- get_by_ids(ids: Sequence[str], /) List[Document] ¶
通过 ID 获取文档。(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 版本新增。
- match_args(query: List[float], filter: Optional[Dict[str, Any]]) Dict[str, Any] [source]¶
- 参数 (Parameters)
query (List[float]) –
filter (Optional[Dict[str, Any]]) –
- 返回类型 (Return type)
Dict[str, Any]
- max_marginal_relevance_search(query: str, k: int = 4, fetch_k: int = 20, lambda_mult: float = 0.5, **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。
kwargs (Any) –
- 返回值 (Returns)
通过最大边际相关性选择的文档列表。
- 返回类型 (Return type)
List[Document]
max_marginal_relevance_search 要求 query_name 在匹配文档旁边返回匹配的嵌入向量。以下函数演示了如何执行此操作
```sql CREATE FUNCTION match_documents_embeddings(query_embedding vector(1536),
match_count int)
- RETURNS TABLE(
id uuid, content text, metadata jsonb, embedding vector(1536), similarity float)
LANGUAGE plpgsql AS $$ # variable_conflict use_column
- BEGIN
RETURN query SELECT
id, content, metadata, embedding, 1 -(docstore.embedding <=> query_embedding) AS similarity
- FROM
docstore
- ORDER BY
docstore.embedding <=> query_embedding
LIMIT match_count;
- 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]¶
返回使用最大边际相关性选择的文档。(Return docs selected using the maximal marginal relevance.)
最大边际相关性优化了与查询的相似性以及所选文档之间的多样性。
- 参数 (Parameters)
embedding (List[float]) – 用于查找相似文档的嵌入向量。
k (int) – 要返回的文档数量。默认为 4。
fetch_k (int) – 要获取并传递给 MMR 算法的文档数量。
lambda_mult (float) – 介于 0 和 1 之间的数字,用于确定结果之间多样性的程度,其中 0 对应于最大多样性,1 对应于最小多样性。默认为 0.5。
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[Dict[str, Any]] = None, **kwargs: Any) List[Document] [source]¶
返回与查询最相似的文档。(Return docs most similar to query.)
- 参数 (Parameters)
query (str) – 输入文本。
k (int) – 要返回的文档数量。默认为 4。
**kwargs (Any) – 传递给搜索方法的参数。
filter (Optional[Dict[str, Any]]) –
**kwargs –
- 返回值 (Returns)
与查询最相似的文档列表。
- 返回类型 (Return type)
List[Document]
- similarity_search_by_vector(embedding: List[float], k: int = 4, filter: Optional[Dict[str, Any]] = None, **kwargs: Any) List[Document] [source]¶
返回与嵌入向量最相似的文档。(Return docs most similar to embedding vector.)
- 参数 (Parameters)
embedding (List[float]) – 用于查找相似文档的嵌入向量。
k (int) – 要返回的文档数量。默认为 4。
**kwargs (Any) – 传递给搜索方法的参数。
filter (Optional[Dict[str, Any]]) –
**kwargs –
- 返回值 (Returns)
与查询向量最相似的文档列表。
- 返回类型 (Return type)
List[Document]
- similarity_search_by_vector_returning_embeddings(query: List[float], k: int, filter: Optional[Dict[str, Any]] = None, postgrest_filter: Optional[str] = None) List[Tuple[Document, float, ndarray[float32, Any]]] [source]¶
- 参数 (Parameters)
query (List[float]) –
k (int) –
filter (Optional[Dict[str, Any]]) –
postgrest_filter (Optional[str]) –
- 返回类型 (Return type)
List[Tuple[Document, float, ndarray[float32, Any]]]
- similarity_search_by_vector_with_relevance_scores(query: List[float], k: int, filter: Optional[Dict[str, Any]] = None, postgrest_filter: Optional[str] = None, score_threshold: Optional[float] = None) List[Tuple[Document, float]] [source]¶
- 参数 (Parameters)
query (List[float]) –
k (int) –
filter (Optional[Dict[str, Any]]) –
postgrest_filter (Optional[str]) –
score_threshold (Optional[float]) –
- 返回类型 (Return type)
List[Tuple[Document, float]]
- similarity_search_with_relevance_scores(query: str, k: int = 4, filter: Optional[Dict[str, Any]] = None, **kwargs: Any) List[Tuple[Document, float]] [source]¶
返回文档和相关性评分,范围为 [0, 1]。(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 之间的浮点值,用于
过滤检索到的文档结果集。
filter (Optional[Dict[str, Any]]) –
**kwargs –
- 返回值 (Returns)
文档和相似度分数元组的列表。
- 返回类型 (Return type)
List[Tuple[Document, float]]
- similarity_search_with_score(*args: Any, **kwargs: Any) List[Tuple[Document, float]] ¶
运行带距离的相似性搜索。(Run similarity search with distance.)
- 参数 (Parameters)
*args (Any) – 传递给搜索方法的参数。
**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 不应包含 ids 以避免语义模糊。相反,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 可能会发生变化。
在向量存储中添加或更新文档。(Add or update documents in the vectorstore.)
如果提供了 Document 对象的 ID 字段,则 upsert 功能应使用该字段。如果未提供 ID,则 upsert 方法可以自由地为文档生成 ID。
当指定了 ID 且文档已存在于向量存储中时,upsert 方法应使用新数据更新文档。如果文档不存在,则 upsert 方法应将文档添加到向量存储中。
- 参数 (Parameters)
items (Sequence[Document]) – 要添加到向量存储的文档序列。
kwargs (Any) – 额外的关键字参数。(Additional keyword arguments.)
- 返回值 (Returns)
响应对象,其中包含已成功添加到向量存储或在向量存储中更新的 ID 列表,以及未能添加或更新的 ID 列表。
- 返回类型 (Return type)
0.2.11 版本新增。