« blib/lib/SignalWire/Agents/Skills/Builtin/WeatherApi.pm
↑ Summary
blib/lib/SignalWire/Agents/REST/Namespaces/Logs.pm »
| 1 | package SignalWire::Agents::Prefabs::FAQBot; | ||||||||||||
| 2 | # Copyright (c) 2025 SignalWire | ||||||||||||
| 3 | # Licensed under the MIT License. | ||||||||||||
| 4 | |||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use JSON qw(encode_json); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 2 | extends 'SignalWire::Agents::Agent::AgentBase'; | |||||||||||
| 10 | |||||||||||||
| 11 | 0 | ▶ | has faqs => (is => 'ro', default => sub { [] }); | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 12 | 10 | ▶ | has suggest_related => (is => 'ro', default => sub { 1 }); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 13 | ▶ | has persona => (is => 'ro', default => sub { 'You are a helpful FAQ bot that provides accurate answers to common questions.' }); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | 13 | ▶ | sub BUILD { | ||||||||||
|
Subroutine
BUILD: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 16 | 13 | my ($self, $args) = @_; | |||||||||||
| 17 | |||||||||||||
| 18 | 13 | ▶ | $self->name('faq_bot') if $self->name eq 'agent'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 19 | 13 | ▶ | $self->route('/faq') if $self->route eq '/'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 20 | 13 | $self->use_pom(1); | |||||||||||
| 21 | |||||||||||||
| 22 | 13 | my $faqs = $self->faqs; | |||||||||||
| 23 | |||||||||||||
| 24 | 13 | ▶ | $self->set_global_data({ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 25 | faqs => $faqs, | ||||||||||||
| 26 | suggest_related => $self->suggest_related ? JSON::true : JSON::false, | ||||||||||||
| 27 | }); | ||||||||||||
| 28 | |||||||||||||
| 29 | 13 | $self->prompt_add_section( | |||||||||||
| 30 | 'Personality', | ||||||||||||
| 31 | $self->persona, | ||||||||||||
| 32 | ); | ||||||||||||
| 33 | |||||||||||||
| 34 | # Build FAQ knowledge | ||||||||||||
| 35 | 13 | my @faq_bullets; | |||||||||||
| 36 | 13 | for my $faq (@$faqs) { | |||||||||||
| 37 | 15 | push @faq_bullets, "Q: $faq->{question} A: $faq->{answer}"; | |||||||||||
| 38 | } | ||||||||||||
| 39 | |||||||||||||
| 40 | $self->prompt_add_section( | ||||||||||||
| 41 | 13 | 'FAQ Knowledge Base', | |||||||||||
| 42 | 'You have knowledge of the following frequently asked questions.', | ||||||||||||
| 43 | bullets => \@faq_bullets, | ||||||||||||
| 44 | ); | ||||||||||||
| 45 | |||||||||||||
| 46 | 13 | ▶ | if ($self->suggest_related) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 47 | 12 | $self->prompt_add_section( | |||||||||||
| 48 | 'Related Questions', | ||||||||||||
| 49 | 'When appropriate, suggest related questions the user might also be interested in.', | ||||||||||||
| 50 | ); | ||||||||||||
| 51 | } | ||||||||||||
| 52 | |||||||||||||
| 53 | # Register lookup tool | ||||||||||||
| 54 | $self->define_tool( | ||||||||||||
| 55 | name => 'lookup_faq', | ||||||||||||
| 56 | description => 'Look up an FAQ answer by keyword matching', | ||||||||||||
| 57 | parameters => { | ||||||||||||
| 58 | type => 'object', | ||||||||||||
| 59 | properties => { | ||||||||||||
| 60 | query => { type => 'string', description => 'The question or keywords to search' }, | ||||||||||||
| 61 | }, | ||||||||||||
| 62 | required => ['query'], | ||||||||||||
| 63 | }, | ||||||||||||
| 64 | 3 | ▶ | handler => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||
| 65 | 3 | my ($a, $raw) = @_; | |||||||||||
| 66 | 3 | require SignalWire::Agents::SWAIG::FunctionResult; | |||||||||||
| 67 | 3 | ▶ | my $query = lc($a->{query} // ''); | ||||||||||
|
Condition50%1/2
$a->{"query"} // ""
MC/DC0%0/1
$a->{"query"} // ""
$a->{"query"}
Truth table50%1/2
A
A= $a->{"query"}
| |||||||||||||
| 68 | 3 | for my $faq (@$faqs) { | |||||||||||
| 69 | 3 | ▶ | if (index(lc($faq->{question}), $query) >= 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | return SignalWire::Agents::SWAIG::FunctionResult->new( | ||||||||||||
| 71 | response => $faq->{answer}, | ||||||||||||
| 72 | 2 | ); | |||||||||||
| 73 | } | ||||||||||||
| 74 | } | ||||||||||||
| 75 | 1 | return SignalWire::Agents::SWAIG::FunctionResult->new( | |||||||||||
| 76 | response => "No FAQ found matching: $a->{query}", | ||||||||||||
| 77 | ); | ||||||||||||
| 78 | }, | ||||||||||||
| 79 | 13 | ); | |||||||||||
| 80 | } | ||||||||||||
| 81 | |||||||||||||
| 82 | 2 | 1; | |||||||||||