prige/User_Input_Tool
0
1from typing import Any, Optional2from smolagents.tools import Tool3import logging4 5class UserInputTool(Tool):6 name = "user_input"7 description = "Asks for user's input on a specific question"8 inputs = {'question': {'type': 'string', 'description': 'The question to ask the user'}}9 output_type = "string"10 11 def __init__(self):12 super().__init__()13 import logging14 self.logger = logging.getLogger(__name__)15 self.logger.setLevel(logging.INFO)16 self.user_input = ""17 18 async def _validate_question(self, question) -> tuple[bool, str]:19 # Helper method to validate the question20 if not isinstance(question, str):21 return False, f"Question must be a string, got {type(question)}"22 if not question.strip():23 return False, "Question cannot be empty"24 return True, question25 26 async def forward(self, question: str) -> str:27 # Validate the question first28 success, response = await self._validate_question(question)29 if not success:30 self.logger.error(response)31 return f"Error: {response}"32 33 # Ask the validated question and ensure non-empty response34 self.logger.info(f"Asking user: {question}")35 while True:36 self.user_input = input(f"{question} => Type your answer here:").strip()37 if self.user_input:38 break39 print("Please provide a non-empty answer.")40 41 self.logger.info(f"Received user input: {self.user_input}")42 return self.user_input43 