WWW::OpenAI (for ChatGPT and other statistical gimmicks)

In brief

The Raku package “WWW::OpenAI” provides access to the machine learning service OpenAI, [OAI1]. For more details of the OpenAI’s API usage see the documentation, [OAI2].

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

Remark: This Raku package is much “less ambitious” than the official Python package, [OAIp1], developed by OpenAI’s team. Gradually, over time, I expect to add features to the Raku package that correspond to features of [OAIp1].

The design and implementation of “WWW::OpenAI” are very similar to those of “Lingua::Translation::DeepL”, [AAp1].


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

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

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

Usage examples

Remark: When the authorization key, auth-key, is specified to be Whatever then openai-playground attempts to use the env variable OPENAI_API_KEY.

Basic usage

Here is a simple call:

use WWW::OpenAI;
say openai-playground('Where is Roger Rabbit?');

# [{finish_reason => stop, index => 0, message => {content => 
# As an AI language model, I do not have access to real-time information or location tracking features. Therefore, I cannot provide an accurate answer to the question "Where is Roger Rabbit?" without additional context. However, if you are referring to the fictional character Roger Rabbit, he is a cartoon character created by Disney and is typically found in various media, including films, television shows, and comic books., role => assistant}}]

Another one using Bulgarian:

say openai-playground('Колко групи могат да се намерят в този облак от точки.');

# [{finish_reason => stop, index => 0, message => {content => 
# Като асистент на AI, не мога да видя облак от точки, за да мога да дам точен отговор на този въпрос. Моля, предоставете повече информация или конкретен пример, за да мога да ви помогна., role => assistant}}]

Command Line Interface

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

openai-playground --help

# Usage:
#   openai-playground <text> [-m|--model=<Str>] [-r|--role=<Str>] [-t|--temperature[=Real]] [-a|--auth-key=<Str>] [--timeout[=UInt]] [--format=<Str>] -- Text processing using the OpenAI API.
#     <text>                     Text to be processed.
#     -m|--model=<Str>           Model. [default: 'Whatever']
#     -r|--role=<Str>            Role. [default: 'user']
#     -t|--temperature[=Real]    Temperature. [default: 0.7]
#     -a|--auth-key=<Str>        Authorization key (to use OpenAI API.) [default: 'Whatever']
#     --timeout[=UInt]           Timeout. [default: 10]
#     --format=<Str>             Format of the result; one of "json" or "hash". [default: 'json']

Remark: When the authorization key argument “auth-key” is specified set to “Whatever” then openai-playground attempts to use the env variable OPENAI_API_KEY.

Mermaid diagram

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


[AAp1] Anton Antonov, Lingua::Translation::DeepL Raku package, (2022), GitHub/antononcube.

[OAI1] OpenAI Platform, OpenAI platform.

[OAI1] OpenAI Platform, OpenAI documentation.

[OAIp1] OpenAI, OpenAI Python Library, (2020), GitHub/openai.

Literate programming via CLI

In this blog post we demonstrate how to do “Literate programming” in Raku via (short) pipelines of Command Line Interface (CLI) scripts.

An alternative of this CLI-based process is to use Mathematica or Jupyter notebooks.

Presentation’s GitHub directory

Video recording

Steps and components

Here is a narration of the diagram above:

  1. Write some text and code in a Markdown file
  2. Weave the Markdown file (i.e. “run it”)
  3. If the woven file the does not have D3.js graphics go to 5
  4. Convert the woven Markdown file into HTML
  5. Examine results
  6. Go to 1
    • Or finish “fiddling with it”

The conversions

The we use the document “Cryptocurrencies-explorations.md” (with Raku code over cryptocurrencies data.)

If no D3.js graphics are specified then we can use the shell command:

file-code-chunks-eval Cryptocurrencies-explorations.md

If D3.js graphics are specified then we can use the shell command:

file-code-chunks-eval Cryptocurrencies-explorations.md && 
  from-markdown Cryptocurrencies-explorations_woven.md -t html -o out.html && 
  open out.html

Remark: It is instructive to examine “Cryptocurrencies-explorations_woven.md” and compare it with “Cryptocurrencies-explorations.md”.

Remark The code chunks with graphics (using “JavaScript::D3”) have to have the chunk option setting results=asis.

Remark The “JavaScript::D3” commands have to have the option settings format => 'html' and div-id => ....



[AA1] Anton Antonov “Raku Text::CodeProcessing”, (2021), RakuForPrediction at WordPress.

[AA2] Anton Antonov “JavaScript::D3”, (2022), RakuForPrediction at WordPress.

[AA3] Anton Antonov “Further work on the Raku-D3.js translation”, (2022), RakuForPrediction at WordPress.


[AAp1] Anton Antonov, Data::Cryptocurrencies Raku package, (2023). GitHub/antononcube.

[AAp2] Anton Antonov, JavaScript::D3 Raku package, (2022). GitHub/antononcube.

[AAp3] Anton Antonov, Text::CodeProcessing Raku package, (2021). GitHub/antononcube.

[AAp4] Anton Antonov, Markdown::Grammar, (2022). GitHub/antononcube.