template ="""Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. Use three sentences maximum. Keep the answer as concise as possible. Always say "thanks for asking!" at the end of the answer. {context}Question: {question}Helpful Answer:"""QA_CHAIN_PROMPT = PromptTemplate( input_variables=["context", "question"], template=template,)
Run chain
question ="Is probability a class topic?"qa_chain = RetrievalQA.from_chain_type(llm, retriever=vectordb.as_retriever(), return_source_documents=True, chain_type_kwargs={"prompt": QA_CHAIN_PROMPT})result = qa_chain({"query": question})result["result"]
‘Yes, probability is a class topic. Thanks for asking!’
question ="Is probability a class topic?"result = qa({"question": question})
result['answer']
‘Yes, probability is a topic that will be covered in this class. The instructor assumes familiarity with basic probability and statistics.’
Second question
question ="why are those prerequesites needed?"result = qa({"question": question})
result['answer']
‘Familiarity with basic probability and statistics is needed as prerequisites because the course will involve concepts and techniques from these fields. The instructor assumes that students already know what random variables, expectation, variance, and probability distributions are. This knowledge is necessary to understand and apply the machine learning algorithms and models that will be taught in the course. Additionally, some of the material covered in the course may require a refresher on probability and statistics, so the discussion sections will provide an opportunity to review these concepts.’
Feel free to copy this code and modify it to add your own features.
You can try alternate memory and retriever models by changing the configuration in load_db function and the convchain method. Panel and Param have many useful features and widgets you can use to extend the GUI.
Acknowledgments
This tutorial is mainly based on the excellent course “LangChain: Chat with Your DataI” provided by Harrison Chase from LangChain and Andrew Ng from DeepLearning.AI.
Panel based chatbot inspired by Sophia Yang, github
What’s next?
Congratulations! You have completed this tutorial 👍