|
|
|
from utils.knowledge_base import KnowledgeBase |
|
from pydantic import PrivateAttr |
|
from crewai.tools import BaseTool |
|
|
|
|
|
|
|
class SearchKnowledgeTool(BaseTool): |
|
name: str = "search_knowledge" |
|
description: str = "Search self-help or spiritual wisdom." |
|
model_config = {"arbitrary_types_allowed": True} |
|
_kp: KnowledgeBase = PrivateAttr() |
|
def __init__(self, config=None): |
|
super().__init__() |
|
self._kb = KnowledgeBase(config) |
|
def _run(self, query: str, k: int = 5): |
|
return self.kb.search(query, k=k) if self.kb.is_initialized() else \ |
|
[{"text": "General wisdom", "score": 1.0}] |
|
|
|
class ExtractWisdomTool(BaseTool): |
|
name: str = "extract_wisdom" |
|
description: str = "Extract most relevant wisdom for a given query." |
|
model_config = {"arbitrary_types_allowed": True} |
|
def __init__(self, config=None): |
|
super().__init__() |
|
def _run(self, search_results: list, user_context: dict): |
|
return search_results[:3] |
|
|
|
class SuggestPracticesTool(BaseTool): |
|
name: str = "suggest_practices" |
|
description: str = "Recommend meditations or self-care practices." |
|
model_config = {"arbitrary_types_allowed": True} |
|
def __init__(self, config=None): |
|
super().__init__() |
|
def _run(self, emotional_state: str, cultural_context: str = None): |
|
return {"name": "Mindful Breathing", "description": "Focus on your breath to calm the mind."} |
|
|
|
class KnowledgeTools: |
|
def __init__(self, config=None): |
|
self.search_knowledge = SearchKnowledgeTool(config) |
|
self.extract_wisdom = ExtractWisdomTool(config) |
|
self.suggest_practices = SuggestPracticesTool(config) |