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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b0f9cd
03375c9
 
 
 
28c0e17
42c445d
 
28c0e17
 
 
42c445d
74aa796
 
 
 
 
28c0e17
 
 
 
 
 
 
 
 
 
42c445d
03375c9
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import os
import pickle
import streamlit as st
import json
from pathlib import Path
from typing import Annotated, List, TypedDict, Dict, Any, Literal, Optional, NotRequired
import operator
import numpy as np
from scipy.spatial.distance import cosine
from dotenv import load_dotenv
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, SystemMessage
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder, PromptTemplate
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langchain_community.tools.arxiv.tool import ArxivQueryRun
from langchain.schema.output_parser import StrOutputParser
from langchain_core.documents import Document
from langchain_core.vectorstores import VectorStore
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.retrievers import BM25Retriever
from langchain.retrievers import EnsembleRetriever
from langchain.retrievers.contextual_compression import ContextualCompressionRetriever
from langchain_cohere import CohereRerank
from langgraph.graph import StateGraph, START, END
from langgraph.prebuilt import ToolNode

from pydantic import BaseModel, Field
import asyncio
import requests
from tavily import TavilyClient, AsyncTavilyClient
from langchain_community.retrievers import ArxivRetriever
from enum import Enum
from dataclasses import dataclass, fields
from langchain_core.runnables import RunnableConfig
from langchain.chat_models import init_chat_model
from langgraph.constants import Send
from langgraph.types import interrupt, Command
from IPython.display import Markdown, display
import uuid



# Debug function to print directory information at startup
def debug_startup_info():
    """Print debug information at startup to help identify file locations"""
    print("=" * 50)
    print("DEBUG STARTUP INFO")
    print("=" * 50)
    # Print current working directory
    print(f"Current working directory: {os.getcwd()}")
    # Check for the data directory
    print("\nChecking for data directory:")
    if os.path.exists("data"):
        print("Found 'data' directory in current directory")
        print(f"Contents: {os.listdir('data')}")
        if os.path.exists("data/processed_data"):
            print(f"Contents of data/processed_data: {os.listdir('data/processed_data')}")
    # Check common paths that might exist in Hugging Face Spaces
    common_paths = [
        "/data",
        "/repository",
        "/app",
        "/app/data",
        "/repository/data",
        "/app/repository",
        "AB_AI_RAG_Agent/data"
    ]
    print("\nChecking common paths:")
    for path in common_paths:
        if os.path.exists(path):
            print(f"Found path: {path}")
            print(f"Contents: {os.listdir(path)}")
            # Check for processed_data subdirectory
            processed_path = os.path.join(path, "processed_data")
            if os.path.exists(processed_path):
                print(f"Found processed_data at: {processed_path}")
                print(f"Contents: {os.listdir(processed_path)}")
    print("=" * 50)


# Run debug info at startup
debug_startup_info()

# Enable debugging for file paths
import os
DEBUG_FILE_PATHS = True

def debug_paths():
    if DEBUG_FILE_PATHS:
        print("Current working directory:", os.getcwd())
        print("Files in /data:", os.listdir("/data") if os.path.exists("/data") else "Not found")
        print("Files in /data/processed_data:", os.listdir("/data/processed_data") if os.path.exists("/data/processed_data") else "Not found")
        for path in ["/repository", "/app", "/app/data"]:
            if os.path.exists(path):
                print(f"Files in {path}:", os.listdir(path))

# Load environment variables
load_dotenv()


# Check for required API keys
required_keys = ["COHERE_API_KEY", "ANTHROPIC_API_KEY", "TAVILY_API_KEY"]
missing_keys = [key for key in required_keys if not os.environ.get(key)]
if missing_keys:
    st.error(f"Missing required API keys: {', '.join(missing_keys)}. Please set them as environment variables.")
    st.stop()


