langchain_community.vectorstores.bigquery_vector_search.BigQueryVectorSearch

class langchain_community.vectorstores.bigquery_vector_search.BigQueryVectorSearch(embedding: Embeddings, project_id: str, dataset_name: str, table_name: str, location: str = 'US', content_field: str = 'content', metadata_field: str = 'metadata', text_embedding_field: str = 'text_embedding', doc_id_field: str = 'doc_id', distance_strategy: DistanceStrategy = DistanceStrategy.EUCLIDEAN_DISTANCE, credentials: Optional[Any] = None)[source]

Deprecated since version 0.0.33: Use langchain_google_community.BigQueryVectorSearch instead.

Google Cloud BigQuery vector store.

To use, you need the following packages installed

google-cloud-bigquery

Constructor for BigQueryVectorSearch.

Parameters
  • embedding (Embeddings) – Text Embedding model to use.

  • project_id (str) – GCP project.

  • dataset_name (str) – BigQuery dataset to store documents and embeddings.

  • table_name (str) – BigQuery table name.

  • location (str, optional) – BigQuery region. Defaults to `US`(multi-region).

  • content_field (str) – Specifies the column to store the content. Defaults to content.

  • metadata_field (str) – Specifies the column to store the metadata. Defaults to metadata.

  • text_embedding_field (str) – Specifies the column to store the embeddings vector. Defaults to text_embedding.

  • doc_id_field (str) – Specifies the column to store the document id. Defaults to doc_id.

  • distance_strategy (DistanceStrategy, optional) –

    Determines the strategy employed for calculating the distance between vectors in the embedding space. Defaults to EUCLIDEAN_DISTANCE. Available options are: - COSINE: Measures the similarity between two vectors of an inner

    product space.

    • EUCLIDEAN_DISTANCE: Computes the Euclidean distance between

      two vectors. This metric considers the geometric distance in the vector space, and might be more suitable for embeddings that rely on spatial relationships. This is the default behavior

  • credentials (Credentials, optional) – Custom Google Cloud credentials to use. Defaults to None.

Attributes

embeddings

Access the query embedding object if available.

full_table_id

Methods

__init__(embedding, project_id, ...[, ...])

Constructor for BigQueryVectorSearch.

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

Run more texts through the embeddings and add to the vectorstore.

add_texts_with_embeddings(texts, embs[, ...])

Run more texts through the embeddings and add to the vectorstore.

adelete([ids])

Delete by vector ID or other criteria.

afrom_documents(documents, embedding, **kwargs)

Async return VectorStore initialized from documents and embeddings.

afrom_texts(texts, embedding[, metadatas])

Async return VectorStore initialized from texts and embeddings.

aget_by_ids(ids, /)

Async get documents by their IDs.

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

Return docs selected using the maximal marginal relevance.

amax_marginal_relevance_search_by_vector(...)

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

Async return docs most similar to query.

asimilarity_search_by_vector(embedding[, k])

Async return docs most similar to embedding vector.

asimilarity_search_with_relevance_scores(query)

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

Delete by vector ID or other criteria.

explore_job_stats(job_id)

Return the statistics for a single job execution.

from_documents(documents, embedding, **kwargs)

Return VectorStore initialized from documents and embeddings.

from_texts(texts, embedding[, metadatas])

Return VectorStore initialized from texts and embeddings.

get_by_ids(ids, /)

Get documents by their IDs.

get_documents([ids, filter])

Search documents by their ids or metadata values.

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 a specified search type.

similarity_search(query[, k, filter, ...])

Run similarity search.

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

Return docs most similar to embedding vector.

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 score.

similarity_search_with_score_by_vector(embedding)

Return docs most similar to embedding vector.

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

upsert(items, /, **kwargs)

__init__(embedding: Embeddings, project_id: str, dataset_name: str, table_name: str, location: str = 'US', content_field: str = 'content', metadata_field: str = 'metadata', text_embedding_field: str = 'text_embedding', doc_id_field: str = 'doc_id', distance_strategy: DistanceStrategy = DistanceStrategy.EUCLIDEAN_DISTANCE, credentials: Optional[Any] = None)[source]

Constructor for BigQueryVectorSearch.

