Closed yazanrisheh closed 6 months ago
🤖
Based on the error message you provided, it seems that the issue lies in the instantiation of the ConversationalRetrievalChain
class. The error message indicates that the retriever
parameter is not a valid dictionary.
In your code, you are passing new_knowledge_base
as the retriever
parameter. However, the retriever
parameter is expected to be an instance of BaseRetriever
. This is evident from the method signature:
@classmethod
def from_llm(
cls,
llm: BaseLanguageModel,
retriever: BaseRetriever,
condense_question_prompt: BasePromptTemplate = CONDENSE_QUESTION_PROMPT,
chain_type: str = "stuff",
verbose: bool = False,
condense_question_llm: Optional[BaseLanguageModel] = None,
combine_docs_chain_kwargs: Optional[Dict] = None,
callbacks: Callbacks = None,
**kwargs: Any,
) -> BaseConversationalRetrievalChain:
Here, the 'retriever' parameter is expected to be an instance of 'BaseRetriever'. You can find this in the LangChain codebase.
To resolve this issue, you should convert the Chroma
object to a retriever using the as_retriever()
method:
new_knowledge_base = Chroma(persist_directory = persist_directory, embedding_function = embeddings).as_retriever()
This should resolve the ValidationError
you are seeing. If you still encounter issues, please provide more details about your implementation of the Chroma
class and how you are loading the local files.
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
If you want to continue the conversation, start your reply with @dosu-bot.
I did do that and it did fix however when I am trying to run my code, it's not answering the questions based on my documents that I have from DirectoryLoader. How do I fix that?
Hi, @yazanrisheh,
I'm helping the LangChain team manage their backlog and am marking this issue as stale. From what I understand, the issue was initially raised due to a code error in the ConversationalRetrievalChain, which was resolved by my suggestion to convert the Chroma
object to a retriever using the as_retriever()
method. However, a new issue was raised regarding the code not answering questions based on the documents from DirectoryLoader.
Could you please confirm if this issue is still relevant to the latest version of the LangChain repository? If it is, please let the LangChain team know by commenting on the issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days.
Thank you for your understanding and cooperation.
Can someone help me fix this code please.
My error: Traceback (most recent call last): File "C:\Users\Asus\Documents\Vendolista\app2.py", line 130, in
main()
File "C:\Users\Asus\Documents\Vendolista\app2.py", line 99, in main
qa = ConversationalRetrievalChain.from_llm(
File "C:\Users\Asus\AppData\Local\Programs\Python\Python310\lib\site-packages\langchain\chains\conversational_retrieval\base.py", line 356, in from_llm
return cls(
File "C:\Users\Asus\AppData\Local\Programs\Python\Python310\lib\site-packages\langchain\load\serializable.py", line 75, in init
super().init(kwargs)
File "pydantic\main.py", line 341, in pydantic.main.BaseModel.init
pydantic.error_wrappers.ValidationError: 1 validation error for ConversationalRetrievalChain
retriever
value is not a valid dict (type=type_error.dict)
PS C:\Users\Asus\Documents\Vendolista> python app2.py
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████| 33/33 [00:02<00:00, 11.17it/s]
87 documents loaded
Traceback (most recent call last):
File "C:\Users\Asus\Documents\Vendolista\app2.py", line 130, in
main()
File "C:\Users\Asus\Documents\Vendolista\app2.py", line 99, in main
qa = ConversationalRetrievalChain.from_llm(
File "C:\Users\Asus\AppData\Local\Programs\Python\Python310\lib\site-packages\langchain\chains\conversational_retrieval\base.py", line 356, in from_llm
return cls(
File "C:\Users\Asus\AppData\Local\Programs\Python\Python310\lib\site-packages\langchain\load\serializable.py", line 75, in init
super().init( kwargs)
File "pydantic\main.py", line 341, in pydantic.main.BaseModel.init
pydantic.error_wrappers.ValidationError: 1 validation error for ConversationalRetrievalChain
retriever
value is not a valid dict (type=type_error.dict)
My code:
from dotenv import load_dotenv import csv import PyPDF2 from PyPDF2 import PdfReader from langchain.document_loaders import DirectoryLoader, PyPDFLoader from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.embeddings.openai import OpenAIEmbeddings from langchain.callbacks import get_openai_callback from langchain.chat_models import ChatOpenAI from langchain.chains import ConversationalRetrievalChain from langchain.memory import ConversationBufferMemory from langchain.prompts import PromptTemplate import time from langchain.vectorstores import Qdrant from langchain.vectorstores import Chroma from langchain.vectorstores import deeplake from langchain.chains.qa_with_sources import load_qa_with_sources_chain from langchain.callbacks import StreamingStdOutCallbackHandler import pandas as pd from docx import Document from nltk.tokenize import sent_tokenize, word_tokenize from collections import Counter from nltk.corpus import stopwords import os
def print_letter_by_letter(text): for char in text: print(char, end='', flush=True) time.sleep(0.02)
def main(): load_dotenv() my_activeloop_org_id = "yazanrisheh" my_activeloop_dataset_name = "langchain_course_customer_support" dataset_path = f"hub://{my_activeloop_org_id}/{my_activeloop_dataset_name}"
directory_path = input("Copy your directory path here or upload a file: ")
if name == 'main': main()