from langchain.chains import RetrievalQA, ConversationalRetrievalChain from langchain.prompts.prompt import PromptTemplate from langchain.vectorstores.base import VectorStoreRetriever from langchain.chat_models import ChatOpenAI from langchain.embeddings import OpenAIEmbeddings from langchain.vectorstores.faiss import FAISS from langchain.memory import ConversationBufferMemory import pickle _template = """Combine the chat history and follow up question into a standalone question. Chat History: {chat_history} Follow up question: {question}""" CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template) template = """You have to provide the list of properties to the user. You have information that contains `propertyId` field with each property. You have to only provide the `propertyId` value in an array if the user's query matches the information; otherwise, return an empty array `[]`. Question: {question}. Provide all property IDs that match the criteria. ========= {context} ========= Answer in Markdown:""" QA_PROMPT = PromptTemplate(template=template, input_variables=[ "question", "context"]) def load_retriever(): embeddings = OpenAIEmbeddings() vectorstore = FAISS.load_local("faiss_index", embeddings) retriever = VectorStoreRetriever(vectorstore=vectorstore) return retriever def get_basic_qa_chain(): llm = ChatOpenAI(model_name="gpt-4", temperature=0) retriever = load_retriever() memory = ConversationBufferMemory( memory_key="chat_history", return_messages=True) model = ConversationalRetrievalChain.from_llm( llm=llm, retriever=retriever, memory=memory, combine_docs_chain_kwargs={"prompt": QA_PROMPT}) return model def get_custom_prompt_qa_chain(): llm = ChatOpenAI(model_name="gpt-4", temperature=0) retriever = load_retriever() memory = ConversationBufferMemory( memory_key="chat_history", return_messages=True) # see: https://github.com/langchain-ai/langchain/issues/6635 # see: https://github.com/langchain-ai/langchain/issues/1497 model = ConversationalRetrievalChain.from_llm( llm=llm, retriever=retriever, memory=memory, combine_docs_chain_kwargs={"prompt": QA_PROMPT}) return model def get_condense_prompt_qa_chain(): llm = ChatOpenAI(model_name="gpt-4", temperature=0) retriever = load_retriever() memory = ConversationBufferMemory( memory_key="chat_history", return_messages=True) # see: https://github.com/langchain-ai/langchain/issues/5890 model = ConversationalRetrievalChain.from_llm( llm=llm, retriever=retriever, memory=memory, condense_question_prompt=CONDENSE_QUESTION_PROMPT, combine_docs_chain_kwargs={"prompt": QA_PROMPT}) return model def get_qa_with_sources_chain(): llm = ChatOpenAI(model_name="gpt-4", temperature=0) retriever = load_retriever() history = [] model = ConversationalRetrievalChain.from_llm( llm=llm, retriever=retriever, return_source_documents=True) def model_func(question): # bug: this doesn't work with the built-in memory # hacking around it for the tutorial # see: https://github.com/langchain-ai/langchain/issues/5630 new_input = {"question": question['question'], "chat_history": history} result = model(new_input) history.append((question['question'], result['answer'])) return result return model_func chain_options = { "basic": get_basic_qa_chain, "with_sources": get_qa_with_sources_chain, "custom_prompt": get_custom_prompt_qa_chain, "condense_prompt": get_condense_prompt_qa_chain }