File size: 11,284 Bytes
cebb474
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from langchain_community.document_loaders import PyPDFLoader, TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.embeddings import OpenAIEmbeddings, HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_openai import ChatOpenAI
from langchain_community.chat_models import ChatOllama
from langchain.chains import RetrievalQA
from langchain.prompts import PromptTemplate
import tempfile
import os
import time

# Initialize session state
if 'processed_data' not in st.session_state:
    st.session_state.processed_data = False
if 'vectorstore' not in st.session_state:
    st.session_state.vectorstore = None
if 'retriever' not in st.session_state:
    st.session_state.retriever = None
if 'chain' not in st.session_state:
    st.session_state.chain = None
if 'chat_history' not in st.session_state:
    st.session_state.chat_history = []

st.set_page_config(page_title="πŸ€– RAG Explorer", layout="wide")
st.title("πŸ€– Retrieval Augmented Generation Explorer")
st.markdown("""
Explore how RAG works by uploading documents, configuring the pipeline, and asking questions!
""")

# Main tabs
setup_tab, chat_tab, learn_tab = st.tabs(["πŸ› οΈ Setup RAG Pipeline", "πŸ’¬ Chat Interface", "πŸ“š Learning Center"])

with setup_tab:
    # Pipeline Configuration Section
    st.header("RAG Pipeline Configuration")
    
    # Document Processing
    doc_col, process_col = st.columns([1, 1])
    
    with doc_col:
        st.subheader("1️⃣ Document Upload")
        file_type = st.selectbox("Select File Type", ["PDF", "Text"])
        uploaded_file = st.file_uploader(
            "Upload your document",
            type=["pdf", "txt"],
            help="Upload a document to create the knowledge base"
        )
        
        if uploaded_file:
            try:
                with tempfile.NamedTemporaryFile(delete=False, suffix=f".{file_type.lower()}") as tmp_file:
                    tmp_file.write(uploaded_file.getvalue())
                    tmp_file_path = tmp_file.name
                
                loader = PyPDFLoader(tmp_file_path) if file_type == "PDF" else TextLoader(tmp_file_path)
                documents = loader.load()
                st.success("Document loaded successfully!")
                
                # Text splitting configuration
                st.subheader("2️⃣ Text Splitting")
                chunk_size = st.slider("Chunk Size", 100, 2000, 500)
                chunk_overlap = st.slider("Chunk Overlap", 0, 200, 50)
                
                text_splitter = RecursiveCharacterTextSplitter(
                    chunk_size=chunk_size,
                    chunk_overlap=chunk_overlap
                )
                splits = text_splitter.split_documents(documents)
                
                # Clean up temp file
                os.unlink(tmp_file_path)
                
                with st.expander("Preview Text Chunks"):
                    for i, chunk in enumerate(splits[:3]):
                        st.markdown(f"**Chunk {i+1}**")
                        st.write(chunk.page_content)
                        st.markdown("---")
                
                st.session_state.splits = splits
                
            except Exception as e:
                st.error(f"Error processing document: {str(e)}")
    
    with process_col:
        st.subheader("3️⃣ Embedding Configuration")
        embedding_type = st.selectbox(
            "Select Embeddings",
            ["OpenAI", "HuggingFace"],
            help="Choose the embedding model"
        )
        
        if embedding_type == "OpenAI":
            api_key = st.text_input("OpenAI API Key", type="password")
            if api_key:
                os.environ["OPENAI_API_KEY"] = api_key
                embeddings = OpenAIEmbeddings()
        else:
            model_name = st.selectbox(
                "Select HuggingFace Model",
                ["sentence-transformers/all-mpnet-base-v2", 
                 "sentence-transformers/all-MiniLM-L6-v2"]
            )
            embeddings = HuggingFaceEmbeddings(model_name=model_name)
        
        st.subheader("4️⃣ LLM Configuration")
        llm_type = st.selectbox(
            "Select Language Model",
            ["OpenAI", "Ollama"],
            help="Choose the Large Language Model"
        )
        
        if llm_type == "OpenAI":
            model_name = st.selectbox("Select Model", ["gpt-3.5-turbo", "gpt-4"])
            temperature = st.slider("Temperature", 0.0, 1.0, 0.7)
            if api_key:
                llm = ChatOpenAI(model_name=model_name, temperature=temperature)
        else:
            model_name = st.selectbox("Select Model", ["llama2", "mistral"])
            temperature = st.slider("Temperature", 0.0, 1.0, 0.7)
            llm = ChatOllama(model=model_name, temperature=temperature)
        
        if 'splits' in st.session_state:
            if st.button("Create RAG Pipeline"):
                with st.spinner("Creating vector store and RAG pipeline..."):
                    # Create vector store
                    vectorstore = FAISS.from_documents(
                        st.session_state.splits,
                        embeddings
                    )
                    retriever = vectorstore.as_retriever(
                        search_type="similarity",
                        search_kwargs={"k": 3}
                    )
                    
                    # Create RAG chain
                    template = """Use the following pieces of context to answer the question at the end. 
                    If you don't know the answer, just say that you don't know, don't try to make up an answer.
                    
                    {context}
                    
                    Question: {question}
                    Answer: """
                    
                    QA_CHAIN_PROMPT = PromptTemplate(
                        input_variables=["context", "question"],
                        template=template,
                    )
                    
                    chain = RetrievalQA.from_chain_type(
                        llm=llm,
                        chain_type="stuff",
                        retriever=retriever,
                        chain_type_kwargs={"prompt": QA_CHAIN_PROMPT}
                    )
                    
                    st.session_state.chain = chain
                    st.session_state.processed_data = True
                    st.success("RAG pipeline created successfully!")

