全词查询
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
)