App-Chit
view release on metacpan or search on metacpan
$ chit "Write a poem about almonds."
=head2 chit show
Show chat history. C<< --colour >> can be used to force ANSI colours.
C<< --nocolour >> or the C<NO_COLOR> environment variable can be used
to disable ANSI colours. If neither is used, ANSI colours will be used
when B<chit> detects it is outputting to a TTY.
Use C<< --msg=N >> to control the number of messages displayed. Defaults
to twenty, but each prompt and response count as two messages.
=head2 chit which
Prints the path to the directory where F<< .chit.yml >> was found.
=head2 chit maxhistory
Sets or shows the currently configured maximum number of history lines
to keep.
lib/App/Chit/Command/chat.pm view on Meta::CPAN
our $AUTHORITY = 'cpan:TOBYINK';
our $VERSION = '0.001001';
sub abstract {
return "Chat with ChatGPT."
}
sub opt_spec {
return (
[ "temperature=f", "temperature for response" ],
[ "stdin", "prompt from STDIN" ],
[ "file=s", "prompt from file" ],
);
}
sub validate_args ( $self, $opt, $args ) {
unless ( $opt->{stdin} or $opt->{file} ) {
$self->usage_error( "expected a prompt on the command line, or -i, or -f" )
unless ( @$args == 1 and length($args->[0]) > 2 );
}
$self->usage_error( "cannot use both -i and -f" )
if ( $opt->{stdin} and $opt->{file} );
}
sub execute ( $self, $opt, $args ) {
my $dir = App::Chit::Util::find_chit_dir()
or $self->usage_error("need to initialize chit first");
my $chit = App::Chit::Util::load_chit( $dir );
my $spin;
$spin = Term::Spinner::Color->new(
'delay' => 0.3,
'colorcycle' => 1,
) if -t STDOUT;
$spin->auto_start if $spin;
my $prompt = do {
if ( $opt->{stdin} ) {
local $/;
<STDIN>;
}
elsif ( $opt->{file} ) {
my $f = path( $opt->{file} );
$f->is_file or croak("File does not exist");
$f->slurp_utf8;
}
else {
$args->[0];
}
};
chomp $prompt;
my @log = @{ $chit->{chat} or [] };
unshift @log, { role => 'system', content => $chit->{role} };
push @log, { role => 'user', content => $prompt };
my $temperature = $opt->{temperature} // $chit->{temperature};
my $gpt = App::Chit::Util::chatgpt( $chit );
my $response = $gpt->chat( \@log, $temperature );
$spin->auto_done if $spin;
if ( defined $response ) {
chomp $response;
say $response;
push @{ $chit->{chat} //= [] },
{ role => 'user', content => $prompt },
{ role => 'assistant', content => $response };
}
else {
croak( "Error: " . $gpt->error );
}
App::Chit::Util::save_chit( $dir, $chit );
}
1;
( run in 1.053 second using v1.01-cache-2.11-cpan-6aa56a78535 )