跳到主要内容

FalkorDBVectorStore

FalkorDB 是一个开源图数据库,集成了向量相似度搜索的支持

它支持

  • 近似最近邻搜索
  • 欧几里得相似度和余弦相似度
  • 结合向量和关键字搜索的混合搜索

此笔记本展示了如何使用 FalkorDB 向量索引 (FalkorDB)

请参阅安装说明

设置

# Pip install necessary package
%pip install --upgrade falkordb
%pip install --upgrade tiktoken
%pip install --upgrade langchain langchain_huggingface
Requirement already satisfied: falkordb in c:\users\dell\desktop\langchain\.venv\lib\site-packages (1.0.10)Note: you may need to restart the kernel to use updated packages.

Requirement already satisfied: redis<6.0.0,>=5.0.1 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from falkordb) (5.2.0)
Requirement already satisfied: async-timeout>=4.0.3 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from redis<6.0.0,>=5.0.1->falkordb) (4.0.3)
Requirement already satisfied: tiktoken in c:\users\dell\desktop\langchain\.venv\lib\site-packages (0.8.0)
Requirement already satisfied: regex>=2022.1.18 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from tiktoken) (2024.11.6)
Requirement already satisfied: requests>=2.26.0 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from tiktoken) (2.32.3)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from requests>=2.26.0->tiktoken) (3.4.0)
Requirement already satisfied: idna<4,>=2.5 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from requests>=2.26.0->tiktoken) (3.10)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from requests>=2.26.0->tiktoken) (1.26.20)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from requests>=2.26.0->tiktoken) (2024.8.30)
Note: you may need to restart the kernel to use updated packages.
Requirement already satisfied: langchain in c:\users\dell\desktop\langchain\.venv\lib\site-packages (0.3.9)Note: you may need to restart the kernel to use updated packages.