Parameters
  • embedding (Embeddings) – Text Embedding model to use.

  • project_id (str) – GCP project.

  • dataset_name (str) – BigQuery dataset to store documents and embeddings.

  • table_name (str) – BigQuery table name.

  • location (str, optional) – BigQuery region. Defaults to `US`(multi-region).

  • content_field (str) – Specifies the column to store the content. Defaults to content.

  • metadata_field (str) – Specifies the column to store the metadata. Defaults to metadata.

  • text_embedding_field (str) – Specifies the column to store the embeddings vector. Defaults to text_embedding.

  • doc_id_field (str) – Specifies the column to store the document id. Defaults to doc_id.

  • distance_strategy (DistanceStrategy, optional) –

    Determines the strategy employed for calculating the distance between vectors in the embedding space. Defaults to EUCLIDEAN_DISTANCE. Available options are: - COSINE: Measures the similarity between two vectors of an inner

    product space.

    • EUCLIDEAN_DISTANCE: Computes the Euclidean distance between

      two vectors. This metric considers the geometric distance in the vector space, and might be more suitable for embeddings that rely on spatial relationships. This is the default behavior

  • credentials (Credentials, optional) – Custom Google Cloud credentials to use. Defaults to 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

List of IDs of the added texts.

Raises

ValueError – 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]]) – Optional list of metadatas associated with the texts. Default is None.

  • **kwargs (Any) – vectorstore specific parameters.

Returns

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 – 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) – Additional keyword arguments. if kwargs contains ids and documents contain ids, the ids in the kwargs will receive precedence.

Returns

List of IDs of the added texts.

Raises

ValueError – If the number of ids does not match the number of documents.

Return type

List[str]

add_texts(texts: List[str], metadatas: Optional[List[dict]] = None, **kwargs: Any) List[str][source]

Run more texts through the embeddings and add to the vectorstore.

Parameters
  • texts (List[str]) – 要添加到向量存储的字符串列表。

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

  • kwargs (Any) –

Returns

List of ids from adding the texts into the vectorstore.

Return type

List[str]

add_texts_with_embeddings(texts: List[str], embs: List[List[float]], metadatas: Optional[List[dict]] = None, **kwargs: Any) List[str][source]

Run more texts through the embeddings and add to the vectorstore.

Parameters
  • texts (List[str]) – 要添加到向量存储的字符串列表。

  • embs (List[List[float]]) – 包含文本嵌入向量的浮点数列表的列表,用于文本。

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

  • kwargs (Any) –

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][source]

Delete by vector ID or other criteria.

Parameters
  • ids (Optional[List[str]]) – 要删除的 ID 列表。

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

Returns

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

Return type

Optional[bool]

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) – Additional keyword arguments.

Returns

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

Return type

VectorStore

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

Async return VectorStore initialized from texts and embeddings.

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

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

  • metadatas (Optional[List[dict]]) – Optional list of metadatas associated with the texts. Default is None.

  • kwargs (Any) – Additional keyword arguments.

Returns

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

Return type

VectorStore

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 版本新增。

Return docs selected using the maximal marginal relevance.

Parameters
  • query (str) –

  • k (int) –

  • fetch_k (int) –

  • lambda_mult (float) –

  • filter (Optional[Dict[str, Any]]) –

  • brute_force (bool) –

  • fraction_lists_to_search (Optional[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, filter: Optional[Dict[str, Any]] = None, brute_force: bool = False, fraction_lists_to_search: Optional[float] = None, **kwargs: Any) List[Document][source]

Return docs selected using the maximal marginal relevance.

Parameters
  • embedding (List[float]) –

  • k (int) –

  • fetch_k (int) –

  • lambda_mult (float) –

  • filter (Optional[Dict[str, Any]]) –

  • brute_force (bool) –

  • fraction_lists_to_search (Optional[float]) –

  • kwargs (Any) –

Return type

List[Document]

as_retriever(**kwargs: Any) VectorStoreRetriever

Return VectorStoreRetriever initialized from this VectorStore.

Parameters

**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:按文档元数据筛选

Returns

VectorStore 的检索器类。

Return type

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]

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

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

(文档,相似度分数)元组列表

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

UpsertResponse

0.2.11 版本新增。

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

Delete by vector ID or other criteria.

Parameters
  • ids (Optional[List[str]]) – 要删除的 ID 列表。

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

Returns

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

Return type

Optional[bool]

explore_job_stats(job_id: str) Dict[source]

Return the statistics for a single job execution.

Parameters

job_id (str) – BigQuery 作业 ID。

Returns

给定作业的作业统计字典。

Return type

Dict

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) – Additional keyword arguments.

Returns

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

Return type

VectorStore

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

Return VectorStore initialized from texts and embeddings.

Parameters
  • texts (List[str]) –

  • embedding (Embeddings) –

  • 元数据 (可选[List[dict]]) –

  • kwargs (Any) –

Return type

BigQueryVectorSearch

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 版本新增。

get_documents(ids: Optional[List[str]] = None, filter: Optional[Dict[str, Any]] = None) List[Document][source]

Search documents by their ids or metadata values.

