|
| 1 | +""" |
| 2 | +Learning tools for PraisonAI agents. |
| 3 | +
|
| 4 | +Provides store_learning and search_learning as standard tool functions. |
| 5 | +These are the Learn system counterparts to store_memory/search_memory. |
| 6 | +
|
| 7 | +Memory stores flat facts ("User's name is Alice"). |
| 8 | +Learning stores categorized knowledge ("User prefers bullet points" → persona). |
| 9 | +
|
| 10 | +Usage: |
| 11 | + from praisonaiagents import Agent |
| 12 | + from praisonaiagents.tools import store_learning, search_learning |
| 13 | +
|
| 14 | + agent = Agent( |
| 15 | + instructions="You learn user preferences.", |
| 16 | + memory=True, |
| 17 | + learn=True, |
| 18 | + tools=[store_learning, search_learning] |
| 19 | + ) |
| 20 | +""" |
| 21 | + |
| 22 | +from typing import Optional, List, Dict, Any |
| 23 | +from .injected import Injected, AgentState, get_current_state |
| 24 | + |
| 25 | + |
| 26 | +# Valid categories mapping to LearnManager capture methods |
| 27 | +_CATEGORY_MAP = { |
| 28 | + "persona": "capture_persona", |
| 29 | + "insights": "capture_insight", |
| 30 | + "patterns": "capture_pattern", |
| 31 | + "decisions": "capture_decision", |
| 32 | + "feedback": "capture_feedback", |
| 33 | + "improvements": "capture_improvement", |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +def store_learning( |
| 38 | + content: str, |
| 39 | + category: str = "persona", |
| 40 | + state: Injected[AgentState] = None, |
| 41 | +) -> str: |
| 42 | + """Store a learning — a pattern, preference, insight, or decision. |
| 43 | +
|
| 44 | + Use this when you discover something worth remembering across sessions, |
| 45 | + like user preferences, behavioral patterns, or important decisions. |
| 46 | +
|
| 47 | + Categories: |
| 48 | + persona — User preferences and profile (default) |
| 49 | + insights — Observations about the user or domain |
| 50 | + patterns — Recurring behaviors or workflows |
| 51 | + decisions — Decision records for consistency |
| 52 | +
|
| 53 | + Args: |
| 54 | + content: The learning to store |
| 55 | + category: Category — "persona", "insights", "patterns", or "decisions" |
| 56 | + """ |
| 57 | + # Resolve injected state |
| 58 | + if state is None: |
| 59 | + state = get_current_state() |
| 60 | + |
| 61 | + learn_mgr = getattr(state, "learn_manager", None) if state else None |
| 62 | + if not learn_mgr: |
| 63 | + return "Learning is not configured for this agent. Enable learn=True to use learning tools." |
| 64 | + |
| 65 | + # Normalize category |
| 66 | + cat = category.lower().strip() |
| 67 | + if cat not in _CATEGORY_MAP: |
| 68 | + return f"Unknown category '{category}'. Use: {', '.join(_CATEGORY_MAP.keys())}" |
| 69 | + |
| 70 | + method_name = _CATEGORY_MAP[cat] |
| 71 | + method = getattr(learn_mgr, method_name, None) |
| 72 | + if not method: |
| 73 | + return f"Learn manager does not support category '{cat}'." |
| 74 | + |
| 75 | + try: |
| 76 | + result = method(content) |
| 77 | + # Friendly category labels for response |
| 78 | + labels = { |
| 79 | + "persona": "persona preference", |
| 80 | + "insights": "insight", |
| 81 | + "patterns": "pattern", |
| 82 | + "decisions": "decision", |
| 83 | + "feedback": "feedback", |
| 84 | + "improvements": "improvement", |
| 85 | + } |
| 86 | + label = labels.get(cat, cat) |
| 87 | + return f"Stored {label}: {content[:100]}" |
| 88 | + except Exception as e: |
| 89 | + return f"Error storing learning: {e}" |
| 90 | + |
| 91 | + |
| 92 | +def search_learning( |
| 93 | + query: str, |
| 94 | + category: str = "", |
| 95 | + limit: int = 5, |
| 96 | + state: Injected[AgentState] = None, |
| 97 | +) -> str: |
| 98 | + """Search learned knowledge — preferences, patterns, insights, decisions. |
| 99 | +
|
| 100 | + Use this to recall previously learned information about the user |
| 101 | + or domain across sessions. |
| 102 | +
|
| 103 | + Args: |
| 104 | + query: What to search for |
| 105 | + category: Optional — filter to specific category (persona, insights, patterns, decisions) |
| 106 | + limit: Maximum number of results to return |
| 107 | + """ |
| 108 | + # Resolve injected state |
| 109 | + if state is None: |
| 110 | + state = get_current_state() |
| 111 | + |
| 112 | + learn_mgr = getattr(state, "learn_manager", None) if state else None |
| 113 | + if not learn_mgr: |
| 114 | + return "Learning is not configured for this agent. Enable learn=True to use learning tools." |
| 115 | + |
| 116 | + try: |
| 117 | + all_results = learn_mgr.search(query, limit=limit) |
| 118 | + except Exception as e: |
| 119 | + return f"Error searching learnings: {e}" |
| 120 | + |
| 121 | + # Filter by category if specified |
| 122 | + if category: |
| 123 | + cat = category.lower().strip() |
| 124 | + all_results = {k: v for k, v in all_results.items() if k == cat} |
| 125 | + |
| 126 | + if not all_results: |
| 127 | + return f"No learnings found matching: {query}" |
| 128 | + |
| 129 | + # Format results grouped by category |
| 130 | + parts: List[str] = [] |
| 131 | + total = 0 |
| 132 | + for store_name, entries in all_results.items(): |
| 133 | + for entry in entries[:limit]: |
| 134 | + text = entry.get("content", "") if isinstance(entry, dict) else str(entry) |
| 135 | + if text: |
| 136 | + parts.append(f"- [{store_name}] {text}") |
| 137 | + total += 1 |
| 138 | + |
| 139 | + if not parts: |
| 140 | + return f"No learnings found matching: {query}" |
| 141 | + |
| 142 | + formatted = "\n".join(parts[:limit]) |
| 143 | + return f"Found {min(total, limit)} learnings:\n{formatted}" |
0 commit comments