langchain_community.vectorstores.matching_engine.MatchingEngine

class langchain_community.vectorstores.matching_engine.MatchingEngine(project_id: str, index: MatchingEngineIndex, endpoint: MatchingEngineIndexEndpoint, embedding: Embeddings, gcs_client: storage.Client, gcs_bucket_name: str, credentials: Optional[Credentials] = None, *, document_id_key: Optional[str] = None)[source]

Deprecated since version 0.0.12: 请使用 langchain_google_vertexai.VectorSearchVectorStore 代替。

Google Vertex AI Vector Search(之前名为 Matching Engine)向量存储。

虽然嵌入存储在 Matching Engine 中,但嵌入的文档将存储在 GCS 中。

现有的索引和相应的端点是使用此模块的先决条件。

请参阅 docs/integrations/vectorstores/google_vertex_ai_vector_search.ipynb 中的用法

请注意,如果您计划进行实时实现,则此实现主要用于读取。虽然读取是实时操作,但更新索引需要接近一个小时。

Google Vertex AI Vector Search(之前名为 Matching Engine)

向量存储的实现。

虽然嵌入存储在 Matching Engine 中,但嵌入的文档将存储在 GCS 中。

现有的索引和相应的端点是使用此模块的先决条件。

请参阅 docs/integrations/vectorstores/google_vertex_ai_vector_search.ipynb 中的用法。

请注意,如果您计划进行实时实现,则此实现主要用于读取。虽然读取是实时操作,但更新索引需要接近一个小时。

参数
  • project_id (str) –

  • index (MatchingEngineIndex) –

  • endpoint (MatchingEngineIndexEndpoint) –

  • embedding (Embeddings) –

  • gcs_client (storage.Client) –

  • gcs_bucket_name (str) –

  • credentials (Optional[Credentials]) –

  • document_id_key (Optional[str]) –

project_id

GCS 项目 ID。

index

创建的索引类。请参阅 ~:func:MatchingEngine.from_components

endpoint

创建的端点类。请参阅 ~:func:MatchingEngine.from_components

embedding

Embeddings,将用于嵌入发送的文本。 如果未发送任何内容,则将使用多语言 Tensorflow Universal Sentence Encoder。

gcs_client

GCS 客户端。

gcs_bucket_name

GCS 存储桶名称。

credentials

创建的 GCP 凭据。

类型

可选

document_id_key

用于在文档元数据中存储文档 ID 的键。 如果为 None,则文档 ID 将不会在文档元数据中返回。

类型

可选

属性

embeddings

如果可用,访问查询嵌入对象。

方法

__init__(project_id, index, endpoint, ...[, ...])

Google Vertex AI Vector Search(之前名为 Matching Engine)

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。

aget_by_ids(ids, /)

异步按 ID 获取文档。

amax_marginal_relevance_search(query[, k, ...])

异步返回使用最大边际相关性选择的文档。

amax_marginal_relevance_search_by_vector(...)

异步返回使用最大边际相关性选择的文档。

as_retriever(**kwargs)

返回从此 VectorStore 初始化的 VectorStoreRetriever。

asearch(query, search_type, **kwargs)

异步返回与使用指定搜索类型查询最相似的文档。

asimilarity_search(query[, k])

异步返回与查询最相似的文档。

asimilarity_search_by_vector(embedding[, k])

异步返回与嵌入向量最相似的文档。

asimilarity_search_with_relevance_scores(query)

异步返回范围在 [0, 1] 内的文档和相关性得分。

asimilarity_search_with_score(*args, **kwargs)

异步运行带距离的相似性搜索。

astreaming_upsert(items, /, batch_size, **kwargs)

aupsert(items, /, **kwargs)

delete([ids])

通过向量 ID 或其他条件删除。

from_components(project_id, region, ...[, ...])

从构造函数中取出对象创建。

from_documents(documents, embedding, **kwargs)

返回从文档和嵌入初始化的 VectorStore。

from_texts(texts, embedding[, metadatas])

请改用 from components。

get_by_ids(ids, /)

按 ID 获取文档。

max_marginal_relevance_search(query[, k, ...])

返回使用最大边际相关性选择的文档。

max_marginal_relevance_search_by_vector(...)

返回使用最大边际相关性选择的文档。

search(query, search_type, **kwargs)

返回与使用指定搜索类型查询最相似的文档。

similarity_search(query[, k, filter])

返回与查询最相似的文档。

