File size: 13,857 Bytes
a784f59
 
5107c88
 
 
6fe2a9f
5107c88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fe2a9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5107c88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71252ae
5107c88
 
 
 
 
 
 
 
 
 
 
 
6fe2a9f
5107c88
6fe2a9f
 
 
 
 
 
 
 
 
 
 
71252ae
 
6fe2a9f
 
 
 
 
 
 
 
 
 
 
5107c88
6fe2a9f
 
 
5107c88
6fe2a9f
 
 
 
5107c88
6fe2a9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71252ae
6fe2a9f
 
 
 
71252ae
6fe2a9f
 
 
 
 
 
5107c88
b5a1192
 
 
 
 
 
 
6fe2a9f
71252ae
6fe2a9f
 
 
 
 
 
 
 
 
 
 
 
 
5107c88
6fe2a9f
 
 
 
 
 
 
 
 
 
71252ae
 
 
 
 
 
 
 
 
 
 
 
5107c88
71252ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5107c88
 
6fe2a9f
 
 
 
 
 
 
 
 
 
 
 
 
 
5107c88
6fe2a9f
 
 
 
 
71252ae
6fe2a9f
 
 
 
 
 
 
 
 
 
 
 
 
71252ae
6fe2a9f
 
 
 
 
 
 
 
71252ae
 
6fe2a9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5107c88
6fe2a9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import os
os.system("pip install --upgrade pip")
import re
import time
import io

from io import StringIO
from typing import Any, Dict, List
#Modules to Import
import openai
import streamlit as st
from langchain import LLMChain, OpenAI
from langchain.agents import AgentExecutor, Tool, ZeroShotAgent
from langchain.chains import RetrievalQA
from langchain.chains.question_answering import load_qa_chain
from langchain.docstore.document import Document
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.llms import OpenAI
from langchain.memory import ConversationBufferMemory
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import VectorStore
from langchain.vectorstores.faiss import FAISS
from pypdf import PdfReader

import os

from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate

from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferWindowMemory
from langchain.memory import ConversationSummaryBufferMemory

from langchain import OpenAI, LLMChain, PromptTemplate
from langchain.vectorstores import Chroma
from langchain.document_loaders import TextLoader, PyPDFLoader
from langchain.chains import ConversationalRetrievalChain
from langchain.chains.summarize import load_summarize_chain
import tempfile

import warnings
warnings.filterwarnings('ignore')

@st.cache_data
def parse_pdf (file: io.BytesIO)-> List[str]:
    pdf = PdfReader(file)
    output = []
    for page in pdf.pages:

        text = page.extract_text()
        #Merge hyphenated words
        text = re.sub(r"(\w+)-\n(\w+)", "\1\2", text)
        # Fix newlines in the middle of sentences 
        text = re.sub(r"(?<!\n\s)\n(?!\s\n)", " ", text.strip())
        #Remove multiple newlines
        text = re.sub(r"\n\s*\n", "\n\n", text)
        
        output.append(text)
    return output

@st.cache_data
def text_to_docs(text: str) -> List [Document]:

    """Converts a string or list of strings to a list of Documents with metadata,"""

    if isinstance(text, str):
        #Take a single string as one page 
        text = [text]
    page_docs = [Document (page_content=page) for page in text]
    # Add page numbers as metadata 
    for i, doc in enumerate(page_docs): 

        doc.metadata["page"] = 1 + 1
    # Split pages into chunks 
    doc_chunks = []
    for doc in page_docs:
        text_splitter = RecursiveCharacterTextSplitter( 
            chunk_size=2500, 
            separators=["\n\n", "\n", ".", "!", "?", ",", " ", ""],
            chunk_overlap=0,
        )
        chunks = text_splitter.split_text(doc.page_content)
        for i, chunk in enumerate(chunks):
            doc = Document(
                page_content=chunk, metadata={"page": doc.metadata["page"], "chunk": 1}
            )
            # Add sources a metadata
            doc.metadata["source"] = f"{doc.metadata['page']}-{doc.metadata['chunk']}" 
            doc_chunks.append(doc)
    return doc_chunks
