Graphiti:一個(gè)用于構(gòu)建和查詢(xún)實(shí)時(shí)知識(shí)圖譜的框架
Graphiti是什么?
Graphiti 是一個(gè)用于構(gòu)建和查詢(xún)知識(shí)圖譜的框架,它具備時(shí)間感知能力,非常適合用在動(dòng)態(tài)環(huán)境中。與傳統(tǒng)的檢索增強(qiáng)生成(RAG)方法不同,Graphiti 可以實(shí)時(shí)整合用戶交互、結(jié)構(gòu)化和非結(jié)構(gòu)化的數(shù)據(jù),以及外部信息,生成一個(gè)可查詢(xún)的圖譜。它的優(yōu)勢(shì)在于支持?jǐn)?shù)據(jù)的實(shí)時(shí)更新、高效檢索和精確的歷史查詢(xún),無(wú)需重新計(jì)算整個(gè)圖譜,非常適合開(kāi)發(fā)交互式、上下文感知的應(yīng)用程序。
Graphiti 的主要功能
實(shí)時(shí)增量更新:能夠立即集成新的數(shù)據(jù)片段,無(wú)需進(jìn)行批量重新計(jì)算。
雙時(shí)態(tài)數(shù)據(jù)模型:明確跟蹤事件發(fā)生時(shí)間和攝入時(shí)間,支持精確的時(shí)間點(diǎn)查詢(xún)。
高效混合檢索:結(jié)合語(yǔ)義嵌入、關(guān)鍵字(BM25)和圖遍歷,實(shí)現(xiàn)低延遲查詢(xún),無(wú)需依賴(lài)大語(yǔ)言模型(LLM)進(jìn)行總結(jié)。
自定義實(shí)體定義:支持通過(guò)簡(jiǎn)單的 Pydantic 模型創(chuàng)建靈活的本體和自定義實(shí)體。
可擴(kuò)展性:通過(guò)并行處理有效管理大型數(shù)據(jù)集,適用于企業(yè)環(huán)境。
與多種 LLM 集成: 支持 Openai、Anthropic 和 Groq 等 LLM 提供商,提供靈活的 LLM 推理和嵌入選項(xiàng)。
MCP 服務(wù)器: 提供一個(gè)基于 FastAPI 的 API 服務(wù),支持通過(guò) MCP 協(xié)議與知識(shí)圖譜進(jìn)行交互,包括事件管理、實(shí)體管理和關(guān)系處理等功能。
選擇 Graphiti 的原因
它能實(shí)時(shí)更新數(shù)據(jù),適應(yīng)動(dòng)態(tài)變化的環(huán)境。
支持精確的時(shí)間點(diǎn)查詢(xún),方便追溯歷史信息。
檢索速度快,不需要依賴(lài)復(fù)雜的語(yǔ)言模型。
可以自定義數(shù)據(jù)結(jié)構(gòu),滿足特定需求。
能高效處理大量數(shù)據(jù),適合企業(yè)級(jí)應(yīng)用。
Graphiti的安裝和使用
安裝 Graphiti
首先,確保你的系統(tǒng)滿足安裝要求,并安裝必要的依賴(lài)項(xiàng):
Python:確保你的系統(tǒng)中安裝了 Python 3.10 或更高版本。
Neo4j:安裝 Neo4j 數(shù)據(jù)庫(kù),版本 5.26 或更高。你可以使用 Neo4j Desktop 來(lái)簡(jiǎn)化安裝和管理過(guò)程。
OpenAI API Key:如果你打算使用 OpenAI 進(jìn)行 LLM 推理和嵌入,你需要一個(gè)有效的 OpenAI API Key。
你可以使用 pip 或 poetry 來(lái)安裝 Graphiti 核心庫(kù):
pip install graphiti-core # 或者 poetry add graphiti-core
初始化 Graphiti
在你的代碼中初始化 Graphiti 實(shí)例,連接到 Neo4j 數(shù)據(jù)庫(kù):
from graphiti_core import Graphiti graphiti = Graphiti("bolt://localhost:7687", "neo4j", "password")
構(gòu)建索引和約束
在添加數(shù)據(jù)之前,初始化圖數(shù)據(jù)庫(kù)并構(gòu)建 Graphiti 的索引和約束:
await graphiti.build_indices_and_constraints()
添加數(shù)據(jù)
使用 add_episode 方法將數(shù)據(jù)添加到知識(shí)圖譜中。每個(gè)數(shù)據(jù)片段稱(chēng)為一個(gè)“episode”:
from graphiti_core.nodes import EpisodeType from datetime import datetime, timezone episodes = [ "Kamala Harris is the Attorney General of California. She was previously the district attorney for San Francisco.", "As AG, Harris was in office from January 3, 2011 – January 3, 2017" ] for i, episode in enumerate(episodes): await graphiti.add_episode( name=f"Freakonomics Radio {i}", episode_body=episode, source=EpisodeType.text, source_description="podcast", reference_time=datetime.now(timezone.utc) )
查詢(xún)數(shù)據(jù)
使用 search 方法查詢(xún)知識(shí)圖譜,結(jié)合語(yǔ)義相似性和關(guān)鍵詞搜索:
results = await graphiti.search('Who was the California Attorney General?')
自定義查詢(xún)
你可以根據(jù)需要自定義查詢(xún),例如通過(guò)指定中心節(jié)點(diǎn)來(lái)優(yōu)先考慮與該節(jié)點(diǎn)更接近的結(jié)果:
await graphiti.search('Who was the California Attorney General?', center_node_uuid)
關(guān)閉連接
在完成聊天狀態(tài)管理后,關(guān)閉 Graphiti 連接:
graphiti.close()
使用 MCP 服務(wù)器
Graphiti 提供了一個(gè) MCP 服務(wù)器,可以通過(guò) FastAPI 進(jìn)行交互。你可以部署這個(gè)服務(wù)器并使用 MCP 協(xié)議與知識(shí)圖譜進(jìn)行交互:
1. 克隆或下載 Graphiti 倉(cāng)庫(kù)。
2. 運(yùn)行 MCP 服務(wù)器:
uvicorn graphiti.mcp_server.main:app --reload
3. 在你的 AI 助手中使用 MCP 協(xié)議與服務(wù)器進(jìn)行通信。
使用 Azure OpenAI
如果你想使用 Azure OpenAI,你需要配置相應(yīng)的客戶端和嵌入器:
from openai import AsyncAzureOpenAI from graphiti_core import Graphiti from graphiti_core.llm_client import OpenAIClient from graphiti_core.embedder.openai import OpenAIEmbedder, OpenAIEmbedderConfig api_key = "<your-api-key>" api_version = "<your-api-version>" azure_endpoint = "<your-azure-endpoint>" azure_openai_client = AsyncAzureOpenAI(api_key=api_key, api_version=api_version, azure_endpoint=azure_endpoint) graphiti = Graphiti( "bolt://localhost:7687", "neo4j", "password", llm_client=OpenAIClient(client=azure_openai_client), embedder=OpenAIEmbedder(config=OpenAIEmbedderConfig(embedding_model="text-embedding-3-small"), client=azure_openai_client) )
確保替換占位符值為你實(shí)際的 Azure OpenAI 憑據(jù),并指定正確的嵌入模型名稱(chēng)。
Graphiti 的優(yōu)勢(shì)
和其他技術(shù)相比,Graphiti 在處理動(dòng)態(tài)數(shù)據(jù)、實(shí)時(shí)更新和查詢(xún)速度上有明顯優(yōu)勢(shì)。
提供了服務(wù)器實(shí)現(xiàn),方便與其他系統(tǒng)集成。
支持多種環(huán)境配置和與其他服務(wù)的集成。
相關(guān)鏈接
論文:https://arxiv.org/abs/2501.13956
GitHub 倉(cāng)庫(kù):https://github.com/getzep/graphiti