similarity_search_by_vector(embedding[, k, ...])

返回与嵌入最相似的文档。

similarity_search_by_vector_with_score(embedding)

返回与嵌入最相似的文档及其余弦距离。

similarity_search_with_relevance_scores(query)

返回范围在 [0, 1] 内的文档和相关性得分。

similarity_search_with_score(query[, k, filter])

返回与查询最相似的文档及其与查询的余弦距离。

streaming_upsert(items, /, batch_size, **kwargs)

upsert(items, /, **kwargs)

__init__(project_id: str, index: MatchingEngineIndex, endpoint: MatchingEngineIndexEndpoint, embedding: Embeddings, gcs_client: storage.Client, gcs_bucket_name: str, credentials: Optional[Credentials] = None, *, document_id_key: Optional[str] = None)[source]
Google Vertex AI Vector Search(之前名为 Matching Engine)

向量存储的实现。

虽然嵌入存储在 Matching Engine 中,但嵌入的文档将存储在 GCS 中。

现有的索引和相应的端点是使用此模块的先决条件。

请参阅 docs/integrations/vectorstores/google_vertex_ai_vector_search.ipynb 中的用法。

请注意,如果您计划进行实时实现,则此实现主要用于读取。虽然读取是实时操作,但更新索引需要接近一个小时。

参数
  • project_id (str) –

  • index (MatchingEngineIndex) –

  • endpoint (MatchingEngineIndexEndpoint) –

  • embedding (Embeddings) –

  • gcs_client (storage.Client) –

  • gcs_bucket_name (str) –

  • credentials (Optional[Credentials]) –

  • document_id_key (Optional[str]) –

project_id

GCS 项目 ID。

index

创建的索引类。请参阅 ~:func:MatchingEngine.from_components

endpoint

创建的端点类。请参阅 ~:func:MatchingEngine.from_components

embedding

Embeddings,将用于嵌入发送的文本。 如果未发送任何内容,则将使用多语言 Tensorflow Universal Sentence Encoder。

gcs_client

GCS 客户端。

gcs_bucket_name

GCS 存储桶名称。

credentials

创建的 GCP 凭据。

类型

可选

document_id_key

用于在文档元数据中存储文档 ID 的键。 如果为 None,则文档 ID 将不会在文档元数据中返回。

类型

可选

async aadd_documents(documents: List[Document], **kwargs: Any) List[str]

异步运行更多文档通过嵌入并添加到向量存储。

参数
  • documents (List[Document]) – 要添加到向量存储的文档。

  • kwargs (Any) – 附加关键字参数。

返回

添加文本的 ID 列表。

引发

ValueError – 如果 ID 数量与文档数量不匹配。

返回类型

List[str]

async aadd_texts(texts: Iterable[str], metadatas: Optional[List[dict]] = None, **kwargs: Any) List[str]

异步运行更多文本通过嵌入并添加到向量存储。

参数
  • texts (Iterable[str]) – 要添加到向量存储的字符串的可迭代对象。

  • metadatas (Optional[List[dict]]) – 与文本关联的可选元数据列表。 默认为 None。

  • **kwargs (Any) – 向量存储特定的参数。

返回

从将文本添加到向量存储中获得的 ID 列表。

引发
  • ValueError – 如果元数据数量与文本数量不匹配。

  • ValueError – 如果 ID 数量与文本数量不匹配。

返回类型

List[str]

add_documents(documents: List[Document], **kwargs: Any) List[str]

在向量存储中添加或更新文档。

参数
  • documents (List[Document]) – 要添加到向量存储的文档。

  • kwargs (Any) – 附加关键字参数。 如果 kwargs 包含 ids 并且文档包含 ids,则 kwargs 中的 ids 将优先。

返回

添加文本的 ID 列表。

引发

ValueError – 如果 ID 数量与文档数量不匹配。

返回类型

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) – 向量存储特定的参数。

返回

从将文本添加到向量存储中获得的 ID 列表。

返回类型

List[str]

async adelete(ids: Optional[List[str]] = None, **kwargs: Any) Optional[bool]

通过向量 ID 或其他条件异步删除。

参数
  • ids (Optional[List[str]]) – 要删除的id列表。如果为None,则删除所有。默认为None。

  • **kwargs (Any) – 子类可能使用的其他关键字参数。

返回

如果删除成功,则为True,否则为False;如果未实现,则为None。

返回类型

Optional[bool]

