File size: 27,107 Bytes
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 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 |
import os
import pickle
import streamlit as st
import json
from pathlib import Path
from typing import Annotated, List, TypedDict, Dict, Any
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
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, END
from langgraph.prebuilt import ToolNode
# 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 = ["OPENAI_API_KEY", "COHERE_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()
# Define prompts
RAG_PROMPT = """
CONTEXT:
{context}
QUERY:
{question}
You are a helpful assistant. Use the available context to answer the question. Do not use your own knowledge! If you cannot answer the question based on the context, you must say "I don't know".
"""
REPHRASE_QUERY_PROMPT = """
QUERY:
{question}
You are a helpful assistant. Rephrase the provided query to be more specific and to the point in order to improve retrieval in our RAG pipeline about AB Testing.
"""
FOLLOW_UP_PROMPT = """
You are an expert question architect. Based ONLY on the final answer below, generate 3 concise, relevant follow-up questions that:
- Probe deeper into specific details mentioned
- Explore related concepts or implications
- Ask for practical applications or examples
- Do not repeat the final answer
Format output as JSON with a "questions" key containing the list. Never include markdown.
Final Answer:
{response}
JSON:
"""
# Define the GraphState for the LangGraph
class GraphState(TypedDict):
messages: Annotated[List[BaseMessage], operator.add]
sources: Annotated[List[Dict[str, Any]], operator.add] # Track all sources
follow_up_questions: List[str] # Only want the most recent follow up questions
# Initialize the AB Testing QA system
@st.cache_resource
def initialize_qa_system(_reranker):
"""Initialize the AB Testing QA system"""
# Create a retriever
retriever = _reranker
# Initialize prompt templates
rag_prompt = ChatPromptTemplate.from_template(RAG_PROMPT)
rephrase_query_prompt = ChatPromptTemplate.from_template(REPHRASE_QUERY_PROMPT)
follow_up_prompt = ChatPromptTemplate.from_template(FOLLOW_UP_PROMPT)
# Initialize models (with streaming enabled)
openai_chat_model = ChatOpenAI(model="gpt-4.1-mini", temperature=0, streaming=True)
# Use gpt-4.1-mini for improving latency
follow_up_llm = ChatOpenAI(model="gpt-4.1-mini", temperature=0.3)
# Define the RAG chain node
def rag_chain_node(state: GraphState) -> GraphState:
query = state["messages"][-1].content
# 1. Retrieve documents. It's a best practice to return contexts in ascending order
docs_descending = retriever.get_relevant_documents(query)
docs = docs_descending[::-1]
# 2. Extract sources from the documents
sources = []
for doc in docs:
source_path = doc.metadata.get("source", "")
filename = source_path.split("/")[-1] if "/" in source_path else source_path
sources.append({
"title": filename,
"section": doc.metadata.get("section_title", "unknown"),
})
# 3. Use a simplified RAG chain without retrieval
# Create context from documents
context = "\n\n".join([doc.page_content for doc in docs])
# Format the prompt with context and query
formatted_prompt = rag_prompt.format(context=context, question=query)
# Get a placeholder for streaming
if "streaming_container" in state:
streaming_container = state["streaming_container"]
full_response = []
# Stream the response
for chunk in openai_chat_model.stream(formatted_prompt):
content = chunk.content
full_response.append(content)
streaming_container.markdown("".join(full_response))
response_text = "".join(full_response)
else:
# If no streaming container provided, fall back to non-streaming
response = openai_chat_model.invoke(formatted_prompt)
response_text = StrOutputParser().invoke(response)
return {
"messages": [AIMessage(content=response_text)],
"sources": sources
}
# Define the tools
@tool
def retrieve_information(
query: Annotated[str, "query to ask the retrieve information tool"]
):
"""Use Retrieval Augmented Generation to retrieve information about AB Testing."""
# 1. Retrieve documents. It's a best practice to return contexts in ascending order
docs_descending = retriever.get_relevant_documents(query)
docs = docs_descending[::-1]
# 2. Extract and store sources
sources = []
for doc in docs:
source_path = doc.metadata.get("source", "")
filename = source_path.split("/")[-1] if "/" in source_path else source_path
sources.append({
"title": filename,
"section": doc.metadata.get("section_title", "unknown"),
})
# Store sources for later access
retrieve_information.last_sources = sources
# 3. Return just the formatted document contents
formatted_content = "\n\n".join([f"Retrieved Information: {i+1}\n{doc.page_content}"
for i, doc in enumerate(docs)])
return formatted_content
@tool
def retrieve_information_with_rephrased_query(
query: Annotated[str, "query to be rephrased before asking the retrieve information tool"]
):
"""This tool will intelligently rephrase your AB testing query and then will use Retrieval Augmented Generation to retrieve information about the rephrased query."""
# 1. Rephrase the query first
rephrased_query = rephrase_query_prompt.format(question=query)
rephrased_query = openai_chat_model.invoke(rephrased_query)
rephrased_query = StrOutputParser().invoke(rephrased_query)
# 2. Retrieve documents using the rephrased query. It's a best practice to return contexts in ascending order
docs_descending = retriever.get_relevant_documents(rephrased_query)
docs = docs_descending[::-1]
# 3. Extract and store sources
sources = []
for doc in docs:
source_path = doc.metadata.get("source", "")
filename = source_path.split("/")[-1] if "/" in source_path else source_path
sources.append({
"title": filename,
"section": doc.metadata.get("section_title", "unknown"),
})
# Store sources for later access
retrieve_information_with_rephrased_query.last_sources = sources
# 4. Return formatted content with rephrased query
formatted_content = f"Rephrased query: {rephrased_query}\n\n" + "\n\n".join(
[f"Retrieved Information: {i+1}\n{doc.page_content}" for i, doc in enumerate(docs)]
)
return formatted_content
# Define follow up questions node
def follow_up_questions_node(state: GraphState) -> GraphState:
# Get last AI response from messages
last_response = state["messages"][-1].content
# Format prompt using template
formatted_prompt = follow_up_prompt.format(response=last_response)
response = follow_up_llm.invoke(formatted_prompt)
response_text = StrOutputParser().invoke(response)
# Parse JSON output
try:
questions_data = json.loads(response_text)
follow_up_questions = questions_data.get("questions", [])[:3]
except Exception as e:
print(f"Error parsing follow-up questions: {e}")
follow_up_questions = []
return {
"follow_up_questions": follow_up_questions
}
# Create tool belt
tool_belt = [
retrieve_information,
retrieve_information_with_rephrased_query,
ArxivQueryRun(),
]
# Create tool node
tool_node = ToolNode(tool_belt)
# Setup agent model (with streaming)
model = ChatOpenAI(model="gpt-4.1", temperature=0, streaming=True)
model = model.bind_tools(tool_belt)
# Define model calling function
def call_model(state):
messages = state["messages"]
# Check if we have a streaming container
streaming_container = state.get("streaming_container", None)
# For streaming response
if streaming_container:
full_response = []
# Stream the response
for chunk in model.stream(messages):
if hasattr(chunk, "content") and chunk.content is not None:
content = chunk.content
full_response.append(content)
streaming_container.markdown("".join(full_response))
# Get the final response
if full_response:
response = AIMessage(content="".join(full_response))
else:
# Fall back to non-streaming if needed
response = model.invoke(messages)
else:
# Non-streaming fallback
response = model.invoke(messages)
# Extract sources if available by examining the last message
sources = []
if len(messages) > 0:
last_message = messages[-1]
if hasattr(last_message, 'content'):
content = last_message.content
# Check for specific patterns in the content
if isinstance(content, str):
if "Rephrased query:" in content and hasattr(retrieve_information_with_rephrased_query, "last_sources"):
sources = retrieve_information_with_rephrased_query.last_sources
elif "Retrieved Information:" in content and hasattr(retrieve_information, "last_sources"):
sources = retrieve_information.last_sources
elif "Title:" in content and "Authors:" in content: # ArxivQueryRun pattern
# Extract paper titles and IDs from ArXiv results
import re
titles = re.findall(r"Title: (.*?)$", content, re.MULTILINE)
# Try to extract the arxiv IDs - match both old and new format IDs
arxiv_ids = re.findall(r"URL: https://arxiv\.org/abs/([0-9v\.]+)", content)
sources = []
for i, title in enumerate(titles):
source = {"title": title, "type": "arxiv_paper"}
# Add arxiv_id if available
if i < len(arxiv_ids):
source["arxiv_id"] = arxiv_ids[i]
sources.append(source)
# Return both the response and sources
return {
"messages": [response],
"sources": sources
}
# Define continuation condition
def should_continue(state):
last_message = state["messages"][-1]
if last_message.tool_calls:
return "action"
return "follow_up_questions_from_llm"
# Define helpfulness check
def NonAB_Testing_or_helpful_RAG_or_continue(state):
initial_query = state["messages"][0]
final_response = state["messages"][-1]
prompt_template = """\
Given an initial query, determine if the initial query is related to AB Testing (even vaguely e.g. statistics, A/B testing, etc.) or not. If not related to AB Testing, return 'Y'. If related to AB Testing, then given the initial query and a final response, determine if the final response is extremely helpful or not. If extremely helpful, return 'Y'. If not extremely helpful, return 'N'.
Initial Query:
{initial_query}
Final Response:
{final_response}"""
prompt_template = PromptTemplate.from_template(prompt_template)
helpfulness_check_model = ChatOpenAI(model="gpt-4.1", temperature=0)
helpfulness_chain = prompt_template | helpfulness_check_model | StrOutputParser()
helpfulness_response = helpfulness_chain.invoke({
"initial_query": initial_query.content,
"final_response": final_response.content
})
if "Y" in helpfulness_response:
return "follow_up_questions_from_llm"
else:
return "agent"
# Create graph
graph = StateGraph(GraphState)
# Add nodes
graph.add_node("Initial_RAG", rag_chain_node)
graph.add_node("agent", call_model)
graph.add_node("action", tool_node)
graph.add_node("follow_up_questions_from_llm", follow_up_questions_node)
# Set entry point
graph.set_entry_point("Initial_RAG")
# Add edges
graph.add_conditional_edges(
"Initial_RAG",
NonAB_Testing_or_helpful_RAG_or_continue,
{
"agent": "agent",
"follow_up_questions_from_llm": "follow_up_questions_from_llm"
}
)
graph.add_conditional_edges(
"agent",
should_continue,
{
"action": "action",
"follow_up_questions_from_llm": "follow_up_questions_from_llm"
}
)
graph.add_edge("action", "agent")
graph.add_edge("follow_up_questions_from_llm", END)
# Compile graph
return graph.compile()
# 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 that answers your A/B Testing questions using a thorough collection of Ron Kohavi's work, including his book, papers, and LinkedIn posts. If A/B<sub><span style='color:green;'>AI</span></sub> can't answer your questions using this collection, it will then search Arxiv. It has been trained to only answer based on the sources it retrieves. 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()
qa_system = initialize_qa_system(vectorstore)
# Clear loading indicator
loading_placeholder.empty()
except Exception as e:
st.error(f"Error initializing the system: {str(e)}")
st.stop()
# Initialize session state for chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat history
for i, message in enumerate(st.session_state.messages):
if message["role"] == "user":
st.chat_message("user").write(message["content"])
else:
with st.chat_message("assistant"):
st.write(message["content"])
# Chat input
query = st.chat_input("Ask me anything about A/B Testing...")
if query:
# Display user message
st.chat_message("user").write(query)
st.session_state.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 # Pass the container for streaming
}
# 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":
# Extract arXiv ID from Entry ID metadata
entry_id = source.get('Entry ID', '') # This is the key field containing the ID
if entry_id:
# Extract arXiv ID from format like "http://arxiv.org/abs/2404.19647v1"
arxiv_id = entry_id.split('/abs/')[-1].split('v')[0] # Removes version suffix
sources_text += f"- ArXiv paper: [{source['title']}](https://arxiv.org/abs/{arxiv_id})\n"
else:
sources_text += f"- ArXiv paper: {source['title']}\n"
else:
# Handle retrieval sources (Ron Kohavi's work)
# Remove .pdf extension if present
title = source['title'].replace('.pdf', '')
# Create a unique identifier for this source
source_id = f"{title}|{source['section']}"
# Only add if not already added
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
# Only add sources and follow-up questions if answer is not "I don't know"
if "I don't know" not in answer:
if sources_text:
answers_and_sources += "\n\n" + "**Sources:**" + "\n\n" + sources_text
# Add follow-up questions if available
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 (still save sources for internal use, even if not displayed)
st.session_state.messages.append({
"role": "assistant",
"content": answers_and_sources,
"sources": sources,
"follow_up_questions": follow_up_questions
})
|