def tool(index):

    qa = RetrievalQA.from_chain_type(
            llm = OpenAI(openai_api_key = api),
            chain_type = "stuff",
            retriever = index.as_retriever()
        )
        # our tool
    tools = [
        Tool(
            name="State of Union QA System",
            func=qa.run,
            description="Useful for when you need to answer questions about the aspects asked.\
            Input may be a partial or fully formed question,\
                it also can be about some things else, use the chat history to reply the questions"
        )
    ]
    return tools,qa
def process(kind, tools, qa):
    if kind == "Sumarized":
        prefix=""""Have a conversation with a human, answering the human questions as best you can based on the context and memory available. \
                You have access to a single tool:"""
        suffix="""Begin!"
        {chat_history}
        Question: {input}
        {agent_scratchpad}"""

    elif kind == "Chat":
        prefix=""""Have a conversation with a human, answering the human questions as best you can \
            You have access to a single tool:"""

        suffix="""Begin!"
        {chat_history}
        the human just say: {input}
        {agent_scratchpad}"""

    prompt = ZeroShotAgent.create_prompt(
        tools,
        prefix=prefix,
        suffix=suffix,
        input_variables=["input", "chat_history", "agent_scratchpad"],
    )
    if "memory" not in st.session_state:
            st.session_state.memory = ConversationBufferMemory(memory_key ="chat_history")
            #Chain
            # ZeroShotAgent
    llm_chain = LLMChain(
        llm=OpenAI(
        temperature=0, openai_api_key=api, model_name="gpt-3.5-turbo"
        ),
        prompt=prompt,
    )
    agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True) 
    agent_chain = AgentExecutor.from_agent_and_tools(
        agent=agent, tools=tools, verbose=True, memory=st.session_state.memory
    )
    return agent_chain,llm_chain


option = st.sidebar.selectbox(
    'What do you want to ?',
    ('Sumarization','Chat'))

api = st.sidebar.text_input(
                    "Open api key",
                    type="password",
                    placeholder="sk-",
                    help="https://platform.openai.com/account/api-keys",
                )
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
# openai.api_key = "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" 
global openai_api_key
openai_api_key = api 
os.environ['OPENAI_API_KEY'] = openai_api_key