async classmethod afrom_documents(documents: List[Document], embedding: Embeddings, **kwargs: Any) VST

异步返回从文档和嵌入初始化的 VectorStore。

参数
  • documents (List[Document]) – 要添加到向量存储的文档列表。

  • embedding (Embeddings) – 要使用的嵌入函数。

  • kwargs (Any) – 附加关键字参数。

返回

从文档和嵌入初始化的VectorStore。

返回类型

VectorStore

async classmethod afrom_texts(texts: List[str], embedding: Embeddings, metadatas: Optional[List[dict]] = None, **kwargs: Any) VST

异步返回从文本和嵌入初始化的 VectorStore。

参数
  • texts (List[str]) – 要添加到向量存储的文本列表。

  • embedding (Embeddings) – 要使用的嵌入函数。

  • metadatas (Optional[List[dict]]) – 与文本关联的可选元数据列表。 默认为 None。

  • kwargs (Any) – 附加关键字参数。

返回

从文本和嵌入初始化的VectorStore。

返回类型

VectorStore

async aget_by_ids(ids: Sequence[str], /) List[Document]

异步按 ID 获取文档。

返回的文档应将ID字段设置为向量存储中文档的ID。

如果某些ID未找到或存在重复ID,则返回的文档可能少于请求的文档。

用户不应假定返回文档的顺序与输入ID的顺序匹配。相反,用户应依赖返回文档的ID字段。

如果某些ID未找到文档,则此方法不应引发异常。

参数

ids (Sequence[str]) – 要检索的id列表。

返回

文档列表。

返回类型

List[Document]

0.2.11版本新增功能。

异步返回使用最大边际相关性选择的文档。

最大边际相关性优化查询的相似性和所选文档之间的多样性。

参数
  • query (str) – 用于查找相似文档的文本。

  • k (int) – 要返回的文档数量。默认为 4。

  • fetch_k (int) – 要获取并传递给 MMR 算法的文档数量。默认为 20。

  • lambda_mult (float) – 介于 0 和 1 之间的数字,用于确定结果之间多样性的程度,其中 0 对应于最大多样性,1 对应于最小多样性。默认为 0.5。

  • kwargs (Any) –

返回

通过最大边际相关性选择的文档列表。

返回类型

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]

异步返回使用最大边际相关性选择的文档。

最大边际相关性优化查询的相似性和所选文档之间的多样性。

参数
  • embedding (List[float]) – 用于查找相似文档的嵌入向量。

  • k (int) – 要返回的文档数量。默认为 4。

  • fetch_k (int) – 要获取并传递给 MMR 算法的文档数量。默认为 20。

  • lambda_mult (float) – 介于 0 和 1 之间的数字,用于确定结果之间多样性的程度,其中 0 对应于最大多样性,1 对应于最小多样性。默认为 0.5。

  • **kwargs (Any) – 传递给搜索方法的参数。

返回

通过最大边际相关性选择的文档列表。

返回类型

List[Document]

as_retriever(**kwargs: Any) VectorStoreRetriever

返回从此 VectorStore 初始化的 VectorStoreRetriever。

参数

**kwargs (Any) –

要传递给搜索函数的关键字参数。可以包括:search_type (Optional[str]): 定义检索器应执行的搜索类型。

可以是“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: 按文档元数据过滤

返回

VectorStore 的检索器类。

返回类型

VectorStoreRetriever

示例

# 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]

异步返回与使用指定搜索类型查询最相似的文档。

参数
  • query (str) – 输入文本。

  • search_type (str) – 要执行的搜索类型。可以是“similarity”、“mmr”或“similarity_score_threshold”。

  • **kwargs (Any) – 传递给搜索方法的参数。

返回

与查询最相似的文档列表。

引发

ValueError – 如果 search_type 不是“similarity”、“mmr”或“similarity_score_threshold”之一。

返回类型

List[Document]

异步返回与查询最相似的文档。

参数
  • query (str) – 输入文本。

  • k (int) – 要返回的文档数量。默认为 4。

  • **kwargs (Any) – 传递给搜索方法的参数。

返回

与查询最相似的文档列表。

返回类型

List[Document]

async asimilarity_search_by_vector(embedding: List[float], k: int = 4, **kwargs: Any) List[Document]

异步返回与嵌入向量最相似的文档。

参数
  • embedding (List[float]) – 用于查找相似文档的嵌入向量。

  • k (int) – 要返回的文档数量。默认为 4。

  • **kwargs (Any) – 传递给搜索方法的参数。

