Jayashree Sridhar
commited on
Commit
·
8bacf7a
1
Parent(s):
b119e68
Written custom base tool
Browse files- agents/tools/base_tool.py +50 -5
- agents/tools/knowledge_tools.py +5 -5
- agents/tools/llm_tools.py +5 -5
- agents/tools/validation_tools.py +3 -2
- agents/tools/voice_tools.py +5 -5
- requirements.txt +1 -1
agents/tools/base_tool.py
CHANGED
|
@@ -1,6 +1,51 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def __call__(self, *args, **kwargs):
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class BaseTool:
|
| 2 |
+
"""
|
| 3 |
+
Minimal base class for CrewAI or modular AI agent tools.
|
| 4 |
+
|
| 5 |
+
Inherit from this class for each custom tool to ensure a consistent interface.
|
| 6 |
+
|
| 7 |
+
Attributes:
|
| 8 |
+
config (dict or object, optional): Configuration provided on initialization.
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
def __init__(self, config=None):
|
| 12 |
+
"""
|
| 13 |
+
Initialize the tool with optional configuration.
|
| 14 |
+
|
| 15 |
+
Args:
|
| 16 |
+
config (dict or object, optional): Configuration dictionary or object.
|
| 17 |
+
"""
|
| 18 |
+
self.config = config
|
| 19 |
+
|
| 20 |
def __call__(self, *args, **kwargs):
|
| 21 |
+
"""
|
| 22 |
+
Abstract method for executing the tool's logic.
|
| 23 |
+
To be implemented in child classes.
|
| 24 |
+
|
| 25 |
+
Raises:
|
| 26 |
+
NotImplementedError: If not overridden in a subclass.
|
| 27 |
+
"""
|
| 28 |
+
raise NotImplementedError(
|
| 29 |
+
f"{self.__class__.__name__} must implement the __call__ method."
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
@property
|
| 33 |
+
def name(self):
|
| 34 |
+
"""
|
| 35 |
+
Returns the name of the tool class.
|
| 36 |
+
|
| 37 |
+
Returns:
|
| 38 |
+
str: Class name by default.
|
| 39 |
+
"""
|
| 40 |
+
return self.__class__.__name__
|
| 41 |
+
|
| 42 |
+
def description(self):
|
| 43 |
+
"""
|
| 44 |
+
Optionally provide a description string for the tool.
|
| 45 |
+
|
| 46 |
+
Returns:
|
| 47 |
+
str: Tool description, can be overridden by child classes.
|
| 48 |
+
"""
|
| 49 |
+
return f"{self.name} (custom tool base class for CrewAI and modular agents)."
|
| 50 |
+
|
| 51 |
+
# You can add more utility methods/properties if you wish (e.g., metadata)
|
agents/tools/knowledge_tools.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
-
|
| 2 |
from utils.knowledge_base import KnowledgeBase
|
| 3 |
#from pydantic import BaseModel, PrivateAttr
|
| 4 |
-
from crewai_tools.tool import BaseTool
|
| 5 |
|
| 6 |
|
| 7 |
|
|
@@ -12,7 +12,7 @@ class SearchKnowledgeTool(BaseTool):
|
|
| 12 |
def __init__(self, config=None):
|
| 13 |
super().__init__()
|
| 14 |
self.kb = KnowledgeBase(config)
|
| 15 |
-
def
|
| 16 |
return self.kb.search(query, k=k) if self.kb.is_initialized() else \
|
| 17 |
[{"text": "General wisdom", "score": 1.0}]
|
| 18 |
|
|
@@ -21,7 +21,7 @@ class ExtractWisdomTool(BaseTool):
|
|
| 21 |
description: str = "Extract most relevant wisdom for a given query."
|
| 22 |
def __init__(self, config=None):
|
| 23 |
super().__init__()
|
| 24 |
-
def
|
| 25 |
return search_results[:3]
|
| 26 |
|
| 27 |
class SuggestPracticesTool(BaseTool):
|
|
@@ -29,7 +29,7 @@ class SuggestPracticesTool(BaseTool):
|
|
| 29 |
description: str = "Recommend meditations or self-care practices."
|
| 30 |
def __init__(self, config=None):
|
| 31 |
super().__init__()
|
| 32 |
-
def
|
| 33 |
return {"name": "Mindful Breathing", "description": "Focus on your breath to calm the mind."}
|
| 34 |
|
| 35 |
class KnowledgeTools:
|
|
|
|
| 1 |
+
from .base_tool import BaseTool
|
| 2 |
from utils.knowledge_base import KnowledgeBase
|
| 3 |
#from pydantic import BaseModel, PrivateAttr
|
| 4 |
+
#from crewai_tools.tool import BaseTool
|
| 5 |
|
| 6 |
|
| 7 |
|
|
|
|
| 12 |
def __init__(self, config=None):
|
| 13 |
super().__init__()
|
| 14 |
self.kb = KnowledgeBase(config)
|
| 15 |
+
def __call__(self, query: str, k: int = 5):
|
| 16 |
return self.kb.search(query, k=k) if self.kb.is_initialized() else \
|
| 17 |
[{"text": "General wisdom", "score": 1.0}]
|
| 18 |
|
|
|
|
| 21 |
description: str = "Extract most relevant wisdom for a given query."
|
| 22 |
def __init__(self, config=None):
|
| 23 |
super().__init__()
|
| 24 |
+
def __call__(self, search_results: list, user_context: dict):
|
| 25 |
return search_results[:3]
|
| 26 |
|
| 27 |
class SuggestPracticesTool(BaseTool):
|
|
|
|
| 29 |
description: str = "Recommend meditations or self-care practices."
|
| 30 |
def __init__(self, config=None):
|
| 31 |
super().__init__()
|
| 32 |
+
def __call__(self, emotional_state: str, cultural_context: str = None):
|
| 33 |
return {"name": "Mindful Breathing", "description": "Focus on your breath to calm the mind."}
|
| 34 |
|
| 35 |
class KnowledgeTools:
|
agents/tools/llm_tools.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
-
|
| 2 |
from models.tinygpt2_model import TinyGPT2Model
|
| 3 |
#from pydantic import BaseModel, PrivateAttr
|
| 4 |
-
from crewai_tools.tool import BaseTool
|
| 5 |
|
| 6 |
|
| 7 |
|
|
@@ -12,7 +12,7 @@ class MistralChatTool(BaseTool):
|
|
| 12 |
def __init__(self, config=None):
|
| 13 |
super().__init__()
|
| 14 |
self.model = TinyGPT2Model()
|
| 15 |
-
def
|
| 16 |
msg = f"Context: {context}\nUser: {prompt}" if context else prompt
|
| 17 |
return self.model.generate(msg)
|
| 18 |
|
|
@@ -23,7 +23,7 @@ class GenerateAdviceTool(BaseTool):
|
|
| 23 |
def __init__(self, config=None):
|
| 24 |
super().__init__()
|
| 25 |
self.model = TinyGPT2Model()
|
| 26 |
-
def
|
| 27 |
prompt = f"Advice for: {user_analysis}, with wisdom: {wisdom_quotes}"
|
| 28 |
return self.model.generate(prompt, max_length=300)
|
| 29 |
|
|
@@ -34,7 +34,7 @@ class SummarizeConversationTool(BaseTool):
|
|
| 34 |
def __init__(self, config=None):
|
| 35 |
super().__init__()
|
| 36 |
self.model = TinyGPT2Model()
|
| 37 |
-
def
|
| 38 |
prompt = f"Summarize: {conversation}"
|
| 39 |
return self.model.generate(prompt, max_length=200)
|
| 40 |
|
|
|
|
| 1 |
+
from .base_tool import BaseTool
|
| 2 |
from models.tinygpt2_model import TinyGPT2Model
|
| 3 |
#from pydantic import BaseModel, PrivateAttr
|
| 4 |
+
#from crewai_tools.tool import BaseTool
|
| 5 |
|
| 6 |
|
| 7 |
|
|
|
|
| 12 |
def __init__(self, config=None):
|
| 13 |
super().__init__()
|
| 14 |
self.model = TinyGPT2Model()
|
| 15 |
+
def __call__(self, prompt: str, context: dict = None):
|
| 16 |
msg = f"Context: {context}\nUser: {prompt}" if context else prompt
|
| 17 |
return self.model.generate(msg)
|
| 18 |
|
|
|
|
| 23 |
def __init__(self, config=None):
|
| 24 |
super().__init__()
|
| 25 |
self.model = TinyGPT2Model()
|
| 26 |
+
def __call__(self, user_analysis: dict, wisdom_quotes: list):
|
| 27 |
prompt = f"Advice for: {user_analysis}, with wisdom: {wisdom_quotes}"
|
| 28 |
return self.model.generate(prompt, max_length=300)
|
| 29 |
|
|
|
|
| 34 |
def __init__(self, config=None):
|
| 35 |
super().__init__()
|
| 36 |
self.model = TinyGPT2Model()
|
| 37 |
+
def __call__(self, conversation: list):
|
| 38 |
prompt = f"Summarize: {conversation}"
|
| 39 |
return self.model.generate(prompt, max_length=200)
|
| 40 |
|
agents/tools/validation_tools.py
CHANGED
|
@@ -9,7 +9,8 @@ import json
|
|
| 9 |
from transformers import pipeline
|
| 10 |
import torch
|
| 11 |
#from pydantic import BaseModel, PrivateAttr
|
| 12 |
-
from crewai_tools.tool import BaseTool
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
|
|
@@ -411,7 +412,7 @@ class ValidateResponseTool(BaseTool):
|
|
| 411 |
super().__init__(**data)
|
| 412 |
self.config = config
|
| 413 |
# ... any required initialization ...
|
| 414 |
-
def
|
| 415 |
# Place your actual validation logic here, include dummy for illustration
|
| 416 |
# For full validation logic, use your own code!
|
| 417 |
# """Result of validation check"""
|
|
|
|
| 9 |
from transformers import pipeline
|
| 10 |
import torch
|
| 11 |
#from pydantic import BaseModel, PrivateAttr
|
| 12 |
+
#from crewai_tools.tool import BaseTool
|
| 13 |
+
from .base_tool import BaseTool
|
| 14 |
|
| 15 |
|
| 16 |
|
|
|
|
| 412 |
super().__init__(**data)
|
| 413 |
self.config = config
|
| 414 |
# ... any required initialization ...
|
| 415 |
+
def __call__(self, response: str, context: dict = None):
|
| 416 |
# Place your actual validation logic here, include dummy for illustration
|
| 417 |
# For full validation logic, use your own code!
|
| 418 |
# """Result of validation check"""
|
agents/tools/voice_tools.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import numpy as np
|
| 2 |
import asyncio
|
| 3 |
-
|
| 4 |
from models.tinygpt2_model import TinyGPT2Model
|
| 5 |
from transformers import pipeline, AutoProcessor, AutoModelForSpeechSeq2Seq
|
| 6 |
import os
|
|
@@ -8,7 +8,7 @@ import tempfile
|
|
| 8 |
import soundfile as sf
|
| 9 |
import torch
|
| 10 |
#from pydantic import BaseModel, PrivateAttr
|
| 11 |
-
from crewai_tools.tool import BaseTool
|
| 12 |
|
| 13 |
|
| 14 |
|
|
@@ -50,7 +50,7 @@ class TranscribeAudioTool(BaseTool):
|
|
| 50 |
def __init__(self, config=None):
|
| 51 |
super().__init__()
|
| 52 |
self.vp = MultilingualVoiceProcessor()
|
| 53 |
-
def
|
| 54 |
text, detected_lang = asyncio.run(self.vp.transcribe(audio_data, language))
|
| 55 |
return {"text": text, "language": detected_lang}
|
| 56 |
|
|
@@ -59,7 +59,7 @@ class DetectEmotionTool(BaseTool):
|
|
| 59 |
description: str = "Detect the emotional state from text."
|
| 60 |
def __init__(self, config=None):
|
| 61 |
super().__init__()
|
| 62 |
-
def
|
| 63 |
model = TinyGPT2Model()
|
| 64 |
prompt = f'Analyse emotions in: "{text}". Format: JSON with primary_emotion, intensity, feelings, concerns.'
|
| 65 |
response = model.generate(prompt)
|
|
@@ -73,7 +73,7 @@ class GenerateReflectiveQuestionsTool(BaseTool):
|
|
| 73 |
description: str = "Generate reflective questions."
|
| 74 |
def __init__(self, config=None):
|
| 75 |
super().__init__()
|
| 76 |
-
def
|
| 77 |
emotion = context.get("primary_emotion", "neutral")
|
| 78 |
questions_map = {
|
| 79 |
"anxiety": ["What triggers your anxiety?", "How do you cope?"],
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import asyncio
|
| 3 |
+
from .base_tool import BaseTool
|
| 4 |
from models.tinygpt2_model import TinyGPT2Model
|
| 5 |
from transformers import pipeline, AutoProcessor, AutoModelForSpeechSeq2Seq
|
| 6 |
import os
|
|
|
|
| 8 |
import soundfile as sf
|
| 9 |
import torch
|
| 10 |
#from pydantic import BaseModel, PrivateAttr
|
| 11 |
+
#from crewai_tools.tool import BaseTool
|
| 12 |
|
| 13 |
|
| 14 |
|
|
|
|
| 50 |
def __init__(self, config=None):
|
| 51 |
super().__init__()
|
| 52 |
self.vp = MultilingualVoiceProcessor()
|
| 53 |
+
def __call__(self, audio_data: np.ndarray, language=None):
|
| 54 |
text, detected_lang = asyncio.run(self.vp.transcribe(audio_data, language))
|
| 55 |
return {"text": text, "language": detected_lang}
|
| 56 |
|
|
|
|
| 59 |
description: str = "Detect the emotional state from text."
|
| 60 |
def __init__(self, config=None):
|
| 61 |
super().__init__()
|
| 62 |
+
def __call__(self, text: str):
|
| 63 |
model = TinyGPT2Model()
|
| 64 |
prompt = f'Analyse emotions in: "{text}". Format: JSON with primary_emotion, intensity, feelings, concerns.'
|
| 65 |
response = model.generate(prompt)
|
|
|
|
| 73 |
description: str = "Generate reflective questions."
|
| 74 |
def __init__(self, config=None):
|
| 75 |
super().__init__()
|
| 76 |
+
def __call__(self, context: dict):
|
| 77 |
emotion = context.get("primary_emotion", "neutral")
|
| 78 |
questions_map = {
|
| 79 |
"anxiety": ["What triggers your anxiety?", "How do you cope?"],
|
requirements.txt
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
|
| 2 |
-
crewai-tools
|
| 3 |
crewai
|
| 4 |
langchain
|
| 5 |
langchain-community
|
|
|
|
| 1 |
|
| 2 |
+
#crewai-tools
|
| 3 |
crewai
|
| 4 |
langchain
|
| 5 |
langchain-community
|