-1

I am trying to create simpl chatbot with Langchain and want to store conversation history as well but it's throwing error while reading data.json while using qa_chain to render chain conversation.

instance of VectorStore expected (type=type_error.arbitrary_type; expected_arbitrary_type=VectorStore)

Here is my code:

_template = """Given the following conversation and a follow up question, 
rephrase the follow up question to be a standalone question.
Chat History:
{chat_history}
Follow Up Input: {question}
Standalone question:"""
CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)

template = """You are an AI assistant for answering questions about restaurant employee taking orders from customer where the user is assumed to be a customer 
who sould like to place an order from the resataurant menu.
start with greeting or responding
If you don't know the answer, just say "Hmm, I'm not sure.".
Don't try to make up an answer. 

Question: {question}
=========
{context}
=========
Answer in Markdown:"""
QA = PromptTemplate(template=template, input_variables=["question", "context"])


def get_chain(vectorstore):
    llm = OpenAI(temperature=0)
    qa_chain = ChatVectorDBChain.from_llm(
        llm,
        vectorstore,
        qa_prompt=QA,
        condense_question_prompt=CONDENSE_QUESTION_PROMPT,
        chain_type="stuff",
        combine_docs_chain_kwargs={'prompt': QA}
    )
    return qa_chain


if "generated" not in st.session_state:
    st.session_state["generated"] = []

if "past" not in st.session_state:
    st.session_state["past"] = []


def get_text(user_input):
    input_text = st.text_input("You: ", user_input, key="input")
    return input_text


chat_history = []


question = ""

user_input = get_text(question)

if user_input:
    question = user_input
    loader = TextLoader('data.json')
    index = VectorstoreIndexCreator().from_loaders([loader]) 
    documents = loader.load()
    embeddings = OpenAIEmbeddings()
    vectorstore = Chroma.from_documents(documents, embeddings).as_retriever()
    qa_chain = get_chain(vectorstore)
    result = qa_chain({"question": question, "chat_history": chat_history})
    

    chat_history.append((result["question"], result["answer"]))

    st.session_state.past.append(result["question"])
    st.session_state.generated.append(result["answer"])

    question = result["question"]

if st.session_state["generated"]:

    for i in range(len(st.session_state["generated"]) - 1, -1, -1):
        message(st.session_state["generated"][i], key=str(i))
        message(st.session_state["past"][i],
                is_user=True, key=str(i) + "_user")

Error:

  return cls(
           ^^^^
  File "pydantic\main.py", line 341, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 2 validation errors for ChatVectorDBChain
vectorstore
  instance of VectorStore expected (type=type_error.arbitrary_type; expected_arbitrary_type=VectorStore)
qa_prompt
  extra fields not permitted (type=value_error.extra)
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
BJ Coder
  • 350
  • 5
  • 16

0 Answers0