README-work

WWW::XAI

In brief

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

Remark: To use XAI'1 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::XAI" 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 XAI'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::XAI

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

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

Universal "front-end"

The package has an universal "front-end" function xai-console for the different functionalities provided by XAI.

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

use WWW::XAI;
my $ans = xai-console('Where is Roger Rabbit?');
to-json(from-json($ans), :pretty)

Remark: By default xai-console returns just a compact JSON string of XAI's response. That is why above, in order to get a pretty JSON display, is used line to-json(from-json($ans), :pretty).

Another one using Bulgarian:

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

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

Models

The current XlAI models can be found with the function xai-models:

.say for |xai-models;

Code generation

XAI'API provides a special endpoint for code generation which is used if xai-console's argument "path" is set to "code". Here is a Raku code generation example:

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

Images

Images can be generated with the sub xai-console with the argument "path" being set to "image". For example, here an image is generated and a URL to is returned:

my $res = xai-console('Generate an image of a raccoon chasing a butterfly.', path => 'image', format => 'values');

Here is an example in which a Base64 string is returned and then rendered as an image:

use Image::Markup::Utilities;
my $img = xai-console(
    'Sketches of butterfly themed playing cards (for bridge, etc.)', 
    path => 'image', 
    response-format => 'b64_json',
    format => 'values');
image-from-base64($img);

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 a chat completion with emojification:

xai-console([ system => $preEmojify, user => 'Python sucks, Raku rocks, and Perl is annoying'], max-tokens => 1024, format => 'values')

Command Line Interface

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

xai-console --help

Remark: When the authorization key argument "auth-key" is specified set to "Whatever" then xai-console attempts to use the env variable XAI_API_KEY.

Remark: When the authorization key argument "auth-key" is specified set to Whatever then xai-console attempts to use the env variable XAI_API_KEY.

Here we submit a video request via the CLI script:

xai-console --path=video 'An otter swimming to boat and offering a fish.' --format='asis'
# {request_id => 938fe8b9-86b9-9b8d-9cfc-3f740e8c4bd7}

Remark: It takes awhile to create the video, hence we just get a video identifier as a response.

Here we get the URL (and other metadata) of the created video:

xai-console --video-id=938fe8b9-86b9-9b8d-9cfc-3f740e8c4bd7
# {model => grok-imagine-video, progress => 100, status => done, usage => {cost_in_usd_ticks => 4000000000}, video => {duration => 8, respect_moderation => True, url => https://vidgen.x.ai/xai-vidgen-bucket/xai-video-938fe8b9-86b9-9b8d-9cfc-3f740e8c4bd7.mp4}}

Mermaid diagram

The following flowchart corresponds to the steps in the package function xai-console:

Integration with "LLM::Functions"

Since XAI's API does not provide embeddings, for now XAI is not by default integrated with "LLM::Functions", [AAp3]. Here is an LLM-configuration object for accessing XAI's LLMs:

use LLM::Functions;

my &xaichat = sub ($prompt, *%args) { xai-console($prompt, path => 'chat', format => 'values', |%args) };

my $conf = llm-configuration('ChatGPT', 
    name => 'ChatXAI', 
    module => 'WWW::XAI',
    model => 'grok-4.2', 
    base-url => xai-base-url, 
    function => &xaichat
)

Here is an LLM-invocation using the XAI-access 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::XAI" in three ways:

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

  • The magic cell %%xai can be used to access with XAI's LLMs

  • The magic cell %%xai-images can be used to generate images with XAI's creation or editing models

For more details see the notebook "Raku-access-to-XAI-LLMs.ipynb" or [AA1].

References

Articles, blog posts

[AA1] Anton Antonov, "Raku access to XAI LLMs", (2026), RakuForPrediction at WordPress.

Dashboard & documentation

[XAI1] XI, XAI console.

[XAI2] XAI Platform documentation, XAI 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::XAI v0.1.3

WWW::XAI provides API access to the xAI console (https://console.x.ai/).

Authors

  • Anton Antonov

License

Artistic-2.0

Dependencies

HTTP::Tiny:ver<0.2.5+>JSON::Fast:ver<0.17+>Image::Markup::Utilities:ver<0.1.2+>

Test Dependencies

Provides

  • WWW::XAI
  • WWW::XAI::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.