| 1 | package Praxo::Adapters::OpenAICompatible; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use HTTP::Tiny (); | ||
| 6 | use Praxo::Errors qw(praxo_error); | ||
| 7 | use Praxo::Json (); | ||
| 8 | use Praxo::Models (); | ||
| 9 | |||
| 10 | sub new { | ||
| 11 | my ($class, %args) = @_; | ||
| 12 | my $base_url = $args{base_url} // ''; | ||
| 13 | $base_url =~ s{/$}{}; | ||
| 14 | return bless { | ||
| 15 | base_url => $base_url, | ||
| 16 | model => $args{model} // '', | ||
| 17 | api_key => $args{api_key}, | ||
| 18 | transport => $args{transport}, | ||
| 19 | timeout => $args{timeout} // 60, | ||
| 20 | }, $class; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub generate { | ||
| 24 | my ($self, $request) = @_; | ||
| 25 | my $payload = _payload($self->{model}, $request); | ||
| 26 | my $data = $self->{transport} | ||
| 27 | ? $self->{transport}->('/chat/completions', $payload) | ||
| 28 | : $self->_post('/chat/completions', $payload); | ||
| 29 | return _parse_response($data); | ||
| 30 | } | ||
| 31 | |||
| 32 | sub _payload { | ||
| 33 | my ($model, $request) = @_; | ||
| 34 | my @messages = map { _message_payload($_) } @{ $request->{messages} // [] }; | ||
| 35 | my @tools = map { | ||
| 36 | { | ||
| 37 | type => 'function', | ||
| 38 | function => { | ||
| 39 | name => $_->{name}, | ||
| 40 | description => $_->{description} // '', | ||
| 41 | parameters => $_->{parameters} // { type => 'object' }, | ||
| 42 | }, | ||
| 43 | } | ||
| 44 | } @{ $request->{tools} // [] }; | ||
| 45 | my %payload = ( | ||
| 46 | model => $model, | ||
| 47 | messages => \@messages, | ||
| 48 | ); | ||
| 49 | $payload{tools} = \@tools if @tools; | ||
| 50 | $payload{tool_choice} = $request->{toolChoice} if defined $request->{toolChoice}; | ||
| 51 | $payload{response_format} = $request->{responseFormat} if defined $request->{responseFormat}; | ||
| 52 | $payload{temperature} = $request->{temperature} if defined $request->{temperature}; | ||
| 53 | return \%payload; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub _message_payload { | ||
| 57 | my ($message) = @_; | ||
| 58 | my $content = $message->{content}; | ||
| 59 | if ((!defined $content || $content eq '') && ref($message->{contentBlocks}) eq 'ARRAY') { | ||
| 60 | $content = join '', map { $_->{text} // '' } | ||
| 61 | grep { ($_->{type} // '') eq 'text' } @{ $message->{contentBlocks} }; | ||
| 62 | } | ||
| 63 | my %out = (role => $message->{role}, content => $content); | ||
| 64 | if ($message->{role} eq Praxo::Models::ASSISTANT() | ||
| 65 | && ref($message->{toolCalls}) eq 'ARRAY' && @{ $message->{toolCalls} }) { | ||
| 66 | $out{tool_calls} = [ | ||
| 67 | map { | ||
| 68 | { | ||
| 69 | id => $_->{id}, | ||
| 70 | type => 'function', | ||
| 71 | function => { | ||
| 72 | name => $_->{name}, | ||
| 73 | arguments => Praxo::Json::stringify($_->{arguments} // {}), | ||
| 74 | }, | ||
| 75 | } | ||
| 76 | } @{ $message->{toolCalls} } | ||
| 77 | ]; | ||
| 78 | } | ||
| 79 | if ($message->{role} eq Praxo::Models::TOOL() | ||
| 80 | && defined $message->{toolId} && $message->{toolId} ne '') { | ||
| 81 | $out{tool_call_id} = $message->{toolId}; | ||
| 82 | } | ||
| 83 | return \%out; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub _parse_response { | ||
| 87 | my ($data) = @_; | ||
| 88 | my $choice = ref($data->{choices}) eq 'ARRAY' ? $data->{choices}[0] : undef; | ||
| 89 | $choice = {} unless ref($choice) eq 'HASH'; | ||
| 90 | my $message = ref($choice->{message}) eq 'HASH' ? $choice->{message} : {}; | ||
| 91 | my @tool_calls = map { | ||
| 92 | my $function = ref($_->{function}) eq 'HASH' ? $_->{function} : {}; | ||
| 93 | my $arguments = $function->{arguments}; | ||
| 94 | if (defined $arguments && !ref($arguments) && $arguments ne '') { | ||
| 95 | $arguments = Praxo::Json::parse($arguments); | ||
| 96 | } | ||
| 97 | Praxo::Models::tool_call( | ||
| 98 | id => $_->{id} // '', | ||
| 99 | name => $function->{name} // '', | ||
| 100 | arguments => ref($arguments) eq 'HASH' ? $arguments : {}, | ||
| 101 | ) | ||
| 102 | } grep { ref($_) eq 'HASH' } @{ ref($message->{tool_calls}) eq 'ARRAY' ? $message->{tool_calls} : [] }; | ||
| 103 | my $usage = ref($data->{usage}) eq 'HASH' ? $data->{usage} : {}; | ||
| 104 | return Praxo::Models::response( | ||
| 105 | message => Praxo::Models::message_assistant($message->{content}, \@tool_calls), | ||
| 106 | usage => Praxo::Models::usage( | ||
| 107 | input_tokens => $usage->{prompt_tokens} // 0, | ||
| 108 | output_tokens => $usage->{completion_tokens} // 0, | ||
| 109 | ), | ||
| 110 | finish_reason => $choice->{finish_reason}, | ||
| 111 | ); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub _post { | ||
| 115 | my ($self, $path, $payload) = @_; | ||
| 116 | praxo_error('PROVIDER_HTTP', 'provider base URL is required') | ||
| 117 | unless $self->{base_url} ne ''; | ||
| 118 | my %headers = ('content-type' => 'application/json'); | ||
| 119 | $headers{authorization} = 'Bearer ' . $self->{api_key} | ||
| 120 | if defined $self->{api_key} && $self->{api_key} ne ''; | ||
| 121 | my $http = HTTP::Tiny->new(timeout => $self->{timeout}); | ||
| 122 | my $response = $http->post( | ||
| 123 | $self->{base_url} . $path, | ||
| 124 | { | ||
| 125 | headers => \%headers, | ||
| 126 | content => Praxo::Json::stringify($payload), | ||
| 127 | }, | ||
| 128 | ); | ||
| 129 | praxo_error( | ||
| 130 | 'PROVIDER_HTTP', | ||
| 131 | 'provider returned HTTP ' . ($response->{status} // 0) . ': ' . ($response->{content} // ''), | ||
| 132 | ) unless $response->{success}; | ||
| 133 | return Praxo::Json::parse($response->{content}); | ||
| 134 | } | ||
| 135 | |||
| 136 | 1; |