Jayashree Sridhar commited on
Commit
10453f3
·
1 Parent(s): 961a175

modified all tools with crew ai BaseModel

Browse files
agents/tools/knowledge_tools.py CHANGED
@@ -1,15 +1,16 @@
1
- from .base_tool import BaseTool
2
  from utils.knowledge_base import KnowledgeBase
3
- from pydantic import BaseModel, PrivateAttr
 
4
 
5
  class SearchKnowledgeTool(BaseTool):
6
  name: str = "search_knowledge"
7
  description: str = "Search self-help or spiritual wisdom."
8
- _kp: KnowledgeBase = PrivateAttr()
9
  def __init__(self, config=None):
10
  super().__init__()
11
- self._kb = KnowledgeBase(config)
12
- def __call__(self, query: str, k: int = 5):
13
  return self.kb.search(query, k=k) if self.kb.is_initialized() else \
14
  [{"text": "General wisdom", "score": 1.0}]
15
 
@@ -18,7 +19,7 @@ class ExtractWisdomTool(BaseTool):
18
  description: str = "Extract most relevant wisdom for a given query."
19
  def __init__(self, config=None):
20
  super().__init__()
21
- def __call__(self, search_results: list, user_context: dict):
22
  return search_results[:3]
23
 
24
  class SuggestPracticesTool(BaseTool):
@@ -26,7 +27,7 @@ class SuggestPracticesTool(BaseTool):
26
  description: str = "Recommend meditations or self-care practices."
27
  def __init__(self, config=None):
28
  super().__init__()
29
- def __call__(self, emotional_state: str, cultural_context: str = None):
30
  return {"name": "Mindful Breathing", "description": "Focus on your breath to calm the mind."}
31
 
32
  class KnowledgeTools:
 
1
+ # .base_tool import BaseTool
2
  from utils.knowledge_base import KnowledgeBase
3
+ #from pydantic import BaseModel, PrivateAttr
4
+ from crewai_tools import BaseTool
5
 
6
  class SearchKnowledgeTool(BaseTool):
7
  name: str = "search_knowledge"
8
  description: str = "Search self-help or spiritual wisdom."
9
+ #_kp: KnowledgeBase = PrivateAttr()
10
  def __init__(self, config=None):
11
  super().__init__()
12
+ self.kb = KnowledgeBase(config)
13
+ def _run(self, query: str, k: int = 5):
14
  return self.kb.search(query, k=k) if self.kb.is_initialized() else \
15
  [{"text": "General wisdom", "score": 1.0}]
16
 
 
19
  description: str = "Extract most relevant wisdom for a given query."
20
  def __init__(self, config=None):
21
  super().__init__()
22
+ def _run(self, search_results: list, user_context: dict):
23
  return search_results[:3]
24
 
25
  class SuggestPracticesTool(BaseTool):
 
27
  description: str = "Recommend meditations or self-care practices."
28
  def __init__(self, config=None):
29
  super().__init__()
30
+ def _run(self, emotional_state: str, cultural_context: str = None):
31
  return {"name": "Mindful Breathing", "description": "Focus on your breath to calm the mind."}
32
 
33
  class KnowledgeTools:
agents/tools/llm_tools.py CHANGED
@@ -1,37 +1,38 @@
1
- from .base_tool import BaseTool
2
  from models.tinygpt2_model import TinyGPT2Model
3
- from pydantic import BaseModel, PrivateAttr
 
4
 
5
  class MistralChatTool(BaseTool):
6
  name: str = "mistral_chat"
7
  description: str = "Generate an empathetic AI chat response."
8
- _model: TinyGPT2Model = PrivateAttr()
9
  def __init__(self, config=None):
10
  super().__init__()
11
- self._model = TinyGPT2Model()
12
- def __call__(self, prompt: str, context: dict = None):
13
  msg = f"Context: {context}\nUser: {prompt}" if context else prompt
