Download app.py from Narra123/rag_lab: direct link, hf CLI and curl.
- Browser
- Download file 2.42 kB
-
https://huggingface.co/spaces/Narra123/rag_lab/resolve/main/app.py
- Command line
-
hf download hf://spaces/Narra123/rag_lab/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Narra123/rag_lab/resolve/main/app.py
2.42 kB
| """ | |
| 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() |