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

发布于:2025-08-20 ⋅ 阅读:(16) ⋅ 点赞:(0)

Langchain二次开发专栏

1. 安装依赖

pip install langchain langchain_community nebula3-python pandas pyjwt

2. 核心代码

2.1. 连接LLM与Nebula

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 

网站公告

今日签到

点亮在社区的每一天
去签到