Class: SignalWire::Prefabs::FaqBot

Inherits:
Object
  • Object
show all
Defined in:
lib/signalwire/prefabs/faq_bot.rb

Overview

Prefab agent for answering frequently asked questions.

agent = FaqBot.new(
  faqs: [
    { 'question' => 'What is SignalWire?', 'answer' => 'A cloud communications platform.' }
  ]
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(faqs:, suggest_related: true, persona: nil, name: 'faq_bot', route: '/faq', **_opts) ⇒ FaqBot

Returns a new instance of FaqBot.

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
# File 'lib/signalwire/prefabs/faq_bot.rb', line 23

def initialize(faqs:, suggest_related: true, persona: nil,
               name: 'faq_bot', route: '/faq', **_opts)
  raise ArgumentError, 'faqs must be a non-empty Array' unless faqs.is_a?(Array) && !faqs.empty?

  @faqs            = faqs.map { |f| f.transform_keys(&:to_s) }
  @suggest_related = suggest_related
  @persona         = persona || 'You are a helpful FAQ bot that provides accurate answers to common questions.'
  @name  = name
  @route = route
end

Instance Attribute Details

#faqsObject (readonly)

Returns the value of attribute faqs.



21
22
23
# File 'lib/signalwire/prefabs/faq_bot.rb', line 21

def faqs
  @faqs
end

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/signalwire/prefabs/faq_bot.rb', line 21

def name
  @name
end

#routeObject (readonly)

Returns the value of attribute route.



21
22
23
# File 'lib/signalwire/prefabs/faq_bot.rb', line 21

def route
  @route
end

Instance Method Details

#global_dataObject



49
50
51
52
53
54
# File 'lib/signalwire/prefabs/faq_bot.rb', line 49

def global_data
  {
    'faqs' => @faqs,
    'suggest_related' => @suggest_related
  }
end

#handle_search(args, _raw_data) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/signalwire/prefabs/faq_bot.rb', line 56

def handle_search(args, _raw_data)
  query = (args['query'] || '').downcase
  match = @faqs.find { |f| f['question'].downcase.include?(query) || query.include?(f['question'].downcase) }
  if match
    Swaig::FunctionResult.new(match['answer'])
  else
    Swaig::FunctionResult.new("I don't have a specific answer for that. Here are the topics I can help with: #{@faqs.map { |f| f['question'] }.join('; ')}")
  end
end

#prompt_sectionsObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/signalwire/prefabs/faq_bot.rb', line 38

def prompt_sections
  bullets = @faqs.map { |f| "Q: #{f['question']}" }
  [
    {
      'title' => 'FAQ Bot',
      'body' => @persona,
      'bullets' => bullets
    }
  ]
end

#toolsObject



34
35
36
# File 'lib/signalwire/prefabs/faq_bot.rb', line 34

def tools
  %w[search_faq]
end