# Custom vector store implementation
class CustomVectorStore(VectorStore):
    def __init__(self, embedded_docs, embedding_model):
        self.embedded_docs = embedded_docs
        self.embedding_model = embedding_model

    def similarity_search_with_score(self, query, k=5):
        # Get the query embedding
        query_embedding = self.embedding_model.embed_query(query)
        # Calculate similarity scores
        results = []
        for doc in self.embedded_docs:
            # Calculate cosine similarity (1 - cosine distance)
            similarity = 1 - cosine(query_embedding, doc["embedding"])
            results.append((doc, similarity))
        # Sort by similarity score (highest first)
        results.sort(key=lambda x: x[1], reverse=True)
        # Convert to Document objects and return top k
        documents_with_scores = []
        for doc, score in results[:k]:
            document = Document(
                page_content=doc["text"],
                metadata=doc["metadata"]
            )
            documents_with_scores.append((document, score))
        return documents_with_scores

    def similarity_search(self, query, k=5):
        docs_with_scores = self.similarity_search_with_score(query, k)
        return [doc for doc, _ in docs_with_scores]


    @classmethod
    def from_texts(cls, texts, embedding, metadatas=None, **kwargs):
        """Implement required abstract method from VectorStore base class."""
        # Create embeddings for the texts
        embeddings = embedding.embed_documents(texts)
        # Create embedded docs format
        embedded_docs = []
        for i, (text, embedding_vector) in enumerate(zip(texts, embeddings)):
            metadata = metadatas[i] if metadatas else {}
            embedded_docs.append({
                "text": text,
                "embedding": embedding_vector,
                "metadata": metadata
            })
        # Return an instance with the embedded docs
        return cls(embedded_docs, embedding)


def find_processed_data():
    """Find the processed_data directory path"""
    possible_paths = [
        "data/processed_data",
        "../data/processed_data",
        "/data/processed_data",
        "/app/data/processed_data",
        "./data/processed_data",
        "/repository/data/processed_data",
        "AB_AI_RAG_Agent/data/processed_data"
    ]
    for path in possible_paths:
        if os.path.exists(path):
            required_files = ["chunks.pkl", "bm25_retriever.pkl", "embedding_info.json", "embedded_docs.pkl"]
            if all(os.path.exists(os.path.join(path, f)) for f in required_files):
                print(f"Found processed_data at: {path}")
                return path
    raise FileNotFoundError("Could not find processed_data directory with required files")



@st.cache_resource
def initialize_vectorstore():
    """Initialize the hybrid retriever system with Cohere reranking"""
    try:
        # Find processed data directory
        processed_data_path = find_processed_data()
        
        # Load documents
        with open(os.path.join(processed_data_path, "chunks.pkl"), "rb") as f:
            documents = pickle.load(f)
        
        # Load BM25 retriever
        with open(os.path.join(processed_data_path, "bm25_retriever.pkl"), "rb") as f:
            bm25_retriever = pickle.load(f)
        bm25_retriever.k = 5 
        
        # Load embedding model info
        with open(os.path.join(processed_data_path, "embedding_info.json"), "r") as f:
            embedding_info = json.load(f)
        
        # Load pre-computed embedded docs
        with open(os.path.join(processed_data_path, "embedded_docs.pkl"), "rb") as f:
            embedded_docs = pickle.load(f)
        
        # Initialize embedding model
        embedding_model = HuggingFaceEmbeddings(
            model_name=embedding_info["model_name"]
        )
        
        # Create custom vectorstore using pre-computed embeddings
        vectorstore = CustomVectorStore(embedded_docs, embedding_model)
        qdrant_retriever = vectorstore.as_retriever(search_kwargs={"k": 5})
        
        # Create hybrid retriever
        hybrid_retriever = EnsembleRetriever(
            retrievers=[qdrant_retriever, bm25_retriever],
            weights=[0.5, 0.5],
        )
        
        # Create Cohere reranker
        cohere_rerank = CohereRerank(
            model="rerank-english-v3.0",
            top_n=5,
        )
        
        reranker = ContextualCompressionRetriever(
            base_compressor=cohere_rerank,
            base_retriever=hybrid_retriever
        )
        
        print("Successfully initialized retriever system")
        return reranker, documents
    except Exception as e:
        st.error(f"Error initializing retrievers: {str(e)}")
        st.stop()


