File Coverage

blib/lib/OpenAI/API/Request/Chat.pm
Criterion Covered Total %
statement 21 34 61.7
branch 0 2 0.0
condition 0 3 0.0
subroutine 7 11 63.6
pod 4 4 100.0
total 32 54 59.2


line stmt bran cond sub pod time code
1             package OpenAI::API::Request::Chat;
2              
3 16     16   113 use strict;
  16         44  
  16         496  
4 16     16   104 use warnings;
  16         37  
  16         452  
5              
6 16     16   98 use Carp qw/croak/;
  16         39  
  16         841  
7              
8 16     16   112 use Moo;
  16         41  
  16         118  
9 16     16   6335 use strictures 2;
  16         130  
  16         669  
10 16     16   2945 use namespace::clean;
  16         83  
  16         129  
11              
12             extends 'OpenAI::API::Request';
13              
14 16     16   4913 use Types::Standard qw(Any Bool Int Num Str Map ArrayRef HashRef);
  16         54  
  16         157  
15              
16             has model => ( is => 'rw', isa => Str, default => 'gpt-3.5-turbo' );
17             has messages => ( is => 'rw', isa => ArrayRef [HashRef], default => sub { [] } );
18              
19             has max_tokens => ( is => 'rw', isa => Int, );
20             has temperature => ( is => 'rw', isa => Num, );
21             has top_p => ( is => 'rw', isa => Num, );
22             has n => ( is => 'rw', isa => Int, );
23             has stream => ( is => 'rw', isa => Bool, );
24             has logprobs => ( is => 'rw', isa => Int, );
25             has echo => ( is => 'rw', isa => Bool, );
26             has stop => ( is => 'rw', isa => Any, );
27             has presence_penalty => ( is => 'rw', isa => Num, );
28             has frequency_penalty => ( is => 'rw', isa => Num, );
29             has logit_bias => ( is => 'rw', isa => Map [ Int, Int ], );
30             has user => ( is => 'rw', isa => Str, );
31              
32 0     0 1   sub endpoint { 'chat/completions' }
33 0     0 1   sub method { 'POST' }
34              
35             sub add_message {
36 0     0 1   my ( $self, $role, $content ) = @_;
37              
38 0 0 0       croak 'add_message() requires two parameters: role and content' if !defined $role || !defined $content;
39              
40 0           push @{ $self->messages }, { role => $role, content => $content };
  0            
41              
42 0           return $self;
43             }
44              
45             sub send_message {
46 0     0 1   my ( $self, $content ) = @_;
47              
48 0           $self->add_message( 'user', $content );
49              
50 0           my $res = $self->send();
51 0           my $assistant_response = $res->{choices}[0]{message}{content};
52              
53 0           $self->add_message( 'assistant', $assistant_response );
54              
55 0           return $res;
56             }
57              
58             1;
59              
60             __END__
61              
62             =head1 NAME
63              
64             OpenAI::API::Request::Chat - Request class for OpenAI API chat-based
65             completion
66              
67             =head1 SYNOPSIS
68              
69             use OpenAI::API::Request::Chat;
70              
71             my $chat = OpenAI::API::Request::Chat->new(
72             messages => [
73             { role => 'system', content => 'You are a helpful assistant.' },
74             { role => 'user', content => 'Who won the world series in 2020?' },
75             ],
76             );
77              
78             my $res = $chat->send(); # or: my $res = $chat->send(%args);
79             my $message = $res->{choices}[0]{message}; # or: my $message = "$res";
80              
81             # continue the conversation...
82             # $res = $chat->send_message('What is the capital of France?');
83              
84             =head1 DESCRIPTION
85              
86             This module provides a request class for interacting with the OpenAI API's
87             chat-based completion endpoint. It inherits from L<OpenAI::API::Request>.
88              
89             =head1 ATTRIBUTES
90              
91             =head2 model
92              
93             ID of the model to use.
94              
95             See L<Models overview|https://platform.openai.com/docs/models/overview>
96             for a reference of them.
97              
98             =head2 messages
99              
100             The messages to generate chat completions for, in the L<chat
101             format|https://platform.openai.com/docs/guides/chat/introduction>.
102              
103             =head2 max_tokens [optional]
104              
105             The maximum number of tokens to generate.
106              
107             Most models have a context length of 2048 tokens (except for the newest
108             models, which support 4096).
109              
110             =head2 temperature [optional]
111              
112             What sampling temperature to use, between 0 and 2. Higher values like
113             0.8 will make the output more random, while lower values like 0.2 will
114             make it more focused and deterministic.
115              
116             =head2 top_p [optional]
117              
118             An alternative to sampling with temperature, called nucleus sampling.
119              
120             We generally recommend altering this or C<temperature> but not both.
121              
122             =head2 n [optional]
123              
124             How many completions to generate for each prompt.
125              
126             Use carefully and ensure that you have reasonable settings for
127             C<max_tokens> and C<stop>.
128              
129             =head2 stop [optional]
130              
131             Up to 4 sequences where the API will stop generating further tokens. The
132             returned text will not contain the stop sequence.
133              
134             =head2 frequency_penalty [optional]
135              
136             Number between -2.0 and 2.0. Positive values penalize new tokens based
137             on their existing frequency in the text so far.
138              
139             =head2 presence_penalty [optional]
140              
141             Number between -2.0 and 2.0. Positive values penalize new tokens based
142             on whether they appear in the text so far.
143              
144             =head2 user [optional]
145              
146             A unique identifier representing your end-user, which can help OpenAI
147             to monitor and detect abuse.
148              
149             =head1 METHODS
150              
151             =head2 add_message($role, $content)
152              
153             This method adds a new message with the given role and content to the
154             messages attribute.
155              
156             =head2 send_message($content)
157              
158             This method adds a user message with the given content, sends the request,
159             and returns the response. It also adds the assistant's response to the
160             messages attribute.
161              
162             =head1 INHERITED METHODS
163              
164             This module inherits the following methods from L<OpenAI::API::Request>:
165              
166             =head2 send(%args)
167              
168             =head2 send_async(%args)
169              
170             =head1 SEE ALSO
171              
172             L<OpenAI::API::Request>, L<OpenAI::API::Config>