|
""" |
|
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", []) |
|
|
|
|
|
scored_results = [] |
|
for result in search_results: |
|
score = result["score"] |
|
|
|
|
|
if emotion.lower() in result["text"].lower(): |
|
score *= 1.5 |
|
|
|
|
|
for concern in concerns: |
|
if concern.lower() in result["text"].lower(): |
|
score *= 1.3 |
|
|
|
result["relevance_score"] = score |
|
scored_results.append(result) |
|
|
|
|
|
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) |