langchain_community.vectorstores.neo4j_vector.Neo4jVector

class langchain_community.vectorstores.neo4j_vector.Neo4jVector(embedding: Embeddings, *, search_type: SearchType = SearchType.VECTOR, username: Optional[str] = None, password: Optional[str] = None, url: Optional[str] = None, keyword_index_name: Optional[str] = 'keyword', database: Optional[str] = None, index_name: str = 'vector', node_label: str = 'Chunk', embedding_node_property: str = 'embedding', text_node_property: str = 'text', distance_strategy: DistanceStrategy = DistanceStrategy.COSINE, logger: Optional[Logger] = None, pre_delete_collection: bool = False, retrieval_query: str = '', relevance_score_fn: Optional[Callable[[float], float]] = None, index_type: IndexType = IndexType.NODE, graph: Optional[Neo4jGraph] = None)[source]

Neo4j 向量索引 (vector index)。

要使用,您应该安装 neo4j python 包。

参数 (Parameters)
  • url (Optional[str]) – Neo4j 连接 url (connection url)

  • username (Optional[str]) – Neo4j 用户名 (username)。

  • password (Optional[str]) – Neo4j 密码 (password)

  • database (Optional[str]) – 可选地提供 Neo4j 数据库。默认为 “neo4j” (Optionally provide Neo4j database Defaults to “neo4j”)

  • embedding (Embeddings) – 任何实现 langchain.embeddings.base.Embeddings 接口的嵌入函数 (Any embedding function implementing langchain.embeddings.base.Embeddings interface.)。

  • distance_strategy (DistanceStrategy) – 要使用的距离策略。(默认值: COSINE) (The distance strategy to use. (default: COSINE))

  • pre_delete_collection (bool) – 如果为 True,如果存在现有数据,则会删除。(默认值: False)。用于测试。(If True, will delete existing data if it exists. (default: False). Useful for testing.)

  • search_type (SearchType) –

  • keyword_index_name (Optional[str]) –

  • index_name (str) –

  • node_label (str) –

  • embedding_node_property (str) –

  • text_node_property (str) –

  • logger (Optional[logging.Logger]) –

  • retrieval_query (str) –

  • relevance_score_fn (Optional[Callable[[float], float]]) –

  • index_type (IndexType) –

  • graph (Optional[Neo4jGraph]) –

示例 (Example)

from langchain_community.vectorstores.neo4j_vector import Neo4jVector
from langchain_community.embeddings.openai import OpenAIEmbeddings

url="bolt://localhost:7687"
username="neo4j"
password="pleaseletmein"
embeddings = OpenAIEmbeddings()
vectorestore = Neo4jVector.from_documents(
    embedding=embeddings,
    documents=docs,
    url=url
    username=username,
    password=password,
)

属性 (Attributes)

embeddings

访问查询嵌入对象(如果可用)。(Access the query embedding object if available.)

方法 (Methods)

__init__(embedding, *[, search_type, ...])

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_embeddings(texts, embeddings[, ...])

将嵌入添加到向量存储。(Add embeddings to the vectorstore.)

add_texts(texts[, metadatas, ids])

运行更多文本通过嵌入并添加到向量存储。(Run more texts through the embeddings and add to the vectorstore.)

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

amax_marginal_relevance_search_by_vector(...)

异步返回使用最大边际相关性选择的文档。(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.)

asimilarity_search_with_relevance_scores(query)

异步返回范围在 [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)

create_new_index()

此方法构造一个 Cypher 查询并执行它以在 Neo4j 中创建新的向量索引。(This method constructs a Cypher query and executes it to create a new vector index in Neo4j.)

create_new_keyword_index([text_node_properties])

此方法构造一个 Cypher 查询并执行它以在 Neo4j 中创建新的全文索引。(This method constructs a Cypher query and executes it to create a new full text index in Neo4j.)

delete([ids])

按向量 ID 或其他条件删除。(Delete by vector ID or other criteria.)

from_documents(documents, embedding[, ...])

返回从文档和嵌入初始化的 Neo4jVector。(Return Neo4jVector initialized from documents and embeddings.)

from_embeddings(text_embeddings, embedding)

从原始文档和预生成的嵌入构造 Neo4jVector 包装器。(Construct Neo4jVector wrapper from raw documents and pre- generated embeddings.)

from_existing_graph(embedding, node_label, ...)

从现有图初始化并返回 Neo4jVector 实例。(Initialize and return a Neo4jVector instance from an existing graph.)

from_existing_index(embedding, index_name[, ...])

获取现有 Neo4j 向量索引的实例。(Get instance of an existing Neo4j vector index.)

