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

【Langchain系列三】GraphGPT——LangChain+NebulaGraph+llm构建智能图数据库问答系统

【Langchain系列二】GraphGPT——LangChain+NebulaGraph+llm构建智能图数据库问答系统

  • 测试数据:https://docs.nebula-graph.io/2.0/basketballplayer-2.X.ngql
  • 具体Nebula环境(本地/在线试用)可参考适合小白的Nebula图数据库教程(部署安装、常用命令、数据迁移全系列教程)
  • 本文基于glm模型进行技术路线说明,可根据需要自行更换为其他模型

1. 安装依赖

pip install langchain langchain_community nebula3-python pandas pyjwt

2. 核心代码

2.1. 连接LLM与Nebula

  • 申请智谱清言的key:https://bigmodel.cn/usercenter/proj-mgmt/apikeys
  • GLM-4-Flash模型为免费模型,不消耗token,适合用于技术路线验证,正式试用中可按需更换为ollama本地模型或者其他在线模型
from langchain_community.chat_models.zhipuai import ChatZhipuAI
from langchain_community.graphs import NebulaGraph
os.environ["ZHIPUAI_API_KEY"] = "自己的key"
llm = ChatZhipuAI(model="GLM-4-Flash")
# ollama—glm4的效果也不错
# llm = ChatOllama(base_url="http://{ip}:{端口}", model="glm4:latest", temperature=0.5)
graph = NebulaGraph(space="工作空间名称",username="用户名",password="密码",address="ip地址",port=9669
)

2.2. 提示词

# 尝试在提示词加上“最后返回ngql语句,并用中文总结最终答案”,但效果不太稳定,因此ngql改成自己提取
answer_prompt = '''
You are an assistant that helps to form nice and human understandable answers.用中文做最后回答
The information part contains the provided information that you must use to construct an answer.
The provided information is authoritative, you must never doubt it or try to use your internal knowledge to correct it.
Make the answer sound as a response to the question. Do not mention that you based the result on the given information.
Here is an example:Question: Which managers own Neo4j stocks?
Context:[manager:CTL LLC, manager:JANE STREET GROUP LLC]
Helpful Answer: CTL LLC, JANE STREET GROUP LLC owns Neo4j stocks.Follow this example when generating answers.
If the provided information is empty, say that you don't know the answer.
Information:
{context}Question: {question}
Helpful Answer:
'''
# 注意特殊字符的转义,如{{、\"
prompt = '''Task: Generate NebulaGraph Cypher statement to query a graph database.
Instructions:
First, generate Cypher then convert it to NebulaGraph Cypher dialect (rather than standard):
1. note explicit label specification is not needed for edge properties, so it's e.name instead of e.`Bar`.name.But it requires explicit label specification only when referring to node properties: v.`Foo`.name instead of v.name.
2. it uses double equals sign for comparison: `==` rather than `=`
3. You should try translating Chinese to English, for instance, if
http://www.lryc.cn/news/624832.html

相关文章:

  • Swift + Xcode 开发环境搭建终极指南
  • 一个月内快速掌握蓝牙原理与应用的全面学习规划
  • 104、【OS】【Nuttx】【周边】文档构建渲染:安装 Sphinx 扩展(上)
  • Day7--滑动窗口与双指针--1695. 删除子数组的最大得分,2958. 最多 K 个重复元素的最长子数组,2024. 考试的最大困扰度
  • 负载均衡终极指南:从流量分发到云原生架构的核心解析
  • Apache IoTDB集群部署实战:1C2D架构的高性能时序数据库搭建与优化指南
  • 第4章-04-用WebDriver页面元素操作
  • onRequestHide at ORIGIN_CLIENT reason HIDE_SOFT_INPUT fromUser false
  • 告别 DOM 的旧时代:从零重塑 Web 渲染的未来
  • scikit-learn/sklearn学习|弹性网络ElasticNet解读
  • LINUX 818 shell:random;for for
  • 咨询进阶——解读咨询顾问技能模型
  • 2025 年世界职业院校技能大赛汽车制造与维修赛道高职组资讯整合
  • Unity开发中的浅拷贝与深拷贝
  • 做一个答题pk小程序多少钱?
  • Golang資源分享
  • USB基础 -- 字符串描述符 (String Descriptor) 系统整理文档
  • C++中内存池(Memory Pool)详解和完整示例
  • Mongodb(文档数据库)的安装与使用(文档的增删改查)
  • 可实时交互的AI生成世界,腾讯发布的AI框架Yan
  • 对象存储 COS 端到端质量系列 —— 终端网络诊断工具
  • EMC PCB 设计规范
  • 上汽通用牵手Momenta,别克至境L7全球首发搭载R6飞轮大模型
  • 用随机森林填补缺失值:原理、实现与实战
  • 深度学习必然用到的概率知识
  • 94、23种设计模式之工厂方法模式
  • Redis--day8--黑马点评--分布式锁(一)
  • 单片机驱动LCD显示模块LM6029BCW
  • 机器学习-决策树:从原理到实战的机器学习入门指南
  • LLM - windows下的Dify离线部署:从镜像打包到无网环境部署(亲测)