使用llama-index连接neo4j知识图谱 达成大模型构建查询知识图谱功能

本文主要是介绍使用llama-index连接neo4j知识图谱 达成大模型构建查询知识图谱功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用llama-index连接neo4j知识图谱 达成本地大模型构建查询知识图谱功能

    • 概述
      • 目标
      • 主要步骤
    • 1. 安装依赖项
    • 2. 配置环境
      • 配置ollama
    • 3. 使用Neo4j构建知识图谱
      • 准备Neo4j
      • 实例化Neo4jGraph KG索引
    • 4. 查询知识图谱
      • 查询并仅发送三元组到LLM
      • 查询并发送包含文本的结果到LLM
    • 5. 使用嵌入查询
    • 6. 手动添加三元组并构建图谱(可选)

当然!我将提供更多详细的解释,帮助您更好地理解如何使用Llama-Index连接Neo4j知识图谱,实现大模型构建和查询功能。

概述

目标

我们的目标是使用Llama-Index来连接到Neo4j,以构建和查询知识图谱。通过这个过程,我们能够将文档中的信息转化为知识图谱,并通过大语言模型进行查询。
以下参考llama-index官方实现

主要步骤

  1. 安装依赖项:我们需要安装一些Python库来支持我们的工作。
  2. 配置环境:我们需要设置一些环境变量来使用OpenAI或Azure OpenAI的API。
  3. 构建知识图谱:我们将使用Neo4j来存储和管理知识图谱。
  4. 查询知识图谱:我们将通过Llama-Index查询Neo4j中的数据。
  5. (可选)手动添加三元组:我们还可以手动添加三元组到知识图谱中。

1. 安装依赖项

首先,我们需要安装一些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-openaillama-index-llms-azure-openai:用于连接OpenAI和Azure OpenAI的API,以获取NLP模型。
  • llama-index-graph-stores-neo4j:用于与Neo4j数据库交互。
  • llama-index-embeddings-openai:用于处理文本嵌入。
  • neo4j:Neo4j数据库的官方Python驱动程序。

2. 配置环境

为了使用OpenAI或Azure OpenAI的API,我们需要配置一些环境变量和API密钥。

配置ollama

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

3. 使用Neo4j构建知识图谱

准备Neo4j

我们需要配置Neo4j数据库的连接信息。

username = "neo4j"
password = "your-neo4j-password"
url = "bolt://your-neo4j-url:7687"
database = "neo4j"

实例化Neo4jGraph KG索引

接下来,我们使用Llama-Index从文档中提取数据,并将其存储到Neo4j图数据库中。

from llama_index.core import KnowledgeGraphIndex, SimpleDirectoryReader
from llama_index.core import StorageContext
from llama_index.graph_stores.neo4j import Neo4jGraphStorefrom 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,
)

4. 查询知识图谱

查询并仅发送三元组到LLM

我们可以查询知识图谱并仅发送三元组到大语言模型进行处理。

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

查询并发送包含文本的结果到LLM

我们还可以查询知识图谱并发送包含文本的结果到大语言模型。

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

5. 使用嵌入查询

我们还可以在构建索引时包含文本嵌入,以便在查询时使用嵌入相似度进行更准确的查询。

# 清理数据集
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>"))

6. 手动添加三元组并构建图谱(可选)

我们还可以手动向知识图谱中添加三元组。

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

这篇关于使用llama-index连接neo4j知识图谱 达成大模型构建查询知识图谱功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1018331

相关文章

MySQL中between and的基本用法、范围查询示例详解

《MySQL中betweenand的基本用法、范围查询示例详解》BETWEENAND操作符在MySQL中用于选择在两个值之间的数据,包括边界值,它支持数值和日期类型,示例展示了如何使用BETWEEN... 目录一、between and语法二、使用示例2.1、betwphpeen and数值查询2.2、be

python中的flask_sqlalchemy的使用及示例详解

《python中的flask_sqlalchemy的使用及示例详解》文章主要介绍了在使用SQLAlchemy创建模型实例时,通过元类动态创建实例的方式,并说明了如何在实例化时执行__init__方法,... 目录@orm.reconstructorSQLAlchemy的回滚关联其他模型数据库基本操作将数据添

Spring配置扩展之JavaConfig的使用小结

《Spring配置扩展之JavaConfig的使用小结》JavaConfig是Spring框架中基于纯Java代码的配置方式,用于替代传统的XML配置,通过注解(如@Bean)定义Spring容器的组... 目录JavaConfig 的概念什么是JavaConfig?为什么使用 JavaConfig?Jav

JavaWeb项目创建、部署、连接数据库保姆级教程(tomcat)

《JavaWeb项目创建、部署、连接数据库保姆级教程(tomcat)》:本文主要介绍如何在IntelliJIDEA2020.1中创建和部署一个JavaWeb项目,包括创建项目、配置Tomcat服务... 目录简介:一、创建项目二、tomcat部署1、将tomcat解压在一个自己找得到路径2、在idea中添加

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

Springboot3 ResponseEntity 完全使用案例

《Springboot3ResponseEntity完全使用案例》ResponseEntity是SpringBoot中控制HTTP响应的核心工具——它能让你精准定义响应状态码、响应头、响应体,相比... 目录Spring Boot 3 ResponseEntity 完全使用教程前置准备1. 项目基础依赖(M

Java使用Spire.Barcode for Java实现条形码生成与识别

《Java使用Spire.BarcodeforJava实现条形码生成与识别》在现代商业和技术领域,条形码无处不在,本教程将引导您深入了解如何在您的Java项目中利用Spire.Barcodefor... 目录1. Spire.Barcode for Java 简介与环境配置2. 使用 Spire.Barco

Android使用java实现网络连通性检查详解

《Android使用java实现网络连通性检查详解》这篇文章主要为大家详细介绍了Android使用java实现网络连通性检查的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录NetCheck.Java(可直接拷贝)使用示例(Activity/Fragment 内)权限要求

C# 预处理指令(# 指令)的具体使用

《C#预处理指令(#指令)的具体使用》本文主要介绍了C#预处理指令(#指令)的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录1、预处理指令的本质2、条件编译指令2.1 #define 和 #undef2.2 #if, #el

C#中Trace.Assert的使用小结

《C#中Trace.Assert的使用小结》Trace.Assert是.NET中的运行时断言检查工具,用于验证代码中的关键条件,下面就来详细的介绍一下Trace.Assert的使用,具有一定的参考价值... 目录1、 什么是 Trace.Assert?1.1 最简单的比喻1.2 基本语法2、⚡ 工作原理3