in reply to Re: Artificial Intelligence with Perl in 2023
in thread Artificial Intelligence with Perl in 2023

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; }

Replies are listed 'Best First'.
Re^3: Artificial Intelligence with Perl in 2023
by Bod (Parson) on Mar 03, 2024 at 19:56 UTC
    $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:

        I've just uploaded version 0.2 to CPAN

        This new version has a new debug parameter. When this is true, the prompt method returns the full error if there is one instead of under.

        I got the api key but I probably need to give it my credit card or something

        When I set up our API key, I got $18 credit. I'm not sure if you still do as it was quite a while since I got the key.

        The API calls are not costly. Most requests to the chat API come in at under $0.01.