from_existing_relationship_index(embedding, ...)

获取现有 Neo4j 关系向量索引的实例。(Get instance of an existing Neo4j relationship vector index.)

from_texts(texts, embedding[, metadatas, ...])

返回从文本和嵌入初始化的 Neo4jVector。(Return Neo4jVector initialized from texts and embeddings.)

get_by_ids(ids, /)

按 ID 获取文档。(Get documents by their IDs.)

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

query(query, *[, params])

此方法向连接的 Neo4j 数据库发送 Cypher 查询,并将结果作为字典列表返回。(This method sends a Cypher query to the connected Neo4j database and returns the results as a list of dictionaries.)

retrieve_existing_fts_index([...])

检查 Neo4j 数据库中是否存在全文索引 (Check if the fulltext index exists in the Neo4j database)

retrieve_existing_index()

检查 Neo4j 数据库中是否存在向量索引,并返回其嵌入维度。(Check if the vector index exists in the Neo4j database and returns its embedding dimension.)

search(query, search_type, **kwargs)

返回使用指定搜索类型与查询最相似的文档。(Return docs most similar to query using a specified search type.)

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

使用 Neo4jVector 运行相似性搜索。(Run similarity search with Neo4jVector.)

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

返回与嵌入向量最相似的文档。(Return docs most similar to embedding vector.)

similarity_search_with_relevance_scores(query)

返回范围在 [0, 1] 内的文档和相关性得分。(Return docs and relevance scores in the range [0, 1].)

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

返回与查询最相似的文档。(Return docs most similar to query.)

similarity_search_with_score_by_vector(embedding)

使用给定的向量在 Neo4j 数据库中执行相似性搜索,并返回前 k 个相似文档及其得分。(Perform a similarity search in the Neo4j database using a given vector and return the top k similar documents with their scores.)

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

upsert(items, /, **kwargs)

verify_version()

检查连接的 Neo4j 数据库版本是否支持向量索引。(Check if the connected Neo4j database version supports vector indexing.)

__init__(embedding: Embeddings, *, search_type: SearchType = SearchType.VECTOR, username: Optional[str] = None, password: Optional[str] = None, url: Optional[str] = None, keyword_index_name: Optional[str] = 'keyword', database: Optional[str] = None, index_name: str = 'vector', node_label: str = 'Chunk', embedding_node_property: str = 'embedding', text_node_property: str = 'text', distance_strategy: DistanceStrategy = DistanceStrategy.COSINE, logger: Optional[Logger] = None, pre_delete_collection: bool = False, retrieval_query: str = '', relevance_score_fn: Optional[Callable[[float], float]] = None, index_type: IndexType = IndexType.NODE, graph: Optional[Neo4jGraph] = None) None[source]
参数 (Parameters)
  • embedding (Embeddings) –

  • search_type (SearchType) –

  • username (Optional[str]) –

  • password (Optional[str]) –

  • url (Optional[str]) –

  • keyword_index_name (Optional[str]) –

  • database (Optional[str]) –

  • index_name (str) –

  • node_label (str) –

  • embedding_node_property (str) –

  • text_node_property (str) –

  • distance_strategy (DistanceStrategy) –

  • logger (Optional[Logger]) –

  • pre_delete_collection (bool) –

  • retrieval_query (str) –

  • relevance_score_fn (Optional[Callable[[float], float]]) –

  • index_type (IndexType) –

  • graph (Optional[Neo4jGraph]) –

返回类型 (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]) – 要添加到向量存储的文档列表。

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

返回值

添加文本的 ID 列表。

Raises

ValueError – 如果 ID 的数量与文档的数量不匹配,则引发此错误。

返回类型 (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]) – 要添加到向量存储的字符串可迭代对象。

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

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

返回值

将文本添加到向量存储后生成的 ID 列表。

Raises
  • ValueError – 如果元数据的数量与文本的数量不匹配,则引发此错误。

  • ValueError – 如果 ID 的数量与文本的数量不匹配,则引发此错误。

返回类型 (Return type)

List[str]

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

在向量存储中添加或更新文档。(Add or update documents in the vectorstore.)

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

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

返回值

添加文本的 ID 列表。

Raises

ValueError – 如果 ID 的数量与文档的数量不匹配,则引发此错误。

返回类型 (Return type)

List[str]

add_embeddings(texts: Iterable[str], embeddings: List[List[float]], metadatas: Optional[List[dict]] = None, ids: Optional[List[str]] = None, **kwargs: Any) List[str][source]

