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 2 |
+
Prompt Agent
|
| 3 |
+
|
| 4 |
+
Auto-generates prompts based on:
|
| 5 |
+
- Learning context
|
| 6 |
+
- User behavior
|
| 7 |
+
- Gesture triggers
|
| 8 |
+
- RL feedback
|
| 9 |
+
|
| 10 |
+
Features:
|
| 11 |
+
- Smart prompt templates
|
| 12 |
+
- Context-aware generation
|
| 13 |
+
- Auto-submit capability
|
| 14 |
+
- Multiple LLM routing
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
import re
|
| 18 |
+
from typing import Dict, List, Any, Optional
|
| 19 |
+
from dataclasses import dataclass, field
|
| 20 |
+
from datetime import datetime
|
| 21 |
+
import json
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
@dataclass
|
| 25 |
+
class PromptTemplate:
|
| 26 |
+
"""A prompt template for specific use cases"""
|
| 27 |
+
name: str
|
| 28 |
+
template: str
|
| 29 |
+
variables: List[str]
|
| 30 |
+
llm_preferences: List[str] = field(default_factory=list)
|
| 31 |
+
priority: int = 1
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
@dataclass
|
| 35 |
+
class GeneratedPrompt:
|
| 36 |
+
"""A generated prompt ready for submission"""
|
| 37 |
+
content: str
|
| 38 |
+
template_used: Optional[str]
|
| 39 |
+
context: Dict[str, Any]
|
| 40 |
+
llm_targets: List[str]
|
| 41 |
+
auto_submit: bool
|
| 42 |
+
generated_at: datetime = field(default_factory=datetime.now)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
@dataclass
|
| 46 |
+
class PromptHistory:
|
| 47 |
+
"""History of generated prompts"""
|
| 48 |
+
prompt: str
|
| 49 |
+
response: str
|
| 50 |
+
llm_used: str
|
| 51 |
+
feedback: Optional[int]
|
| 52 |
+
timestamp: datetime
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
class PromptAgent:
|
| 56 |
+
"""
|
| 57 |
+
Auto-generates prompts for LLM queries based on context.
|
| 58 |
+
|
| 59 |
+
Features:
|
| 60 |
+
- Context-aware templates
|
| 61 |
+
- Gesture-triggered generation
|
| 62 |
+
- Smart routing to appropriate LLMs
|
| 63 |
+
- Auto-submit capability
|
| 64 |
+
|
| 65 |
+
Inspired by GestureGPT triple-agent system:
|
| 66 |
+
- Gesture Description Agent → identifies intent
|
| 67 |
+
- Context Management Agent → maintains context
|
| 68 |
+
- Gesture Inference Agent → generates prompts
|
| 69 |
+
"""
|
| 70 |
+
|
| 71 |
+
def __init__(self):
|
| 72 |
+
self.templates = self._initialize_templates()
|
| 73 |
+
self.history: List[PromptHistory] = []
|
| 74 |
+
self.context_buffer: List[Dict] = []
|
| 75 |
+
self.max_context_size = 20
|
| 76 |
+
|
| 77 |
+
self.gesture_intent_mappings = {
|
| 78 |
+
"2_finger_swipe_right": "query_multi_llm",
|
| 79 |
+
"2_finger_swipe_left": "query_specific",
|
| 80 |
+
"1_finger_tap": "trigger_rl",
|
| 81 |
+
"pinch": "capture_and_query",
|
| 82 |
+
"open_palm": "pause_or_stop"
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
def _initialize_templates(self) -> Dict[str, PromptTemplate]:
|
| 86 |
+
"""Initialize prompt templates for different scenarios"""
|
| 87 |
+
return {
|
| 88 |
+
"learning_explain": PromptTemplate(
|
| 89 |
+
name="Learning Explanation",
|
| 90 |
+
template="""I am learning about {topic}. Please explain the key concepts in a clear, structured way.
|
| 91 |
+
|
| 92 |
+
Context from my learning session:
|
| 93 |
+
- Current progress: {progress}%
|
| 94 |
+
- I was confused about: {confusion_point}
|
| 95 |
+
- My learning goal is: {learning_goal}
|
| 96 |
+
|
| 97 |
+
Please provide:
|
| 98 |
+
1. A brief overview
|
| 99 |
+
2. Key concepts to understand
|
| 100 |
+
3. Common misconceptions to avoid
|
| 101 |
+
4. A simple example I can relate to""",
|
| 102 |
+
variables=["topic", "progress", "confusion_point", "learning_goal"],
|
| 103 |
+
llm_preferences=["chatgpt", "gemini"],
|
| 104 |
+
priority=3
|
| 105 |
+
),
|
| 106 |
+
|
| 107 |
+
"doubt_resolution": PromptTemplate(
|
| 108 |
+
name="Doubt Resolution",
|
| 109 |
+
template="""I'm struggling with this concept: {concept}
|
| 110 |
+
|
| 111 |
+
I've tried understanding it this way: {attempted_approach}
|
| 112 |
+
|
| 113 |
+
What specifically confuses me is: {confusion}
|
| 114 |
+
|
| 115 |
+
Please help me understand:
|
| 116 |
+
1. The simplest explanation
|
| 117 |
+
2. A step-by-step breakdown
|
| 118 |
+
3. An analogy or real-world example""",
|
| 119 |
+
variables=["concept", "attempted_approach", "confusion"],
|
| 120 |
+
llm_preferences=["chatgpt"],
|
| 121 |
+
priority=5
|
| 122 |
+
),
|
| 123 |
+
|
| 124 |
+
"summarize_content": PromptTemplate(
|
| 125 |
+
name="Content Summarization",
|
| 126 |
+
template="""Please summarize this content in a way that helps me learn:
|
| 127 |
+
|
| 128 |
+
{content}
|
| 129 |
+
|
| 130 |
+
Include:
|
| 131 |
+
1. Main takeaways (3-5 bullet points)
|
| 132 |
+
2. Key definitions
|
| 133 |
+
3. How this relates to {topic}""",
|
| 134 |
+
variables=["content", "topic"],
|
| 135 |
+
llm_preferences=["gemini", "chatgpt"],
|
| 136 |
+
priority=2
|
| 137 |
+
),
|
| 138 |
+
|
| 139 |
+
"practice_questions": PromptTemplate(
|
| 140 |
+
name="Practice Questions",
|
| 141 |
+
template="""Generate 5 practice questions to test my understanding of {topic}.
|
| 142 |
+
|
| 143 |
+
Difficulty level: {difficulty}
|
| 144 |
+
|
| 145 |
+
Include:
|
| 146 |
+
- 2 factual recall questions
|
| 147 |
+
- 2 application questions
|
| 148 |
+
- 1 analysis/evaluation question""",
|
| 149 |
+
variables=["topic", "difficulty"],
|
| 150 |
+
llm_preferences=["chatgpt"],
|
| 151 |
+
priority=2
|
| 152 |
+
),
|
| 153 |
+
|
| 154 |
+
"compare_concepts": PromptTemplate(
|
| 155 |
+
name="Concept Comparison",
|
| 156 |
+
template="""Compare and contrast these concepts for my learning:
|
| 157 |
+
|
| 158 |
+
Concept A: {concept_a}
|
| 159 |
+
Concept B: {concept_b}
|
| 160 |
+
|
| 161 |
+
Please structure your response as:
|
| 162 |
+
1. Similarities
|
| 163 |
+
2. Differences
|
| 164 |
+
3. When to use each
|
| 165 |
+
4. Common confusion points""",
|
| 166 |
+
variables=["concept_a", "concept_b"],
|
| 167 |
+
llm_preferences=["chatgpt", "gemini"],
|
| 168 |
+
priority=3
|
| 169 |
+
),
|
| 170 |
+
|
| 171 |
+
"spaced_repetition": PromptTemplate(
|
| 172 |
+
name="Spaced Repetition Review",
|
| 173 |
+
template="""Help me review what I learned about {topic}.
|
| 174 |
+
|
| 175 |
+
Based on spaced repetition principles, create:
|
| 176 |
+
1. A quick 3-question review
|
| 177 |
+
2. Key points to remember
|
| 178 |
+
3. What to focus on next
|
| 179 |
+
|
| 180 |
+
Previous understanding level: {mastery_level}/5""",
|
| 181 |
+
variables=["topic", "mastery_level"],
|
| 182 |
+
llm_preferences=["chatgpt"],
|
| 183 |
+
priority=2
|
| 184 |
+
),
|
| 185 |
+
|
| 186 |
+
"gesture_query": PromptTemplate(
|
| 187 |
+
name="Gesture-Triggered Query",
|
| 188 |
+
template="""Based on my current learning context:
|
| 189 |
+
- Topic: {topic}
|
| 190 |
+
- Confusion level: {confusion_level}%
|
| 191 |
+
- Recent question: {recent_question}
|
| 192 |
+
|
| 193 |
+
And my gesture action: {gesture_action}
|
| 194 |
+
|
| 195 |
+
Please provide a helpful response.""",
|
| 196 |
+
variables=["topic", "confusion_level", "recent_question", "gesture_action"],
|
| 197 |
+
llm_preferences=["chatgpt", "gemini"],
|
| 198 |
+
priority=4
|
| 199 |
+
),
|
| 200 |
+
|
| 201 |
+
"rl_optimization": PromptTemplate(
|
| 202 |
+
name="RL-Optimized Response",
|
| 203 |
+
template="""Learning context:
|
| 204 |
+
{context}
|
| 205 |
+
|
| 206 |
+
Previous interaction quality: {quality}/5
|
| 207 |
+
|
| 208 |
+
Based on my feedback and learning patterns, please:
|
| 209 |
+
1. Adjust explanation complexity
|
| 210 |
+
2. Focus on my weak areas
|
| 211 |
+
3. Provide practice opportunities""",
|
| 212 |
+
variables=["context", "quality"],
|
| 213 |
+
llm_preferences=["chatgpt"],
|
| 214 |
+
priority=3
|
| 215 |
+
)
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
def generate_prompt(
|
| 219 |
+
self,
|
| 220 |
+
template_name: str,
|
| 221 |
+
context: Dict[str, Any],
|
| 222 |
+
auto_submit: bool = True
|
| 223 |
+
) -> GeneratedPrompt:
|
| 224 |
+
"""Generate a prompt from template and context"""
|
| 225 |
+
|
| 226 |
+
if template_name not in self.templates:
|
| 227 |
+
template_name = "learning_explain"
|
| 228 |
+
|
| 229 |
+
template = self.templates[template_name]
|
| 230 |
+
|
| 231 |
+
try:
|
| 232 |
+
content = template.template.format(**context)
|
| 233 |
+
except KeyError as e:
|
| 234 |
+
content = template.template
|
| 235 |
+
for key in context:
|
| 236 |
+
content = content.replace(f"{{{key}}}", str(context[key]))
|
| 237 |
+
|
| 238 |
+
prompt = GeneratedPrompt(
|
| 239 |
+
content=content,
|
| 240 |
+
template_used=template_name,
|
| 241 |
+
context=context,
|
| 242 |
+
llm_targets=template.llm_preferences,
|
| 243 |
+
auto_submit=auto_submit
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
return prompt
|
| 247 |
+
|
| 248 |
+
def generate_from_gesture(
|
| 249 |
+
self,
|
| 250 |
+
gesture: str,
|
| 251 |
+
learning_context: Dict[str, Any]
|
| 252 |
+
) -> GeneratedPrompt:
|
| 253 |
+
"""Generate prompt based on gesture action"""
|
| 254 |
+
|
| 255 |
+
intent = self.gesture_intent_mappings.get(gesture, "query_multi_llm")
|
| 256 |
+
|
| 257 |
+
context = {
|
| 258 |
+
"topic": learning_context.get("topic", "this topic"),
|
| 259 |
+
"progress": learning_context.get("progress", 50),
|
| 260 |
+
"confusion_point": learning_context.get("confusion_point", ""),
|
| 261 |
+
"learning_goal": learning_context.get("learning_goal", "understand the basics"),
|
| 262 |
+
"confusion_level": learning_context.get("confusion_level", 30),
|
| 263 |
+
"recent_question": learning_context.get("recent_question", ""),
|
| 264 |
+
"gesture_action": gesture,
|
| 265 |
+
"content": learning_context.get("content", ""),
|
| 266 |
+
"difficulty": learning_context.get("difficulty", "intermediate"),
|
| 267 |
+
"concept": learning_context.get("concept", ""),
|
| 268 |
+
"attempted_approach": learning_context.get("attempted_approach", ""),
|
| 269 |
+
"concept_a": learning_context.get("concept_a", ""),
|
| 270 |
+
"concept_b": learning_context.get("concept_b", "")
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
if intent == "query_multi_llm":
|
| 274 |
+
template_name = "gesture_query"
|
| 275 |
+
elif intent == "trigger_rl":
|
| 276 |
+
template_name = "rl_optimization"
|
| 277 |
+
elif intent == "capture_and_query":
|
| 278 |
+
template_name = "summarize_content"
|
| 279 |
+
else:
|
| 280 |
+
template_name = "learning_explain"
|
| 281 |
+
|
| 282 |
+
return self.generate_prompt(template_name, context, auto_submit=True)
|
| 283 |
+
|
| 284 |
+
def generate_doubt_prompt(
|
| 285 |
+
self,
|
| 286 |
+
doubt_text: str,
|
| 287 |
+
context: Dict[str, Any]
|
| 288 |
+
) -> GeneratedPrompt:
|
| 289 |
+
"""Generate prompt for doubt resolution"""
|
| 290 |
+
|
| 291 |
+
context.update({
|
| 292 |
+
"concept": doubt_text,
|
| 293 |
+
"attempted_approach": context.get("attempted_approach", "I've read the material but don't understand"),
|
| 294 |
+
"confusion": context.get("confusion", "the underlying concept")
|
| 295 |
+
})
|
| 296 |
+
|
| 297 |
+
return self.generate_prompt("doubt_resolution", context)
|
| 298 |
+
|
| 299 |
+
def update_context(self, new_context: Dict):
|
| 300 |
+
"""Update the context buffer"""
|
| 301 |
+
self.context_buffer.append({
|
| 302 |
+
**new_context,
|
| 303 |
+
"timestamp": datetime.now().isoformat()
|
| 304 |
+
})
|
| 305 |
+
|
| 306 |
+
if len(self.context_buffer) > self.max_context_size:
|
| 307 |
+
self.context_buffer.pop(0)
|
| 308 |
+
|
| 309 |
+
def get_current_context(self) -> Dict:
|
| 310 |
+
"""Get the most recent context"""
|
| 311 |
+
if not self.context_buffer:
|
| 312 |
+
return {}
|
| 313 |
+
return self.context_buffer[-1]
|
| 314 |
+
|
| 315 |
+
def record_response(
|
| 316 |
+
self,
|
| 317 |
+
prompt: str,
|
| 318 |
+
response: str,
|
| 319 |
+
llm: str,
|
| 320 |
+
feedback: Optional[int] = None
|
| 321 |
+
):
|
| 322 |
+
"""Record a prompt-response pair for learning"""
|
| 323 |
+
history_entry = PromptHistory(
|
| 324 |
+
prompt=prompt,
|
| 325 |
+
response=response,
|
| 326 |
+
llm_used=llm,
|
| 327 |
+
feedback=feedback,
|
| 328 |
+
timestamp=datetime.now()
|
| 329 |
+
)
|
| 330 |
+
|
| 331 |
+
self.history.append(history_entry)
|
| 332 |
+
|
| 333 |
+
if len(self.history) > 100:
|
| 334 |
+
self.history = self.history[-50:]
|
| 335 |
+
|
| 336 |
+
def get_best_template_for_context(self, context: Dict) -> str:
|
| 337 |
+
"""Determine the best template for current context"""
|
| 338 |
+
|
| 339 |
+
if context.get("action") == "doubt":
|
| 340 |
+
return "doubt_resolution"
|
| 341 |
+
|
| 342 |
+
if context.get("action") == "review":
|
| 343 |
+
return "spaced_repetition"
|
| 344 |
+
|
| 345 |
+
if context.get("action") == "compare":
|
| 346 |
+
return "compare_concepts"
|
| 347 |
+
|
| 348 |
+
if context.get("action") == "practice":
|
| 349 |
+
return "practice_questions"
|
| 350 |
+
|
| 351 |
+
if context.get("action") == "summarize":
|
| 352 |
+
return "summarize_content"
|
| 353 |
+
|
| 354 |
+
return "learning_explain"
|
| 355 |
+
|
| 356 |
+
def get_suggested_prompts(self, context: Dict) -> List[GeneratedPrompt]:
|
| 357 |
+
"""Get suggested prompts based on context"""
|
| 358 |
+
suggestions = []
|
| 359 |
+
|
| 360 |
+
if context.get("confusion_level", 0) > 50:
|
| 361 |
+
suggestions.append(self.generate_prompt(
|
| 362 |
+
"doubt_resolution",
|
| 363 |
+
context,
|
| 364 |
+
auto_submit=False
|
| 365 |
+
))
|
| 366 |
+
|
| 367 |
+
if context.get("topic"):
|
| 368 |
+
suggestions.append(self.generate_prompt(
|
| 369 |
+
"learning_explain",
|
| 370 |
+
context,
|
| 371 |
+
auto_submit=False
|
| 372 |
+
))
|
| 373 |
+
|
| 374 |
+
if context.get("needs_review"):
|
| 375 |
+
suggestions.append(self.generate_prompt(
|
| 376 |
+
"spaced_repetition",
|
| 377 |
+
context,
|
| 378 |
+
auto_submit=False
|
| 379 |
+
))
|
| 380 |
+
|
| 381 |
+
return suggestions[:3]
|
| 382 |
+
|
| 383 |
+
def analyze_and_suggest(self, user_input: str, context: Dict) -> Dict:
|
| 384 |
+
"""Analyze user input and suggest appropriate action"""
|
| 385 |
+
|
| 386 |
+
user_lower = user_input.lower()
|
| 387 |
+
|
| 388 |
+
suggestions = {
|
| 389 |
+
"action": "explain",
|
| 390 |
+
"template": "learning_explain",
|
| 391 |
+
"confidence": 0.5
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
if any(word in user_lower for word in ["what", "how", "why", "explain"]):
|
| 395 |
+
suggestions["action"] = "explain"
|
| 396 |
+
suggestions["template"] = "learning_explain"
|
| 397 |
+
suggestions["confidence"] = 0.8
|
| 398 |
+
|
| 399 |
+
elif any(word in user_lower for word in ["compare", "difference", "versus", "vs"]):
|
| 400 |
+
suggestions["action"] = "compare"
|
| 401 |
+
suggestions["template"] = "compare_concepts"
|
| 402 |
+
suggestions["confidence"] = 0.9
|
| 403 |
+
|
| 404 |
+
elif any(word in user_lower for word in ["confused", "don't understand", "stuck", "help"]):
|
| 405 |
+
suggestions["action"] = "doubt"
|
| 406 |
+
suggestions["template"] = "doubt_resolution"
|
| 407 |
+
suggestions["confidence"] = 0.85
|
| 408 |
+
|
| 409 |
+
elif any(word in user_lower for word in ["practice", "quiz", "test", "question"]):
|
| 410 |
+
suggestions["action"] = "practice"
|
| 411 |
+
suggestions["template"] = "practice_questions"
|
| 412 |
+
suggestions["confidence"] = 0.85
|
| 413 |
+
|
| 414 |
+
elif any(word in user_lower for word in ["summary", "summarize", "overview"]):
|
| 415 |
+
suggestions["action"] = "summarize"
|
| 416 |
+
suggestions["template"] = "summarize_content"
|
| 417 |
+
suggestions["confidence"] = 0.9
|
| 418 |
+
|
| 419 |
+
return suggestions
|
| 420 |
+
|
| 421 |
+
|
| 422 |
+
class AutoSubmitAgent:
|
| 423 |
+
"""
|
| 424 |
+
Auto-submits prompts to LLMs and manages the submission flow.
|
| 425 |
+
|
| 426 |
+
Features:
|
| 427 |
+
- Automatic prompt submission
|
| 428 |
+
- Tab/input field simulation
|
| 429 |
+
- Rate limit awareness
|
| 430 |
+
- Multi-LLM coordination
|
| 431 |
+
"""
|
| 432 |
+
|
| 433 |
+
def __init__(self, prompt_agent: PromptAgent):
|
| 434 |
+
self.prompt_agent = prompt_agent
|
| 435 |
+
self.pending_submissions: List[Dict] = []
|
| 436 |
+
self.submission_results: List[Dict] = []
|
| 437 |
+
|
| 438 |
+
self.auto_submit_enabled = True
|
| 439 |
+
self.submit_delay = 0.5
|
| 440 |
+
|
| 441 |
+
def prepare_submission(
|
| 442 |
+
self,
|
| 443 |
+
prompt: GeneratedPrompt,
|
| 444 |
+
target_elements: Optional[Dict] = None
|
| 445 |
+
) -> Dict:
|
| 446 |
+
"""Prepare a prompt for submission"""
|
| 447 |
+
|
| 448 |
+
submission = {
|
| 449 |
+
"prompt": prompt,
|
| 450 |
+
"target_elements": target_elements or {
|
| 451 |
+
"input_selector": "textarea[placeholder*='message'], textarea[placeholder*='Ask'], input[type='text']",
|
| 452 |
+
"submit_selector": "button[type='submit'], button:contains('Send'), button:contains('Submit')"
|
| 453 |
+
},
|
| 454 |
+
"status": "ready",
|
| 455 |
+
"created_at": datetime.now().isoformat()
|
| 456 |
+
}
|
| 457 |
+
|
| 458 |
+
self.pending_submissions.append(submission)
|
| 459 |
+
return submission
|
| 460 |
+
|
| 461 |
+
def execute_submission(
|
| 462 |
+
self,
|
| 463 |
+
submission: Dict,
|
| 464 |
+
browser_controller=None
|
| 465 |
+
) -> Dict:
|
| 466 |
+
"""Execute the submission (simulated)"""
|
| 467 |
+
|
| 468 |
+
result = {
|
| 469 |
+
"status": "submitted",
|
| 470 |
+
"timestamp": datetime.now().isoformat(),
|
| 471 |
+
"prompt_content": submission["prompt"].content,
|
| 472 |
+
"target_url": "simulated"
|
| 473 |
+
}
|
| 474 |
+
|
| 475 |
+
self.submission_results.append(result)
|
| 476 |
+
self.pending_submissions.remove(submission)
|
| 477 |
+
|
| 478 |
+
return result
|
| 479 |
+
|
| 480 |
+
def get_submission_status(self) -> Dict:
|
| 481 |
+
"""Get current submission status"""
|
| 482 |
+
return {
|
| 483 |
+
"pending": len(self.pending_submissions),
|
| 484 |
+
"completed": len(self.submission_results),
|
| 485 |
+
"auto_submit_enabled": self.auto_submit_enabled
|
| 486 |
+
}
|
| 487 |
+
|
| 488 |
+
def cancel_pending(self):
|
| 489 |
+
"""Cancel all pending submissions"""
|
| 490 |
+
self.pending_submissions = []
|
| 491 |
+
|
| 492 |
+
def get_recent_results(self, limit: int = 10) -> List[Dict]:
|
| 493 |
+
"""Get recent submission results"""
|
| 494 |
+
return self.submission_results[-limit:]
|