Spaces:
Runtime error
Runtime error
File size: 3,754 Bytes
2b5fd10 ae89023 2b5fd10 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | 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
}
|