Class: SignalWire::Skills::Builtin::DatasphereServerlessSkill

Inherits:
SkillBase
  • Object
show all
Defined in:
lib/signalwire/skills/builtin/datasphere_serverless.rb

Instance Attribute Summary

Attributes inherited from SkillBase

#agent, #logger, #params, #swaig_fields

Instance Method Summary collapse

Methods inherited from SkillBase

#cleanup, #get_hints, #get_param, #initialize, #required_env_vars, #version

Constructor Details

This class inherits a constructor from SignalWire::Skills::SkillBase

Instance Method Details

#descriptionObject



14
# File 'lib/signalwire/skills/builtin/datasphere_serverless.rb', line 14

def description; 'Search knowledge using SignalWire DataSphere with serverless DataMap execution'; end

#get_global_dataObject



67
68
69
70
71
72
73
# File 'lib/signalwire/skills/builtin/datasphere_serverless.rb', line 67

def get_global_data
  {
    'datasphere_serverless_enabled' => true,
    'document_id'                   => @document_id,
    'knowledge_provider'            => 'SignalWire DataSphere (Serverless)'
  }
end

#get_parameter_schemaObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/signalwire/skills/builtin/datasphere_serverless.rb', line 90

def get_parameter_schema
  {
    'space_name'  => { 'type' => 'string', 'required' => true },
    'project_id'  => { 'type' => 'string', 'required' => true },
    'token'       => { 'type' => 'string', 'required' => true, 'hidden' => true },
    'document_id' => { 'type' => 'string', 'required' => true },
    'count'       => { 'type' => 'integer', 'default' => 1 },
    'distance'    => { 'type' => 'number', 'default' => 3.0 }
  }
end

#get_prompt_sectionsObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/signalwire/skills/builtin/datasphere_serverless.rb', line 75

def get_prompt_sections
  [
    {
      'title' => 'Knowledge Search Capability (Serverless)',
      'body' => "You can search a knowledge base for information using the #{@tool_name} tool.",
      'bullets' => [
        "Use the #{@tool_name} tool when users ask for information",
        'Search for relevant information using clear, specific queries',
        'Summarize search results in a clear, helpful way',
        'This tool executes on SignalWire servers for optimal performance'
      ]
    }
  ]
end

#instance_keyObject



37
# File 'lib/signalwire/skills/builtin/datasphere_serverless.rb', line 37

def instance_key; "datasphere_serverless_#{@tool_name}"; end

#nameObject



13
# File 'lib/signalwire/skills/builtin/datasphere_serverless.rb', line 13

def name;        'datasphere_serverless'; end

#register_toolsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/signalwire/skills/builtin/datasphere_serverless.rb', line 39

def register_tools
  dm = DataMap.new(@tool_name)
        .description('Search the knowledge base for information on any topic and return relevant results')
        .parameter('query', 'string', 'The search query', required: true)
        .webhook('POST', @api_url,
                 headers: {
                   'Content-Type'  => 'application/json',
                   'Authorization' => "Basic #{@auth_header}"
                 })
        .params({
          'document_id'  => @document_id,
          'query_string' => '${args.query}',
          'count'        => @count,
          'distance'     => @distance
        })
        .foreach({
          'input_key'  => 'chunks',
          'output_key' => 'formatted_results',
          'max'        => @count,
          'append'     => "=== RESULT ===\n${this.text}\n#{'=' * 50}\n\n"
        })
        .output(Swaig::FunctionResult.new('I found results for "${args.query}":\n\n${formatted_results}'))
        .error_keys(%w[error])
        .fallback_output(Swaig::FunctionResult.new(@no_results_msg))

  [{ datamap: dm.to_swaig_function }]
end

#setupObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/signalwire/skills/builtin/datasphere_serverless.rb', line 17

def setup
  @space_name  = get_param('space_name')
  @project_id  = get_param('project_id', env_var: 'SIGNALWIRE_PROJECT_ID')
  @token       = get_param('token', env_var: 'SIGNALWIRE_TOKEN')
  @document_id = get_param('document_id')
  @count       = (get_param('count', default: 1)).to_i
  @distance    = (get_param('distance', default: 3.0)).to_f
  @tool_name   = get_param('tool_name', default: 'search_knowledge')
  @no_results_msg = get_param('no_results_message',
    default: "I couldn't find any relevant information in the knowledge base.")

  %w[space_name project_id token document_id].each do |k|
    return false if instance_variable_get("@#{k}").nil? || instance_variable_get("@#{k}").to_s.empty?
  end

  @api_url     = "https://#{@space_name}.signalwire.com/api/datasphere/documents/search"
  @auth_header = Base64.strict_encode64("#{@project_id}:#{@token}")
  true
end

#supports_multiple_instances?Boolean

Returns:

  • (Boolean)


15
# File 'lib/signalwire/skills/builtin/datasphere_serverless.rb', line 15

def supports_multiple_instances?; true; end