返回

与查询向量最相似的文档列表。

返回类型

List[Document]

async asimilarity_search_with_relevance_scores(query: str, k: int = 4, **kwargs: Any) List[Tuple[Document, float]]

异步返回范围在 [0, 1] 内的文档和相关性得分。

0 表示不相似,1 表示最相似。

参数
  • query (str) – 输入文本。

  • k (int) – 要返回的文档数量。默认为 4。

  • **kwargs (Any) –

    要传递给相似度搜索的kwargs。应包括:score_threshold:可选,介于 0 到 1 之间的浮点值,用于

    过滤检索到的文档结果集

返回

(doc, similarity_score) 元组列表

返回类型

List[Tuple[Document, float]]

async asimilarity_search_with_score(*args: Any, **kwargs: Any) List[Tuple[Document, float]]

异步运行带距离的相似性搜索。

参数
  • *args (Any) – 传递给搜索方法的参数。

  • **kwargs (Any) – 传递给搜索方法的参数。

返回

(doc, similarity_score) 元组列表。

返回类型

List[Tuple[Document, float]]

astreaming_upsert(items: AsyncIterable[Document], /, batch_size: int, **kwargs: Any) AsyncIterator[UpsertResponse]

Beta

0.2.11版本新增功能。API可能会发生变化。

以流式方式更新文档。streaming_upsert 的异步版本。

参数
  • items (AsyncIterable[Document]) – 要添加到向量存储的文档的可迭代对象。

  • batch_size (int) – 每次批量更新的大小。

  • kwargs (Any) – 附加关键字参数。kwargs 应仅包括所有文档通用的参数。(例如,索引超时、重试策略等)kwargs 不应包括 id,以避免语义不明确。相反,ID 应作为 Document 对象的一部分提供。

Yields

UpsertResponse – 响应对象,其中包含已成功添加到向量存储或在向量存储中更新的 ID 列表,以及未能添加或更新的 ID 列表。

返回类型

AsyncIterator[UpsertResponse]

0.2.11版本新增功能。

async aupsert(items: Sequence[Document], /, **kwargs: Any) UpsertResponse

Beta

0.2.11版本新增功能。API可能会发生变化。

在向量存储中添加或更新文档。upsert 的异步版本。

如果提供了 Document 对象的 ID 字段,则更新功能应使用该字段。如果未提供 ID,则更新方法可以自由地为文档生成 ID。

当指定了 ID 并且文档已存在于向量存储中时,更新方法应使用新数据更新文档。如果文档不存在,则更新方法应将文档添加到向量存储中。

参数
  • items (Sequence[Document]) – 要添加到向量存储的文档序列。

  • kwargs (Any) – 附加关键字参数。

返回

响应对象,其中包含已成功添加到向量存储或在向量存储中更新的 ID 列表,以及未能添加或更新的 ID 列表。

返回类型

UpsertResponse

0.2.11版本新增功能。

delete(ids: Optional[List[str]] = None, **kwargs: Any) Optional[bool]

通过向量 ID 或其他条件删除。

参数
  • ids (Optional[List[str]]) – 要删除的id列表。如果为None,则删除所有。默认为None。

  • **kwargs (Any) – 子类可能使用的其他关键字参数。

返回

如果删除成功,则为True,否则为False;如果未实现,则为None。

返回类型

Optional[bool]

classmethod from_components(project_id: str, region: str, gcs_bucket_name: str, index_id: str, endpoint_id: str, credentials_path: Optional[str] = None, embedding: Optional[Embeddings] = None, **kwargs: Any) MatchingEngine[source]

从构造函数中取出对象创建。

参数
  • project_id (str) – GCP 项目 ID。

  • region (str) – 进行 API 调用的默认位置。它必须具有

  • regional. (与 GCS 存储桶相同的位置,并且必须是) –

  • gcs_bucket_name (str) – 向量将存储在其中的位置,以便

  • created. (创建索引) –

  • index_id (str) – 已创建索引的 ID。

  • endpoint_id (str) – 已创建端点的 ID。

  • credentials_path (Optional[str]) – (可选)本地文件系统上 Google 凭据的路径。

  • system. (本地文件) –

  • embedding (Optional[Embeddings]) – 将用于嵌入文本的 Embeddings

  • texts. (嵌入文本) –

  • kwargs (Any) – 要传递给 MatchingEngine.__init__() 的其他关键字参数。

