Coder Social home page Coder Social logo

langchain_neo4j_rag_app's Introduction

Build an LLM RAG Chatbot With LangChain

This repo contains the source code for Build an LLM RAG Chatbot With LangChain.

Create a .env file in the root directory and add the following environment variables:

NEO4J_URI=<YOUR_NEO4J_URI>
NEO4J_USERNAME=<YOUR_NEO4J_USERNAME>
NEO4J_PASSWORD=<YOUR_NEO4J_PASSWORD>

OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>

HOSPITALS_CSV_PATH=https://raw.githubusercontent.com/hfhoffman1144/langchain_neo4j_rag_app/main/data/hospitals.csv
PAYERS_CSV_PATH=https://raw.githubusercontent.com/hfhoffman1144/langchain_neo4j_rag_app/main/data/payers.csv
PHYSICIANS_CSV_PATH=https://raw.githubusercontent.com/hfhoffman1144/langchain_neo4j_rag_app/main/data/physicians.csv
PATIENTS_CSV_PATH=https://raw.githubusercontent.com/hfhoffman1144/langchain_neo4j_rag_app/main/data/patients.csv
VISITS_CSV_PATH=https://raw.githubusercontent.com/hfhoffman1144/langchain_neo4j_rag_app/main/data/visits.csv
REVIEWS_CSV_PATH=https://raw.githubusercontent.com/hfhoffman1144/langchain_neo4j_rag_app/main/data/reviews.csv

CHATBOT_URL=http://host.docker.internal:8000/hospital-rag-agent

HOSPITAL_AGENT_MODEL=gpt-3.5-turbo-1106
HOSPITAL_CYPHER_MODEL=gpt-3.5-turbo-1106
HOSPITAL_QA_MODEL=gpt-3.5-turbo

The three NEO4J_ variables are used to connect to your Neo4j AuraDB instance. Follow the directions here to create a free instance.

The chatbot uses OpenAI LLMs, so you'll need to create an OpenAI API key and store it as OPENAI_API_KEY.

Once you have a running Neo4j instance, and have filled out all the environment variables in .env, you can run the entire project with Docker Compose. You can install Docker Compose by following these directions.

Once you've filled in all of the environment variables, set up a Neo4j AuraDB instance, and installed Docker Compose, open a terminal and run:

$ docker-compose up --build

After each container finishes building, you'll be able to access the chatbot api at http://localhost:8000/docs and the Streamlit app at http://localhost:8501/.

Demo

langchain_neo4j_rag_app's People

Contributors

hfhoffman1144 avatar

Stargazers

 avatar Alex avatar  avatar  avatar subete avatar  avatar ben stear avatar 黑月です avatar rizky avatar Fred Feng Coding Center avatar Gautam Ahuja avatar  avatar Byron avatar  avatar Bhaskar Jayaraman avatar Zhinong Li avatar  avatar Juexiao Zhou avatar  avatar Rahul V Ramesh avatar Joel avatar Anderson L Amaral avatar  avatar 王haha avatar Yetmens avatar  avatar Markus Gritsch avatar  avatar Kyle Rodriguez avatar Yangpor avatar  avatar Liu Chenyang avatar LambdaX avatar Dhurika Vijayanand avatar Bryan Lim avatar Giacomo Pracucci avatar  avatar RyzeNGrind avatar  avatar TonyLi avatar Sayantan Das avatar  avatar  avatar  avatar  avatar Huynh Anh Kiet avatar  avatar Kartikey Bhatnagar avatar Prashant Solanki avatar Zihan "Zach" Xing avatar Srinivas Venkatanarayanan avatar  avatar Yash Varshney avatar  avatar Anirban Basu avatar iambuffett avatar Anestis Kousis avatar  avatar tieli avatar Gordon Wright avatar Prasanth R avatar Donal Simmie avatar Cong avatar John Cole avatar Vassil Verguilov avatar Edson Andrade avatar  avatar Drew Burt avatar Sangmin Ahn avatar  avatar  avatar  avatar Hao-Ming Hsu avatar BuxianChen avatar  avatar  avatar  avatar Beckett avatar lightfield botanist avatar MJ Shin avatar  avatar  avatar Leo Lu avatar dgo2dance avatar David LOIRET avatar

Watchers

 avatar  avatar

langchain_neo4j_rag_app's Issues

IndexError at hospital_review_chain.py

@hfhoffman1144 Thank you for the amazing article. I am learning a lot.

I'm Currently at Step 4>Create a Neo4j Vector Chain from the article
I'm using Cohere chat and embed model instead of OpenAI because of RateLimits.
and I'm using following cohere models

HOSPITAL_QA_MODEL=command-r
COHERE_EMBED_MODEL=embed-english-light-v3.0

I tried to run review_chain in Python interpreter.
In the First run, I was able to get a response, then I closed the interpreter and created a new interpreter following the same steps I'm facing issue IndexError: list index out of range.

