line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package OpenAI::API; |
2
|
|
|
|
|
|
|
|
3
|
16
|
|
|
16
|
|
1437806
|
use strict; |
|
16
|
|
|
|
|
272
|
|
|
16
|
|
|
|
|
634
|
|
4
|
16
|
|
|
16
|
|
97
|
use warnings; |
|
16
|
|
|
|
|
31
|
|
|
16
|
|
|
|
|
473
|
|
5
|
|
|
|
|
|
|
|
6
|
16
|
|
|
16
|
|
86
|
use Carp (); |
|
16
|
|
|
|
|
29
|
|
|
16
|
|
|
|
|
293
|
|
7
|
|
|
|
|
|
|
|
8
|
16
|
|
|
16
|
|
7545
|
use OpenAI::API::Config; |
|
16
|
|
|
|
|
60
|
|
|
16
|
|
|
|
|
1914
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = 0.35; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
BEGIN { |
13
|
16
|
|
|
16
|
|
267
|
my %module_dispatcher = ( |
14
|
|
|
|
|
|
|
chat => 'OpenAI::API::Request::Chat', |
15
|
|
|
|
|
|
|
completions => 'OpenAI::API::Request::Completion', |
16
|
|
|
|
|
|
|
edits => 'OpenAI::API::Request::Edit', |
17
|
|
|
|
|
|
|
embeddings => 'OpenAI::API::Request::Embedding', |
18
|
|
|
|
|
|
|
files => 'OpenAI::API::Request::File::List', |
19
|
|
|
|
|
|
|
file_retrieve => 'OpenAI::API::Request::File::Retrieve', |
20
|
|
|
|
|
|
|
image_create => 'OpenAI::API::Request::Image::Generation', |
21
|
|
|
|
|
|
|
models => 'OpenAI::API::Request::Model::List', |
22
|
|
|
|
|
|
|
model_retrieve => 'OpenAI::API::Request::Model::Retrieve', |
23
|
|
|
|
|
|
|
moderations => 'OpenAI::API::Request::Moderation', |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
16
|
|
|
|
|
85
|
for my $sub_name ( keys %module_dispatcher ) { |
27
|
160
|
|
|
|
|
501
|
my $module = $module_dispatcher{$sub_name}; |
28
|
|
|
|
|
|
|
|
29
|
160
|
50
|
|
|
|
9265
|
eval "require $module" or die $@; |
30
|
|
|
|
|
|
|
|
31
|
16
|
|
|
16
|
|
141
|
no strict 'refs'; |
|
16
|
|
|
|
|
40
|
|
|
16
|
|
|
|
|
1626
|
|
32
|
160
|
|
|
|
|
3406
|
*{"$sub_name"} = sub { |
33
|
10
|
|
|
10
|
|
41581
|
my ( $self, %params ) = @_; |
34
|
10
|
|
|
|
|
50
|
my $request = $module->new( %params, config => $self->config ); |
35
|
4
|
|
|
|
|
186
|
return $request->send(); |
36
|
160
|
|
|
|
|
1146
|
}; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub new { |
41
|
3
|
|
|
3
|
1
|
330
|
my $class = shift; |
42
|
|
|
|
|
|
|
|
43
|
3
|
50
|
|
|
|
18
|
my %param = ref $_[0] ? %{ $_[0] } : @_; |
|
0
|
|
|
|
|
0
|
|
44
|
|
|
|
|
|
|
|
45
|
3
|
|
66
|
|
|
19
|
my $self = bless \%param, ref $class || $class; |
46
|
|
|
|
|
|
|
|
47
|
3
|
|
|
|
|
46
|
$self->{config} = OpenAI::API::Config->new(%param); |
48
|
|
|
|
|
|
|
|
49
|
2
|
|
|
|
|
49
|
return $self; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub config { |
53
|
10
|
|
|
10
|
1
|
36
|
my ( $self, %param ) = @_; |
54
|
10
|
|
|
|
|
108
|
return $self->{config}; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
1; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
__END__ |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 NAME |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
OpenAI::API - Perl interface to OpenAI API |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=for readme plugin version |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head1 SYNOPSIS |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
{ |
70
|
|
|
|
|
|
|
use OpenAI::API; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
my $openai = OpenAI::API->new(); # uses OPENAI_API_KEY environment variable |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my $res = $openai->chat( |
75
|
|
|
|
|
|
|
messages => [ |
76
|
|
|
|
|
|
|
{ "role" => "system", "content" => "You are a helpful assistant." }, |
77
|
|
|
|
|
|
|
{ "role" => "user", "content" => "How can I access OpenAI's APIs in Perl?" }, |
78
|
|
|
|
|
|
|
{ "role" => "assistant", "content" => "You can use the OpenAI::API module." }, |
79
|
|
|
|
|
|
|
{ "role" => "user", "content" => "How do I use this module?" }, |
80
|
|
|
|
|
|
|
], |
81
|
|
|
|
|
|
|
max_tokens => 20, |
82
|
|
|
|
|
|
|
temperature => 0, |
83
|
|
|
|
|
|
|
); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
my $message = $res->{choices}[0]{message}; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
{ |
89
|
|
|
|
|
|
|
use OpenAI::API; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
my $openai = OpenAI::API->new(); # uses OPENAI_API_KEY environment variable |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
my $res = $openai->completions( |
94
|
|
|
|
|
|
|
model => "text-davinci-003", |
95
|
|
|
|
|
|
|
prompt => "Say this is a test", |
96
|
|
|
|
|
|
|
max_tokens => 20, |
97
|
|
|
|
|
|
|
temperature => 0, |
98
|
|
|
|
|
|
|
); |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
my $text = $res->{choices}[0]{text}; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 DESCRIPTION |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
OpenAI::API is a Perl module that provides an interface to the OpenAI API, |
106
|
|
|
|
|
|
|
which allows you to generate text, translate languages, summarize text, |
107
|
|
|
|
|
|
|
and perform other tasks using the language models developed by OpenAI. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
To use the OpenAI::API module, you will need an API key, which you can obtain by |
110
|
|
|
|
|
|
|
signing up for an account on the L<OpenAI website|https://platform.openai.com>. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=begin :readme |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 INSTALLATION |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
If you have cpanm, you only need one line: |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
% cpanm OpenAI::API |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Alternatively, if your CPAN shell is set up, you should just be able |
121
|
|
|
|
|
|
|
to do: |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
% cpan OpenAI::API |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
As a last resort, you can manually install it: |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
perl Makefile.PL |
128
|
|
|
|
|
|
|
make |
129
|
|
|
|
|
|
|
make test |
130
|
|
|
|
|
|
|
make install |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
If your perl is system-managed, you can create a L<local::lib> in your |
133
|
|
|
|
|
|
|
home directory to install modules to. For details, see the |
134
|
|
|
|
|
|
|
L<local::lib documentation|https://metacpan.org/pod/local::lib>. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 DOCUMENTATION |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
After installing, you can find documentation for this module with the |
139
|
|
|
|
|
|
|
perldoc command. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
perldoc OpenAI::API |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=end :readme |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=for readme stop |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 METHODS |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
OpenAI::API acts as a high-level interface for the OpenAI API, handling |
150
|
|
|
|
|
|
|
different actions while utilizing the configuration class. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head2 new() |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Creates a new OpenAI::API object. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=over 4 |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=item * config [optional] |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
An OpenAI::API::Config object including the following properties: |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=over 4 |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item * api_key [optional] |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Your API key. Default: C<$ENV{OPENAI_API_KEY}>. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Attention: never commit API keys to your repository. Use the C<OPENAI_API_KEY> |
169
|
|
|
|
|
|
|
environment variable instead. |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
See: L<Best Practices for API Key Safety|https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety>. |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=item * api_base [optional] |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
The api_base URL for the OpenAI API. Default: 'https://api.openai.com/v1/'. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=item * timeout [optional] |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
The timeout value, in seconds. Default: 60 seconds. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=back |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=back |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=head2 chat() |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
L<Chat|OpenAI::API::Request::Chat> request. |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head2 completions() |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
L<Completion|OpenAI::API::Request::Completion> request. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head2 edits() |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
L<Edit|OpenAI::API::Request::Edit> request. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=head2 embeddings() |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
L<Embedding|OpenAI::API::Request::Embedding> request. |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=head2 files() |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
L<File List|OpenAI::API::Request::File::List> request. |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=head2 file_retrieve() |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
L<File Retrieve|OpenAI::API::Request::File::Retrieve> request. |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=head2 image_create() |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
L<Image Generation|OpenAI::API::Request::Image::Generation> request. |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=head2 models() |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
L<Model List|OpenAI::API::Request::Model::List> request. |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=head2 model_retrieve() |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
L<Model Retrieve|OpenAI::API::Request::Model::Retrieve> request. |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head2 moderations() |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
L<Moderation|OpenAI::API::Request::Moderation> request. |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=head1 RESOURCES |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=over |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=item * L<OpenAI::API::Request::Chat> |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=item * L<OpenAI::API::Request::Completion> |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=item * L<OpenAI::API::Request::Edit> |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=item * L<OpenAI::API::Request::Embedding> |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=item * L<OpenAI::API::Request::File::List> |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=item * L<OpenAI::API::Request::File::Retrieve> |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=item * L<OpenAI::API::Request::Image::Generation> |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=item * L<OpenAI::API::Request::Model::List> |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=item * L<OpenAI::API::Request::Model::Retrieve> |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=item * L<OpenAI::API::Request::Moderation> |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=back |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=head1 SEE ALSO |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
L<OpenAI Reference Overview|https://platform.openai.com/docs/api-reference/overview> |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=for readme start |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=head1 AUTHOR |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
Nelson Ferraz E<lt>nferraz@gmail.comE<gt> |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=head1 SUPPORT |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
This module is developed on |
264
|
|
|
|
|
|
|
L<GitHub|https://github.com/nferraz/perl-openai-api>. |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
Send ideas, feedback, tasks, or bugs to |
267
|
|
|
|
|
|
|
L<GitHub Issues|https://github.com/nferraz/perl-openai-api/issues>. |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
Copyright (C) 2022, 2023 by Nelson Ferraz |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
274
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.30.2 or, |
275
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=for readme stop |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
=cut |