in reply to Artificial Intelligence with Perl in 2023

Last evening I released AI::Chat which allows prompts to be sent to the OpenAI API. It makes integrating AI into Perl applications quite easy.

AI::Image will follow soon which taps into DALL-E to generate AI images from within Perl.

  • Comment on Re: Artificial Intelligence with Perl in 2023

Replies are listed 'Best First'.
Re^2: Artificial Intelligence with Perl in 2023
by Danny (Chaplain) on Mar 03, 2024 at 18:30 UTC
    According to google, you need to fund your openai account to get an api key. Is that correct?

    Also possible small typo in your "description": "non-deterministic, that, is the same prompt" > "non-deterministic, that is, the same prompt"

    UPDATE: I was able to get an API key without paying. https://platform.openai.com/api-keys

      The API key is free but there is a charge everytime you use it. The cost is dependent on the model used and the number of tokens input and output.

      I have set the default model to the lowest price of the useful models as this is a good starting point. If you want more capable models such as ChatGPT-4, you will pay more.

      UPDATE - I'll add this to the documentation when I do an update

Re^2: Artificial Intelligence with Perl in 2023
by Danny (Chaplain) on Mar 03, 2024 at 18:54 UTC
    When I run the script below as:
    gpt.pl "what is the meaning of life"
    $chat->prompt($prompt, $TEMP);
    is returning undefined.
    what is the meaning of life Use of uninitialized value $reply in concatenation (.) or string at /h +ome/****/com/gpt.pl line 25.
    #!/usr/bin/env perl my @BOOL = qw(false true); my $VERBOSE = 0; use warnings; use strict; use Getopt::Long; use AI::Chat; our $KEY = "my-key"; our $API = "OpenAI"; our $MODEL = "gpt-3.5-turbo-0125"; our $ROLE = undef; our $TEMP = 1.0; my ($prompt) = parseArgs(); my %h = (key => $KEY, api => $API, model => $MODEL); defined $ROLE and $h{role} = $ROLE; my $chat = AI::Chat->new(%h); my $reply = $chat->prompt($prompt, $TEMP); print "$prompt\n\n"; print "$reply\n"; sub parseArgs { my @args = qw(prompt); my $usage = qq{usage: $0 @args [options] prompt should be a quoted string. options ------- -key (default: $KEY) API key -role The role to use for the bot in conversations. This tells the bot + what it's purpose when answering prompts. For example: "You are a world class c +opywriter famed for creating content that is immediately engaging with a lighth +earted, storytelling style". -temperature (default: $TEMP) The creativity level of the response (de +fault: 1.0). Temperature ranges from 0 to 2. The higher the temperature, the + more creative the bot will be in it's responses. -verbose (default: $BOOL[$VERBOSE]) }; my $result = GetOptions ( 'key=s' => \$KEY, 'role=s' => \$ROLE, 'temperature=f' => \$TEMP, 'verbose!' => \$VERBOSE, ); $result or print $usage and exit; @ARGV == @args or print $usage and exit; return @ARGV; }
      $chat->prompt($prompt, $TEMP);
      is returning undefined.

      Have you tried checking $chat->success?

      promt returns undef if no prompt parameter is supplied - or more precisely, if the prompt parameter is not a non-zero length string.

        Yes. $chat->success() is true in the following. But $reply is undef.

        It's probably because I haven't properly set up my openai account. I got the api key but I probably need to give it my credit card or something.

        The following

        my $chat = AI::Chat->new(%h); $chat->success() or die sprintf "error 1: %s\n", $chat->error(); print "$chat\n"; my $reply = $chat->prompt($prompt, $TEMP); $chat->success() or die sprintf "error 2: %s\n", $chat->error(); defined $reply or die sprintf "error 3: %s\n", $chat->error();
        prints:
        AI::Chat=HASH(0xa0042da30) error 3:
AI::Image (was: Re^2: Artificial Intelligence with Perl in 2023)
by Bod (Parson) on Mar 06, 2024 at 23:32 UTC

    The first version of AI::Image is now on CPAN

    Comments welcome as always...