赞
踩
我们的目标是使用Llama-Index来连接到Neo4j,以构建和查询知识图谱。通过这个过程,我们能够将文档中的信息转化为知识图谱,并通过大语言模型进行查询。
以下参考llama-index官方实现
首先,我们需要安装一些Python库。这些库包括Llama-Index的相关组件和Neo4j的连接库。
%pip install llama-index-llms-openai
%pip install llama-index-graph-stores-neo4j
%pip install llama-index-embeddings-openai
%pip install llama-index-llms-azure-openai
%pip install neo4j
这些库的功能如下:
llama-index-llms-openai
和 llama-index-llms-azure-openai
:用于连接OpenAI和Azure OpenAI的API,以获取NLP模型。llama-index-graph-stores-neo4j
:用于与Neo4j数据库交互。llama-index-embeddings-openai
:用于处理文本嵌入。neo4j
:Neo4j数据库的官方Python驱动程序。为了使用OpenAI或Azure OpenAI的API,我们需要配置一些环境变量和API密钥。
import os
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings, StorageContext, KnowledgeGraphIndex
from llama_index.embeddings.ollama import OllamaEmbedding
from llama_index.llms.ollama import Ollama
from llama_index.core import KnowledgeGraphIndex, SimpleDirectoryReader, StorageContext
from llama_index.graph_stores.neo4j import Neo4jGraphStore
# 设置嵌入模型
Settings.embed_model = OllamaEmbedding(model_name="znbang/bge:large-zh-v1.5-f32")
# 设置LLM模型
Settings.llm = Ollama(model="qwen:7b", request_timeout=360.0
我们需要配置Neo4j数据库的连接信息。
username = "neo4j"
password = "your-neo4j-password"
url = "bolt://your-neo4j-url:7687"
database = "neo4j"
接下来,我们使用Llama-Index从文档中提取数据,并将其存储到Neo4j图数据库中。
from llama_index.core import KnowledgeGraphIndex, SimpleDirectoryReader from llama_index.core import StorageContext from llama_index.graph_stores.neo4j import Neo4jGraphStore from llama_index.llms.openai import OpenAI from IPython.display import Markdown, display # 加载文档数据 documents = SimpleDirectoryReader( "path_to_your_documents" ).load_data() # 初始化Neo4j图存储 graph_store = Neo4jGraphStore( username=username, password=password, url=url, database=database, ) # 创建存储上下文 storage_context = StorageContext.from_defaults(graph_store=graph_store) # 构建知识图谱索引 index = KnowledgeGraphIndex.from_documents( documents, storage_context=storage_context, max_triplets_per_chunk=2, )
我们可以查询知识图谱并仅发送三元组到大语言模型进行处理。
query_engine = index.as_query_engine(
include_text=False, response_mode="tree_summarize"
)
response = query_engine.query("Tell me more about Interleaf")
display(Markdown(f"<b>{response}</b>"))
我们还可以查询知识图谱并发送包含文本的结果到大语言模型。
query_engine = index.as_query_engine(
include_text=True, response_mode="tree_summarize"
)
response = query_engine.query(
"Tell me more about what the author worked on at Interleaf"
)
display(Markdown(f"<b>{response}</b>"))
我们还可以在构建索引时包含文本嵌入,以便在查询时使用嵌入相似度进行更准确的查询。
# 清理数据集 graph_store.query( """ MATCH (n) DETACH DELETE n """ ) # 构建包含嵌入的索引 index = KnowledgeGraphIndex.from_documents( documents, storage_context=storage_context, max_triplets_per_chunk=2, include_embeddings=True, ) query_engine = index.as_query_engine( include_text=True, response_mode="tree_summarize", embedding_mode="hybrid", similarity_top_k=5, ) response = query_engine.query( "Tell me more about what the author worked on at Interleaf" ) display(Markdown(f"<b>{response}</b>"))
我们还可以手动向知识图谱中添加三元组。
from llama_index.core.node_parser import SentenceSplitter node_parser = SentenceSplitter() nodes = node_parser.get_nodes_from_documents(documents) # 初始化一个空的索引 index = KnowledgeGraphIndex.from_documents([], storage_context=storage_context) # 手动添加三元组 node_0_tups = [ ("author", "worked on", "writing"), ("author", "worked on", "programming"), ] for tup in node_0_tups: index.upsert_triplet_and_node(tup, nodes[0]) node_1_tups = [ ("Interleaf", "made software for", "creating documents"), ("Interleaf", "added", "scripting language"), ("software", "generate", "web sites"), ] for tup in node_1_tups: index.upsert_triplet_and_node(tup, nodes[1]) query_engine = index.as_query_engine( include_text=False, response_mode="tree_summarize" ) response = query_engine.query("Tell me more about Interleaf") display(Markdown(f"<b>{response}</b>"))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。