Class: LLM::Skill

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/skill.rb

Overview

LLM::Skill represents a directory-backed packaged capability. A skill directory must contain a ‘SKILL.md` file with YAML frontmatter. Skills can expose themselves as normal LLM::Tool classes through #to_tool. This keeps skills on the same execution path as local tools.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Skill

Returns a new instance of Skill.



48
49
50
51
52
53
54
55
# File 'lib/llm/skill.rb', line 48

def initialize(path)
  @path = path.to_s
  @name = ::File.basename(@path)
  @description = "Skill: #{@name}"
  @instructions = ""
  @frontmatter = LLM::Object.from({})
  @tools = []
end

Instance Attribute Details

#descriptionString (readonly)

Returns the skill description.

Returns:

  • (String)


31
32
33
# File 'lib/llm/skill.rb', line 31

def description
  @description
end

#frontmatterLLM::Object (readonly)

Returns the skill frontmatter.

Returns:



41
42
43
# File 'lib/llm/skill.rb', line 41

def frontmatter
  @frontmatter
end

#instructionsString (readonly)

Returns the skill instructions.

Returns:

  • (String)


36
37
38
# File 'lib/llm/skill.rb', line 36

def instructions
  @instructions
end

#nameString (readonly)

Returns the skill name.

Returns:

  • (String)


26
27
28
# File 'lib/llm/skill.rb', line 26

def name
  @name
end

#pathString (readonly)

Returns the skill directory.

Returns:

  • (String)


21
22
23
# File 'lib/llm/skill.rb', line 21

def path
  @path
end

#toolsArray<Class<LLM::Tool>> (readonly)

Returns the skill tools.

Returns:



46
47
48
# File 'lib/llm/skill.rb', line 46

def tools
  @tools
end

Class Method Details

.load(path) ⇒ LLM::Skill

Load a skill from a directory.

Parameters:

  • path (String, Pathname)

Returns:



14
15
16
# File 'lib/llm/skill.rb', line 14

def self.load(path)
  new(path).tap(&:load!)
end

Instance Method Details

#call(llm) ⇒ Hash

Execute the skill by wrapping it in a small agent with the skill instructions. The provider is bound explicitly by the caller.

Parameters:

Returns:

  • (Hash)


72
73
74
75
76
77
78
79
80
81
# File 'lib/llm/skill.rb', line 72

def call(llm, **)
  instructions = self.instructions
  tools = self.tools
  agent = Class.new(LLM::Agent) do
    instructions instructions
    tools(*tools)
  end.new(llm)
  res = agent.talk(instructions)
  {content: res.content}
end

#load!LLM::Skill

Load and parse the skill.

Returns:



60
61
62
63
64
# File 'lib/llm/skill.rb', line 60

def load!
  path = ::File.join(@path, "SKILL.md")
  parse(::File.read(path))
  self
end

#to_tool(llm) ⇒ Class<LLM::Tool>

Expose the skill as a normal LLM::Tool. The provider is bound explicitly when the tool class is built.

Parameters:

Returns:



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/llm/skill.rb', line 88

def to_tool(llm)
  skill = self
  Class.new(LLM::Tool) do
    name skill.name
    description skill.description

    define_method(:call) do |**input|
      skill.call(llm, **input)
    end
  end
end