/ src / python / txtai / agent / tool / skill.py
skill.py
 1  """
 2  Skill imports
 3  """
 4  
 5  import yaml
 6  
 7  from smolagents import Tool
 8  
 9  
10  class SkillTool(Tool):
11      """
12      A SkillTool loads a skill.md file.
13      """
14  
15      # pylint: disable=W0231
16      def __init__(self, path):
17          """
18          Creates a SkillTool.
19  
20          Args:
21              path: skill file path
22          """
23  
24          # Load skill.md
25          metadata, content = self.load(path)
26  
27          # Tool parameters
28          self.name = metadata["name"]
29          self.description = metadata["description"]
30          self.inputs = {"request": {"type": "string", "description": "The user requested action"}}
31          self.output_type = "any"
32          self.target = content
33  
34          # Validate parameters and initialize tool
35          super().__init__()
36  
37      # pylint: disable=W0221
38      def forward(self, request):
39          """
40          Searchs the skill markdown for the best answer.
41  
42          Args:
43              request: user request
44  
45          Returns:
46              result
47          """
48  
49          return f"""Given the request {request}, find the best answer using the content below.
50  
51  {self.target}
52  """
53  
54      def load(self, path):
55          """
56          Loads a skill.md file.
57  
58          Args:
59              path: path to skill.md
60  
61          Returns:
62              metadata, content
63          """
64  
65          # Read file content
66          with open(path, "r", encoding="utf-8") as f:
67              content = f.read()
68  
69          # Stores frontmatter metadata, if any
70          metadata = {}
71  
72          # Split by "---"" to separate frontmatter and markdown
73          if content.startswith("---"):
74              _, frontmatter, content = content.split("---", 2)
75              metadata = yaml.safe_load(frontmatter)
76  
77          # Return parsed metadata and content
78          return metadata, content