Coder Social home page Coder Social logo

llm-chatbot-python's Introduction

llm-chatbot-python's People

Contributors

adam-cowley avatar martinohanlon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

llm-chatbot-python's Issues

Agent with GraphCypherQAChain tool failed to save into memory.

By following the course "Build a Neo4j-backed Chatbot using Python", when running the code the agent enters into the cypher_qa tool and generates the cypher code to query the database.

The problem is that when saving into memory it trows the following error:
raise validation_error
pydantic.v1.error_wrappers.ValidationError: 2 validation errors for AIMessage
content
str type expected (type=type_error.str)
content
value is not a valid list (type=type_error.list)

It is this a bug, the code is out-of-day as per the langchain verison?

Course material and code don't match up -> gives type error

It seems that there's either something missing or wrong in the course material, compared to your code in the /solutions folder. If you follow along the course, you end up with a type error at the end of the following section: https://graphacademy.neo4j.com/courses/llm-chatbot-python/3-tools/1-vector-tool/

Error: "pydantic.v1.error_wrappers.ValidationError: 2 validation errors for AIMessage
content
str type expected (type=type_error.str)
content
value is not a valid list (type=type_error.list)"

In your solutions/tools/vector.py you seem to have the generate_response(prompt) function and tools =[] defined in vectory.py file but nowhere in the course material they were moved there (up until the above section). They were originally put in the agent.py file.

I.e. there seems to be some kind of mismatch between your solutions code and the code outlined in the course material. Here's the code from the vector.py file if you follow along with the course material:

import streamlit as st
from langchain_community.vectorstores.neo4j_vector import Neo4jVector
from llm import llm, embeddings
from langchain.chains import RetrievalQA

neo4jvector = Neo4jVector.from_existing_index(
embeddings, # (1)
url=st.secrets["NEO4J_URI"], # (2)
username=st.secrets["NEO4J_USERNAME"], # (3)
password=st.secrets["NEO4J_PASSWORD"], # (4)
index_name="moviePlots", # (5)
node_label="Movie", # (6)
text_node_property="plot", # (7)
embedding_node_property="plotEmbedding", # (8)
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
"""
)

retriever = neo4jvector.as_retriever()

kg_qa = RetrievalQA.from_chain_type(
llm,
chain_type="stuff",
retriever=retriever,
)

Rebuilding code and db locally I get error

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[]

Agent using tool failed to add output into memory

By following the course "Build a Neo4j-backed Chatbot using Python", when running the code to use agent to call vector search tool, it triggered such error.

Traceback (most recent call last):
File "/Users/skywater/PycharmProjects/pythonProject/KG_LLM/llm-chatbot-python/tets.py", line 121, in
response = agent_executor.invoke({"input": prompt})
File "/Users/skywater/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/langchain/chains/base.py", line 164, in invoke
final_outputs: Dict[str, Any] = self.prep_outputs(
File "/Users/skywater/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/langchain/chains/base.py", line 440, in prep_outputs
self.memory.save_context(inputs, outputs)
File "/Users/skywater/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/langchain/memory/chat_memory.py", line 39, in save_context
self.chat_memory.add_ai_message(output_str)
File "/Users/skywater/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/langchain_core/chat_history.py", line 65, in add_ai_message
self.add_message(AIMessage(content=message))
File "/Users/skywater/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/langchain_core/load/serializable.py", line 107, in init
super().init(**kwargs)
File "pydantic/main.py", line 341, in pydantic.main.BaseModel.init
pydantic.error_wrappers.ValidationError: 2 validation errors for AIMessage
content
str type expected (type=type_error.str)
content
value is not a valid list (type=type_error.list)

As I troubleshooted, found the error was due to the output is a dict format, something like
{'query': query, 'result':result}, rather than string, making it failed to add in memory.
I think my code is exactly same with courses and this repo, anyone knows the reason and how to solve it.
My langchain version is 0.1.1

Solution bot repeating the input question

Hi guys, really cool course, thanks! I encountered something strange, when simply using the solution code, the bot would just return what I've typed in:

Bot: Hi, I'm the GraphAcademy Chatbot! How can I help you?

Me: Hey

Bot: Hey

Me: Who is the CEO of neo4j?

Bot: Who is the CEO of neo4j?

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.