将嵌入添加到向量存储。(Add embeddings to the vectorstore.)

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

  • embeddings (List[List[float]]) – 嵌入向量的列表的列表。

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

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

  • ids (Optional[List[str]]) –

返回类型 (Return type)

List[str]

add_texts(texts: Iterable[str], metadatas: Optional[List[dict]] = 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]) – 要添加到向量存储的字符串可迭代对象。

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

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

  • ids (Optional[List[str]]) –

返回值

将文本添加到向量存储后生成的 ID 列表。

返回类型 (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。

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

返回值

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

返回类型 (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) – 附加的关键字参数。

返回值

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

返回类型 (Return type)

VectorStore

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。

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

返回值

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

返回类型 (Return type)

VectorStore

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 列表。

返回值

文档列表。

返回类型 (Return type)

List[Document]

0.2.11 版本新增。

异步返回使用最大边际相关性选择的文档。(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) –

返回值

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

返回类型 (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) – 传递给搜索方法的参数。

返回值

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

返回类型 (Return type)

List[Document]

as_retriever(**kwargs: Any) VectorStoreRetriever

返回从该 VectorStore 初始化的 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: 按文档元数据筛选

返回值

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) – 传递给搜索方法的参数。

返回值

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

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) – 传递给搜索方法的参数。

返回值

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

返回类型 (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) – 传递给搜索方法的参数。

返回值

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

