File size: 5,115 Bytes
20d720d |
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 |
"""
Knowledge Base Tools for RAG
"""
from crewai.tools import BaseTool
from utils.knowledge_base import KnowledgeBase
from typing import List, Dict
class SearchKnowledgeTool(BaseTool):
name: str = "search_spiritual_texts"
description: str = "Search spiritual and self-help texts for relevant wisdom"
def __init__(self):
super().__init__()
self.kb = KnowledgeBase()
def _run(self, query: str, k: int = 5) -> List[Dict]:
"""Search knowledge base"""
if not self.kb.is_initialized():
return [{
"text": "Wisdom comes from understanding ourselves.",
"source": "General Wisdom",
"score": 1.0
}]
results = self.kb.search(query, k=k)
return results
class ExtractWisdomTool(BaseTool):
name: str = "extract_relevant_wisdom"
description: str = "Extract most relevant wisdom for user's situation"
def _run(self, search_results: List[Dict], user_context: Dict) -> List[Dict]:
"""Filter and rank wisdom based on relevance"""
emotion = user_context.get("primary_emotion", "neutral")
concerns = user_context.get("concerns", [])
# Score each result based on relevance
scored_results = []
for result in search_results:
score = result["score"]
# Boost score if emotion matches
if emotion.lower() in result["text"].lower():
score *= 1.5
# Boost score if concerns match
for concern in concerns:
if concern.lower() in result["text"].lower():
score *= 1.3
result["relevance_score"] = score
scored_results.append(result)
# Sort by relevance
scored_results.sort(key=lambda x: x["relevance_score"], reverse=True)
return scored_results[:3]
class SuggestPracticeTool(BaseTool):
name: str = "suggest_meditation_practice"
description: str = "Suggest appropriate meditation or practice"
def _run(self, emotional_state: str, cultural_context: str = None) -> Dict:
"""Suggest practice based on emotional state"""
practices = {
"anxiety": {
"name": "Box Breathing Technique",
"description": "A powerful technique used by Navy SEALs to calm anxiety",
"steps": [
"Sit comfortably with back straight",
"Exhale all air from your lungs",
"Inhale through nose for 4 counts",
"Hold breath for 4 counts",
"Exhale through mouth for 4 counts",
"Hold empty for 4 counts",
"Repeat 4-8 times"
],
"benefits": "Activates parasympathetic nervous system, reduces cortisol",
"duration": "5-10 minutes",
"origin": "Modern breathwork"
},
"sadness": {
"name": "Metta (Loving-Kindness) Meditation",
"description": "Ancient Buddhist practice to cultivate compassion",
"steps": [
"Sit comfortably, close your eyes",
"Place hand on heart",
"Begin with self: 'May I be happy, may I be peaceful'",
"Extend to loved ones",
"Include neutral people",
"Embrace difficult people",
"Radiate to all beings"
],
"benefits": "Increases self-compassion, reduces depression",
"duration": "15-20 minutes",
"origin": "Buddhist tradition"
},
"stress": {
"name": "Progressive Muscle Relaxation",
"description": "Systematic tension and release technique",
"steps": [
"Lie down comfortably",
"Start with toes - tense for 5 seconds",
"Release suddenly, notice relaxation",
"Move up through each muscle group",
"Face and scalp last",
"Rest in full body relaxation"
],
"benefits": "Reduces physical tension, improves sleep",
"duration": "15-20 minutes",
"origin": "Dr. Edmund Jacobson, 1920s"
}
}
default = {
"name": "Mindful Breathing",
"description": "Foundation of all meditation practices",
"steps": [
"Sit comfortably",
"Follow natural breath",
"Count breaths 1-10",
"Start again when distracted",
"No judgment, just awareness"
],
"benefits": "Calms mind, improves focus",
"duration": "5-15 minutes",
"origin": "Universal practice"
}
return practices.get(emotional_state.lower(), default) |