利用neo4j构建就以咨询知识图谱,通过智普ai实现用户问题理解,实现知识图谱查询


import pandas as pd
from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraph
from py2neo.matching import RelationshipMatcher
from py2neo import NodeMatch
df = pd.read_excel('doctor.xlsx')
df1 = pd.read_excel('hospital.xlsx')
df2=pd.read_excel('title.xlsx')
uri = "http://localhost:7474"
user = "neo4j"
password = "neo4j123456"
graph = Graph(uri, auth=(user, password))
#查询所有标签为"doctor"和"hospital"的结点
nodes_matcher= NodeMatcher(graph)
relation_matcher= RelationshipMatcher(graph)



print(graph.schema.node_labels )
print(graph.schema.relationship_types)
nodes = nodes_matcher.match("Doctor").where(name='吴庆军').first()
node_hosp = nodes_matcher.match("Hospital").all()
relation=list(relation_matcher.match([nodes],r_type=None))
print(relation)
print(nodes)


from zhipuai import ZhipuAI
import re
from py2neo import Graph
client = ZhipuAI(api_key="-------------")  # 填写您自己的APIKey
# 创建一个Graph实例
graph = Graph("http://localhost:7474", auth=("neo4j", "neo4j123456"))
#提取关键词函数(例如提取问题中的政策名称)
def extract_policy(question):
    prompt = f"""
    请从{{{question}}}中抽取关键词(医生名称),仅保留医生名称的部分,医生名称必须被包含在 f"{{{question}}}" 中。
    例如: f"{{{question}}}"="这个问题属于疾病关系查询中的“{{xxx(医生)}}擅长疾病有哪些?”类型。询问的是关于“赵宏”的擅长疾病,即想要了解这个医生擅长哪方面的病症。"时输出:名称:赵宏,问题类型:疾病关系查询
    输出格式严格按照以下输出(注意:请不要输出其它多余内容):
    名称:xxx 
    问题类型:xxx
    """
    if not question:
        return jsonify({'error': 'Please provide both question and essay'}), 400


    response = client.chat.completions.create(
        model="glm-4",
        messages=[
            {
                "role": "user",
                "content": prompt
            }
        ]
    )

    output = response.choices[0].message.content#调用大模型


    #从输出的结果中提取出需要的政策名称
    #此处可以根据具体情况对输出做一些处理,比如需要的政策名称前后不能带有空格和引号
    output = output.strip()
    output = output.strip('"')
    output = output.strip('”')


    name = re.split('名称:|问题类型:|\n', output)[1]
    question_type = re.split('名称:|问题类型:|\n', output)[3]
    return name,question_type
question =  "赵宏医生擅长疾病有哪些?"
response = client.chat.completions.create(
model="glm-4",
messages=[
{
"role": "user",
"content": f"问题'{question}'是属于疾病关系查询:({{xxx(医生)}}擅长疾病有哪些?),就职关系查询:({{xxx(医生)}}就职于哪所医院?),科室关系查询:({{xxx(医生)}}科室为什么?)中的哪一类查询?如果没有匹配上模板,输出即为None"
}
]
)
question = response.choices[0].message.content
print(question)
name, question_type = extract_policy(question)
print(name)
print(question_type)


if question_type == "疾病关系查询" :
    cypher_query = (
        f"MATCH (m:Doctor{{name: '{name}'}})-[r:擅长疾病]->(n:specialty) RETURN n "

    )
    result = graph.run(cypher_query)
    for record in result:
        print(record)
    print(1)
elif "就职关系查询" == question_type:
    cypher_query = (
        f"MATCH (m:Doctor{{name: '{name}'}})-[r:就职于]->(n:Hospital) "
        f"RETURN n"
    )

    result = graph.run(cypher_query)
    for record in result:
        print(record)
    print(2)
else:
    cypher_query = (
        f"MATCH (m:Doctor{{name: '{name}'}})-[r:科室为]->(n:dept_name) "
        f"RETURN n"
    )

    result = graph.run(cypher_query)
    for record in result:
        print(record)
    print(3)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值