適用於 Python 的官方 Neo4j GraphRAG 套件使開發人員能夠利用 Neo4j 和 Python 的強大功能來建立圖形檢索增強生成 (GraphRAG) 應用程式。作為第一方庫,它提供了強大的、功能豐富的高效能解決方案,並直接從 Neo4j 額外保證了長期支援和維護。
文件可以在這裡找到
版本 | 支持嗎? |
---|---|
3.12 | ✓ |
3.11 | ✓ |
3.10 | ✓ |
3.9 | ✓ |
3.8 | ✗ |
若要安裝最新的穩定版本,請執行:
pip install neo4j-graphrag
pygraphviz
用於可視化管道。您可以在此處找到安裝說明。
下面的腳本示範如何開始使用該套件並利用其主要功能。要執行這些範例,請確保您有一個 Neo4j 實例已啟動並正在運行,並使用 Neo4j 實例的詳細資訊更新每個腳本中的NEO4J_URI
、 NEO4J_USERNAME
和NEO4J_PASSWORD
變數。對於範例,請確保將 OpenAI 金鑰匯出為名為OPENAI_API_KEY
的環境變數。 examples
夾中提供了其他範例。
注意:APOC 核心庫必須安裝在 Neo4j 實例中才能使用此功能
該套件提供了兩種建立知識圖的方法。
Pipeline
類別提供了廣泛的自訂選項,使其成為高級用例的理想選擇。有關如何使用此類別的範例,請參閱examples/pipeline
資料夾。
對於更簡化的方法, SimpleKGPipeline
類別在Pipeline
上提供了一個簡化的抽象層,使建立知識圖變得更容易。這兩個類別都支援直接處理文字和 PDF。
import asyncio
from neo4j import GraphDatabase
from neo4j_graphrag . embeddings import OpenAIEmbeddings
from neo4j_graphrag . experimental . pipeline . kg_builder import SimpleKGPipeline
from neo4j_graphrag . llm . openai_llm import OpenAILLM
NEO4J_URI = "neo4j://localhost:7687"
NEO4J_USERNAME = "neo4j"
NEO4J_PASSWORD = "password"
# Connect to the Neo4j database
driver = GraphDatabase . driver ( NEO4J_URI , auth = ( NEO4J_USERNAME , NEO4J_PASSWORD ))
# List the entities and relations the LLM should look for in the text
entities = [ "Person" , "House" , "Planet" ]
relations = [ "PARENT_OF" , "HEIR_OF" , "RULES" ]
potential_schema = [
( "Person" , "PARENT_OF" , "Person" ),
( "Person" , "HEIR_OF" , "House" ),
( "House" , "RULES" , "Planet" ),
]
# Create an Embedder object
embedder = OpenAIEmbeddings ( model = "text-embedding-3-large" )
# Instantiate the LLM
llm = OpenAILLM (
model_name = "gpt-4o" ,
model_params = {
"max_tokens" : 2000 ,
"response_format" : { "type" : "json_object" },
"temperature" : 0 ,
},
)
# Instantiate the SimpleKGPipeline
kg_builder = SimpleKGPipeline (
llm = llm ,
driver = driver ,
embedder = embedder ,
entities = entities ,
relations = relations ,
on_error = "IGNORE" ,
from_pdf = False ,
)
# Run the pipeline on a piece of text
text = (
"The son of Duke Leto Atreides and the Lady Jessica, Paul is the heir of House "
"Atreides, an aristocratic family that rules the planet Caladan."
)
asyncio . run ( kg_builder . run_async ( text = text ))
driver . close ()
使用上述腳本建立的知識圖範例:
建立向量索引時,請確保索引中的維數與嵌入的維數相符。
from neo4j import GraphDatabase
from neo4j_graphrag . indexes import create_vector_index
NEO4J_URI = "neo4j://localhost:7687"
NEO4J_USERNAME = "neo4j"
NEO4J_PASSWORD = "password"
INDEX_NAME = "vector-index-name"
# Connect to the Neo4j database
driver = GraphDatabase . driver ( NEO4J_URI , auth = ( NEO4J_USERNAME , NEO4J_PASSWORD ))
# Create the index
create_vector_index (
driver ,
INDEX_NAME ,
label = "Chunk" ,
embedding_property = "embedding" ,
dimensions = 3072 ,
similarity_fn = "euclidean" ,
)
driver . close ()
此範例示範了在 Neo4j 資料庫中更新插入資料的方法。值得注意的是,還有其他方法,例如使用 Neo4j Python 驅動程式。
確保在執行此範例之前建立向量索引。
from neo4j import GraphDatabase
from neo4j_graphrag . embeddings import OpenAIEmbeddings
from neo4j_graphrag . indexes import upsert_vector
NEO4J_URI = "neo4j://localhost:7687"
NEO4J_USERNAME = "neo4j"
NEO4J_PASSWORD = "password"
# Connect to the Neo4j database
driver = GraphDatabase . driver ( NEO4J_URI , auth = ( NEO4J_USERNAME , NEO4J_PASSWORD ))
# Create an Embedder object
embedder = OpenAIEmbeddings ( model = "text-embedding-3-large" )
# Generate an embedding for some text
text = (
"The son of Duke Leto Atreides and the Lady Jessica, Paul is the heir of House "
"Atreides, an aristocratic family that rules the planet Caladan."
)
vector = embedder . embed_query ( text )
# Upsert the vector
upsert_vector (
driver ,
node_id = 0 ,
embedding_property = "embedding" ,
vector = vector ,
)
driver . close ()
請注意,當查詢 Neo4j 向量索引時,使用近似最近鄰搜索,這可能並不總是提供準確的結果。有關更多信息,請參閱有關向量索引的限制和問題的 Neo4j 文件。
在下面的範例中,我們使用檢索器執行簡單的向量搜索,該檢索器對vector-index-name
向量索引進行相似性搜尋。
該庫提供了除VectorRetriever
之外的更多檢索器。有關如何使用這些檢索器的範例,請參閱examples
資料夾。
在運行此範例之前,請確保已建立並填入向量索引。
from neo4j import GraphDatabase
from neo4j_graphrag . embeddings import OpenAIEmbeddings
from neo4j_graphrag . generation import GraphRAG
from neo4j_graphrag . llm import OpenAILLM
from neo4j_graphrag . retrievers import VectorRetriever
NEO4J_URI = "neo4j://localhost:7687"
NEO4J_USERNAME = "neo4j"
NEO4J_PASSWORD = "password"
INDEX_NAME = "vector-index-name"
# Connect to the Neo4j database
driver = GraphDatabase . driver ( NEO4J_URI , auth = ( NEO4J_USERNAME , NEO4J_PASSWORD ))
# Create an Embedder object
embedder = OpenAIEmbeddings ( model = "text-embedding-3-large" )
# Initialize the retriever
retriever = VectorRetriever ( driver , INDEX_NAME , embedder )
# Instantiate the LLM
llm = OpenAILLM ( model_name = "gpt-4o" , model_params = { "temperature" : 0 })
# Instantiate the RAG pipeline
rag = GraphRAG ( retriever = retriever , llm = llm )
# Query the graph
query_text = "Who is Paul Atreides?"
response = rag . search ( query_text = query_text , retriever_config = { "top_k" : 5 })
print ( response . answer )
driver . close ()
您必須簽署貢獻者許可協議才能為本專案做出貢獻。
我們的 Python 依賴項是使用 Poetry 來管理的。如果您的系統上尚未安裝 Poetry,您可以按照此處的說明進行設定。要開始此專案的開發,請先複製儲存庫,然後使用以下命令安裝所有必要的依賴項,包括開發依賴項:
poetry install --with dev
如果您需要報告錯誤或請求功能,請先搜尋以查看問題是否已存在。如果不存在相關問題,請使用問題表單提出新問題。
如果您是 Neo4j Enterprise 客戶,您也可以聯絡客戶支援。
如果您沒有要報告的錯誤或功能請求,但需要庫的幫助;可透過 Neo4j 線上社群和/或 Discord 獲得社群支持。
main
建立一個工作分支並開始進行更改!我們的程式碼庫遵循嚴格的格式和 linting 標準,使用 Ruff 進行程式碼品質檢查,使用 Mypy 進行類型檢查。在貢獻之前,請確保所有程式碼格式正確,沒有 linting 問題,並且包含準確的類型註解。
貢獻必須遵守這些標準才能被接受。
我們建議設定預提交來自動檢查程式碼品質。這可以確保您的更改在提交之前符合我們的準則。
請按照安裝指南安裝預提交。
透過執行以下命令設定預提交掛鉤:
pre-commit install
若要手動檢查文件是否符合品質要求,請執行:
pre-commit run --file path/to/file
完成變更後,請使用下列工作流程建立拉取請求 (PR)。
main
。CHANGELOG.md
,其中包括:CHANGELOG.md
更改簡短並關注最重要的更改。CHANGELOG.md
@CodiumAI-Agent /update_changelog
CHANGELOG.md
檔案中「下一步」下的相應小節。安裝專案依賴項,然後執行以下命令在本機執行單元測試:
poetry run pytest tests/unit
要執行端對端 (e2e) 測試,您需要在本機上執行以下服務:
設定這些的最簡單方法是使用 Docker Compose:
docker compose -f tests/e2e/docker-compose.yml up
(提示:如果您在資料庫中遇到任何快取問題,可以透過執行docker compose -f tests/e2e/docker-compose.yml down
來完全刪除它們)
所有服務運行後,執行以下命令來執行 e2e 測試:
poetry run pytest tests/e2e