OpenAIAsync/Types/Requests.pm

filter: Statementstmt 60.0% 3 / 5 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 71.4% 5 / 7 CC 1 SCAR 1.4
CC
1
Cov
60%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« OpenAIAsync/Types.pm ↑ Summary OpenAIAsync/Types/Results.pm »
1 package OpenAIAsync::Types::Requests;
2 1 ▶use v5.36.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 0 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=2.6
4  
5 use Object::PadX::Role::AutoMarshal;
6 use Object::PadX::Role::AutoJSON;
7 use Object::Pad::ClassAttr::Struct;
8 use OpenAIAsync::Types;
9  
10 role OpenAIAsync::Types::Requests::Base :does(OpenAIAsync::Types::Base) :Struct {
11 method _endpoint(); # How the client finds where to send the request
12 }
13  
14 #### Base Request Types
15  
16 class OpenAIAsync::Types::Requests::ChatCompletion :does(OpenAIAsync::Types::Requests::Base) :Struct {
17 method _endpoint() {"/chat/completions"}
18 field $messages :MarshalTo([OpenAIAsync::Types::Requests::ChatCompletion::Messages::Union]);
19 field $model :JSONStr = "gpt-3.5-turbo";
20 field $frequency_penalty :JSONNum = undef;
21 field $presence_penalty :JSONNum = undef;
22 field $logit_bias = undef; # TODO wtf is this?
23 field $max_tokens :JSONNum = undef;
24 field $response_format :JSONStr :JSONExclude = undef; # I'm not supporting this this version yet
25  
26 field $seed :JSONNum = undef;
27 field $stop = undef; # String, array or null, todo handle
28 field $stream :JSONBool = undef; # TODO handle
29 field $temperature :JSONNum = undef;
30 field $top_p :JSONNum = undef;
31 field $tools :JSONExclude = undef; # TODO handle this
32 field $tool_choice :JSONExclude = undef; # TODO handle this
33  
34 field $function_call :JSONExclude = undef;
35 field $functions :JSONExclude = undef;
36 }
37  
38 class OpenAIAsync::Types::Requests::Completion :does(OpenAIAsync::Types::Requests::Base) :Struct {
39 method _endpoint() {"/completions"}
40
41 field $model :JSONStr = "gpt-3.5-turbo"; # This is how 99% of everyone else seems to default this
42 field $prompt :JSONStr;
43
44 field $max_tokens :JSONNum = undef; # use the platform default usually
45 field $temperature :JSONNum = undef;
46 field $top_p :JSONNum = undef;
47 field $seed :JSONNum = undef;
48 field $echo :JSONBool = undef; # true or false only
49 field $suffix :JSONStr = undef;
50 field $stop :JSONStr = undef; # array of stop tokens
51 field $user :JSONStr = undef; # used for tracking purposes later
52  
53 field $frequency_penalty :JSONNum = undef;
54 field $presence_penalty :JSONNum = undef;
55
56 field $logit_bias = undef; # TODO make this work
57 field $log_probs = undef; # TODO
58
59 field $n :JSONNum = undef; # Danger will robinson! easy to cause $$$$$$$ costs
60 field $best_of :JSONNum = undef;
61  
62 field $stream :JSONBool = undef; # TODO FALSE ALWAYS RIGHT NOW
63  
64 ADJUST {
65 # Type assertions here
66 die "Streaming unsupported" if $self->stream;
67 }
68 }
69  
70 class OpenAIAsync::Types::Requests::Embedding :does(OpenAIAsync::Types::Requests::Base) :Struct {
71 method _endpoint() {"/embeddings"}
72 field $input :JSONStr;
73 field $model :JSONStr;
74 field $encoding_format :JSONStr = undef;
75 field $user :JSONStr = undef;
76 }
77  
78 ### Request Subtypes
79  
80 class OpenAIAsync::Types::Requests::ChatCompletion::Messages::Assistant::ToolCall :does(OpenAIAsync::Types::Base) :Struct {
81 field $id :JSONStr;
82 field $arguments :JSONStr;
83 field $type :JSONStr;
84 field $function :MarshalTo(OpenAIAsync::Types::Requests::ChatCompletion::Messages::Assistant::FunctionCall);
85 }
86  
87 class OpenAIAsync::Types::Requests::ChatCompletion::Messages::Assistant::FunctionCall :does(OpenAIAsync::Types::Base) {
88 field $arguments :JSONStr;
89 field $name :JSONStr;
90 }
91  
92 class OpenAIAsync::Types::Requests::ChatCompletion::Messages::User::Text :does(OpenAIAsync::Types::Base) :Struct {
93 field $type :JSONStr;
94 field $text :JSONStr;
95 }
96  
97 class OpenAIAsync::Types::Requests::ChatCompletion::Messages::User::ImageUrl :does(OpenAIAsync::Types::Base) :Struct {
98 field $url :JSONStr;
99 field $detail :JSONStr = undef;
100 }
101  
102 class OpenAIAsync::Types::Requests::ChatCompletion::Messages::User::Image :does(OpenAIAsync::Types::Base) :Struct {
103 field $type :JSONStr;
104 field $image_url :MarshalTo(OpenAIAsync::Types::Requests::ChatCompletion::Messages::User::ImageUrl);
105 }
106  
107 # TODO, why have two of these? just shove it into the big one below
108  
109 package
110 OpenAIAsync::Types::Requests::ChatCompletion::Messages::User::ContentUnion {
111 # This guy does some additional checks to give us the right type here
112
113 sub new {
114 my $class = shift @_;
115 my %input = @_;
116
117 die "Missing type in creation" unless $input{type};
118  
119 if ($input{type} eq 'text') {
120 return OpenAIAsync::Types::Requests::ChatCompletion::Messages::User::Text->new(%input);
121 } elsif ($input{type} eq 'image_url') {
122 return OpenAIAsync::Types::Requests::ChatCompletion::Messages::User::Image->new(%input);
123 } else {
124 die "Unsupported ChatCompletion User Message type: [".$input{type}."]";
125 }
126 }
127 };
128  
129 class OpenAIAsync::Types::Requests::ChatCompletion::Messages::User :does(OpenAIAsync::Types::Base) :Struct {
130 # This particular type is more complicated than AutoMarshal can handle, so we need to
131 # do this in a custom manner.
132 field $role;
133 field $name = undef;
134 field $content;
135  
136 ADJUST {
137 my $create_obj = sub {
138 my $cont = shift;
139  
140 if (ref($cont) eq 'HASH') {
141 # We've got a more detailed type here, create the union type here
142 my $obj = OpenAIAsync::Types::Requests::ChatCompletion::Messages::User::ContentUnion->new(%$cont);
143 } elsif (ref($cont) eq '') {
144 return $cont; # Bare string/scalar is fine
145 } else {
146 die "Can't nest other types in \$content of a ChatCompletion user message: ".ref($cont);
147 }
148 };
149  
150 if (ref($content) eq 'ARRAY') {
151 $content = [map {$create_obj->($_)} $content->@*];
152 } else {
153 # TODO check that this is acutally doing the right thing. I think it might not be for user messages that are just text
154 $content = $create_obj->($content);
155 }
156 }
157 }
158  
159 class OpenAIAsync::Types::Requests::ChatCompletion::Messages::Assistant :does(OpenAIAsync::Types::Base) :Struct {
160 field $role :JSONStr;
161 field $content :JSONStr;
162 field $name = undef;
163 field $tool_calls :MarshalTo([OpenAIAsync::Types::Requests::ChatCompletion::Messages::Assistant::ToolCall]) = undef;
164 field $function_call :MarshalTo(OpenAIAsync::Types::Requests::ChatCompletion::Messages::Assistant::FunctionCall) = undef;
165 }
166  
167 class OpenAIAsync::Types::Requests::ChatCompletion::Messages::Function :does(OpenAIAsync::Types::Base) :Struct {
168 field $role :JSONStr;
169 field $content :JSONStr;
170 field $name :JSONStr;
171 }
172  
173 class OpenAIAsync::Types::Requests::ChatCompletion::Messages::Tool :does(OpenAIAsync::Types::Base) :Struct {
174 field $role :JSONStr;
175 field $content :JSONStr;
176 field $tool_call_id :JSONStr;
177 }
178  
179 class OpenAIAsync::Types::Requests::ChatCompletion::Messages::System :does(OpenAIAsync::Types::Base) :Struct {
180 field $role :JSONStr;
181 field $name :JSONStr = undef;
182 field $content :JSONStr;
183 }
184  
185  
186 package
187 OpenAIAsync::Types::Requests::ChatCompletion::Messages::Union {
188 # This guy does some additional checks to give us the right type here
189
190 sub new {
191 my ($class, %input) = @_;
192 die "Missing role in creation" unless $input{role};
193  
194 if ($input{role} eq 'system') {
195 return OpenAIAsync::Types::Requests::ChatCompletion::Messages::System->new(%input);
196 } elsif ($input{role} eq 'user') {
197 return OpenAIAsync::Types::Requests::ChatCompletion::Messages::User->new(%input);
198 } elsif ($input{role} eq 'tool') {
199 return OpenAIAsync::Types::Requests::ChatCompletion::Messages::Tool->new(%input);
200 } elsif ($input{role} eq 'function') {
201 return OpenAIAsync::Types::Requests::ChatCompletion::Messages::Function->new(%input);
202 } elsif ($input{role} eq 'assistant') {
203 return OpenAIAsync::Types::Requests::ChatCompletion::Messages::Assistant->new(%input);
204 } else {
205 die "Unsupported ChatCompletion Message role: [".$input{role}."]";
206 }
207 }
208 };
209  
210 1;
« OpenAIAsync/Types.pm ↑ Summary OpenAIAsync/Types/Results.pm »