blib/lib/AI/Chat.pm

filter: Statementstmt 56.9% 33 / 58 Branchbran 30.7% 8 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 72.7% 8 / 11 Podpod 100% 5 / 5 totaltotal 51.4% 54 / 105 CC 15 SCAR 30.1
CC
15
Cov
47%
chat
56.0
prompt
20.0
new
5.0
CRAP 20.4
↑ Summary
1 package AI::Chat;
2
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 3 our $VERSION = '0.6';
11 3 $VERSION = eval $VERSION;
12
13 3 my $http = HTTP::Tiny->new;
14
15 # Create Chat object
16 3 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
17 3 my $class = shift;
18 3 my %attr = @_;
19
20 3 $attr{'error'} = '';
21
22 3 ▶ $attr{'api'} = 'OpenAI' unless $attr{'api'};
Branch: 1 branch
BranchTrueFalseTotal
unless $attr{"api"} 1 2 3
23 3 ▶ $attr{'error'} = 'Invalid API' unless $attr{'api'} eq 'OpenAI';
Branch: 1 branch
BranchTrueFalseTotal
unless $attr{"api"} eq "OpenAI" 1 2 3
24 3 ▶ $attr{'error'} = 'API Key missing' unless $attr{'key'};
Branch: 1 branch
BranchTrueFalseTotal
unless $attr{"key"} 1 2 3
25
26 3 ▶ $attr{'model'} = 'gpt-4o-mini' unless $attr{'model'};
Branch: 1 branch
BranchTrueFalseTotal
unless $attr{"model"} 3 0 3
27
28 3 return bless \%attr, $class;
29 }
30
31 # Define endpoints for APIs
32 3 my %url = (
33 'OpenAI' => 'https://api.openai.com/v1/chat/completions',
34 );
35
36 # Define HTTP Headers for APIs
37 3 my %header = (
38 'OpenAI' => &_get_header_openai,
39 );
40
41 # Returns true if last operation was success
42 3 ▶sub success {
Subroutine success: called CC=1 SCAR=0.0
Pod documented
43 3 my $self = shift;
44 3 return !$self->{'error'};
45 }
46
47 # Returns error if last operation failed
48 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
49 0 my $self = shift;
50 0 return $self->{'error'};
51 }
52
53 # Header for calling OpenAI
54 3 ▶sub _get_header_openai {
Subroutine _get_header_openai: called CC=2 SCAR=7.1
55 3 my $self = shift;
56 3 ▶ $self->{'key'} = '' unless defined $self->{'key'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"key"} 3 0 3
57 return {
58 3 'Authorization' => 'Bearer ' . $self->{'key'},
59 'Content-type' => 'application/json'
60 };
61 }
62
63 # Get a reply from a single prompt
64 0 ▶ sub prompt {
Subroutine prompt: not called CC=4 SCAR=30.0
Pod documented
65 0 my ($self, $prompt, $temperature) = @_;
66
67 0 $self->{'error'} = '';
68 0 ▶ unless ($prompt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prompt) 0 0 0
69 0 $self->{'error'} = "Missing prompt calling 'prompt' method";
70 0 return undef;
71 }
72
73 0 ▶ $temperature = 1.0 unless $temperature;
Branch: 1 branch
BranchTrueFalseTotal
unless $temperature 0 0 0
74
75 0 my @messages;
76 push @messages, {
77 role => 'system',
78 content => $self->{'role'},
79 0 ▶ } if $self->{'role'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"role"} 0 0 0
80 0 push @messages, {
81 role => 'user',
82 content => $prompt,
83 };
84
85 0 return $self->chat(\@messages, $temperature);
86 }
87
88 # Get a reply from a full chat
89 0 ▶sub chat {
Subroutine chat: not called CC=7 SCAR=40.3
Pod documented
90 0 my ($self, $chat, $temperature) = @_;
91
92 0 ▶ if (ref($chat) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $chat ne "ARRAY") 0 0 0
93 0 $self->{'error'} = 'chat method requires an arrayref';
94 0 return undef;
95 }
96
97 0 ▶ $temperature = 1.0 unless $temperature;
Branch: 1 branch
BranchTrueFalseTotal
unless $temperature 0 0 0
98
99 my $response = $http->post($url{$self->{'api'}}, {
100 'headers' => {
101 'Authorization' => 'Bearer ' . $self->{'key'},
102 'Content-type' => 'application/json'
103 },
104 content => encode_json {
105 0 model => $self->{'model'},
106 messages => [ @$chat ],
107 temperature => $temperature,
108 }
109 });
110 0 ▶ if ($response->{'content'} =~ 'invalid_api_key') {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"content"} =~ /invalid_api_key/) 0 0 0
111 0 croak 'Incorrect API Key - check your API Key is correct';
112 }
113
114 0 ▶ if ($self->{'debug'} and !$response->{'success'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"} and not $response->{"success"}) 0 0 0
Condition0%0/3
$self->{"debug"} and not $response->{"success"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"debug"} and not $response->{"success"}
$self->{"debug"} not $response->{"success"}
Truth table0%0/3
A and not(B)
A= $self->{"debug"}B= not $response->{"success"}
ABresult
0X0
100
111
115 0 ▶ croak $response if $self->{'debug'} eq 'verbose';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} eq "verbose" 0 0 0
116 0 croak $response->{'content'};
117 }
118
119 0 my $reply = decode_json($response->{'content'});
120
121 0 return $reply->{'choices'}[0]->{'message'}->{'content'};
122 }
123
124
125 __END__
↑ Summary