[Python] Output consistently returns “I don’t know the answer” even when the context isn’t...

Discussão em 'Python' iniciado por Stack, Outubro 23, 2024.

  1. Stack

    Stack Membro Participativo

    I’ve been working on a project integrating LangChain, Google Generative AI (Gemini-1.5-pro), and Neo4j to build a Q&A system that answers questions by querying a Neo4j graph database.

    My setup is based on the GraphCypherQAChain from LangChain, but I'm hitting a snag where the LLM consistently returns “I don’t know the answer” even when the context isn’t empty and data seems to exist.

    Moreover, it works fine in Python but does not work in 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,
    },
    ],
    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();


    Output:


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


    Working Python code:

    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)


    Output:

    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

    Continue reading...

Compartilhe esta Página