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