Class: Raif::ModelTool

Inherits:
Object
  • Object
show all
Includes:
Concerns::JsonSchemaDefinition
Defined in:
app/models/raif/model_tool.rb

Class Method Summary collapse

Class Method Details

.description_for_llmObject

The description of the tool that will be provided to the model when giving it a list of available tools.



11
12
13
14
15
16
17
18
19
20
# File 'app/models/raif/model_tool.rb', line 11

def description_for_llm
  <<~DESCRIPTION
    Name: #{tool_name}
    Description: #{tool_description}
    Arguments Schema:
    #{JSON.pretty_generate(tool_arguments_schema)}
    Example Usage:
    #{JSON.pretty_generate(example_model_invocation)}
  DESCRIPTION
end

.example_model_invocation(&block) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'app/models/raif/model_tool.rb', line 38

def example_model_invocation(&block)
  if block_given?
    @example_model_invocation = block.call
  elsif @example_model_invocation.present?
    @example_model_invocation
  else
    raise NotImplementedError, "#{name}#example_model_invocation is not implemented"
  end
end

.invocation_partial_nameObject



52
53
54
# File 'app/models/raif/model_tool.rb', line 52

def invocation_partial_name
  name.gsub("Raif::ModelTools::", "").underscore
end

.invoke_tool(tool_arguments:, source:) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/raif/model_tool.rb', line 75

def invoke_tool(tool_arguments:, source:)
  tool_invocation = Raif::ModelToolInvocation.new(
    source: source,
    tool_type: name,
    tool_arguments: tool_arguments
  )

  ActiveRecord::Base.transaction do
    tool_invocation.save!
    process_invocation(tool_invocation)
    tool_invocation.completed!
  end

  tool_invocation
rescue StandardError => e
  tool_invocation.failed!
  raise e
end

.process_invocation(invocation) ⇒ Object

Raises:

  • (NotImplementedError)


48
49
50
# File 'app/models/raif/model_tool.rb', line 48

def process_invocation(invocation)
  raise NotImplementedError, "#{name}#process_invocation is not implemented"
end

.renderable?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'app/models/raif/model_tool.rb', line 67

def renderable?
  true
end

.tool_arguments_schema(&block) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'app/models/raif/model_tool.rb', line 56

def tool_arguments_schema(&block)
  if block_given?
    json_schema_definition(:tool_arguments, &block)
  elsif schema_defined?(:tool_arguments)
    schema_for(:tool_arguments)
  else
    raise NotImplementedError,
      "#{name} must define tool arguments schema via tool_arguments_schema or override #{name}.tool_arguments_schema"
  end
end

.tool_description(&block) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'app/models/raif/model_tool.rb', line 28

def tool_description(&block)
  if block_given?
    @tool_description = block.call
  elsif @tool_description.present?
    @tool_description
  else
    raise NotImplementedError, "#{name}#tool_description is not implemented"
  end
end

.tool_nameObject

The name of the tool as it will be provided to the model & used in the model invocation. Default for something like Raif::ModelTools::WikipediaSearch would be “wikipedia_search”



24
25
26
# File 'app/models/raif/model_tool.rb', line 24

def tool_name
  name.split("::").last.underscore
end

.triggers_observation_to_model?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'app/models/raif/model_tool.rb', line 71

def triggers_observation_to_model?
  false
end