with chat_tab:
    st.header("Chat with your Documents")
    
    if not st.session_state.processed_data:
        st.warning("Please set up the RAG pipeline first in the Setup tab!")
    else:
        # Chat interface
        st.markdown("### Ask questions about your documents")
        
        # Query input
        query = st.text_input("Enter your question:")
        
        if query:
            with st.spinner("Generating response..."):
                try:
                    response = st.session_state.chain.invoke(query)
                    
                    # Add to chat history
                    st.session_state.chat_history.append(("user", query))
                    st.session_state.chat_history.append(("assistant", response['result']))
                except Exception as e:
                    st.error(f"Error generating response: {str(e)}")
        
        # Display chat history
        st.markdown("### Chat History")
        for role, message in st.session_state.chat_history:
            if role == "user":
                st.markdown(f"**You:** {message}")
            else:
                st.markdown(f"**Assistant:** {message}")
            st.markdown("---")

with learn_tab:
    concept_tab, architecture_tab, tips_tab = st.tabs(["Core Concepts", "RAG Architecture", "Best Practices"])
    
    with concept_tab:
        st.markdown("""
        ### What is RAG?
        
        Retrieval Augmented Generation (RAG) is a technique that enhances Large Language Models by:
        1. Retrieving relevant information from a knowledge base
        2. Augmenting the prompt with this information
        3. Generating responses based on both the question and retrieved context
        
        ### Key Components
        
        1. **Document Loader**
           - Imports documents into the system
           - Supports various file formats
        
        2. **Text Splitter**
           - Breaks documents into manageable chunks
           - Maintains context while splitting
        
        3. **Embeddings**
           - Converts text into vector representations
           - Enables semantic search
        
        4. **Vector Store**
           - Stores and indexes embeddings
           - Enables efficient retrieval
        
        5. **Language Model**
           - Generates responses using retrieved context
           - Ensures accurate and relevant answers
        """)
    
    with architecture_tab:
        st.markdown("""
        ### RAG Pipeline Architecture
        
        ```mermaid
        graph LR
            A[Document] --> B[Text Splitter]
            B --> C[Embeddings]
            C --> D[Vector Store]
            E[Query] --> F[Embedding]
            F --> G[Retriever]
            D --> G
            G --> H[Context]
            H --> I[LLM]
            E --> I
            I --> J[Response]
        ```
        
        ### Data Flow
        
        1. **Document Processing**
           - Document β†’ Chunks β†’ Embeddings β†’ Vector Store
        
        2. **Query Processing**
           - Query β†’ Embedding β†’ Similarity Search β†’ Retrieved Context
        
        3. **Response Generation**
           - Context + Query β†’ LLM β†’ Generated Response
        """)
    
    with tips_tab:
        st.markdown("""
        ### RAG Best Practices
        
        1. **Document Processing**
           - Choose appropriate chunk sizes
           - Ensure sufficient chunk overlap
           - Maintain document metadata
        
        2. **Retrieval Strategy**
           - Tune the number of retrieved chunks
           - Consider hybrid search approaches
           - Implement relevance filtering
        
        3. **Prompt Engineering**
           - Design clear and specific prompts
           - Include system instructions
           - Handle edge cases gracefully
        
        4. **Performance Optimization**
           - Cache frequent queries
           - Batch process documents
           - Monitor resource usage
        
        5. **Quality Control**
           - Implement answer validation
           - Track retrieval quality
           - Monitor LLM output
        """)

# Sidebar
st.sidebar.header("πŸ“‹ Quick Guide")
st.sidebar.markdown("""
1. **Setup Pipeline**
   - Upload document
   - Configure text splitting
   - Set up embeddings
   - Choose LLM
   
2. **Ask Questions**
   - Switch to Chat tab
   - Enter your question
   - Review responses
   
3. **Learn More**
   - Explore concepts
   - Understand architecture
   - Review best practices
""")

# Footer
st.sidebar.markdown("---")
st.sidebar.markdown("Made with ❀️ using LangChain 0.3")