返回

配置了 MatchingEngine,并将文本添加到索引中。

返回类型

MatchingEngine

classmethod from_documents(documents: List[Document], embedding: Embeddings, **kwargs: Any) VST

返回从文档和嵌入初始化的 VectorStore。

参数
  • documents (List[Document]) – 要添加到向量存储的文档列表。

  • embedding (Embeddings) – 要使用的嵌入函数。

  • kwargs (Any) – 附加关键字参数。

返回

从文档和嵌入初始化的VectorStore。

返回类型

VectorStore

classmethod from_texts(texts: List[str], embedding: Embeddings, metadatas: Optional[List[dict]] = None, **kwargs: Any) MatchingEngine[source]

请改用 from components。

参数
  • texts (List[str]) –

  • embedding (Embeddings) –

  • metadatas (Optional[List[dict]]) –

  • kwargs (Any) –

返回类型

MatchingEngine

get_by_ids(ids: Sequence[str], /) List[Document]

按 ID 获取文档。

返回的文档应将ID字段设置为向量存储中文档的ID。

如果某些ID未找到或存在重复ID,则返回的文档可能少于请求的文档。

用户不应假定返回文档的顺序与输入ID的顺序匹配。相反,用户应依赖返回文档的ID字段。

如果某些ID未找到文档,则此方法不应引发异常。

参数

ids (Sequence[str]) – 要检索的id列表。

返回

文档列表。

返回类型

List[Document]

0.2.11版本新增功能。

返回使用最大边际相关性选择的文档。

最大边际相关性优化查询的相似性和所选文档之间的多样性。

参数
  • query (str) – 用于查找相似文档的文本。

  • k (int) – 要返回的文档数量。默认为 4。

  • fetch_k (int) – 要获取并传递给 MMR 算法的文档数量。默认为 20。

  • lambda_mult (float) – 介于 0 和 1 之间的数字,用于确定结果之间多样性的程度,其中 0 对应于最大多样性,1 对应于最小多样性。默认为 0.5。

  • **kwargs (Any) – 传递给搜索方法的参数。

返回

通过最大边际相关性选择的文档列表。

返回类型

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]

返回使用最大边际相关性选择的文档。

最大边际相关性优化查询的相似性和所选文档之间的多样性。

参数
  • embedding (List[float]) – 用于查找相似文档的嵌入向量。

  • k (int) – 要返回的文档数量。默认为 4。

  • fetch_k (int) – 要获取并传递给 MMR 算法的文档数量。默认为 20。

  • lambda_mult (float) – 介于 0 和 1 之间的数字,用于确定结果之间多样性的程度,其中 0 对应于最大多样性,1 对应于最小多样性。默认为 0.5。

  • **kwargs (Any) – 传递给搜索方法的参数。

返回

通过最大边际相关性选择的文档列表。

返回类型

List[Document]

search(query: str, search_type: str, **kwargs: Any) List[Document]

返回与使用指定搜索类型查询最相似的文档。

参数
  • query (str) – 输入文本

  • search_type (str) – 要执行的搜索类型。可以是“similarity”、“mmr”或“similarity_score_threshold”。

  • **kwargs (Any) – 传递给搜索方法的参数。

返回

与查询最相似的文档列表。

引发

ValueError – 如果 search_type 不是“similarity”、“mmr”或“similarity_score_threshold”之一。

返回类型

List[Document]

返回与查询最相似的文档。

参数
  • query (str) – 将用于搜索相似文档的字符串。

  • k (int) – 将要检索的邻居数量。

  • filter (Optional[List[Namespace]]) –

    可选参数。用于过滤匹配结果的命名空间列表。例如:[Namespace(“color”, [“red”], []), Namespace(“shape”, [], [“squared”])] 将匹配满足“红色颜色”的数据点,但不包括具有“方形形状”的数据点。请参阅 https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#json

    了解更多详情。

  • kwargs (Any) –

返回

包含 k 个匹配文档的列表。

返回类型

List[Document]

similarity_search_by_vector(embedding: List[float], k: int = 4, filter: Optional[List]Namespace]] = None, **kwargs: Any) List[Document][source]

返回与嵌入最相似的文档。

