neo4j+LLM构建成品油&医疗跨领域知识图谱(初)&neo4j查询某个节点

全词查询

MATCH(n) WHERE n.name = '成品油' RETURN n

关键词查询:

MATCH (n) WHERE n.id CONTAINS '油' RETURN n

计算节点数

MATCH (n) RETURN count(n) AS number_of_nodes;

计算关系数:

MATCH ()-[r]->() RETURN count(r) AS number_of_relationships;

具体代码如下(仅做备份自查):

python 复制代码
from langchain_core.runnables import (
    RunnableBranch,
    RunnableLambda,
    RunnableParallel,
    RunnablePassthrough,
)
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.prompts.prompt import PromptTemplate
from langchain_core.pydantic_v1 import BaseModel, Field
from typing import Tuple, List, Optional
from langchain_core.messages import AIMessage, HumanMessage
from langchain_core.output_parsers import StrOutputParser
import os
from langchain_community.graphs import Neo4jGraph
from langchain.document_loaders import WikipediaLoader
from langchain.text_splitter import TokenTextSplitter
from langchain_openai import ChatOpenAI
from langchain_experimental.graph_transformers import LLMGraphTransformer
from neo4j import GraphDatabase
from yfiles_jupyter_graphs import GraphWidget
from langchain_community.vectorstores import Neo4jVector
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores.neo4j_vector import remove_lucene_chars
from langchain_core.runnables import ConfigurableField, RunnableParallel, RunnablePassthrough

try:
  import google.colab
  from google.colab import output
  output.enable_custom_widget_manager()
except:
  pass
python 复制代码
os.environ["OPENAI_API_KEY"] = "sk-**********"
os.environ["NEO4J_URI"] = "bolt://localhost:7687"
os.environ["NEO4J_USERNAME"] = "neo4j"
os.environ["NEO4J_PASSWORD"] = "123456cys"

graph = Neo4jGraph()
python 复制代码
# 假设你有一个本地文件路径
file_path = 'data/oil072801.txt'  # 替换为你的文件路径

# 读取本地文件内容
with open(file_path, 'r', encoding='utf-8') as file:
    raw_documents = file.read()

# 创建 Document 类,添加 metadata 属性
class Document:
    def __init__(self, content, metadata=None):
        self.page_content = content
        self.metadata = metadata  # 这里添加了 metadata 属性

# 创建文档对象,并设置 metadata 属性
documents = [Document(content, {'source': 'oil0728'}) for content in raw_documents.split('\n')]

# Define chunking strategy
text_splitter = TokenTextSplitter(chunk_size=512, chunk_overlap=24)
split_documents = text_splitter.split_documents(documents)

# Initialize the LLM
llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo-0125")  # gpt-4-0125-preview occasionally has issues
llm_transformer = LLMGraphTransformer(llm=llm)

# Transform documents to graph documents
graph_documents = llm_transformer.convert_to_graph_documents(split_documents)

# 存储到 neo4j
graph.add_graph_documents(
    graph_documents,
    baseEntityLabel=True,
    include_source=True
)
相关推荐
果冻人工智能22 分钟前
2025 年将颠覆商业的 8 大 AI 应用场景
人工智能·ai员工
代码不行的搬运工24 分钟前
神经网络12-Time-Series Transformer (TST)模型
人工智能·神经网络·transformer
石小石Orz26 分钟前
Three.js + AI:AI 算法生成 3D 萤火虫飞舞效果~
javascript·人工智能·算法
孤独且没人爱的纸鹤35 分钟前
【深度学习】:从人工神经网络的基础原理到循环神经网络的先进技术,跨越智能算法的关键发展阶段及其未来趋势,探索技术进步与应用挑战
人工智能·python·深度学习·机器学习·ai
阿_旭37 分钟前
TensorFlow构建CNN卷积神经网络模型的基本步骤:数据处理、模型构建、模型训练
人工智能·深度学习·cnn·tensorflow
羊小猪~~38 分钟前
tensorflow案例7--数据增强与测试集, 训练集, 验证集的构建
人工智能·python·深度学习·机器学习·cnn·tensorflow·neo4j
Sxiaocai39 分钟前
使用TensorFlow实现简化版 GoogLeNet 模型进行 MNIST 图像分类
分类·tensorflow·neo4j
极客代码1 小时前
【Python TensorFlow】进阶指南(续篇三)
开发语言·人工智能·python·深度学习·tensorflow
zhangfeng11331 小时前
pytorch 的交叉熵函数,多分类,二分类
人工智能·pytorch·分类
Seeklike1 小时前
11.22 深度学习-pytorch自动微分
人工智能·pytorch·深度学习