File size: 1,569 Bytes
3fbf461
 
520b75d
3fbf461
 
 
 
 
 
 
520b75d
 
 
 
 
dee5736
3fbf461
dee5736
520b75d
 
 
 
 
 
 
 
 
dee5736
520b75d
 
 
 
dca8350
520b75d
dca8350
 
 
 
 
 
520b75d
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from typing import Any, Optional
from smolagents.tools import Tool
import logging

class UserInputTool(Tool):
    name = "user_input"
    description = "Asks for user's input on a specific question"
    inputs = {'question': {'type': 'string', 'description': 'The question to ask the user'}}
    output_type = "string"

    def __init__(self):
        super().__init__()
        import logging
        self.logger = logging.getLogger(__name__)
        self.logger.setLevel(logging.INFO)
        self.user_input = ""

    async def _validate_question(self, question) -> tuple[bool, str]:
        # Helper method to validate the question
        if not isinstance(question, str):
            return False, f"Question must be a string, got {type(question)}"
        if not question.strip():
            return False, "Question cannot be empty"
        return True, question

    async def forward(self, question: str) -> str:
        # Validate the question first
        success, response = await self._validate_question(question)
        if not success:
            self.logger.error(response)
            return f"Error: {response}"

        # Ask the validated question and ensure non-empty response
        self.logger.info(f"Asking user: {question}")
        while True:
            self.user_input = input(f"{question} => Type your answer here:").strip()
            if self.user_input:
                break
            print("Please provide a non-empty answer.")

        self.logger.info(f"Received user input: {self.user_input}")
        return self.user_input