14
  return self.model.generate(msg)
15
 
16
  class GenerateAdviceTool(BaseTool):
17
  name: str = "generate_advice"
18
  description: str = "Generate personalized advice."
19
- _model: TinyGPT2Model = PrivateAttr()
20
  def __init__(self, config=None):
21
  super().__init__()
22
- self._model = TinyGPT2Model()
23
- def __call__(self, user_analysis: dict, wisdom_quotes: list):
24
  prompt = f"Advice for: {user_analysis}, with wisdom: {wisdom_quotes}"
25
  return self.model.generate(prompt, max_length=300)
26
 
27
  class SummarizeConversationTool(BaseTool):
28
  name: str = "summarize_conversation"
29
  description: str = "Summarize chat with insights and next steps."
30
- _model: TinyGPT2Model = PrivateAttr()
31
  def __init__(self, config=None):
32
  super().__init__()
33
- self._model = TinyGPT2Model()
34
- def __call__(self, conversation: list):
35
  prompt = f"Summarize: {conversation}"
36
  return self.model.generate(prompt, max_length=200)
37
 
 
1
+ #from .base_tool import BaseTool
2
  from models.tinygpt2_model import TinyGPT2Model
3
+ #from pydantic import BaseModel, PrivateAttr
4
+ from crewai_tools import BaseTool
5
 
6
  class MistralChatTool(BaseTool):
7
  name: str = "mistral_chat"
8
  description: str = "Generate an empathetic AI chat response."
9
+ #_model: TinyGPT2Model = PrivateAttr()
10
  def __init__(self, config=None):
11
  super().__init__()
12
+ self.model = TinyGPT2Model()
13
+ def _run(self, prompt: str, context: dict = None):
14
  msg = f"Context: {context}\nUser: {prompt}" if context else prompt
15
  return self.model.generate(msg)
16
 
17
  class GenerateAdviceTool(BaseTool):
18
  name: str = "generate_advice"
19
  description: str = "Generate personalized advice."
20
+ #_model: TinyGPT2Model = PrivateAttr()
21
  def __init__(self, config=None):
22
  super().__init__()
23
+ self.model = TinyGPT2Model()
24
+ def _run(self, user_analysis: dict, wisdom_quotes: list):
25
  prompt = f"Advice for: {user_analysis}, with wisdom: {wisdom_quotes}"
26
  return self.model.generate(prompt, max_length=300)
27
 
28
  class SummarizeConversationTool(BaseTool):
29
  name: str = "summarize_conversation"
30
  description: str = "Summarize chat with insights and next steps."
31
+ #_model: TinyGPT2Model = PrivateAttr()
32
  def __init__(self, config=None):
33
  super().__init__()
34
+ self.model = TinyGPT2Model()
35
+ def _run(self, conversation: list):
36
  prompt = f"Summarize: {conversation}"
37
  return self.model.generate(prompt, max_length=200)
38
 
agents/tools/validation_tools.py CHANGED
@@ -397,17 +397,18 @@ class ValidationResult:
397
 
398
  # return resources.get(location.lower(), resources["global"])
399
 
400
- from .base_tool import BaseTool
 
401
 
402
  class ValidateResponseTool(BaseTool):
403
  name: str = "validate_response"
404
  description: str = "Validates safety and helpfulness."
405
- _config: object = PrivateAttr()
406
  def __init__(self, config=None, **data):
407
  super().__init__(**data)
408
- self._config = config
409
  # ... any required initialization ...
410
- def __call__(self, response: str, context: dict = None):
411
  # Place your actual validation logic here, include dummy for illustration
412
  # For full validation logic, use your own code!
413
  # """Result of validation check"""
 
397
 
398
  # return resources.get(location.lower(), resources["global"])
399
 
400
+ #from .base_tool import BaseTool
401
+ from crewai_tools import BaseTool
402
 
403
  class ValidateResponseTool(BaseTool):