Parameters
  • ids (可选[List[str]]) – 从向量数据库检索的文档 ID 列表。

  • filter (Optional[Dict[str, Any]]) –

    元数据属性过滤器,例如:{

    ”str_property”: “foo”, “int_property”: 123

    }

Returns

List of ids from adding the texts into the vectorstore.

Return type

List[Document]

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[Dict[str, Any]]) –

    元数据属性过滤器,例如:{

    ”str_property”: “foo”, “int_property”: 123

    }

  • brute_force (bool) – 是否使用暴力搜索。默认为 False。

  • fraction_lists_to_search (可选[float]) – 可选的搜索列表百分比,必须在 0.0 到 1.0 范围内(不包括 1.0)。如果为 Node,则使用服务的默认值 0.05。

  • kwargs (Any) –

Returns

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

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, filter: Optional[Dict[str, Any]] = None, brute_force: bool = False, fraction_lists_to_search: Optional[float] = None, **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。

  • filter (Optional[Dict[str, Any]]) –

    元数据属性过滤器,例如:{

    ”str_property”: “foo”, “int_property”: 123

    }

  • brute_force (bool) – 是否使用暴力搜索。默认为 False。

  • fraction_lists_to_search (可选[float]) – 可选的搜索列表百分比,必须在 0.0 到 1.0 范围内(不包括 1.0)。如果为 Node,则使用服务的默认值 0.05。

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

Run similarity search.

Parameters
  • query (str) – 搜索查询文本。

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

  • filter (Optional[Dict[str, Any]]) –

    元数据属性过滤器,例如:{

    ”str_property”: “foo”, “int_property”: 123

    }

  • brute_force (bool) – 是否使用暴力搜索。默认为 False。

  • fraction_lists_to_search (可选[float]) – 可选的搜索列表百分比,必须在 0.0 到 1.0 范围内(不包括 1.0)。如果为 Node,则使用服务的默认值 0.05。

  • kwargs (Any) –

Returns

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

Return type

List[Document]

similarity_search_by_vector(embedding: List[float], k: int = 4, filter: Optional[Dict[str, Any]] = None, brute_force: bool = False, fraction_lists_to_search: Optional[float] = None, **kwargs: Any) List[Document][source]

Return docs most similar to embedding vector.

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

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

  • filter (Optional[Dict[str, Any]]) –

    元数据属性过滤器,例如:{

    ”str_property”: “foo”, “int_property”: 123

    }

  • brute_force (bool) – 是否使用暴力搜索。默认为 False。

  • fraction_lists_to_search (可选[float]) – 可选的搜索列表百分比,必须在 0.0 到 1.0 范围内(不包括 1.0)。如果为 Node,则使用服务的默认值 0.05。

  • kwargs (Any) –

Returns

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

Return type

List[Document]

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

(文档,相似度分数)元组列表。

Return type

List[Tuple[Document, float]]

similarity_search_with_score(query: str, k: int = 4, filter: Optional[Dict[str, Any]] = None, brute_force: bool = False, fraction_lists_to_search: Optional[float] = None, **kwargs: Any) List[Tuple[Document, float]][source]

Run similarity search with score.

Parameters
  • query (str) – 搜索查询文本。

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

  • filter (Optional[Dict[str, Any]]) –

    元数据属性过滤器,例如:{

    ”str_property”: “foo”, “int_property”: 123

    }

  • brute_force (bool) – 是否使用暴力搜索。默认为 False。

  • fraction_lists_to_search (可选[float]) – 可选的搜索列表百分比,必须在 0.0 到 1.0 范围内(不包括 1.0)。如果为 Node,则使用服务的默认值 0.05。

  • kwargs (Any) –

Returns

最接近查询向量的文档列表,带有相似度分数。

Return type

List[Tuple[Document, float]]

similarity_search_with_score_by_vector(embedding: List[float], k: int = 4, filter: Optional[Dict[str, Any]] = None, brute_force: bool = False, fraction_lists_to_search: Optional[float] = None, **kwargs: Any) List[Tuple[Document, float]][source]

Return docs most similar to embedding vector.

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

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

  • filter (Optional[Dict[str, Any]]) –

    元数据属性过滤器,例如:{

    ”str_property”: “foo”, “int_property”: 123

    }

  • brute_force (bool) – 是否使用暴力搜索。默认为 False。

  • fraction_lists_to_search (可选[float]) – 可选的搜索列表百分比,必须在 0.0 到 1.0 范围内(不包括 1.0)。如果为 Node,则使用服务的默认值 0.05。

  • 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 可能会发生变化。

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

UpsertResponse

0.2.11 版本新增。

BigQueryVectorSearch 的使用示例