当前位置: 首页 > java >正文

使用 LangChain 和 Neo4j 构建知识图谱

在现代 AI 应用中,将文本内容结构化为知识图谱是一种非常实用的方法,可以帮助我们更高效地进行信息查询、知识推理和数据分析。本文将结合 LangChain 官方文档 和实战代码,演示如何使用 LangChain + Neo4j 将文本内容自动转换为知识图谱。

环境准备

我们需要以下组件:

  • Python 3.10+

  • Neo4j 数据库 (可以通过docker 来安装)

  • LangChain 及社区扩展模块

  • 统一 AI 模型(如 ChatTongyi 或 Qwen 系列)

首先,安装必要依赖:

pip install langchain langchain-neo4j langchain-community

并确保 Neo4j 服务在本地运行:

docker 安装的命令:

docker run --name neo4j -p7474:7474 -p7687:7687 -d -e NEO4J_AUTH=neo4j/xxxx  -e NEO4JLABS_PLUGINS='["apoc"]'  -e NEO4J_apoc_export_file_enabled=true -e NEO4J_apoc_import_file_enabled=true  -e NEO4J_apoc_import_file_use__neo4j__config=true neo4j:latest

核心思路

本文示例展示了如何:

  1. 使用 LLM(大语言模型)分析文本;

  2. 将文本转换为图谱节点和关系;

  3. 将生成的图谱写入 Neo4j 数据库。

主要流程图如下:

文本 → LLM → 图谱节点/关系 → Neo4j

示例代码解析

下面是完整代码示例:

import asyncio
import osfrom langchain_neo4j import Neo4jGraph
from langchain_community.chat_models import ChatTongyios.environ["NEO4J_URI"] = "bolt://localhost:7687"
os.environ["NEO4J_USERNAME"] = "neo4j"
os.environ["NEO4J_PASSWORD"] = "xxxx"async def test():# 初始化 LLMllm = ChatTongyi(model="qwen-plus", api_key="YOUR_API_KEY")# 连接 Neo4j 图数据库graph = Neo4jGraph(refresh_schema=False)# 引入图谱转换器from langchain_experimental.graph_transformers import LLMGraphTransformerllm_transformer = LLMGraphTransformer(llm=llm)# 准备文本from langchain_core.documents import Documenttext = """Marie Curie, born in 1867, was a Polish and naturalised-French physicist and chemist who conducted pioneering research on radioactivity.She was the first woman to win a Nobel Prize, the first person to win a Nobel Prize twice, and the only person to win a Nobel Prize in two scientific fields.Her husband, Pierre Curie, was a co-winner of her first Nobel Prize, making them the first-ever married couple to win the Nobel Prize and launching the Curie family legacy of five Nobel Prizes.She was, in 1906, the first woman to become a professor at the University of Paris."""documents = [Document(page_content=text)]# 将文本转换为图谱文档graph_documents = await llm_transformer.aconvert_to_graph_documents(documents)print(f"Nodes:{graph_documents[0].nodes}")print(f"Relationships:{graph_documents[0].relationships}")# 写入 Neo4jgraph.add_graph_documents(graph_documents)if __name__ == '__main__':asyncio.run(test())

运行结果

Nodes:[Node(id='Marie Curie', type='Person', properties={}), Node(id='Polish', type='Nationality', properties={}), Node(id='French', type='Nationality', properties={}), Node(id='Physicist', type='Occupation', properties={}), Node(id='Chemist', type='Occupation', properties={}), Node(id='Radioactivity', type='Scientific field', properties={}), Node(id='Nobel Prize', type='Award', properties={}), Node(id='Pierre Curie', type='Person', properties={}), Node(id='University Of Paris', type='Institution', properties={})]
Relationships:[Relationship(source=Node(id='Marie Curie', type='Person', properties={}), target=Node(id='Polish', type='Nationality', properties={}), type='NATIONALITY', properties={}), Relationship(source=Node(id='Marie Curie', type='Person', properties={}), target=Node(id='French', type='Nationality', properties={}), type='NATIONALITY', properties={}), Relationship(source=Node(id='Marie Curie', type='Person', properties={}), target=Node(id='Physicist', type='Occupation', properties={}), type='OCCUPATION', properties={}), Relationship(source=Node(id='Marie Curie', type='Person', properties={}), target=Node(id='Chemist', type='Occupation', properties={}), type='OCCUPATION', properties={}), Relationship(source=Node(id='Marie Curie', type='Person', properties={}), target=Node(id='Radioactivity', type='Scientific field', properties={}), type='RESEARCHED', properties={}), Relationship(source=Node(id='Marie Curie', type='Person', properties={}), target=Node(id='Nobel Prize', type='Award', properties={}), type='AWARDED', properties={}), Relationship(source=Node(id='Marie Curie', type='Person', properties={}), target=Node(id='Nobel Prize', type='Award', properties={}), type='AWARDED_TWICE', properties={}), Relationship(source=Node(id='Marie Curie', type='Person', properties={}), target=Node(id='Pierre Curie', type='Person', properties={}), type='SPOUSE', properties={}), Relationship(source=Node(id='Marie Curie', type='Person', properties={}), target=Node(id='University Of Paris', type='Institution', properties={}), type='PROFESSOR', properties={})]

运行效果

http://localhost:7474/browser/preview/

http://www.xdnf.cn/news/18605.html

相关文章:

  • 一文学会vue的动态权限控制
  • 00后AI创业者崛起与AI商业应用新玩法:从Mercor到历史人物复刻的机遇
  • 【剖析高并发秒杀】从流量削峰到数据一致性的架构演进与实践
  • MySQL GPG 密钥更新问题解决文档
  • Kubernetes网络服务全解析
  • Linux netfilter工作原理详解
  • Mac简单测试硬盘读写速度
  • 暴雨环境漏检率下降78%!陌讯动态融合算法在道路积水识别的工程突破
  • LeetCode 面试经典 150_数组/字符串_找出字符串中第一个匹配项的下标(23_28_C++_简单)(KMP 算法)
  • PyTorch 面试题及详细答案120题(71-85)-- 高级特性与工具
  • Base64 编码优化 Web 图片加载:异步响应式架构(Java 后端 + 前端全流程实现)
  • vue实现小程序oss分片上传
  • 合合信息acge模型获C-MTEB第一,文本向量化迎来新突破
  • 微前端架构核心要点对比
  • C++ 使用最新 MySQL Connector/C++(X DevAPI)+ CMake 完整教程
  • 力扣 30 天 JavaScript 挑战 第38天 (第九题)学习了 语句表达式的区别 高级函数 promise async await 节流
  • 《P3623 [APIO2008] 免费道路》
  • Redis Set 类型详解:从基础命令到实战应用
  • git实战(8)git高阶命令分析【结合使用场景】
  • 本地Docker部署开源Web相册图库Piwigo与在线远程访问实战方案
  • 如何优雅解决 OpenCV 分段错误(Segfault):子进程隔离实战
  • pig框架导入总结
  • 动手学深度学习(pytorch版):第六章节—卷积神经网络(1)从全连接层到卷积
  • 新能源汽车热管理仿真:蒙特卡洛助力神经网络训练
  • Text2SQL、ChatBI简介
  • [Vid-LLM] 功能分类体系 | 视频如何被“观看“ | LLM的主要作用
  • Flink2.0学习笔记:使用HikariCP 自定义sink实现数据库连接池化
  • 一键部署开源 Coze Studio
  • 第三阶段数据库-9:循环,编号,游标,分页
  • 字节跳动开源Seed-OSS:36B参数模型以512K上下文与可控思考预算重新定义AI实用主义