First Run:

>>> from chatbot_api.src.chains.hospital_review_chain import reviews_vector_chain
>>> query = """What have patients said about hospital efficiency?Mention details from specific reviews."""
>>> response = reviews_vector_chain.invoke(query)
>>> response.get("result")
"Patients have offered varied assessments of hospital efficiency based on their experiences. Here's a breakdown of the comments mentioned in the reviews:\n\n- Kim Franklin, a patient at Wallace-Hamilton Hospital, mentioned frustration over a lack of communication regarding her treatment plan. This absence of information left her feeling uninformed and disengaged from the medical process.\n\n- Kendra Carroll, who sought treatment at Schultz-Powers Hospital, praised the medical staff's knowledge and the excellent care she received. However, she criticized the slow administrative processes that resulted in unnecessary delays in her treatment. \n\n- Karen Fox, another patient at Schultz-Powers Hospital, had a more positive experience. She highlighted the staff's friendliness and efficiency and appreciated the prompt service she received during her stay. The swift attention and a clean hospital environment left her with a favorable impression.\n\n- Lisa Smith, also a patient at Schultz-Powers Hospital, noted the hospital's dedication to continuous improvement and its feedback mechanisms in place to enhance the patient experience.\n\nOverall, while some patients have experienced frustrations with administrative processes and communication gaps, others have received efficient and friendly care. The reviews suggest a somewhat mixed picture of hospital efficiency, with some hospitals excelling in certain areas and others needing improvement."
>>>
>>> query="what are overal emotions from patients?"
>>> response = reviews_vector_chain.invoke(query)
>>> response.get("result")
"Overall, the emotions expressed by patients in the reviews are mixed. \n\nStephanie Murray, whose physician was Alexis Nolan, appreciated the hospital's emphasis on patient comfort, which created a positive sentiment. However, the frequent changes in nursing staff left her with a sense of inconsistency regarding the quality of care, which could be seen as a negative emotion. \n\nJoshua Duncan, treated at Lewis-Nelson, had a more positive experience, feeling that the staff's empathy and kindness made a challenging time more manageable. The aging infrastructure of the hospital didn't seem to dampen his overall positive emotions regarding his stay.\n\nWendy Davis, who was under the care of Julie Long at Walton LLC, also expressed positive emotions about the top-notch medical care and compassionate staff. But the noise levels in her shared room impacted her rest, which likely influenced her ability to feel positive about her hospital experience. \n\nShelly Evans, Rhonda Diaz's patient at Cunningham and Sons, seemed to have a generally positive experience, too. She felt that the staff was compassionate and understanding, providing her with much-needed emotional support. Her only complaint was the limited visiting hours, which may have left her feeling isolated at times. \n\nSo, while there are some positive emotions regarding the care and empathy shown by staff across the hospitals, there are also sentiments of frustration and discomfort due to infrastructure issues, noise, and visiting restrictions. The consistency of care seems to be a significant factor in patient emotions, too."

Second Run:

>>> from chatbot_api.src.chains.hospital_review_chain import reviews_vector_chain
Retrying langchain_cohere.embeddings.CohereEmbeddings.embed_with_retry.<locals>._embed_with_retry in 4.0 seconds as it raised IndexError: list index out of range.
Retrying langchain_cohere.embeddings.CohereEmbeddings.embed_with_retry.<locals>._embed_with_retry in 4.0 seconds as it raised IndexError: list index out of range.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Misc\app\chatbot_api\src\chains\hospital_review_chain.py", line 16, in <module>
    neo4j_vector_index = Neo4jVector.from_existing_graph(
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Misc\app\env\Lib\site-packages\langchain_community\vectorstores\neo4j_vector.py", line 944, in from_existing_graph
    text_embeddings = embedding.embed_documents([el["text"] for el in data])
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Misc\app\env\Lib\site-packages\langchain_cohere\embeddings.py", line 140, in embed_documents
    return self.embed(texts, input_type="search_document")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Misc\app\env\Lib\site-packages\langchain_cohere\embeddings.py", line 107, in embed
    embeddings = self.embed_with_retry(
                 ^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Misc\app\env\Lib\site-packages\langchain_cohere\embeddings.py", line 89, in embed_with_retry
    return _embed_with_retry(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Misc\app\env\Lib\site-packages\tenacity\__init__.py", line 289, in wrapped_f
    return self(f, *args, **kw)
           ^^^^^^^^^^^^^^^^^^^^
  File "D:\Misc\app\env\Lib\site-packages\tenacity\__init__.py", line 379, in __call__
    do = self.iter(retry_state=retry_state)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Misc\app\env\Lib\site-packages\tenacity\__init__.py", line 325, in iter
    raise retry_exc.reraise()
          ^^^^^^^^^^^^^^^^^^^
  File "D:\Misc\app\env\Lib\site-packages\tenacity\__init__.py", line 158, in reraise
    raise self.last_attempt.result()
          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\concurrent\futures\_base.py", line 449, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\concurrent\futures\_base.py", line 401, in __get_result
    raise self._exception
  File "D:\Misc\app\env\Lib\site-packages\tenacity\__init__.py", line 382, in __call__
    result = fn(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^
  File "D:\Misc\app\env\Lib\site-packages\langchain_cohere\embeddings.py", line 87, in _embed_with_retry
    return self.client.embed(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Misc\app\env\Lib\site-packages\cohere\client.py", line 141, in embed
    return merge_embed_responses(responses)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Misc\app\env\Lib\site-packages\cohere\utils.py", line 187, in merge_embed_responses
    meta = merge_meta_field([response.meta for response in responses if response.meta])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Misc\app\env\Lib\site-packages\cohere\utils.py", line 167, in merge_meta_field
    api_version = metas[0].api_version
                  ~~~~~^^^
IndexError: list index out of range

I'm not able to debug why and where is error is occurring.

My Temporary Solution and Intuition:

I think RetrievalQA chain again tries to embed again whenever im calling

from chatbot_api.src.chains.hospital_review_chain import reviews_vector_chain

So I deleted all nodes and relations in Neo4j, Tried again in new interpreter voila it works for one time.Same thing repeats in new second run.

hospital_review_chain.py

import os
from langchain.vectorstores.neo4j_vector import Neo4jVector
from langchain_cohere import CohereEmbeddings
from langchain.chains import RetrievalQA
from langchain_cohere import ChatCohere
from langchain.prompts import (
    PromptTemplate,
    SystemMessagePromptTemplate,
    HumanMessagePromptTemplate,
    ChatPromptTemplate,
)

HOSPITAL_QA_MODEL = os.getenv("HOSPITAL_QA_MODEL")
COHERE_EMBED_MODEL=os.getenv("COHERE_EMBED_MODEL")

neo4j_vector_index = Neo4jVector.from_existing_graph(
    embedding=CohereEmbeddings(model=COHERE_EMBED_MODEL),
    url=os.getenv("NEO4J_URI"),
    username=os.getenv("NEO4J_USERNAME"),
    password=os.getenv("NEO4J_PASSWORD"),
    index_name="reviews",
    node_label="Review",
    text_node_properties=[
        "physician_name",
        "patient_name",
        "text",
        "hospital_name",
    ],
    embedding_node_property="embedding",
)

review_template = """Your job is to use patient
reviews to answer questions about their experience at a hospital. Use
the following context to answer questions. Be as detailed as possible, but
don't make up any information that's not from the context. If you don't know
an answer, say you don't know.
{context}
"""

review_system_prompt = SystemMessagePromptTemplate(
    prompt=PromptTemplate(input_variables=["context"], template=review_template)
)

review_human_prompt = HumanMessagePromptTemplate(
    prompt=PromptTemplate(input_variables=["question"], template="{question}")
)
messages = [review_system_prompt, review_human_prompt]

review_prompt = ChatPromptTemplate(
    input_variables=["context", "question"], messages=messages
)

reviews_vector_chain = RetrievalQA.from_chain_type(
    llm=ChatCohere(model=HOSPITAL_QA_MODEL, temperature=0),
    chain_type="stuff",
    retriever=neo4j_vector_index.as_retriever(k=12),
)
reviews_vector_chain.combine_documents_chain.llm_chain.prompt = review_prompt

I'm using following cohere models

HOSPITAL_QA_MODEL=command-r
COHERE_EMBED_MODEL=embed-english-light-v3.0

Thank you for taking the time to read this issue. I hope if someone could help me understand why this is happening and provide some guidance on how to overcome it.

ConnectionError to chatbot api? please kindly help

Everything seems fine util I start to chat with the chatbot UI.
An ConnectionError occurs. Here's the tracelog. Could you please kindly help?

ConnectionError: HTTPConnectionPool(host='host.docker.internal', port=8000): Max retries exceeded with url: /hospital-rag-agent (Caused by NameResolutionError("<urllib3.connection.HTTPConnection object at 0x7f84e026d1d0>: Failed to resolve 'host.docker.internal' ([Errno -2] Name or service not known)"))
Traceback:
File "/usr/local/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 534, in _run_script
exec(code, module.dict)
File "/app/main.py", line 102, in
response = requests.post(CHATBOT_URL, json=data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/requests/api.py", line 115, in post
return request("post", url, data=data, json=json, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/requests/api.py", line 59, in request
return session.request(method=method, url=url, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/requests/sessions.py", line 589, in request
resp = self.send(prep, **send_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/requests/sessions.py", line 703, in send
r = adapter.send(request, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/requests/adapters.py", line 519, in send
raise ConnectionError(e, request=request)

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.