# Streamlit interface
st.markdown(
    "<h1>πŸ“Š A/B<sub><span style='color:green;'>AI</span></sub></h1>",
    unsafe_allow_html=True
)
st.markdown("""
A/B<sub><span style='color:green;'>AI</span></sub> is a specialized agent with 2 modes: Q&A Mode and Report-Generating Mode. The Q&A Mode answers your A/B Testing questions and the Report-Generating Mode generates comprehensive reports on your provided A/B testing topics. Both modes use a thorough collection of Ron Kohavi's work, including his book, papers, and LinkedIn posts. If the Q&A Mode can't answer your questions using this collection, it will then search arXiv. For each section of the Report-Generating Mode's report, if it can't answer your questions using this collection, it will then search arXiv. If that's not enough, it will finally search the web using Tavily. It provides ALL sources, section by section. Both modes have been trained to only respond based on the sources they retrieve. You can toggle between both modes using the sidebar on the left. Let's begin!
""", unsafe_allow_html=True)


# Initialize the system
try:
    # Show loading indicator
    loading_placeholder = st.empty()
    with loading_placeholder.container():
        import time
        for dots in [".", "..", "..."]:
            loading_placeholder.text(f"Loading{dots}")
            time.sleep(0.2)
    
    # Initialize components (but hide the details)
    vectorstore, chunks = initialize_vectorstore()

       
    # Clear loading indicator
    loading_placeholder.empty()
except Exception as e:
    st.error(f"Error initializing the system: {str(e)}")
    st.stop()

# Add mode toggle in sidebar
with st.sidebar:
    st.markdown("### A/B<sub><span style='color:green;'>AI</span></sub> Mode", unsafe_allow_html=True)
    mode_version = st.radio(
        "Choose Mode:",
        ["Q&A Mode", "Report-Generating Mode"],
        index=0  # Default to Q&A Mode
    )


# Define mode functions
def run_qa_mode():
    # Import Q&A system from app_1
    import sys
    import os
    sys.path.append(os.path.dirname(os.path.abspath(__file__)))
    
    from app_1 import initialize_qa_system
    
    # Initialize QA system
    qa_system = initialize_qa_system(vectorstore)
    
    # Initialize session state for chat history
    if "qa_messages" not in st.session_state:
        st.session_state.qa_messages = []


    # Clear and create chat area
    chat_container = st.container()
    with chat_container:
        # Display chat history
        for i, message in enumerate(st.session_state.qa_messages):
            if message["role"] == "user":
                st.chat_message("user").write(message["content"])
            else:
                with st.chat_message("assistant"):
                    st.write(message["content"])


    # Chat input - only for Q&A mode
    query = st.chat_input("Ask me anything about A/B Testing...", key="qa_mode_input")

    if query:
        # Display user message
        st.chat_message("user").write(query)
        st.session_state.qa_messages.append({"role": "user", "content": query})
        
        # Process query
        with st.spinner("Thinking..."):
            # Create a placeholder for streaming output
            with st.chat_message("assistant"):
                streaming_container = st.empty()
            
                # Create input state for the graph with streaming container
                input_state = {
                    "messages": [HumanMessage(content=query)],
                    "sources": [],
                    "follow_up_questions": [],
                    "streaming_container": streaming_container
                }
            
                # Execute graph
                result = qa_system.invoke(input_state)
            
                # Extract result
                answer = result["messages"][-1].content
                sources = result["sources"]
                follow_up_questions = result.get("follow_up_questions", [])

                # Process sources to remove duplicates and format properly
                unique_sources = set()
                sources_text = ""
                
                for source in sources:
                    if "type" in source and source["type"] == "arxiv_paper":
                        entry_id = source.get('Entry ID', '')
                        if entry_id:
                            arxiv_id = entry_id.split('/abs/')[-1].split('v')[0]
                            sources_text += f"- ArXiv paper: [{source['title']}](https://arxiv.org/abs/{arxiv_id})\n"
                        else:
                            sources_text += f"- ArXiv paper: {source['title']}\n"
                    else:
                        title = source['title'].replace('.pdf', '')
                        source_id = f"{title}|{source['section']}"
                        if source_id not in unique_sources:
                            unique_sources.add(source_id)
                            sources_text += f"- Ron Kohavi: {title}, Section: {source['section']}\n"

                # Final display with the complete answer and sources
                answers_and_sources = answer
                
                if "I don't know" not in answer:
                    if sources_text:
                        answers_and_sources += "\n\n" + "**Sources:**" + "\n\n" + sources_text
                    
                    if follow_up_questions:
                        follow_up_text = "\n\n**Follow-up Questions:**\n\n"
                        for i, question in enumerate(follow_up_questions):
                            follow_up_text += f"{i+1}. {question}\n"
                        answers_and_sources += follow_up_text
                    
                streaming_container.markdown(answers_and_sources)
        
        # Save to chat history
        st.session_state.qa_messages.append({
            "role": "assistant", 
            "content": answers_and_sources,
            "sources": sources,
            "follow_up_questions": follow_up_questions
        })

