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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from typing import List, Dict, Any, Generator
|
| 4 |
+
|
| 5 |
+
# Gradio & LLM/Agent Imports
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from gradio.components.chatbot import ChatMessage
|
| 8 |
+
import openai # Using the standard OpenAI client (will be configured for Nebius)
|
| 9 |
+
|
| 10 |
+
# LlamaIndex Imports (Essential for RAG and Agent Context)
|
| 11 |
+
# In a real app, you would import LlamaIndex components here, e.g.,
|
| 12 |
+
# from llama_index.core.agent import FunctionCallingAgentWorker
|
| 13 |
+
# from llama_index.core.query_engine import RetrieverQueryEngine
|
| 14 |
+
|
| 15 |
+
# --- CONFIGURATION PLACEHOLDERS ---
|
| 16 |
+
# You will need to configure the OpenAI client to point to the Nebius Token Factory endpoint.
|
| 17 |
+
# NEBIUS_API_KEY should be set as a Hugging Face Secret.
|
| 18 |
+
NEBIUS_API_KEY = os.getenv("NEBIUS_API_KEY")
|
| 19 |
+
NEBIUS_BASE_URL = os.getenv("NEBIUS_BASE_URL", "https://api.nebiustokenfactory.com/v1")
|
| 20 |
+
LLM_MODEL = "openai/gpt-oss-120b" # Using the specified Nebius model ID
|
| 21 |
+
|
| 22 |
+
# --- MOCK RAG SETUP (Needs replacement with real LlamaIndex code) ---
|
| 23 |
+
class MockRAG:
|
| 24 |
+
"""A placeholder for the real LlamaIndex RAG query engine."""
|
| 25 |
+
def query(self, query: str) -> str:
|
| 26 |
+
if "quantum physics" in query.lower() or "explain" in query.lower():
|
| 27 |
+
return "Quantum physics studies matter and energy at the most fundamental level, where classical mechanics breaks down. Key concepts include wave-particle duality and the uncertainty principle. This explanation is grounded in the latest 2025 educational materials."
|
| 28 |
+
else:
|
| 29 |
+
return "Hello! I'm your AI Tutor. Let's learn about that! Ask me to explain a topic, show a video, or give you a quiz!"
|
| 30 |
+
|
| 31 |
+
# --- MCP TOOL DEFINITIONS ---
|
| 32 |
+
|
| 33 |
+
def video_search_tool(topic: str) -> str:
|
| 34 |
+
"""
|
| 35 |
+
Searches YouTube for a highly rated educational video relevant to the user's topic.
|
| 36 |
+
|
| 37 |
+
Args:
|
| 38 |
+
topic (str): The subject or question provided by the user.
|
| 39 |
+
|
| 40 |
+
Returns:
|
| 41 |
+
str: An HTML snippet embedding the YouTube video in the chat.
|
| 42 |
+
"""
|
| 43 |
+
# Placeholder Logic: A real tool would call the YouTube API or Google Search
|
| 44 |
+
video_map = {
|
| 45 |
+
"quantum physics": "https://www.youtube.com/embed/gIWy5p4MZVE", # Kurzgesagt
|
| 46 |
+
"biology": "https://www.youtube.com/embed/A8gNf1p60pE", # Amoeba Sisters
|
| 47 |
+
"history": "https://www.youtube.com/embed/Yocja_N5s1I", # Crash Course
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
url = next((v for k, v in video_map.items() if k in topic.lower()), video_map["quantum physics"])
|
| 51 |
+
|
| 52 |
+
# Use HTML to embed the video directly in the Chatbot
|
| 53 |
+
html_embed = f"""
|
| 54 |
+
<div style="padding: 10px; border: 1px solid #e0e0e0; border-radius: 8px; background: #f9f9f9; max-width: 100%; border-radius: 8px;">
|
| 55 |
+
<p style="font-weight: bold; margin-bottom: 5px; color: #1f2937;">🎥 Tutor Found a Video Resource!</p>
|
| 56 |
+
<iframe width="100%" height="315" src="{url}" frameborder="0" allowfullscreen style="border-radius: 6px;"></iframe>
|
| 57 |
+
<p style="font-style: italic; font-size: 0.9em; margin-top: 5px; color: #6b7280;">Click play above to watch the lesson.</p>
|
| 58 |
+
</div>
|
| 59 |
+
"""
|
| 60 |
+
return html_embed
|
| 61 |
+
|
| 62 |
+
def quiz_generator_tool(topic: str) -> str:
|
| 63 |
+
"""
|
| 64 |
+
Generates a short, three-question multiple-choice quiz on a specific subject.
|
| 65 |
+
|
| 66 |
+
Args:
|
| 67 |
+
topic (str): The subject or knowledge area for the quiz.
|
| 68 |
+
|
| 69 |
+
Returns:
|
| 70 |
+
str: A Markdown/HTML formatted quiz for the user to answer.
|
| 71 |
+
"""
|
| 72 |
+
# Placeholder Logic: A real tool would use the LLM to generate the quiz JSON
|
| 73 |
+
quiz_markdown = """
|
| 74 |
+
<div style="background-color: #ecfdf5; padding: 15px; border-radius: 8px; border-left: 5px solid #059669; color: #065f46;">
|
| 75 |
+
<h4 style="margin-top: 0; color: #059669;">📝 Quiz Time: Understanding the Fundamentals!</h4>
|
| 76 |
+
<ol style="padding-left: 20px;">
|
| 77 |
+
<li style="margin-bottom: 10px;">**Which of these phenomena demonstrates wave-particle duality?**
|
| 78 |
+
<ul><li>A. An apple falling from a tree</li><li>B. The double-slit experiment</li><li>C. Water boiling at 100°C</li></ul>
|
| 79 |
+
</li>
|
| 80 |
+
<li style="margin-bottom: 10px;">**What does the Heisenberg Uncertainty Principle state?**
|
| 81 |
+
<ul><li>A. It's impossible to know a particle's position and velocity simultaneously.</li><li>B. Energy is conserved in a closed system.</li><li>C. Light travels fastest in a vacuum.</li></ul>
|
| 82 |
+
</li>
|
| 83 |
+
<li style="margin-bottom: 0;">**In quantum mechanics, what is an "orbital"?**
|
| 84 |
+
<ul><li>A. The fixed path of an electron around the nucleus</li><li>B. A region of space where an electron is most likely to be found</li><li>C. A subatomic particle of light</li></ul>
|
| 85 |
+
</li>
|
| 86 |
+
</ol>
|
| 87 |
+
<p style="font-style: italic; font-size: 0.85em; margin-top: 15px;">**Hint:** Your tutor will check your answers in the next message!</p>
|
| 88 |
+
</div>
|
| 89 |
+
"""
|
| 90 |
+
return quiz_markdown
|
| 91 |
+
|
| 92 |
+
# --- AGENT CORE (SIMULATED FOR TOOL-CALLING) ---
|
| 93 |
+
|
| 94 |
+
class NebiusAIAgent:
|
| 95 |
+
"""
|
| 96 |
+
Simulates the Agentic workflow using the OpenAI Client (configured for Nebius)
|
| 97 |
+
and LlamaIndex tools.
|
| 98 |
+
"""
|
| 99 |
+
def __init__(self, tools: List, rag_query_engine: Any):
|
| 100 |
+
self.rag_query_engine = rag_query_engine
|
| 101 |
+
self.tools = {t.__name__: t for t in tools}
|
| 102 |
+
self.tool_descriptions = [
|
| 103 |
+
{"type": "function", "function": {
|
| 104 |
+
"name": "video_search_tool",
|
| 105 |
+
"description": "Searches for an educational video to help the student learn a topic visually.",
|
| 106 |
+
"parameters": {"type": "object", "properties": {"topic": {"type": "string", "description": "The educational topic to search for."}}, "required": ["topic"]}
|
| 107 |
+
}},
|
| 108 |
+
{"type": "function", "function": {
|
| 109 |
+
"name": "quiz_generator_tool",
|
| 110 |
+
"description": "Generates a structured quiz (3-5 questions) to test the student's knowledge on a specific topic.",
|
| 111 |
+
"parameters": {"type": "object", "properties": {"topic": {"type": "string", "description": "The topic to base the quiz on."}}, "required": ["topic"]}
|
| 112 |
+
}}
|
| 113 |
+
]
|
| 114 |
+
|
| 115 |
+
# Initialize the OpenAI Client, pointing to Nebius Token Factory
|
| 116 |
+
self.client = openai.OpenAI(
|
| 117 |
+
api_key=NEBIUS_API_KEY,
|
| 118 |
+
base_url=NEBIUS_BASE_URL,
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
def stream_chat(self, prompt: str, history: List) -> Generator[str, None, None]:
|
| 122 |
+
|
| 123 |
+
# 1. Build Messages
|
| 124 |
+
messages = [{"role": "system", "content": "You are a patient, world-class AI Tutor. Use your available tools to find videos or create quizzes when requested. Otherwise, use your RAG knowledge to explain topics. Be encouraging and use markdown formatting."}]
|
| 125 |
+
# Convert history format
|
| 126 |
+
for user_msg, assistant_msg in history:
|
| 127 |
+
if user_msg: messages.append({"role": "user", "content": user_msg})
|
| 128 |
+
if assistant_msg: messages.append({"role": "assistant", "content": assistant_msg})
|
| 129 |
+
messages.append({"role": "user", "content": prompt})
|
| 130 |
+
|
| 131 |
+
try:
|
| 132 |
+
# 2. Call the LLM (Nebius via OpenAI client)
|
| 133 |
+
# NOTE: In a real app, you would stream the response using 'stream=True'
|
| 134 |
+
# and handle the tool call logic from the response object.
|
| 135 |
+
response = self.client.chat.completions.create(
|
| 136 |
+
model=LLM_MODEL,
|
| 137 |
+
messages=messages,
|
| 138 |
+
tools=self.tool_descriptions,
|
| 139 |
+
tool_choice="auto" # Let the model decide whether to call a tool
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
# --- MOCK TOOL CALLING LOGIC (Simplified) ---
|
| 143 |
+
# NOTE: For this mock, we manually check keywords to simulate tool calling
|
| 144 |
+
# to ensure the demo works without a live agent model that can interpret tool descriptions.
|
| 145 |
+
|
| 146 |
+
tool_call = None
|
| 147 |
+
if "video" in prompt.lower() or "show me" in prompt.lower():
|
| 148 |
+
tool_call = {"function": {"name": "video_search_tool", "arguments": json.dumps({"topic": prompt})}}
|
| 149 |
+
elif "quiz" in prompt.lower() or "test me" in prompt.lower():
|
| 150 |
+
tool_call = {"function": {"name": "quiz_generator_tool", "arguments": json.dumps({"topic": prompt})}}
|
| 151 |
+
|
| 152 |
+
if tool_call:
|
| 153 |
+
# Agent Thought: Tool Call
|
| 154 |
+
yield ChatMessage(role="assistant", content="", metadata={"title": f"🧠 Agent Thought: Decided to call **{tool_call['function']['name']}**."})
|
| 155 |
+
|
| 156 |
+
# Execute the tool (local mock execution)
|
| 157 |
+
tool_func = self.tools[tool_call['function']['name']]
|
| 158 |
+
args = json.loads(tool_call['function']['arguments'])
|
| 159 |
+
tool_output = tool_func(args['topic'])
|
| 160 |
+
|
| 161 |
+
# Display Tool Result
|
| 162 |
+
yield ChatMessage(role="assistant", content=tool_output, metadata={"title": "Tool Result"})
|
| 163 |
+
|
| 164 |
+
else:
|
| 165 |
+
# Agent Thought: RAG/General LLM
|
| 166 |
+
yield ChatMessage(role="assistant", content="", metadata={"title": "🧠 Agent Thought: Retrieving context from knowledge base (RAG)."})
|
| 167 |
+
|
| 168 |
+
# Simulate RAG and response synthesis
|
| 169 |
+
rag_result = self.rag_query_engine.query(prompt)
|
| 170 |
+
|
| 171 |
+
# Stream the final response
|
| 172 |
+
full_response = f"**Tutor's Explanation:**\n\n{rag_result}"
|
| 173 |
+
for chunk in full_response.split():
|
| 174 |
+
yield chunk + " "
|
| 175 |
+
|
| 176 |
+
except Exception as e:
|
| 177 |
+
error_message = f"🚨 **Error during chat processing:** Ensure your Nebius API Key and Base URL are correctly configured. Error: {e}"
|
| 178 |
+
for chunk in error_message.split():
|
| 179 |
+
yield chunk + " "
|
| 180 |
+
|
| 181 |
+
# --- INITIALIZATION ---
|
| 182 |
+
|
| 183 |
+
# 1. Initialize RAG
|
| 184 |
+
rag_knowledge_base = MockRAG()
|
| 185 |
+
|
| 186 |
+
# 2. Define the list of tools available to the Agent
|
| 187 |
+
available_tools = [video_search_tool, quiz_generator_tool]
|
| 188 |
+
|
| 189 |
+
# 3. Initialize the Agent Executor
|
| 190 |
+
agent_executor = NebiusAIAgent(available_tools, rag_knowledge_base)
|
| 191 |
+
|
| 192 |
+
# The function that the Gradio ChatInterface will call
|
| 193 |
+
def tutor_chat_function(message: str, history: List[List[str]]) -> Generator[str, None, None]:
|
| 194 |
+
# This block handles the streaming output from the agent
|
| 195 |
+
# Stream the full response to the Gradio Chatbot
|
| 196 |
+
response_stream = agent_executor.stream_chat(message, history)
|
| 197 |
+
for chunk in response_stream:
|
| 198 |
+
yield chunk
|
| 199 |
+
|
| 200 |
+
# --- GRADIO UI ---
|
| 201 |
+
|
| 202 |
+
with gr.Blocks(theme=gr.themes.Monochrome(), title="AI Tutor Agent (MCP in Action)") as demo:
|
| 203 |
+
gr.Markdown("# 🎓 AI Tutor Agent (Nebius/OpenAI Client & Gradio MCP)")
|
| 204 |
+
gr.Markdown(
|
| 205 |
+
"**Ask me anything about science, history, or tech!** "
|
| 206 |
+
"Try saying things like: *'Explain quantum physics'* or *'Test me on biology'* or *'Show me a video on the Cold War'*."
|
| 207 |
+
"<br>_This agent uses RAG for articles and is tool-enabled via MCP. **Remember to set your NEBIUS_API_KEY secret.**_"
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
# Use gr.ChatInterface for the main interaction
|
| 211 |
+
gr.ChatInterface(
|
| 212 |
+
fn=tutor_chat_function,
|
| 213 |
+
textbox=gr.Textbox(placeholder="What would you like to learn today?"),
|
| 214 |
+
chatbot=gr.Chatbot(
|
| 215 |
+
label="AI Tutor",
|
| 216 |
+
show_copy_button=True,
|
| 217 |
+
height=600,
|
| 218 |
+
# Added a class for mobile responsiveness
|
| 219 |
+
elem_classes=["min-h-[70vh]", "mobile-full-width"]
|
| 220 |
+
),
|
| 221 |
+
# Sets the initial user message and prompt for the user
|
| 222 |
+
examples=[
|
| 223 |
+
"Explain the water cycle in detail.",
|
| 224 |
+
"Give me a quiz on World War 2 history.",
|
| 225 |
+
"Show me a video about photosynthesis."
|
| 226 |
+
]
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
+
if __name__ == "__main__":
|
| 230 |
+
demo.launch()
|