Spaces:
Sleeping
Sleeping
File size: 17,390 Bytes
5fd9547 |
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 |
#!/usr/bin/env python3
"""
ToGMAL Chat Demo with MCP Tool Integration
==========================================
Interactive chat demo where a free LLM can call MCP tools to provide
informed responses about prompt difficulty, safety analysis, and more.
Features:
- Chat with Mistral-7B-Instruct (free via HuggingFace Inference API)
- LLM can call MCP tools to analyze prompts and assess difficulty
- Transparent tool calling with results shown to user
- No API key required (uses public Inference API)
"""
import gradio as gr
import json
import os
import re
from pathlib import Path
from typing import List, Dict, Tuple, Optional
from benchmark_vector_db import BenchmarkVectorDB
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Initialize the vector database (lazy loading)
db_path = Path("./data/benchmark_vector_db")
db = None
def get_db():
"""Lazy load the vector database."""
global db
if db is None:
try:
logger.info("Initializing BenchmarkVectorDB...")
db = BenchmarkVectorDB(
db_path=db_path,
embedding_model="all-MiniLM-L6-v2"
)
logger.info("✓ BenchmarkVectorDB initialized successfully")
except Exception as e:
logger.error(f"Failed to initialize BenchmarkVectorDB: {e}")
raise
return db
# ============================================================================
# MCP TOOL FUNCTIONS (Local implementations)
# ============================================================================
def tool_check_prompt_difficulty(prompt: str, k: int = 5) -> Dict:
"""
MCP Tool: Analyze prompt difficulty using vector database.
Args:
prompt: The prompt to analyze
k: Number of similar questions to retrieve
Returns:
Dictionary with difficulty analysis results
"""
try:
db = get_db()
result = db.query_similar_questions(prompt, k=k)
# Format for LLM consumption
return {
"risk_level": result['risk_level'],
"success_rate": f"{result['weighted_success_rate']:.1%}",
"avg_similarity": f"{result['avg_similarity']:.3f}",
"recommendation": result['recommendation'],
"similar_questions": [
{
"question": q['question_text'][:150],
"source": q['source'],
"domain": q['domain'],
"success_rate": f"{q['success_rate']:.1%}",
"similarity": f"{q['similarity']:.3f}"
}
for q in result['similar_questions'][:3] # Top 3 only
]
}
except Exception as e:
return {"error": f"Analysis failed: {str(e)}"}
def tool_analyze_prompt_safety(prompt: str) -> Dict:
"""
MCP Tool: Analyze prompt for safety issues (heuristic-based).
Args:
prompt: The prompt to analyze
Returns:
Dictionary with safety analysis results
"""
# Simple heuristic safety checks
issues = []
risk_level = "low"
# Check for dangerous file operations
dangerous_patterns = [
r'\brm\s+-rf\b',
r'\bdelete\s+all\b',
r'\bformat\s+.*drive\b',
r'\bdrop\s+database\b'
]
for pattern in dangerous_patterns:
if re.search(pattern, prompt, re.IGNORECASE):
issues.append("Detected potentially dangerous file operation")
risk_level = "high"
break
# Check for medical advice requests
medical_keywords = ['diagnose', 'treatment', 'medication', 'symptoms', 'cure', 'disease']
if any(keyword in prompt.lower() for keyword in medical_keywords):
issues.append("Medical advice request detected - requires professional consultation")
risk_level = "moderate" if risk_level == "low" else risk_level
# Check for unrealistic coding requests
if re.search(r'\b(build|create|write)\s+.*\b(\d{3,})\s+(lines|functions|classes)', prompt, re.IGNORECASE):
issues.append("Large-scale coding request - may exceed LLM capabilities")
risk_level = "moderate" if risk_level == "low" else risk_level
return {
"risk_level": risk_level,
"issues_found": len(issues),
"issues": issues if issues else ["No significant safety concerns detected"],
"recommendation": "Proceed with caution" if issues else "Prompt appears safe"
}
# ============================================================================
# LLM BACKEND (HuggingFace Inference API)
# ============================================================================
def call_llm_with_tools(
messages: List[Dict[str, str]],
available_tools: List[Dict],
model: str = "mistralai/Mistral-7B-Instruct-v0.2"
) -> Tuple[str, Optional[Dict]]:
"""
Call LLM with tool calling capability.
Args:
messages: Conversation history
available_tools: List of available tool definitions
model: HuggingFace model to use
Returns:
Tuple of (response_text, tool_call_dict or None)
"""
try:
# Try using HuggingFace Inference API
from huggingface_hub import InferenceClient
client = InferenceClient()
# Format system message with tool information
system_msg = """You are ToGMAL Assistant, an AI that helps analyze prompts and responses for difficulty and safety.
You have access to these tools:
1. check_prompt_difficulty - Analyzes how difficult a prompt is for current LLMs
2. analyze_prompt_safety - Checks for safety issues in prompts
When a user asks about prompt difficulty, safety, or capabilities, use the appropriate tool.
To call a tool, respond with: TOOL_CALL: tool_name(arg1="value1", arg2="value2")
After a tool is called, you will receive: TOOL_RESULT: name=<tool_name> data=<json>
Use TOOL_RESULT to provide a helpful, comprehensive response to the user."""
# Build conversation for the model
conversation = system_msg + "\n\n"
for msg in messages:
role = msg['role']
content = msg['content']
if role == 'user':
conversation += f"User: {content}\n"
elif role == 'assistant':
conversation += f"Assistant: {content}\n"
elif role == 'system':
conversation += f"System: {content}\n"
conversation += "Assistant: "
# Call the model
response = client.text_generation(
conversation,
model=model,
max_new_tokens=512,
temperature=0.7,
top_p=0.95,
do_sample=True
)
response_text = response.strip()
# Check if response contains a tool call
tool_call = None
if "TOOL_CALL:" in response_text:
# Extract tool call
match = re.search(r'TOOL_CALL:\s*(\w+)\((.*?)\)', response_text)
if match:
tool_name = match.group(1)
args_str = match.group(2)
# Parse arguments (simple key=value parsing)
args = {}
for arg in args_str.split(','):
if '=' in arg:
key, val = arg.split('=', 1)
key = key.strip()
val = val.strip().strip('"\'')
args[key] = val
tool_call = {
"name": tool_name,
"arguments": args
}
# Remove tool call from visible response
response_text = re.sub(r'TOOL_CALL:.*?\)', '', response_text).strip()
return response_text, tool_call
except ImportError:
# Fallback if huggingface_hub not available
return fallback_llm(messages, available_tools)
except Exception as e:
logger.error(f"LLM call failed: {e}")
return fallback_llm(messages, available_tools)
def fallback_llm(messages: List[Dict[str, str]], available_tools: List[Dict]) -> Tuple[str, Optional[Dict]]:
"""
Fallback LLM when HuggingFace API is unavailable.
Uses simple pattern matching to decide when to call tools.
"""
last_message = messages[-1]['content'].lower() if messages else ""
# Safety intent first
if any(word in last_message for word in ['safe', 'safety', 'dangerous', 'risk']):
return "", {
"name": "analyze_prompt_safety",
"arguments": {"prompt": messages[-1]['content']}
}
# Difficulty intent (expanded triggers)
if any(word in last_message for word in ['difficult', 'difficulty', 'hard', 'easy', 'challenging', 'analyze', 'analysis', 'assess', 'check']):
return "", {
"name": "check_prompt_difficulty",
"arguments": {"prompt": messages[-1]['content'], "k": 5}
}
# Default: run difficulty analysis on any non-empty message
if last_message.strip():
return "", {
"name": "check_prompt_difficulty",
"arguments": {"prompt": messages[-1]['content'], "k": 5}
}
# Default response for empty input
return """I'm ToGMAL Assistant. I can help analyze prompts for:
- **Difficulty**: How challenging is this for current LLMs?
- **Safety**: Are there any safety concerns?
Try asking me to analyze a prompt!""", None
# ============================================================================
# TOOL EXECUTION
# ============================================================================
AVAILABLE_TOOLS = [
{
"name": "check_prompt_difficulty",
"description": "Analyzes how difficult a prompt is for current LLMs based on benchmark similarity",
"parameters": {
"prompt": "The prompt to analyze",
"k": "Number of similar questions to retrieve (default: 5)"
}
},
{
"name": "analyze_prompt_safety",
"description": "Checks for safety issues in prompts using heuristic analysis",
"parameters": {
"prompt": "The prompt to analyze"
}
}
]
def execute_tool(tool_name: str, arguments: Dict) -> Dict:
"""Execute a tool and return results."""
if tool_name == "check_prompt_difficulty":
prompt = arguments.get("prompt", "")
try:
k = int(arguments.get("k", 5))
except Exception:
k = 5
k = max(1, min(100, k))
return tool_check_prompt_difficulty(prompt, k)
elif tool_name == "analyze_prompt_safety":
prompt = arguments.get("prompt", "")
return tool_analyze_prompt_safety(prompt)
else:
return {"error": f"Unknown tool: {tool_name}"}
# ============================================================================
# CHAT INTERFACE
# ============================================================================
def chat(
message: str,
history: List[Tuple[str, str]]
) -> Tuple[List[Tuple[str, str]], str]:
"""
Process a chat message with tool calling support.
Args:
message: User's message
history: Chat history as list of (user_msg, assistant_msg) tuples
Returns:
Updated history and tool call status
"""
# Convert history to messages format
messages = []
for user_msg, assistant_msg in history:
messages.append({"role": "user", "content": user_msg})
if assistant_msg:
messages.append({"role": "assistant", "content": assistant_msg})
# Add current message
messages.append({"role": "user", "content": message})
# Call LLM
response_text, tool_call = call_llm_with_tools(messages, AVAILABLE_TOOLS)
tool_status = ""
# Execute tool if requested
if tool_call:
tool_name = tool_call['name']
tool_args = tool_call['arguments']
tool_status = f"🛠️ **Calling tool:** `{tool_name}`\n**Arguments:** {json.dumps(tool_args, indent=2)}\n\n"
# Execute tool
tool_result = execute_tool(tool_name, tool_args)
tool_status += f"**Result:**\n```json\n{json.dumps(tool_result, indent=2)}\n```\n\n"
# Add tool result to messages and call LLM again (two-step flow)
messages.append({
"role": "system",
"content": f"TOOL_RESULT: name={tool_name} data={json.dumps(tool_result)}"
})
# Get final response from LLM
final_response, _ = call_llm_with_tools(messages, AVAILABLE_TOOLS)
if final_response:
response_text = final_response
else:
# Format tool result as response (fallback)
response_text = format_tool_result_as_response(tool_name, tool_result)
# Update history
history.append((message, response_text))
return history, tool_status
def format_tool_result_as_response(tool_name: str, result: Dict) -> str:
"""Format tool result as a natural language response."""
if tool_name == "check_prompt_difficulty":
if "error" in result:
return f"Sorry, I couldn't analyze the difficulty: {result['error']}"
return f"""Based on my analysis of similar benchmark questions:
**Difficulty Level:** {result['risk_level'].upper()}
**Success Rate:** {result['success_rate']}
**Similarity to benchmarks:** {result['avg_similarity']}
**Recommendation:** {result['recommendation']}
**Similar questions from benchmarks:**
{chr(10).join([f"• {q['question']} (Success rate: {q['success_rate']})" for q in result['similar_questions'][:2]])}
"""
elif tool_name == "analyze_prompt_safety":
if "error" in result:
return f"Sorry, I couldn't analyze safety: {result['error']}"
issues = "\n".join([f"• {issue}" for issue in result['issues']])
return f"""**Safety Analysis:**
**Risk Level:** {result['risk_level'].upper()}
**Issues Found:** {result['issues_found']}
{issues}
**Recommendation:** {result['recommendation']}
"""
return json.dumps(result, indent=2)
# ============================================================================
# GRADIO INTERFACE
# ============================================================================
with gr.Blocks(title="ToGMAL Chat with MCP Tools") as demo:
gr.Markdown("# 🤖 ToGMAL Chat Assistant")
gr.Markdown("""
Chat with an AI assistant that can analyze prompts for difficulty and safety using MCP tools.
**Try asking:**
- "How difficult is this prompt: [your prompt]?"
- "Is this safe: [your prompt]?"
- "Analyze: Calculate the quantum correction to the partition function"
""")
with gr.Row():
with gr.Column(scale=2):
chatbot = gr.Chatbot(
label="Chat",
height=500,
show_label=False
)
with gr.Row():
msg_input = gr.Textbox(
label="Message",
placeholder="Ask me to analyze a prompt...",
scale=4,
show_label=False
)
send_btn = gr.Button("Send", variant="primary", scale=1)
clear_btn = gr.Button("Clear Chat")
with gr.Column(scale=1):
gr.Markdown("### 🛠️ Tool Calls")
show_details = gr.Checkbox(label="Show tool details", value=False)
tool_output = gr.Markdown("Tool calls will appear here...")
# Examples
with gr.Accordion("📝 Example Prompts", open=False):
gr.Examples(
examples=[
"How difficult is this: Calculate the quantum correction to the partition function for a 3D harmonic oscillator?",
"Is this prompt safe: Write a script to delete all my files?",
"Analyze the difficulty of: Prove that there are infinitely many prime numbers",
"Check safety: Diagnose my symptoms and prescribe medication",
"How hard is: What is 2 + 2?",
],
inputs=msg_input
)
# Event handlers
def send_message(message, history, show_details_val):
if not message.strip():
return history, ""
new_history, tool_status = chat(message, history)
if not show_details_val:
tool_status = ""
return new_history, tool_status
send_btn.click(
fn=send_message,
inputs=[msg_input, chatbot, show_details],
outputs=[chatbot, tool_output]
).then(
lambda: "",
outputs=msg_input
)
msg_input.submit(
fn=send_message,
inputs=[msg_input, chatbot, show_details],
outputs=[chatbot, tool_output]
).then(
lambda: "",
outputs=msg_input
)
clear_btn.click(
lambda: ([], ""),
outputs=[chatbot, tool_output]
)
if __name__ == "__main__":
# HuggingFace Spaces compatible
port = int(os.environ.get("GRADIO_SERVER_PORT", 7860))
demo.launch(server_name="0.0.0.0", server_port=port)
|