-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChat Bot
118 lines (90 loc) · 4.1 KB
/
Chat Bot
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
108
109
110
111
112
113
114
115
116
117
118
## download all the lib and create a donenv file where you can store the API key
import os
import streamlit as st
from streamlit_chat import message
from langchain.chains import ConversationalRetrievalChain
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.llms import CTransformers
from langchain.llms import Replicate
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import FAISS
from langchain.memory import ConversationBufferMemory
from langchain.document_loaders import PyPDFLoader
from langchain.document_loaders import TextLoader
from langchain.document_loaders import Docx2txtLoader
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from dotenv import load_dotenv
import tempfile
load_dotenv()
def initialize_state():
if " histroy " not in st.session_state:
st.session_state['history'] = []
if "generated" not in st.session_state:
st.session_state['generated'] = ["Hello! I am a chatbot trained on your documents. Ask me anything!"]
if "past" not in st.session_state:
st.session_state['past'] = ["Hey!"]
def conversation_chat(query, chain, history):
result = chain({"question": query, "chat_history": history})
history.append((query, result["answer"]))
return result["answer"]
def display_chat_history(chain):
reply_container = st.container()
container = st.container()
with container:
with st.form(key = "chat_form", clear_on_submit=True):
user_input = st.text_input("Question:", placeholder="Type your question here", key="input")
submit = st.form_submit_button(label="Submit", type="primary")
if submit and user_input:
with st.spinner("Generating response......"):
output = conversation_chat(user_input, chain, st.session_state['history'])
st.session_state['past'].append(user_input)
st.session_state['generated'].append(output)
if st.session_state['generated']:
with reply_container:
for i in range(len(st.session_state['generated'])):
message(st.session_state['past'][i], is_user = True, key = str(i) + "_user", avatar_style = 'thumbs')
message(st.session_state['generated'][i], key = str(i))
def create_conversational_chain(vector_store):
load_dotenv()
llm = Replicate(
streaming = True,
model = "meta/llama-2-70b-chat",
callbacks = [StreamingStdOutCallbackHandler()],
model_kwargs = { " temperature ": 0.5, "max_length": 500, "top_1": 1 }
)
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
chain = ConversationalRetrievalChain.from_llm(llm = llm, chain_type = "stuff", retriever = vector_store.as_retriever(search_kwargs = { "k": 2 }, memory=memory))
return chain
def main():
initialize_state()
st.title ("📚Document AI Chatbot")
st.sidebar.title("Upload documents")
uploaded_files = st.sidebar.file_uploader("Upload your docs, pdf etc", accept_multiple_files=True)
if uploaded_files:
text = []
for file in uploaded_files:
file_extension = os.path.splitext(file.name)[1]
with tempfile.NamedTemporaryFile(delete = False) as temp:
temp.write(file.read())
temp_path = temp.name
loader = None
if file_extension == ".pdf":
loader = PyPDFLoader(temp_path)
elif file_extension == ".docx" or file_extension == ".doc":
loader = Docx2txtLoader(temp_path)
elif file_extension == ".txt":
loader = TextLoader(temp_path)
if loader:
text.extend(loader.load())
os.remove(temp_path)
text_splitter = CharacterTextSplitter(separator = "\n", chunk_size = 1000, chunk_overlap = 100, length_function = len)
text_chunks = text_splitter.split_documents(text)
# Embeddings
embeddings = HuggingFaceEmbeddings(model_name = "sentence-transformers/all-MiniLM-L6-v2", model_kwargs = { 'device': "cpu"})
# Vector Store
vector_store = FAISS.from_documents(text_chunks, embedding = embeddings)
# Conversational Chain
chain = create_conversational_chain(vector_store)
display_chat_history(chain)
if __name__ == "__main__":
main()