neo4j-graphacademy / llm-chatbot-python

https://graphacademy.neo4j.com/courses/llm-chatbot-python/
62 stars 165 forks source link

Rebuilding code and db locally I get error #13

Open jfkproductions opened 1 month ago

jfkproductions commented 1 month ago
2024-08-01 16:13:27.848 Uncaught app exception
Traceback (most recent call last):
  File "C:\Users\KraaiduToit\AppData\Local\Programs\Python\Python312\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 600, in _run_script
    exec(code, module.__dict__)
  File "C:\1.Pvt\1.Projects\llm-chatbot-python\bot.py", line 4, in <module>
    from agent import generate_response
  File "C:\1.Pvt\1.Projects\llm-chatbot-python\agent.py", line 13, in <module>
    from tools.vector import get_movie_plot
  File "C:\1.Pvt\1.Projects\llm-chatbot-python\tools\vector.py", line 19, in <module>
    neo4jvector = Neo4jVector.from_existing_index(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\KraaiduToit\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_community\vectorstores\neo4j_vector.py", line 1209, in from_existing_index
    raise ValueError(
ValueError: The specified vector index name does not exist. Make sure to check if you spelled it correctly
2024-08-01 16:13:27.849 Uncaught app exception
Traceback (most recent call last):
  File "C:\Users\KraaiduToit\AppData\Local\Programs\Python\Python312\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 600, in _run_script
    exec(code, module.__dict__)
  File "C:\1.Pvt\1.Projects\llm-chatbot-python\bot.py", line 4, in <module>
    from agent import generate_response
ImportError: cannot import name 'generate_response' from 'agent' (C:\1.Pvt\1.Projects\llm-chatbot-python\agent.py)
2024-08-01 16:13:27.849 Uncaught app exception
Traceback (most recent call last):
  File "C:\Users\KraaiduToit\AppData\Local\Programs\Python\Python312\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 600, in _run_script
    exec(code, module.__dict__)
  File "C:\1.Pvt\1.Projects\llm-chatbot-python\bot.py", line 4, in <module>
    from agent import generate_response
ImportError: cannot import name 'generate_response' from 'agent' (C:\1.Pvt\1.Projects\llm-chatbot-python\agent.py)

which suggest the index does not exist if i do a show indexes I do see my index moviePlots did i recreate it wrongly if so how can I fix this

image

here is he vectopr.py I am using

import streamlit as st
from llm import llm, embeddings
from graph import graph

# tag::import_vector[]
from langchain_community.vectorstores.neo4j_vector import Neo4jVector
# end::import_vector[]
# tag::import_chain[]
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain.chains import create_retrieval_chain
# end::import_chain[]

# tag::import_chat_prompt[]
from langchain_core.prompts import ChatPromptTemplate
# end::import_chat_prompt[]

# tag::vector[]
neo4jvector = Neo4jVector.from_existing_index(
    embeddings,                              # <1>
    graph=graph,                             # <2>
    index_name="moviePlots",                 # <3>
    node_label="Movie",                      # <4>
    text_node_property="plot",               # <5>
    embedding_node_property="plotEmbedding", # <6>
    retrieval_query="""
RETURN
    node.plot AS text,
    score,
    {
        title: node.title,
        directors: [ (person)-[:DIRECTED]->(node) | person.name ],
        actors: [ (person)-[r:ACTED_IN]->(node) | [person.name, r.role] ],
        tmdbId: node.tmdbId,
        source: 'https://www.themoviedb.org/movie/'+ node.tmdbId
    } AS metadata
"""
)
# end::vector[]

# tag::retriever[]
retriever = neo4jvector.as_retriever()
# end::retriever[]

# tag::prompt[]
instructions = (
    "Use the given context to answer the question."
    "If you don't know the answer, say you don't know."
    "Context: {context}"
)

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", instructions),
        ("human", "{input}"),
    ]
)
# end::prompt[]

# tag::chain[]
question_answer_chain = create_stuff_documents_chain(llm, prompt)
plot_retriever = create_retrieval_chain(
    retriever, 
    question_answer_chain
)
# end::chain[]

# tag::get_movie_plot[]
def get_movie_plot(input):
    return plot_retriever.invoke({"input": input})
# end::get_movie_plot[]
martinohanlon commented 1 month ago

I have edited your issue to format the code. It was really difficult to read otherwise.

martinohanlon commented 1 month ago

The code and index both look ok. Are you sure your Python program is connecting to the same database?