blib/lib/OpenAPI/Client/OpenAI.pm

filter: Statementstmt 45.8% 22 / 48 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 55.5% 5 / 9 Podpod 100% 1 / 1 totaltotal 41.1% 28 / 68 CC 7 SCAR 29.0
CC
7
Cov
38%
new
42.0
__ANON__
6.0
__ANON__
2.0
CRAP 18.2
↑ Summary
1 package OpenAPI::Client::OpenAI;
2  
3 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use File::ShareDir 'dist_file';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Mojo::Base 'OpenAPI::Client';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.25';
10  
11 sub new {
12 0 ▶ my ( $class, $specification ) = ( shift, shift );
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
13 0 ▶ my $attrs = @_ == 1 ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 0 0
14  
15 0 ▶ if ( !$ENV{OPENAI_API_KEY} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"OPENAI_API_KEY"}) 0 0 0
16 0 Carp::croak('OPENAI_API_KEY environment variable must be set');
17 }
18  
19 0 ▶ if ( !$specification ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($specification) 0 0 0
20 eval {
21 0 $specification = dist_file( 'OpenAPI-Client-OpenAI', 'openapi.yaml' );
22 0 1;
23 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $specification = dist_file("OpenAPI-Client-OpenAI", "openapi.yaml"); 1 } } or do { warn $@; $specification = catfile("share", "openapi.yaml") } 0 0 0
24 # Fallback to local share directory during development
25 0 warn $@;
26 0 $specification = catfile( 'share', 'openapi.yaml' );
27 };
28 }
29 0 my %headers = ( 'Authorization' => "Bearer $ENV{OPENAI_API_KEY}", );
30 0 ▶ if ( delete $attrs->{assistants} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $attrs->{"assistants"}) 0 0 0
31 0 $headers{'OpenAI-Beta'} = 'assistants=v2';
32 }
33  
34 # 'message' => 'You must provide the \'OpenAI-Beta\' header to access the
35 # Assistants API. Please try again by setting the header \'OpenAI-Beta:
36 # assistants=v1\'.'
37  
38 0 my $self = $class->SUPER::new( $specification, %{$attrs} );
39  
40 # you use this via $client->createTranscription({}, file_upload => { file => $filename, model => ...})
41 # note that you pass in a filename, so you don't have to read it yourself
42 $self->ua->transactor->add_generator(
43 file_upload => sub {
44 0 ▶ my ( $t, $tx, $data ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 0 return $t->_form( $tx, { %$data, file => { file => $data->{file} } } );
46 }
47 0 );
48  
49 $self->ua->on(
50 start => sub {
51 0 ▶ my ( $ua, $tx ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
52 0 foreach my $header ( keys %headers ) {
53 0 $tx->req->headers->header( $header => $headers{$header} );
54 }
55 }
56 0 );
57  
58 0 return $self;
59 }
60  
61 # install snake case aliases
62  
63 {
64 1 my %snake_case_alias = (
65 createChatCompletion => 'create_chat_completion',
66 createCompletion => 'create_completion',
67 createEmbedding => 'create_embedding',
68 createImage => 'create_image',
69 createModeration => 'create_moderation',
70 listModels => 'list_models',
71 );
72  
73 1 for my $camel_case_method ( keys %snake_case_alias ) {
74 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
75 6 my $method = $snake_case_alias{$camel_case_method};
76 *$method = sub {
77 0 ▶ warn "Calling '$method' is deprecated. Please use '$camel_case_method' instead.";
Subroutine __ANON__: not called CC=1 SCAR=6.9
78 0 my $self = shift;
79 0 $self->$camel_case_method(@_);
80 }
81 6 }
82 }
83  
84 1 1;
85  
86 __END__
87  
88 =encoding UTF-8
89  
90 =head1 NAME
91  
92 OpenAPI::Client::OpenAI - A client for the OpenAI API
93  
94 =head1 SYNOPSIS
95  
96 use OpenAPI::Client::OpenAI;
97  
98 # The OPENAI_API_KEY environment variable must be set
99 # See https://platform.openai.com/api-keys and ENVIRONMENT VARIABLES below
100 my $client = OpenAPI::Client::OpenAI->new();
101  
102 my $tx = $client->createCompletion(
103 {
104 body => {
105 model => 'gpt-3.5-turbo-instruct',
106 prompt => 'What is the capital of France?'
107 temperature => 0, # optional, between 0 and 1, with 0 being the least random
108 max_tokens => 100, # optional, the maximum number of tokens to generate
109 }
110 }
111 );
112  
113 my $response_data = $tx->res->json;
114  
115 print Dumper($response_data);
116  
117 =head1 DESCRIPTION
118  
119 OpenAPI::Client::OpenAI is a client for the OpenAI API built on
120 top of L<OpenAPI::Client>. This module automatically handles the API
121 key authentication according to the provided environment.
122  
123 Note that the OpenAI API is a paid service. You will need to sign up for an
124 account.
125  
126 See L<OpenAI::Client::OpenAI::Path> for a list of all available paths and methods.
127  
128 See the C<examples/> directory in the distribution for more examples, along
129 with the tests.
130  
131 =head1 WARNING
132  
133 Due to the extremely rapid development of OpenAI's API, this module may may
134 not be up-to-date with the latest changes. Further releases of this module may
135 break your code if OpenAI changes their API.
136  
137 =head1 METHODS
138  
139 =head2 Constructor
140  
141 =head3 new
142  
143 my $client = OpenAPI::Client::OpenAI->new( $specification, %options );
144  
145 Create a new OpenAI API client. The following options can be provided:
146  
147 =over
148  
149 =item * C<$specification>
150  
151 The path to the OpenAPI specification file (YAML). Defaults to the
152 "openai.yaml" file in the distribution's "share" directory.
153  
154 You can find the latest version of this file at
155 L<https://github.com/openai/openai-openapi>.
156  
157 Examples can be found in the C<t/> and C<examples/> directories of the
158 distribution.
159  
160 =back
161  
162 Additional options are passed to the parent class, OpenAPI::Client, with the
163 exception of the following extra options:
164  
165 =head2 Other Methods
166  
167 Other methods are documented in L<OpenAPI::Client::OpenAI::Methods>. These
168 method are deprecated and will be removed in a future version.
169  
170 See L<OpenAPI::Client::OpenAI::Path> for an index of all paths available. You
171 can click through each of them for more detail.
172  
173 =head1 DEPRECATED METHODS
174  
175 The following methods are deprecated and will be removed in a future release:
176  
177 =over
178  
179 =item * create_chat_completion
180  
181 Replaced with C<createChatCompletion>.
182  
183 =item * create_completion
184  
185 Replaced with C<createCompletion>.
186  
187 =item * create_embedding
188  
189 Replaced with C<createEmbedding>.
190  
191 =item * create_image
192  
193 Replaced with C<createImage>.
194  
195 =item * create_moderation
196  
197 Replaced with C<createModeration>.
198  
199 =item * list_models
200  
201 Replaced with C<listModels>.
202  
203 =back
204  
205 Originally, these methods were named using C<snake_case>, but to simplify the
206 code, we retained the C<camelCase> names in the main module.
207  
208 =head1 ENVIRONMENT VARIABLES
209  
210 The following environment variables are used by this module:
211  
212 =over 4
213  
214 =item * OPENAI_API_KEY
215  
216 The API key used to authenticate requests to the OpenAI API.
217  
218 =back
219  
220 =head1 SEE ALSO
221  
222 L<OpenAI::API> - the deprecated precursor to this module.
223  
224 =head1 AUTHOR
225  
226 Nelson Ferraz, E<lt>nferraz@gmail.comE<gt>
227  
228 =head1 CONTRIBUTORS
229  
230 =over 4
231  
232 =item * Curtis "Ovid" Poe, https://github.com/Ovid
233  
234 =item * Veesh Goldman, https://github.com/rabbiveesh
235  
236 =item * Graham Knop, https://github.com/haarg
237  
238 =back
239  
240 =head1 COPYRIGHT AND LICENSE
241  
242 Copyright (C) 2023-2024 by Nelson Ferraz
243  
244 This library is free software; you can redistribute it and/or modify
245 it under the same terms as Perl itself, either Perl version 5.14.0 or,
246 at your option, any later version of Perl 5 you may have available.
247  
248 =cut
↑ Summary