-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot.py
59 lines (44 loc) · 2.25 KB
/
chatbot.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
52
53
54
55
56
57
58
59
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationalRetrievalChain
from langchain.prompts.prompt import PromptTemplate
from langchain.callbacks import get_openai_callback
# fix Error: module 'langchain' has no attribute 'verbose'
import langchain
langchain.verbose = False
conversation_history = {}
class Chatbot:
def __init__(self, model_name, temperature, vectors):
self.model_name = model_name
self.temperature = temperature
self.vectors = vectors
qa_template = """
You are a helpful AI assistant named Dobby. The user gives you a file its content is represented by the following pieces of context, use them to answer the question at the end.
If you don't know the answer, just say you don't know. Do NOT try to make up an answer.
If the question is not related to the context, politely respond that you are tuned to only answer questions that are related to the context.
Use as much detail as possible when responding.
context: {context}
=========
question: {question}
======
"""
QA_PROMPT = PromptTemplate(template=qa_template, input_variables=["context", "question"])
def conversational_chat(self, query, chat_id):
"""
Start a conversational chat with a model via Langchain
:param id1:
"""
llm = ChatOpenAI(model_name=self.model_name, temperature=self.temperature)
retriever = self.vectors.as_retriever()
chain = ConversationalRetrievalChain.from_llm(llm=llm,
retriever=retriever, verbose=True, return_source_documents=True,
max_tokens_limit=4097,
combine_docs_chain_kwargs={'prompt': self.QA_PROMPT})
chain_input = {"question": query, "chat_history": st.session_state["history"]}
result = chain(chain_input)
# count_tokens_chain(chain, chain_input)
return result["answer"]
def count_tokens_chain(chain, query):
with get_openai_callback() as cb:
result = chain.run(query)
st.write(f'###### Tokens used in this conversation : {cb.total_tokens} tokens')
return result