Requirement already satisfied: langchain_huggingface in c:\users\dell\desktop\langchain\.venv\lib\site-packages (0.1.2)
Requirement already satisfied: PyYAML>=5.3 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from langchain) (6.0.2)
Requirement already satisfied: SQLAlchemy<3,>=1.4 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from langchain) (2.0.36)
Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from langchain) (3.11.8)
Requirement already satisfied: async-timeout<5.0.0,>=4.0.0 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from langchain) (4.0.3)
Requirement already satisfied: langchain-core<0.4.0,>=0.3.21 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from langchain) (0.3.21)
Requirement already satisfied: langchain-text-splitters<0.4.0,>=0.3.0 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from langchain) (0.3.2)
Requirement already satisfied: langsmith<0.2.0,>=0.1.17 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from langchain) (0.1.147)
Requirement already satisfied: numpy<2,>=1.22.4 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from langchain) (1.26.4)
Requirement already satisfied: pydantic<3.0.0,>=2.7.4 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from langchain) (2.9.2)
Requirement already satisfied: requests<3,>=2 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from langchain) (2.32.3)
Requirement already satisfied: tenacity!=8.4.0,<10,>=8.1.0 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from langchain) (9.0.0)
Requirement already satisfied: huggingface-hub>=0.23.0 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from langchain_huggingface) (0.26.3)
Requirement already satisfied: sentence-transformers>=2.6.0 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from langchain_huggingface) (3.3.1)
Requirement already satisfied: tokenizers>=0.19.1 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from langchain_huggingface) (0.20.3)
Requirement already satisfied: transformers>=4.39.0 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from langchain_huggingface) (4.46.3)
Requirement already satisfied: aiohappyeyeballs>=2.3.0 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (2.4.4)
Requirement already satisfied: aiosignal>=1.1.2 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.1)
Requirement already satisfied: attrs>=17.3.0 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (24.2.0)
Requirement already satisfied: frozenlist>=1.1.1 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.5.0)
Requirement already satisfied: multidict<7.0,>=4.5 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.1.0)
Requirement already satisfied: propcache>=0.2.0 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (0.2.0)
Requirement already satisfied: yarl<2.0,>=1.17.0 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.18.0)
Requirement already satisfied: filelock in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from huggingface-hub>=0.23.0->langchain_huggingface) (3.16.1)
Requirement already satisfied: fsspec>=2023.5.0 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from huggingface-hub>=0.23.0->langchain_huggingface) (2024.10.0)
Requirement already satisfied: packaging>=20.9 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from huggingface-hub>=0.23.0->langchain_huggingface) (24.2)
Requirement already satisfied: tqdm>=4.42.1 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from huggingface-hub>=0.23.0->langchain_huggingface) (4.67.1)
Requirement already satisfied: typing-extensions>=3.7.4.3 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from huggingface-hub>=0.23.0->langchain_huggingface) (4.12.2)
Requirement already satisfied: jsonpatch<2.0,>=1.33 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from langchain-core<0.4.0,>=0.3.21->langchain) (1.33)
Requirement already satisfied: httpx<1,>=0.23.0 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from langsmith<0.2.0,>=0.1.17->langchain) (0.27.2)
Requirement already satisfied: orjson<4.0.0,>=3.9.14 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from langsmith<0.2.0,>=0.1.17->langchain) (3.10.12)
Requirement already satisfied: requests-toolbelt<2.0.0,>=1.0.0 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from langsmith<0.2.0,>=0.1.17->langchain) (1.0.0)
Requirement already satisfied: annotated-types>=0.6.0 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from pydantic<3.0.0,>=2.7.4->langchain) (0.7.0)
Requirement already satisfied: pydantic-core==2.23.4 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from pydantic<3.0.0,>=2.7.4->langchain) (2.23.4)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from requests<3,>=2->langchain) (3.4.0)
Requirement already satisfied: idna<4,>=2.5 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from requests<3,>=2->langchain) (3.10)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from requests<3,>=2->langchain) (1.26.20)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from requests<3,>=2->langchain) (2024.8.30)
Requirement already satisfied: torch>=1.11.0 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from sentence-transformers>=2.6.0->langchain_huggingface) (2.5.1)
Requirement already satisfied: scikit-learn in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from sentence-transformers>=2.6.0->langchain_huggingface) (1.5.2)
Requirement already satisfied: scipy in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from sentence-transformers>=2.6.0->langchain_huggingface) (1.13.1)
Requirement already satisfied: Pillow in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from sentence-transformers>=2.6.0->langchain_huggingface) (11.0.0)
Requirement already satisfied: greenlet!=0.4.17 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from SQLAlchemy<3,>=1.4->langchain) (3.1.1)
Requirement already satisfied: regex!=2019.12.17 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from transformers>=4.39.0->langchain_huggingface) (2024.11.6)
Requirement already satisfied: safetensors>=0.4.1 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from transformers>=4.39.0->langchain_huggingface) (0.4.5)
Requirement already satisfied: anyio in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from httpx<1,>=0.23.0->langsmith<0.2.0,>=0.1.17->langchain) (4.6.2.post1)
Requirement already satisfied: httpcore==1.* in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from httpx<1,>=0.23.0->langsmith<0.2.0,>=0.1.17->langchain) (1.0.7)
Requirement already satisfied: sniffio in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from httpx<1,>=0.23.0->langsmith<0.2.0,>=0.1.17->langchain) (1.3.1)
Requirement already satisfied: h11<0.15,>=0.13 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from httpcore==1.*->httpx<1,>=0.23.0->langsmith<0.2.0,>=0.1.17->langchain) (0.14.0)
Requirement already satisfied: jsonpointer>=1.9 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.4.0,>=0.3.21->langchain) (3.0.0)
Requirement already satisfied: networkx in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from torch>=1.11.0->sentence-transformers>=2.6.0->langchain_huggingface) (3.2.1)
Requirement already satisfied: jinja2 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from torch>=1.11.0->sentence-transformers>=2.6.0->langchain_huggingface) (3.1.4)
Requirement already satisfied: sympy==1.13.1 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from torch>=1.11.0->sentence-transformers>=2.6.0->langchain_huggingface) (1.13.1)
Requirement already satisfied: mpmath<1.4,>=1.1.0 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from sympy==1.13.1->torch>=1.11.0->sentence-transformers>=2.6.0->langchain_huggingface) (1.3.0)
Requirement already satisfied: colorama in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from tqdm>=4.42.1->huggingface-hub>=0.23.0->langchain_huggingface) (0.4.6)
Requirement already satisfied: joblib>=1.2.0 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from scikit-learn->sentence-transformers>=2.6.0->langchain_huggingface) (1.4.2)
Requirement already satisfied: threadpoolctl>=3.1.0 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from scikit-learn->sentence-transformers>=2.6.0->langchain_huggingface) (3.5.0)
Requirement already satisfied: exceptiongroup>=1.0.2 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from anyio->httpx<1,>=0.23.0->langsmith<0.2.0,>=0.1.17->langchain) (1.2.2)
Requirement already satisfied: MarkupSafe>=2.0 in c:\users\dell\desktop\langchain\.venv\lib\site-packages (from jinja2->torch>=1.11.0->sentence-transformers>=2.6.0->langchain_huggingface) (3.0.2)