404
  name: str = "validate_response"
405
  description: str = "Validates safety and helpfulness."
406
+ #_config: object = PrivateAttr()
407
  def __init__(self, config=None, **data):
408
  super().__init__(**data)
409
+ self.config = config
410
  # ... any required initialization ...
411
+ def _run(self, response: str, context: dict = None):
412
  # Place your actual validation logic here, include dummy for illustration
413
  # For full validation logic, use your own code!
414
  # """Result of validation check"""
agents/tools/voice_tools.py CHANGED
@@ -1,13 +1,14 @@
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
7
  import tempfile
8
  import soundfile as sf
9
  import torch
10
- from pydantic import BaseModel, PrivateAttr
 
11
  class MultilingualVoiceProcessor:
12
 
13
  def __init__(self, model_name="openai/whisper-base", device=None):
@@ -42,11 +43,11 @@ class MultilingualVoiceProcessor:
42
  class TranscribeAudioTool(BaseTool):
43
  name: str = "transcribe_audio"
44
  description: str = "Transcribe audio to text and detect language."
45
- _vp: MultilingualVoiceProcessor = PrivateAttr()
46
  def __init__(self, config=None):
47
  super().__init__()
48
- self._vp = MultilingualVoiceProcessor()
49
- def __call__(self, audio_data: np.ndarray, language=None):
50
  text, detected_lang = asyncio.run(self.vp.transcribe(audio_data, language))
51
  return {"text": text, "language": detected_lang}
52
 
@@ -55,7 +56,7 @@ class DetectEmotionTool(BaseTool):
55
  description: str = "Detect the emotional state from text."
56
  def __init__(self, config=None):
57
  super().__init__()
58
- def __call__(self, text: str):
59
  model = TinyGPT2Model()
60
  prompt = f'Analyse emotions in: "{text}". Format: JSON with primary_emotion, intensity, feelings, concerns.'
61
  response = model.generate(prompt)
@@ -69,7 +70,7 @@ class GenerateReflectiveQuestionsTool(BaseTool):
69
  description: str = "Generate reflective questions."
70
  def __init__(self, config=None):
71
  super().__init__()
72
- def __call__(self, context: dict):
73
  emotion = context.get("primary_emotion", "neutral")
74
  questions_map = {
75
  "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
7
  import tempfile
8
  import soundfile as sf
9
  import torch
10
+ #from pydantic import BaseModel, PrivateAttr
11
+ from crewai_tools import BaseTool
12
  class MultilingualVoiceProcessor:
13
 
14
  def __init__(self, model_name="openai/whisper-base", device=None):
 
43
  class TranscribeAudioTool(BaseTool):
44
  name: str = "transcribe_audio"
45
  description: str = "Transcribe audio to text and detect language."
46
+ #_vp: MultilingualVoiceProcessor = PrivateAttr()
47
  def __init__(self, config=None):
48
  super().__init__()
49
+ self.vp = MultilingualVoiceProcessor()
50
+ def _run(self, audio_data: np.ndarray, language=None):
51
  text, detected_lang = asyncio.run(self.vp.transcribe(audio_data, language))
52
  return {"text": text, "language": detected_lang}
53
 
 
56
  description: str = "Detect the emotional state from text."
57
  def __init__(self, config=None):
58
  super().__init__()
59
+ def _run(self, text: str):
60
  model = TinyGPT2Model()
61
  prompt = f'Analyse emotions in: "{text}". Format: JSON with primary_emotion, intensity, feelings, concerns.'
62
  response = model.generate(prompt)
 
70
  description: str = "Generate reflective questions."
71
  def __init__(self, config=None):
72
  super().__init__()
73
+ def _run(self, context: dict):
74
  emotion = context.get("primary_emotion", "neutral")
75
  questions_map = {
76
  "anxiety": ["What triggers your anxiety?", "How do you cope?"],