File size: 6,473 Bytes
0dff71c
5df6164
 
 
0dff71c
 
5df6164
 
 
 
0dff71c
8531bb5
0dff71c
8531bb5
0dff71c
15e8427
5df6164
fd911f4
0dff71c
fd911f4
 
 
 
 
 
 
5df6164
 
 
 
 
 
 
df3b267
fd911f4
df3b267
 
 
 
 
 
 
 
 
 
8531bb5
0dff71c
 
 
2651156
 
 
 
8531bb5
2651156
 
0dff71c
2651156
 
0dff71c
2651156
 
0dff71c
2651156
 
0dff71c
 
5df6164
8531bb5
 
a52e98e
 
 
8531bb5
a52e98e
8531bb5
 
a52e98e
 
8531bb5
 
 
004193f
8531bb5
df3b267
8531bb5
a52e98e
8531bb5
df3b267
 
8531bb5
df3b267
 
8531bb5
 
 
 
 
 
 
 
 
 
 
 
 
 
5df6164
a52e98e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8531bb5
5df6164
8531bb5
 
 
2651156
 
 
8531bb5
 
2651156
8531bb5
 
2651156
8531bb5
 
0dff71c
2651156
8531bb5
0dff71c
 
 
2651156
 
0dff71c
2651156
8531bb5
 
2651156
8531bb5
 
5df6164
 
0dff71c
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import os
from dotenv import load_dotenv
import streamlit as st
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import FAISS
from langchain.prompts import PromptTemplate
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import PyPDFLoader, Docx2txtLoader, TextLoader, CSVLoader
import tempfile

# Load environment variables
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")

# Custom template to guide LLM model
custom_template = """
<s>[INST]You will start the conversation by greeting the user and introducing yourself as an Expert PDF documents analyze and assistant,
stating your availability for assistance. Your next step will depend on the user's response.
If the user expresses a need for assistance in pdf or document or txt or csv, you will ask them to describe their question.
However, if the user asks questions out of context from the knowledge base, you will immediately thank them and
say goodbye, ending the conversation. Remember to base your responses on the user's needs, providing accurate and
concise information regarding the data within the knowledge base. Your interactions should be professional and
focused, ensuring the user's queries are addressed efficiently without deviating from the set flows.
CHAT HISTORY: {chat_history}
QUESTION: {question}
ANSWER:
</s>[INST]
"""
CUSTOM_QUESTION_PROMPT = PromptTemplate.from_template(custom_template)

prompt_template = """<s>[INST]
You will answer from the provided files stored in knowledge base
CONTEXT: {context}
CHAT HISTORY: {chat_history}
QUESTION: {question}
ANSWER:
</s>[INST]

"""

prompt = PromptTemplate(template=prompt_template,
                        input_variables=['context', 'question', 'chat_history'])
# Function to extract text from documents
def get_document_text(uploaded_files):
    documents = []
    for uploaded_file in uploaded_files:
        with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[-1]) as temp_file:
            temp_file.write(uploaded_file.read())
            temp_file_path = temp_file.name
        
        # Load document based on its type
        if uploaded_file.name.endswith(".pdf"):
            loader = PyPDFLoader(temp_file_path)
            documents.extend(loader.load())
        elif uploaded_file.name.endswith(".docx") or uploaded_file.name.endswith(".doc"):
            loader = Docx2txtLoader(temp_file_path)
            documents.extend(loader.load())
        elif uploaded_file.name.endswith(".txt"):
            loader = TextLoader(temp_file_path)
            documents.extend(loader.load())
        elif uploaded_file.name.endswith(".csv"):
            loader = CSVLoader(temp_file_path)
            documents.extend(loader.load())
    return documents

# Split text into chunks
def get_chunks(documents):
    text_splitter = CharacterTextSplitter(separator="\n", chunk_size=600, chunk_overlap=200, length_function=len)
    chunks = [chunk for doc in documents for chunk in text_splitter.split_text(doc.page_content)]
    return chunks
# Create vectorstore

def get_vectorstore(chunks):
    embeddings = OpenAIEmbeddings()
    vectorstore = FAISS.from_texts(texts=chunks, embedding=embeddings)
    return vectorstore

# Create a conversational chain
def get_conversationchain(vectorstore):
    llm = ChatOpenAI(temperature=0.1, model_name='gpt-4o-mini')
    memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
    conversation_chain = ConversationalRetrievalChain.from_llm(
        llm=llm,
        retriever=vectorstore.as_retriever(search_type="similarity",search_kwargs={"k": 10}),
        condense_question_prompt=CUSTOM_QUESTION_PROMPT,
        memory=memory,  
        combine_docs_chain_kwargs={'prompt': prompt}
    )
    return conversation_chain
    
# Handle user questions and update chat history
def handle_question(question):
    if not st.session_state.conversation:
        st.warning("Please process your documents first.")
        return
    
    response = st.session_state.conversation({'question': question})
    st.session_state.chat_history = response['chat_history']
    
    for i, msg in enumerate(st.session_state.chat_history):
        if i % 2 == 0:
            st.markdown(f"**You:** {msg.content}")
        else:
            st.markdown(f"**Bot:** {msg.content}")

def handle_question(question):
    if not st.session_state.conversation:
        st.warning("Please process your documents first.")
        return
    
    # Get the response from the conversation chain
    response = st.session_state.conversation({'question': question})
    
    # Update chat history
    st.session_state.chat_history = response['chat_history']
    
    # Display chat history
    for i, msg in enumerate(st.session_state.chat_history):
        if i % 2 == 0:
            st.markdown(f"**You:** {msg.content}")
        else:
            st.markdown(f"**Bot:** {msg.content}")


# Main Streamlit app
def main():
    st.set_page_config(page_title="Chat with Documents", page_icon="πŸ“š")
    st.title("πŸ“š Chat with Your Documents")
    st.sidebar.title("Upload Your Files")
    
    if "conversation" not in st.session_state:
        st.session_state.conversation = None
    if "chat_history" not in st.session_state:
        st.session_state.chat_history = None
    
    # File uploader
    uploaded_files = st.sidebar.file_uploader("Upload your files (PDF, DOCX, TXT, CSV):", accept_multiple_files=True)
    
    # Process button
    if st.sidebar.button("Process Documents"):
        if uploaded_files:
            with st.spinner("Processing documents..."):
                # Extract text and create conversation chain
                raw_documents = get_document_text(uploaded_files)
                text_chunks = get_chunks(raw_documents)
                vectorstore = get_vectorstore(text_chunks)
                st.session_state.conversation = get_conversationchain(vectorstore)
                st.success("Documents processed successfully!")
        else:
            st.warning("Please upload at least one document.")
    
    # User input
    question = st.text_input("Ask a question about the uploaded documents:")
    if question:
        handle_question(question)

if __name__ == '__main__':
    main()