def run_report_mode():
    # Import report system from app_2
    import sys
    import os
    sys.path.append(os.path.dirname(os.path.abspath(__file__)))
    
    from app_2 import initialize_report_system
    import asyncio
    
    # Initialize report system
    report_system = initialize_report_system(vectorstore)
    
    # Initialize session state for chat history
    if "report_messages" not in st.session_state:
        st.session_state.report_messages = []


    # Clear and create chat area
    chat_container = st.container()
    with chat_container:
        # Display chat history
        for i, message in enumerate(st.session_state.report_messages):
            if message["role"] == "user":
                st.chat_message("user").write(message["content"])
            else:
                with st.chat_message("assistant"):
                    st.write(message["content"])


    # Chat input - only for Report mode
    query = st.chat_input("Please give me a topic on anything regarding A/B Testing...", key="report_mode_input")

    if query:
        # Display user message
        st.chat_message("user").write(query)
        st.session_state.report_messages.append({"role": "user", "content": query})

        # Create assistant container immediately
        with st.chat_message("assistant"):
            report_placeholder = st.empty()
        
        # Start new report generation
        def start_new_report(topic, report_placeholder):
            """Start a new report generation process"""
            with st.spinner("Generating comprehensive report...This may take about 3-7 minutes."):
                
                # Create input state
                input_state = {"topic": topic}
                
                # Run graph to completion
                try:
                    config = {}

                    # Use asyncio.run to handle async function
                    async def run_graph_to_completion(input_state, config):
                        """Run the graph to completion"""
                        result = await report_system.ainvoke(input_state, config)
                        return result
                    
                    result = asyncio.run(run_graph_to_completion(input_state, config))
                    
                    if result.get("ab_testing_check") == False:
                        # Not AB testing related
                        response = result.get("final_report", "This topic is not related to A/B testing.")
                        report_placeholder.markdown(response)
                        return response
                    else:
                        # AB testing related - show final report
                        final_report = result.get("final_report", "")
                        if final_report:
                            final_content = f"## πŸ“„ Final Report\n\n{final_report}"
                            report_placeholder.markdown(final_content)
                            return final_content
                        else:
                            error_msg = "No report was generated."
                            report_placeholder.error(error_msg)
                            return None
                            
                except Exception as e:
                    error_msg = f"Error generating report: {str(e)}"
                    report_placeholder.error(error_msg)
                    return None
        
        # Start new report generation with placeholder
        final_content = start_new_report(query, report_placeholder)
        
        # Add to session state only after completion
        if final_content:
            st.session_state.report_messages.append({
                "role": "assistant", 
                "content": final_content
            })

# Track mode changes without using st.rerun()
if "current_mode" not in st.session_state:
    st.session_state.current_mode = mode_version

# Only update mode if it actually changed
if st.session_state.current_mode != mode_version:
    st.session_state.current_mode = mode_version

    # Clear conflicting widget/input states
    for key in ["qa_mode_input", "report_mode_input", "qa_messages", "report_messages"]:
        if key in st.session_state:
            del st.session_state[key]

# Call the appropriate mode function
if mode_version == "Q&A Mode":
    run_qa_mode()
else:  # Report-Generating Mode
    run_report_mode()