@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.