返回类型 (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 之间的浮点值,用于

    筛选检索文档的结果集

返回值

(doc, similarity_score) 元组的列表

返回类型 (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) – 传递给搜索方法的参数。

返回值

(doc, similarity_score) 元组的列表。

返回类型 (Return type)

List[Tuple[Document, float]]

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

Beta

在 0.2.11 版本中添加。API 可能会发生变化。

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

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

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

  • 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 字段,则更新功能应使用该字段。如果未提供 ID,则更新方法可以自由地为文档生成 ID。

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

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

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

返回值

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

返回类型 (Return type)

UpsertResponse

0.2.11 版本新增。

create_new_index() None[source]

此方法构造一个 Cypher 查询并执行它以在 Neo4j 中创建新的向量索引。(This method constructs a Cypher query and executes it to create a new vector index in Neo4j.)

返回类型 (Return type)

None

create_new_keyword_index(text_node_properties: List[str] = []) None[source]

此方法构造一个 Cypher 查询并执行它以在 Neo4j 中创建新的全文索引。(This method constructs a Cypher query and executes it to create a new full text index in Neo4j.)

参数 (Parameters)

text_node_properties (List[str]) –

返回类型 (Return type)

None

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

按向量 ID 或其他条件删除。(Delete by vector ID or other criteria.)

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

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

返回值

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

返回类型 (Return type)

Optional[bool]

classmethod from_documents(documents: List[Document], embedding: Embeddings, distance_strategy: DistanceStrategy = DistanceStrategy.COSINE, ids: Optional[List[str]] = None, **kwargs: Any) Neo4jVector[source]

返回从文档和嵌入初始化的 Neo4jVector。Neo4j 凭据是必需的,形式为 urlusernamepassword,以及可选的 database 参数。

参数 (Parameters)
返回类型 (Return type)

Neo4jVector

classmethod from_embeddings(text_embeddings: List[Tuple[str, List[float]]], embedding: Embeddings, metadatas: Optional[List[dict]] = None, distance_strategy: DistanceStrategy = DistanceStrategy.COSINE, ids: Optional[List[str]] = None, pre_delete_collection: bool = False, **kwargs: Any) Neo4jVector[source]

从原始文档和预生成的嵌入构造 Neo4jVector 包装器。(Construct Neo4jVector wrapper from raw documents and pre- generated embeddings.)

返回从文档和嵌入初始化的 Neo4jVector。Neo4j 凭据是必需的,形式为 urlusernamepassword,以及可选的 database 参数。

示例 (Example)

from langchain_community.vectorstores.neo4j_vector import Neo4jVector
from langchain_community.embeddings import OpenAIEmbeddings
embeddings = OpenAIEmbeddings()
text_embeddings = embeddings.embed_documents(texts)
text_embedding_pairs = list(zip(texts, text_embeddings))
vectorstore = Neo4jVector.from_embeddings(
    text_embedding_pairs, embeddings)
参数 (Parameters)
  • text_embeddings (List[Tuple[str, List[float]]]) –

  • embedding (Embeddings) –

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

  • distance_strategy (DistanceStrategy) –

  • ids (Optional[List[str]]) –

  • pre_delete_collection (bool) –

  • kwargs (Any) –

返回类型 (Return type)

Neo4jVector

classmethod from_existing_graph(embedding: Embeddings, node_label: str, embedding_node_property: str, text_node_properties: List[str], *, keyword_index_name: Optional[str] = 'keyword', index_name: str = 'vector', search_type: SearchType = SearchType.VECTOR, retrieval_query: str = '', **kwargs: Any) Neo4jVector[source]

从现有图初始化并返回 Neo4jVector 实例。(Initialize and return a Neo4jVector instance from an existing graph.)

此方法使用提供的参数和现有图初始化 Neo4jVector 实例。它验证索引的存在性,并在索引不存在时创建新索引。

返回值:Neo4jVector:使用提供的参数和现有图初始化的 Neo4jVector 实例

和现有图。

示例: >>> neo4j_vector = Neo4jVector.from_existing_graph( … embedding=my_embedding, … node_label=”Document”, … embedding_node_property=”embedding”, … text_node_properties=[“title”, “content”] … )

注意:Neo4j 凭据是必需的,形式为 urlusernamepassword,以及作为附加关键字参数传递的可选 database 参数。

参数 (Parameters)
  • embedding (Embeddings) –

  • node_label (str) –

  • embedding_node_property (str) –

  • text_node_properties (List[str]) –

  • keyword_index_name (Optional[str]) –

  • index_name (str) –

  • search_type (SearchType) –

  • retrieval_query (str) –

  • kwargs (Any) –

返回类型 (Return type)

Neo4jVector

classmethod from_existing_index(embedding: Embeddings, index_name: str, search_type: SearchType = SearchType.VECTOR, keyword_index_name: Optional[str] = None, **kwargs: Any) Neo4jVector[source]

获取现有 Neo4j 向量索引的实例。此方法将返回存储的实例,而无需插入任何新的嵌入。Neo4j 凭据是必需的,形式为 urlusernamepassword,以及可选的 database 参数以及 index_name 定义。

参数 (Parameters)
  • embedding (Embeddings) –

  • index_name (str) –

  • search_type (SearchType) –

  • keyword_index_name (Optional[str]) –

  • kwargs (Any) –

返回类型 (Return type)

Neo4jVector

classmethod from_existing_relationship_index(embedding: Embeddings, index_name: str, search_type: SearchType = SearchType.VECTOR, **kwargs: Any) Neo4jVector[source]

获取现有 Neo4j 关系向量索引的实例。此方法将返回存储的实例,而无需插入任何新的嵌入。Neo4j 凭据是必需的,形式为 urlusernamepassword,以及可选的 database 参数以及 index_name 定义。

参数 (Parameters)
返回类型 (Return type)

Neo4jVector

classmethod from_texts(texts: List[str], embedding: Embeddings, metadatas: Optional[List[dict]] = None, distance_strategy: DistanceStrategy = DistanceStrategy.COSINE, ids: Optional[List[str]] = None, **kwargs: Any) Neo4jVector[source]

返回从文本和嵌入初始化的 Neo4jVector。Neo4j 凭据是必需的,形式为 urlusernamepassword,以及可选的 database 参数。

参数 (Parameters)
  • texts (List[str]) –

  • embedding (Embeddings) –

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

  • distance_strategy (DistanceStrategy) –

  • ids (Optional[List[str]]) –

  • kwargs (Any) –

返回类型 (Return type)

Neo4jVector

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 列表。

返回值

文档列表。

返回类型 (Return type)

List[Document]

0.2.11 版本新增。

返回使用最大边际相关性选择的文档。(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) – 传递给搜索方法的参数。

返回值

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

返回类型 (Return type)

List[Document]

max_marginal_relevance_search_by_vector(embedding: List[float], k: int = 4, fetch_k: int = 20, lambda_mult: float = 0.5, **kwargs: Any) List[Document]

返回使用最大边际相关性选择的文档。(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) – 传递给搜索方法的参数。

返回值

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

返回类型 (Return type)

List[Document]

query(query: str, *, params: Optional[dict] = None) List[Dict[str, Any]][source]

此方法向连接的 Neo4j 数据库发送 Cypher 查询,并将结果作为字典列表返回。(This method sends a Cypher query to the connected Neo4j database and returns the results as a list of dictionaries.)

参数 (Parameters)
  • query (str) – 要执行的 Cypher 查询。

  • params (dict, optional) – 查询参数字典。默认为 {}。

返回值

包含查询结果的字典列表。

返回类型 (Return type)

List[Dict[str, Any]]

retrieve_existing_fts_index(text_node_properties: List[str] = []) Optional[str][source]

检查 Neo4j 数据库中是否存在全文索引 (Check if the fulltext index exists in the Neo4j database)

此方法在 Neo4j 数据库中查询具有指定名称的现有 fts 索引。

返回值

关键字索引信息

返回类型 (Return type)

(Tuple)

参数 (Parameters)

text_node_properties (List[str]) –

retrieve_existing_index() Tuple[Optional[int], Optional[str]][source]

检查 Neo4j 数据库中是否存在向量索引,并返回其嵌入维度。(Check if the vector index exists in the Neo4j database and returns its embedding dimension.)

此方法在 Neo4j 数据库中查询现有索引,并尝试检索具有指定名称的向量索引的维度。如果索引存在,则返回其维度。如果索引不存在,则返回 None

返回值

如果找到,则返回现有索引的嵌入维度。

返回类型 (Return type)

int 或 None

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) – 传递给搜索方法的参数。

