rag_lab / app.py
Narra123's picture
Update app.py
3447098 verified
"""
LAB 1: What is RAG? β€” Side-by-Side Comparison
================================================
Plain LLM vs RAG-powered LLM using Abacus.AI RouteLLM.
Deploy on HuggingFace Spaces with Gradio.
"""
import os
import gradio as gr
from openai import OpenAI
# Abacus.AI RouteLLM client (drop-in replacement for OpenAI)
client = OpenAI(
base_url="https://routellm.abacus.ai/v1",
api_key=os.getenv("ABACUS_API_KEY"),
)
MODEL = "gpt-4o-mini"
# Simulated private knowledge base
KNOWLEDGE_BASE = """
PavanCoaching AI Bootcamp starts on June 1, 2025.
The fee is β‚Ή15,000 for the full course.
The course covers Python, ML, LLMs, RAG, and Agents.
Instructor: Pavan Kumar. Contact: pavan@pavancoaching.org
Batch size is limited to 30 students.
"""
def plain_llm(question: str) -> str:
"""Ask the LLM WITHOUT any context."""
try:
response = client.chat.completions.create(
model=MODEL,
messages=[{"role": "user", "content": question}],
)
return response.choices[0].message.content
except Exception as e:
return f"❌ Error: {e}"
def rag_llm(question: str) -> str:
"""Ask the LLM WITH retrieved context."""
try:
response = client.chat.completions.create(
model=MODEL,
messages=[
{
"role": "system",
"content": (
"Answer ONLY using the context below.\n"
"If the answer is not in the context, say 'I don't know.'\n\n"
f"Context:\n{KNOWLEDGE_BASE}"
),
},
{"role": "user", "content": question},
],
temperature=0.1,
)
return response.choices[0].message.content
except Exception as e:
return f"❌ Error: {e}"
with gr.Blocks(title="Lab 1: RAG vs Plain LLM", theme=gr.themes.Soft()) as demo:
gr.Markdown("## πŸ§ͺ Lab 1: Plain LLM vs RAG-Powered LLM")
question = gr.Textbox(label="Your Question", value="When does the AI Bootcamp start?")
btn = gr.Button("πŸ” Compare", variant="primary")
with gr.Row():
out_plain = gr.Textbox(label="❌ Plain LLM (no context)", lines=8)
out_rag = gr.Textbox(label="βœ… RAG LLM (with context)", lines=8)
btn.click(fn=lambda q: (plain_llm(q), rag_llm(q)), inputs=question, outputs=[out_plain, out_rag])
demo.launch()