凭据

我们想使用 HuggingFace,因此必须获取 HuggingFace API 密钥

import getpass
import os

if "HUGGINGFACE_API_KEY" not in os.environ:
os.environ["HUGGINGFACE_API_KEY"] = getpass.getpass("HuggingFace API Key:")

如果您想自动跟踪模型调用,您还可以通过取消注释下方内容来设置 LangSmith API 密钥

# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
# os.environ["LANGSMITH_TRACING"] = "true"

初始化

from langchain_community.vectorstores.falkordb_vector import FalkorDBVector
from langchain_core.documents import Document
from langchain_huggingface import HuggingFaceEmbeddings

您可以在本地使用 Docker 运行 FalkorDBVector。请参阅安装说明

host = "localhost"
port = 6379

或者您可以使用 FalkorDB Cloud 运行 FalkorDBVector

# E.g
# host = "r-6jissuruar.instance-zwb082gpf.hc-v8noonp0c.europe-west1.gcp.f2e0a955bb84.cloud"
# port = 62471
# username = "falkordb" # SET ON FALKORDB CLOUD
# password = "password" # SET ON FALKORDB CLOUD
vector_store = FalkorDBVector(host=host, port=port, embedding=HuggingFaceEmbeddings())

管理向量存储

向向量存储添加项目

from langchain_core.documents import Document

document_1 = Document(page_content="foo", metadata={"source": "https://example.com"})

document_2 = Document(page_content="bar", metadata={"source": "https://example.com"})

document_3 = Document(page_content="baz", metadata={"source": "https://example.com"})

documents = [document_1, document_2, document_3]

vector_store.add_documents(documents=documents, ids=["1", "2", "3"])
API 参考:Document
['1', '2', '3']

更新向量存储中的项目

updated_document = Document(
page_content="qux", metadata={"source": "https://another-example.com"}
)

vector_store.update_documents(document_id="1", document=updated_document)

从向量存储中删除项目

vector_store.delete(ids=["3"])

查询向量存储

一旦创建了向量存储并添加了相关文档,您很可能希望在运行链或代理期间查询它。

直接查询

执行简单的相似性搜索可以按如下方式完成

results = vector_store.similarity_search(
query="thud", k=1, filter={"source": "https://another-example.com"}
)
for doc in results:
print(f"* {doc.page_content} [{doc.metadata}]")
* qux [{'text': 'qux', 'id': '1', 'source': 'https://another-example.com'}]

如果要执行相似性搜索并接收相应的分数,可以运行

results = vector_store.similarity_search_with_score(query="bar")
for doc, score in results:
print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")
* [SIM=0.000001] bar [{'text': 'bar', 'id': '2', 'source': 'https://example.com'}]

通过转换为检索器进行查询

您还可以将向量存储转换为检索器,以便在链中更轻松地使用。

retriever = vector_store.as_retriever(search_type="mmr", search_kwargs={"k": 1})
retriever.invoke("thud")
[Document(metadata={'text': 'qux', 'id': '1', 'source': 'https://another-example.com'}, page_content='qux')]

用于检索增强生成的用法

有关如何使用此向量存储进行检索增强生成 (RAG) 的指南,请参阅以下部分

API 参考

有关所有 FalkorDBVector 功能和配置的详细文档,请访问 API 参考:https://python.langchain.ac.cn/api_reference/community/vectorstores/langchain_community.vectorstores.falkordb_vector.FalkorDBVector.html


此页面是否有帮助?