You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LangChain+Gemini+Neo4j问答系统JS端异常:LLM返回‘不知道答案’

问题:LangChain + Gemini-1.5-Pro + Neo4j 问答系统JS版本失效

我正在开发一个集成LangChain、Google Gemini-1.5-Pro与Neo4j的问答系统,通过查询Neo4j图数据库回答问题。系统基于LangChain的GraphCypherQAChain搭建,但遇到异常:即使数据库有数据、上下文非空,LLM始终返回“I don’t know the answer”。Python版本代码运行正常,但JavaScript版本无法正常工作。

JavaScript 代码示例

// Neo4j credentials
const url = "";
const username = "neo4j";
const password = "";

// Initialize the model
const model = new ChatGoogleGenerativeAI({
  model: "gemini-1.5-pro",
  apiKey: "",
  temperature: 0,
  safetySettings: [
    {
      category: HarmCategory.HARM_CATEGORY_HARASSMENT,
      threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
    },
  ],
  verbose: true,
});

async function main() {
  try {
    const graph = await Neo4jGraph.initialize({
      url,
      username,
      password,
    });
    const chain = GraphCypherQAChain.fromLLM({
      llm: model,
      graph: graph,
    });

    const res = await chain.invoke({
      question: "What areas have the highest population density",
    });

    // Log the result of the query
    console.log(res);
  } catch (error) {
    console.error(
      "Error during graph initialization or query execution:",
      error,
    );
  }
}

main();

JavaScript 输出

{ result: "I don't know the answer. \n" }

可正常运行的Python代码

import getpass
import os
from langchain_community.graphs import Neo4jGraph
from langchain.chains import GraphCypherQAChain
from langchain_google_genai import (
    ChatGoogleGenerativeAI,
    HarmBlockThreshold,
    HarmCategory,
)

if "GOOGLE_API_KEY" not in os.environ:
    os.environ["GOOGLE_API_KEY"] = ""

os.environ["NEO4J_URI"] = ""
os.environ["NEO4J_USERNAME"] = "neo4j"
os.environ["NEO4J_PASSWORD"] = ""
graph = Neo4jGraph()

graph.refresh_schema()
print(graph.schema)

llm = ChatGoogleGenerativeAI(
    allow_dangerous_requests=True,
    model="gemini-1.5-pro",
    temperature=0,
    max_tokens=None,
    timeout=None,
    max_retries=2,
    verbose=True,
    safety_settings={
        HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
    },

)
chain = GraphCypherQAChain.from_llm(graph=graph, llm=llm, verbose=True, allow_dangerous_requests=True)
response = chain.invoke({"query": "What areas have the highest population density"})
print(response)

Python 输出

Entering new GraphCypherQAChain chain...
Generated Cypher:
cypher
MATCH (l:Location)-[r:HAS_UNIQUE_POPULATION_DENSITY]->(pd:DensityData) RETURN l.name, pd.population_density ORDER BY pd.population_density DESC LIMIT 10

Full Context:
[{'l.name': 'Tower Hamlets', 'pd.population_density': 15702.9}, {'l.name': 'Islington', 'pd.population_density': 14574.9}, {'l.name': 'Hackney', 'pd.population_density': 13593.3}, {'l.name': 'Lambeth', 'pd.population_density': 11839.1}, {'l.name': 'Kensington and Chelsea', 'pd.population_density': 11816.5}, {'l.name': 'Hammersmith and Fulham', 'pd.population_density': 11161.1}, {'l.name': 'Southwark', 'pd.population_density': 10659.0}, {'l.name': 'Newham', 'pd.population_density': 9690.1}, {'l.name': 'Camden', 'pd.population_density': 9640.9}, {'l.name': 'Wandsworth', 'pd.population_density': 9560.0}]

 Finished chain.
{'query': 'What areas have the highest population density', 'result': 'Tower Hamlets, Islington, Hackney, Lambeth, Kensington and Chelsea, Hammersmith and Fulham, Southwark, Newham, Camden and Wandsworth have the highest population density. \n'}

Process finished with exit code 0

问题分析与修复方案

对比Python和JS代码的差异,以下几点是JS版本失效的关键原因:

  • Schema未刷新:Python代码调用graph.refresh_schema()主动拉取数据库结构,JS代码缺少这一步。GraphCypherQAChain依赖Schema生成正确的Cypher查询,无Schema会导致LLM无法生成有效查询。
    修复:在JS初始化Neo4jGraph后添加await graph.refreshSchema();

  • 参数名称不匹配:Python中chain.invoke传入{"query": "..."},而JS中用{"question": "..."}。LangChain JS版本的GraphCypherQAChain期望输入参数为query,参数名错误会导致链无法处理提问。
    修复:将chain.invoke的参数改为{ query: "What areas have the highest population density" }

  • 缺少危险请求许可:Python中LLM和Chain都设置了allow_dangerous_requests=True,Gemini生成Cypher可能被判定为危险操作,JS未设置该参数会导致生成受限。
    修复:在ChatGoogleGenerativeAI初始化时添加allowDangerousRequests: true,同时在GraphCypherQAChain.fromLLM中添加allowDangerousRequests: true

  • 安全设置差异:Python放宽了危险内容拦截阈值,JS仅设置骚扰内容拦截,可能导致Cypher生成被误拦截。
    修复:补充危险内容的安全设置,参考Python版本调整。

修改后的JS代码示例

// Neo4j credentials
const url = "";
const username = "neo4j";
const password = "";

// Initialize the model
const model = new ChatGoogleGenerativeAI({
  model: "gemini-1.5-pro",
  apiKey: "",
  temperature: 0,
  safetySettings: [
    {
      category: HarmCategory.HARM_CATEGORY_HARASSMENT,
      threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
    },
    {
      category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
      threshold: HarmBlockThreshold.BLOCK_NONE,
    },
  ],
  verbose: true,
  allowDangerousRequests: true,
});

async function main() {
  try {
    const graph = await Neo4jGraph.initialize({
      url,
      username,
      password,
    });
    await graph.refreshSchema();
    const chain = GraphCypherQAChain.fromLLM({
      llm: model,
      graph: graph,
      verbose: true,
      allowDangerousRequests: true,
    });

    const res = await chain.invoke({
      query: "What areas have the highest population density",
    });

    console.log(res);
  } catch (error) {
    console.error(
      "Error during graph initialization or query execution:",
      error,
    );
  }
}

main();

内容的提问来源于stack exchange,提问作者Okvictory234

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 00:59:58