uploaded_file = st.sidebar.file_uploader(":blue[Upload]", type=["pdf"])
global agent_chain,llm_chain
if api:
    if option == "Sumarization":
    
        if uploaded_file:

            doc = parse_pdf(uploaded_file)

            pages = text_to_docs(doc)
            # pages
            if pages:
                with st.expander('Show page contents', expanded=False):
                    page_sel =st.number_input(
                        label="selected page", min_value=1, max_value=len(pages), step=1
                    )
                    st.write(pages[page_sel-1])
                    
                    
                    embeddings = OpenAIEmbeddings(openai_api_key = api)
                    # Indexing
                    # Save in a Vector DB_
                    with st.spinner("It's indexing. .."):
                        index = FAISS.from_documents(pages, embeddings)
                    
                    tools,qa = tool(index)
                    prefix=""""Have a conversation with a human, answering the human questions as best you can based on the context and memory available. \
                        He may ask some not about the context but just answer the the question with a short sentence"""
                    suffix="""Begin!"
                    {chat_history}
                    Question: {input}
                    {agent_scratchpad}"""
                    prompt = ZeroShotAgent.create_prompt(
                        tools,
                        prefix=prefix,
                        suffix=suffix,
                        input_variables=["input", "chat_history", "agent_scratchpad"],
                    )

                    if "memory" not in st.session_state:
                        st.session_state.memory = ConversationBufferMemory(memory_key ="chat_history")
                            #Chain
                            # ZeroShotAgent
                    llm_chain = LLMChain(
                        llm=OpenAI(
                        temperature=0, openai_api_key=api, model_name="gpt-3.5-turbo"
                        ),
                        prompt=prompt,
                    )
                    agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True) 
                    agent_chain = AgentExecutor.from_agent_and_tools(
                        agent=agent, tools=tools, verbose=True, memory=st.session_state.memory
                    )
                    # agent_chain,llm_chain = process("Sumarized",tools, qa)


                container = st.container()
                with container:
                    st.title("🤖 AI ChatBot")
                                
                # Initialize chat history
                if "messages" not in st.session_state:
                    st.session_state.messages = []
                # Display chat messages from history on app rerun
                for message in st.session_state.messages:
                    with st.chat_message(message["role"]):
                        st.markdown(message["content"])    


                if query := st.chat_input("Hey yo !!! Wazzups!"):
                
                    st.chat_message("user").markdown(query)
                    # Add user message to chat history
                    st.session_state.messages.append({"role": "user", "content": query})
                
                    # response=llm_chain.memory.chat_memory.add_user_message(prompt)

                    if len(api) == 0:
                        response = f"""I will answer the question "{query}" if you give the API key"""
                        # st.write(response)
                        # #f"Echo: {prompt}" get_completion(template_string) #
                        # Display assistant response in chat message container
                        with st.chat_message("assistant"):
                            st.markdown(response)
                        # Add assistant response to chat history
                        st.session_state.messages.append({"role": "assistant", "content": response})

                    else:
                            
                        with st.spinner("It's indexing. .."):
                            
                            response = agent_chain.run(query)
                        # st.write(response)
                        # #f"Echo: {prompt}" get_completion(template_string) #
                        # Display assistant response in chat message container
                        with st.chat_message("assistant"):
                            st.markdown(response)
                        # Add assistant response to chat history
                        st.session_state.messages.append({"role": "assistant", "content": response})
                        # with st.expander("History/Memory"):
                        #     st.write(st.session_state.memory)

    elif option == "Chat":

        def get_completion(prompt, model="gpt-3.5-turbo"):
            messages = [{"role": "user", "content": prompt}]
            response = openai.ChatCompletion.create(
                model=model,
                messages=messages,
                temperature=0, 
            )
            return response.choices[0].message["content"]


        chat = ChatOpenAI(temperature=0.0, max_tokens=20) 
        memory = ConversationBufferWindowMemory(k=15)      
        conversation = ConversationChain(
                    llm=chat, 
                    memory = memory,
                    verbose=False, 
        )


        def reply(message, custom_style):
            style = """ in a funny \
            and joke tone
            """
            if len(custom_style) > 0: style = custom_style

            template_string = f"""You are talking with a person \
            replying to the message\
            with a style that is {style}. \
            the person just say: {message}.
            """
            
            prompt_template = ChatPromptTemplate.from_template(template_string)

            bot_messages = prompt_template.format_messages(
                            style= style,
                            text= message)
            
            response = conversation.predict(input=message)


            return response

        def sumarization():
            pass

        def document_question(question):
            pass
        ask_about_doc = False
        with st.sidebar:
            st.subheader("How do you want your bot reply to your message ?")
            custom_style = st.text_input("Tell me here", placeholder="joke tone")


        container = st.container()
        with container:
            st.title("🤖 AI ChatBot")
        

        # Initialize chat history
        if "messages" not in st.session_state:
            st.session_state.messages = []

        # Display chat messages from history on app rerun
        for message in st.session_state.messages:
            with st.chat_message(message["role"]):
                st.markdown(message["content"])
        # React to user input
        if prompt := st.chat_input("What is up?"):
            # Display user message in chat message container
            st.chat_message("user").markdown(prompt)
            # Add user message to chat history
            st.session_state.messages.append({"role": "user", "content": prompt})
            
            with st.spinner("It's indexing. .."):
                response = reply(prompt,custom_style)
            # with st.spinner("It's indexing. .."):
            #     tools,qa = tool()
            #     process("chat", tools, qa)
            #     response = agent_chain.run(query)
            if memory not in st.session_state:
                st.session_state.memory = ConversationBufferWindowMemory(k=15)
            # response=llm_chain.memory.chat_memory.add_user_message(prompt)
            # st.write(memory.buffer)
            # #f"Echo: {prompt}" get_completion(template_string) #
            # Display assistant response in chat message container
            with st.chat_message("assistant"):
                st.markdown(response)
            # Add assistant response to chat history
            st.session_state.messages.append({"role": "assistant", "content": response})