#!/usr/bin/env raku
use v6.d;

use WWW::LLaMA;
use JSON::Fast;

my %*SUB-MAIN-OPTS =
        :named-anywhere,
        # allow named variables at any location
        ;

#| Text processing using the LLaMA Web API.
multi sub llama-front(Str $text is copy,                          #= Text to be processed or audio file name.
                      Str :$path = 'chat/completions',            #= Path, one of 'completions', 'chat/completions', 'embeddings', 'models', 'tokenize', and 'detokenize'.
                      Int :mt(:$max-tokens) = 2048,               #= The maximum number of tokens to generate in the completion.
                      Str :m(:$model) is copy = 'Whatever',       #= Model.
                      Str :r(:$role) is copy = 'user',            #= Role.
                      Real :t(:$temperature) = 0.7,               #= Temperature.
                      Str :$response-format = 'url',              #= The format in which the response is returned.
                      Str :a(:$auth-key) = 'Whatever',            #= Authorization key (to use LLaMA server Web API.)
                      UInt :$timeout= 10,                         #= Timeout.
                      Str :f(:$format) is copy = 'Whatever',      #= Format of the result; one of "json", "hash", "values", or "Whatever".
                      Str :$method is copy = 'tiny',              #= Method for the HTTP POST query; one of "tiny" or "curl".
                      Str :$base-url = 'http://127.0.0.1:8080' #= Base URL of the LLaMA server.
                      ) {

    if $text.chars == 0 {
        note 'Nothing.';
        return;
    }

    if $base-url.lc eq 'whatever' {
        $base-url = 'http://127.0.0.1:8080';
    }

    my $input = $text;
    if $path.lc ∈ <de-tokenize detokenize> {
       try {
           $input = from-json($text);
       }
       if $! || $input !~~ Positional {
            die "When --path is 'detokeinize' the first argument is expected to be a list of numbers in JSON format.";
        }
    }

    my $authKeyLocal = $auth-key;
    if $auth-key.lc ∈ <whatever none> {
        $authKeyLocal = Whatever
    }

    if $format.lc ∈ <v value auto whatever> { $format = 'values'; }

    my $res =
            llama-playground($input,
                    :$path,
                    model => $model eq 'Whatever' ?? Whatever !! $model,
                    role => $role eq 'Whatever' ?? Whatever !! $role,
                    :$max-tokens,
                    :$response-format,
                    :$temperature,
                    auth-key => $authKeyLocal,
                    :$timeout,
                    :$format,
                    :$method,
                    :$base-url);

    if $format.lc ∈ <hash raku> {
        say $res.raku;
    } else {
        say $res;
    }
}

multi sub MAIN
#= Command given as a sequence of words.
(*@words,
 Str :$path = 'chat/completions',            #= Path, one of 'completions', 'chat/completions', 'embeddings', 'models', 'tokenize', and 'detokenize'.
 Int :mt(:$max-tokens) = 2048,               #= The maximum number of tokens to generate in the completion.
 Str :m(:$model) is copy = 'Whatever',       #= Model.
 Str :r(:$role) is copy = 'user',            #= Role.
 Real :t(:$temperature) = 0.7,               #= Temperature.
 Str :$response-format = 'url',              #= The format in which the response is returned.
 Str :a(:$auth-key) = 'Whatever',            #= Authorization key (to use LLaMA server Web API.)
 UInt :$timeout= 10,                         #= Timeout.
 Str :f(:$format) is copy = 'Whatever',      #= Format of the result; one of "json", "hash", "values", or "Whatever".
 Str :$method is copy = 'tiny',              #= Method for the HTTP POST query; one of "tiny" or "curl".
 Str :$base-url = 'http://127.0.0.1:8080'    #= Base URL of the LLaMA server.
 ) {
    return llama-front(@words.join(' ').Str, :$model, :$path, :$max-tokens, :$role, :$temperature, :$response-format, :$auth-key, :$timeout, :$format, :$method, :$base-url);
}