返回值

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

Raises

ValueError – 如果 search_type 不是 “similarity”、“mmr” 或 “similarity_score_threshold” 之一,则引发此错误。

返回类型 (Return type)

List[Document]

使用 Neo4jVector 运行相似性搜索。(Run similarity search with Neo4jVector.)

参数 (Parameters)
  • query (str) – 要搜索的查询文本。

  • k (int) – 要返回的结果数。默认为 4。

  • params (Dict[str, Any]) – 索引类型的搜索参数。默认为空字典。

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

    要应用于元数据的

    参数字典。

    默认为 None。

  • kwargs (Any) –

返回值

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

返回类型 (Return type)

List[Document]

similarity_search_by_vector(embedding: List[float], k: int = 4, filter: Optional[Dict[str, Any]] = None, params: Dict[str, Any] = {}, **kwargs: Any) List[Document][source]

返回与嵌入向量最相似的文档。(Return docs most similar to embedding vector.)

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

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

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

    要应用于元数据的

    参数字典。

    默认为 None。

  • params (Dict[str, Any]) – 索引类型的搜索参数。默认为空字典。

  • kwargs (Any) –

返回值

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

返回类型 (Return type)

List[Document]

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

返回范围在 [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 之间的浮点值,用于

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

返回值

(doc, similarity_score) 元组的列表。

返回类型 (Return type)

List[Tuple[Document, float]]

similarity_search_with_score(query: str, k: int = 4, params: Dict[str, Any] = {}, filter: Optional[Dict[str, Any]] = None, **kwargs: Any) List[Tuple[Document, float]][source]

返回与查询最相似的文档。(Return docs most similar to query.)

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

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

  • params (Dict[str, Any]) – 索引类型的搜索参数。默认为空字典。

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

    要应用于元数据的

    参数字典。

    默认为 None。

  • kwargs (Any) –

返回值

查询最相似的文档列表以及每个文档的分数

返回类型 (Return type)

List[Tuple[Document, float]]

similarity_search_with_score_by_vector(embedding: List[float], k: int = 4, filter: Optional[Dict[str, Any]] = None, params: Dict[str, Any] = {}, **kwargs: Any) List[Tuple[Document, float]][source]

使用给定的向量在 Neo4j 数据库中执行相似性搜索,并返回前 k 个相似文档及其得分。(Perform a similarity search in the Neo4j database using a given vector and return the top k similar documents with their scores.)

此方法使用 Cypher 查询来查找与给定嵌入向量最相似的前 k 个文档。相似度是通过 Neo4j 数据库中的向量索引来衡量的。结果以元组列表的形式返回,每个元组包含一个 Document 对象及其相似度分数。

参数 (Parameters)
  • embedding (List[float]) – 要比较的嵌入向量。

  • k (int, optional) – 要检索的最相似文档的数量。

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

    要应用于元数据的

    参数字典。

    默认为 None。

  • params (Dict[str, Any]) – 索引类型的搜索参数。默认为空字典。

  • kwargs (Any) –

返回值

元组列表,每个元组包含

一个 Document 对象及其相似度分数。

返回类型 (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]) – 要添加到向量存储的 Document 的可迭代对象。

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

  • 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 字段,则更新功能应使用该字段。如果未提供 ID,则更新方法可以自由地为文档生成 ID。

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

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

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

返回值

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

返回类型 (Return type)

UpsertResponse

0.2.11 版本新增。

verify_version() None[source]

检查连接的 Neo4j 数据库版本是否支持向量索引。(Check if the connected Neo4j database version supports vector indexing.)

查询 Neo4j 数据库以检索其版本,并将其与已知支持向量索引的目标版本 (5.11.0) 进行比较。如果连接的 Neo4j 版本不受支持,则会引发 ValueError。

返回类型 (Return type)

None

Neo4jVector 的使用示例