README-work

WWW::OpenRouter

In brief

This Raku package provides API access to the Large Language Models (LLMs) service OpenRouter, [OR1]. For more details of the OpenRouter's API usage see the documentation, [OR2].

Remark: To use the OpenRouter API one has to register and obtain authorization key.

This package is very similar to the packages "WWW::OpenAI", [AAp1], and "WWW::Gemini", [AAp2].

"WWW::OpenRouter" 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 OpenRouter's functions.

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::OpenRouter

To install the package from the GitHub repository use the shell command:

zef install https://github.com/antononcube/Raku-WWW-OpenRouter.git

Universal "front-end"

The package has an universal "front-end" function openrouter-playground for the different functionalities provided by OpenRouter.

Here is a simple call for a "chat completion":

use WWW::OpenRouter;
openrouter-playground('Where is Roger Rabbit?');

Remark: When the authorization key, auth-key, is specified to be Whatever then the functions openrouter-* attempt to use the env variable OPENROUTER_API_KEY.

Another one using Bulgarian:

openrouter-playground('Колко групи могат да се намерят в този облак от точки.', max-tokens => 300, format => 'values');

Remark: The functions openrouter-chat-completion or openrouter-completion can be used instead in the examples above. (The latter is synonym of the former.)

Models

The current OpenRouter models can be found with the function openrouter-models:

openrouter-models.elems;

Video generating models can be found by setting the argument :output(:$output-modalities) to "video":

.say for openrouter-models(output => 'video').map(*<name>)

Code generation

Here is an example of Raku code generation:

openrouter-completion(
        'generate Raku code for making a loop over a list',
        max-tokens => 1024,
        format => 'values');

Embeddings

Embeddings can be obtained with the function openrouter-embeddings. 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 $model = 'nvidia/nemotron-3-embed-1b:free';
my $embs = openrouter-embeddings(@queries, format => 'values', :$model);
$embs.elems;

Here we show:

  • That the result is an array of four vectors each with length 2048

  • 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:

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.)

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:

[
    assistant => $preEmojify,
    user => 'Python sucks, Raku rocks, and Perl is annoying'
]
==> openrouter-chat-completion(:1024max-tokens, format => 'values')

Command Line Interface

The package provides a Command Line Interface (CLI) script:

openrouter-playground --help

Remark: When the authorization key argument "auth-key" is specified set to "Whatever" then openrouter-playground attempts to use the env variable OPENROUTER_API_KEY.

Mermaid diagram

The following flowchart corresponds to the steps in the package function openrouter-playground:

Integration with "LLM::Functions"

"WWW::OpenRouter" is integrated with "LLM::Functions", [AAp3]. Here is an LLM-configuration object for accessing OpenRouter's LLMs:

use LLM::Functions;

my $conf = llm-configuration('OpenRouter');

Here is an LLM-invocation using the LLM-configuration above:

llm-synthesize('Hi! What model are you? From which service? When you were trained?', e => $conf)

Integration with "Jupyter::Chatbook"

Jupyter chatbook (i.e., LLM-enabled Jupyter notebook) is integrated with the package "WWW::OpenRouter" in three ways:

  • "WWW::OpenRouter" is loaded in each chatbook session

  • The magic cell %%openrouter can be used to access with OpenRouter's LLMs

References

Dashboard & documentation

[OR1] OpenRouter, Inc., OpenRouter dashboard.

[OR2] OpenRouter, Inc., OpenRouter documentation.

Packages

[AAp1] Anton Antonov, WWW::OpenAI Raku package, (2023-2026), GitHub/antononcube.

[AAp2] Anton Antonov, WWW::Gemini Raku package, (2023-2026), GitHub/antononcube.

[AAp3] Anton Antonov, LLM::Functions Raku package, (2023-2026), GitHub/antononcube.

[AAp4] Anton Antonov, LLM::Prompts Raku package, (2023-2026), GitHub/antononcube.

[AAp5] Anton Antonov, Jupyter::Chatbook Raku package, (2023-2026), GitHub/antononcube.

WWW::OpenRouter v0.0.3

WWW::OpenRouter provides access to the OpenRouter playground (https://openrouter.ai).

Authors

  • Anton Antonov

License

Artistic-2.0

Dependencies

HTTP::Tiny:ver<0.2.5+>JSON::Fast:ver<0.20+>

Test Dependencies

Provides

  • WWW::OpenRouter
  • WWW::OpenRouter::ChatCompletions
  • WWW::OpenRouter::Embeddings
  • WWW::OpenRouter::Models
  • WWW::OpenRouter::Request

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.

Built with Podlite — the markup and publishing tools behind this site.