README-work
WWW::LLaMA
In brief
This Raku package provides access to the machine learning service llamafile, [MO1]. For more details of the llamafile's API usage see the documentation, [MO2].
This package is very similar to the packages "WWW::OpenAI", [AAp1], and "WWW::MistralAI", [AAp2].
"WWW::LLaMA" can be used with (is integrated with) "LLM::Functions", [AAp3], and "Jupyter::Chatbook", [AAp5].
Also, of course, prompts from "LLM::Prompts", [AAp4], can be used with LLaMA's functions.
Remark: The package "WWW::OpenAI" can be also used to access
"llamafile" chat completions.
That is done by specifying appropriate base URL to the openai-chat-completion
function.
Installation
Package installations from both sources use zef installer (which should be bundled with the "standard" Rakudo installation file.)
To install the package from Zef ecosystem use the shell command:
zef install WWW::LLaMA
To install the package from the GitHub repository use the shell command:
zef install https://github.com/antononcube/Raku-WWW-LLaMA.git
Install and run LLaMA server
In order to use the package access to LLaMA server is required.
Since the package follows closely the Web API of "llamafile", [MO1], it is advised to follow first the installation steps in the section of "Quickstart" of [MO1] before trying the functions of the package.
Usage examples
Remark: When the authorization key, auth-key
, is specified to be Whatever
then it is assigned the string sk-no-key-required
.
If an authorization key is required then the env variable LLAMA_API_KEY
can be also used.
Universal "front-end"
The package has an universal "front-end" function llama-playground
for the
different functionalities provided by llamafile.
Here is a simple call for a "chat completion":
use WWW::LLaMA;
llama-playground('What is the speed of a rocket leaving Earth?');
Another one using Bulgarian:
llama-playground('Колко групи могат да се намерят в този облак от точки.', max-tokens => 300, random-seed => 234232, format => 'values');
Remark: The functions llama-chat-completion
or llama-completion
can be used instead in the examples above.
(The latter is synonym of the former.)
Models
The current LLaMA model can be found with the function llama-model
:
llama-model;
Remark: Since there is no dedicated API endpoint for getting the model(s), the current model is obtained via "simple" (non-chat) completion.
Code generation
There are two types of completions : text and chat. Let us illustrate the differences of their usage by Raku code generation. Here is a text completion:
llama-completion(
'generate Raku code for making a loop over a list',
max-tokens => 120,
format => 'values');
Here is a chat completion:
llama-completion(
'generate Raku code for making a loop over a list',
max-tokens => 120,
format => 'values');
Embeddings
Embeddings can be obtained with the function llama-embedding
. Here is an example of finding the embedding vectors
for each of the elements of an array of strings:
my @queries = [
'make a classifier with the method RandomForeset over the data dfTitanic',
'show precision and accuracy',
'plot True Positive Rate vs Positive Predictive Value',
'what is a good meat and potatoes recipe'
];
my $embs = llama-embedding(@queries, format => 'values', method => 'tiny');
$embs.elems;
Here we show:
That the result is an array of four vectors each with length 1536
The distributions of the values of each vector
use Data::Reshapers;
use Data::Summarizers;
say "\$embs.elems : { $embs.elems }";
say "\$embs>>.elems : { $embs>>.elems }";
records-summary($embs.kv.Hash.&transpose);
Here we find the corresponding dot products and (cross-)tabulate them:
use Data::Reshapers;
use Data::Summarizers;
my @ct = (^$embs.elems X ^$embs.elems).map({ %( i => $_[0], j => $_[1], dot => sum($embs[$_[0]] >>*<< $embs[$_[1]])) }).Array;
say to-pretty-table(cross-tabulate(@ct, 'i', 'j', 'dot'), field-names => (^$embs.elems)>>.Str);
Remark: Note that the fourth element (the cooking recipe request) is an outlier. (Judging by the table with dot products.)
Tokenizing and de-tokenizing
Here we tokenize some text:
my $txt = @queries.head;
my $res = llama-tokenize($txt, format => 'values');
Here we get the original text be de-tokenizing:
llama-detokenize($res);
Chat completions with engineered prompts
Here is a prompt for "emojification" (see the Wolfram Prompt Repository entry "Emojify"):
my $preEmojify = q:to/END/;
Rewrite the following text and convert some of it into emojis.
The emojis are all related to whatever is in the text.
Keep a lot of the text, but convert key words into emojis.
Do not modify the text except to add emoji.
Respond only with the modified text, do not include any summary or explanation.
Do not respond with only emoji, most of the text should remain as normal words.
END
Here is an example of chat completion with emojification:
llama-chat-completion([ system => $preEmojify, user => 'Python sucks, Raku rocks, and Perl is annoying'], max-tokens => 200, format => 'values')
Command Line Interface
Playground access
The package provides a Command Line Interface (CLI) script:
llama-playground --help
Remark: When the authorization key, auth-key
, is specified to be Whatever
then it is assigned the string sk-no-key-required
.
If an authorization key is required then the env variable LLAMA_API_KEY
can be also used.
Mermaid diagram
The following flowchart corresponds to the steps in the package function llama-playground
:
References
Packages
[AAp1] Anton Antonov, WWW::OpenAI Raku package, (2023-2024), GitHub/antononcube.
[AAp2] Anton Antonov, WWW::MistralAI Raku package, (2023-2024), GitHub/antononcube.
[AAp3] Anton Antonov, LLM::Functions Raku package, (2023-2024), GitHub/antononcube.
[AAp4] Anton Antonov, LLM::Prompts Raku package, (2023-2024), GitHub/antononcube.
[AAp5] Anton Antonov, Jupyter::Chatbook Raku package, (2023), GitHub/antononcube.
[MO1] Mozilla Ocho, llamafile.
[MO2] Mozilla Ocho, llamafile documentation.