参数
  • embedding (List[float]) – 用于查找相似文档的嵌入向量。

  • k (int) – 将要检索的邻居数量。

  • filter (Optional[List[Namespace]]) –

    可选参数。用于过滤匹配结果的命名空间列表。例如:[Namespace(“color”, [“red”], []), Namespace(“shape”, [], [“squared”])] 将匹配满足“红色颜色”的数据点,但不包括具有“方形形状”的数据点。请参阅 https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#json

    了解更多详情。

  • kwargs (Any) –

返回

包含 k 个匹配文档的列表。

返回类型

List[Document]

similarity_search_by_vector_with_score(embedding: List[float], k: int = 4, filter: Optional[List]Namespace]] = None) List[Tuple[Document, float]][source]

返回与嵌入最相似的文档及其余弦距离。

参数
  • embedding (List[float]) – 用于查找相似文档的嵌入向量。

  • k (int) – 要返回的文档数量。默认为 4。

  • filter (Optional[List[Namespace]]) – 可选参数。用于过滤匹配结果的命名空间列表。例如:[Namespace(“color”, [“red”], []), Namespace(“shape”, [], [“squared”])] 将匹配满足“红色颜色”的数据点,但不包括具有“方形形状”的数据点。请参阅 https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#json 了解更多详情。

返回

与查询文本最相似的文档列表,以及每个文档的余弦距离浮点数。分数越低表示相似度越高。

返回类型

List[Tuple[Document, float]]

similarity_search_with_relevance_scores(query: str, k: int = 4, **kwargs: Any) List[Tuple[Document, float]]

返回范围在 [0, 1] 内的文档和相关性得分。

0 表示不相似,1 表示最相似。

参数
  • query (str) – 输入文本。

  • k (int) – 要返回的文档数量。默认为 4。

  • **kwargs (Any) –

    要传递给相似度搜索的kwargs。应包括:score_threshold:可选,介于 0 到 1 之间的浮点值,用于

    过滤检索到的文档结果集。

返回

(doc, similarity_score) 元组列表。

返回类型

List[Tuple[Document, float]]

similarity_search_with_score(query: str, k: int = 4, filter: Optional[List]Namespace]] = None) List[Tuple[Document, float]][source]

返回与查询最相似的文档及其与查询的余弦距离。

参数
  • query (str) – 字符串查询,查找与之相似的文档。

  • k (int) – 要返回的文档数量。默认为 4。

  • filter (Optional[List[Namespace]]) – 可选参数。用于过滤匹配结果的命名空间列表。例如:[Namespace(“color”, [“red”], []), Namespace(“shape”, [], [“squared”])] 将匹配满足“红色颜色”的数据点,但不包括具有“方形形状”的数据点。请参阅 https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#json 了解更多详情。

返回

与查询文本最相似的文档列表,以及每个文档的余弦距离浮点数。分数越低表示相似度越高。

返回类型

List[Tuple[Document, float]]

streaming_upsert(items: Iterable[Document], /, batch_size: int, **kwargs: Any) Iterator[UpsertResponse]

Beta

0.2.11版本新增功能。API可能会发生变化。

以流式方式更新文档。

参数
  • items (Iterable[Document]) – 要添加到向量存储的可迭代文档。

  • batch_size (int) – 每次批量更新的大小。

  • kwargs (Any) – 额外的关键字参数。 kwargs 应该只包含所有文档通用的参数。(例如,索引超时、重试策略等)kwargs 不应包含 id 以避免语义不明确。相反,ID 应该作为 Document 对象的一部分提供。

Yields

UpsertResponse – 响应对象,其中包含已成功添加到向量存储或在向量存储中更新的 ID 列表,以及未能添加或更新的 ID 列表。

返回类型

Iterator[UpsertResponse]

0.2.11版本新增功能。

upsert(items: Sequence[Document], /, **kwargs: Any) UpsertResponse

Beta

0.2.11版本新增功能。API可能会发生变化。

在向量存储中添加或更新文档。

如果提供了 Document 对象的 ID 字段,则更新功能应使用该字段。如果未提供 ID,则更新方法可以自由地为文档生成 ID。

当指定了 ID 并且文档已存在于向量存储中时,更新方法应使用新数据更新文档。如果文档不存在,则更新方法应将文档添加到向量存储中。

参数
  • items (Sequence[Document]) – 要添加到向量存储的文档序列。

  • kwargs (Any) – 附加关键字参数。

返回

响应对象,其中包含已成功添加到向量存储或在向量存储中更新的 ID 列表,以及未能添加或更新的 ID 列表。

返回类型

UpsertResponse

0.2.11版本新增功能。