forked from mpaepper/content-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_chat_app.py
51 lines (45 loc) · 1.75 KB
/
start_chat_app.py
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
import pickle
from langchain.prompts.prompt import PromptTemplate
from langchain.llms import OpenAI
from langchain.chains import ChatVectorDBChain
_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 machine learning
and technical blog posts. You are given the following extracted parts of
a long document and a question. Provide a conversational answer.
If you don't know the answer, just say "Hmm, I'm not sure.".
Don't try to make up an answer. If the question is not about
machine learning or technical topics, politely inform them that you are tuned
to only answer questions about machine learning and technical topics.
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,
)
return qa_chain
if __name__ == "__main__":
with open("faiss_store.pkl", "rb") as f:
vectorstore = pickle.load(f)
qa_chain = get_chain(vectorstore)
chat_history = []
print("Chat with the Paepper.com bot:")
while True:
print("Your question:")
question = input()
result = qa_chain({"question": question, "chat_history": chat_history})
chat_history.append((question, result["answer"]))
print(f"AI: {result['answer']}")