ONO::Lib::AI::OpenAI::ChatGPT

package ONO::Lib::AI::OpenAI::ChatGPT;
################################################################################
# COPYRIGHT / LICENSE #
################################################################################
#
# This file is part of the ONO Software Project.
#
# Copyright (C) 2000-2025 Jos KIRPS [ www.kirps.com | jos_AT_kirps_DOT_com ]
# and The Joopita Project [ www.joopita.org | contact_AT_joopita_DOT_com ]
#
# This file, as well as other parts of the ONO Software Project or related
# elements, are FREE SOFTWARE available under the ARTISTIC LICENSE 2.0.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# For the full license, see /ono/osr/license/LICENSE.txt, or write to
# jos_AT_kirps_DOT_com or contact_AT_joopita_DOT_com.
#
################################################################################
# END OF COPYRIGHT / LICENSE, HERE COMES THE CODE ... #
################################################################################


use strict;

use ONO::IO;

use ONO::Lib::AI::OpenAI::ToolBox;
use ONO::Lib::Text::LoremIpsum;

###############################################################################
# ONO
###############################################################################

sub ask {

my (
$self,
$question,
$switches,
) = @_;

#: Ask ChatGPT, which will return an answer if OpenAI software is installed
#: on the local machine. Check ChatGPT_README.txt for info on how to install
#: the required modules.
#:
#: You may use ChatGPT.pl to test your setup.
#:
#: ChatGPT.py is responsible for the actual I/O. Don't use it directly,
#: always, use this module.
#:
#: -D override devstation mode (always execute)

$question = substr($question,0,255);
$question =~ s~(\'|\||\n|\r|\t)~ ~g;

my $ANSWER;

if ($question =~ /[A-Za-z]/ && length $question > 3) {

if (ONO::IO->devstation && $switches !~ /D/) {

$ANSWER = "ONO_Lib_AI_OpenAI_ChatGPT Dev Message:\n\n".ONO::Lib::Text::LoremIpsum->gen();

} else {

if (ONO::IO->exists("etc/keys/openai.txt")) {

# option 1: use the ONO python script

my $vpath = ONO::IO->path;

$ANSWER = `$vpath/ono/sys/ONO/Lib/AI/OpenAI/ChatGPT.py '$question' '$vpath/etc/keys/openai.txt'`;

# option 2: if this doesn't work, then try to call openai directly:

if ($ANSWER !~ /[A-Za-z0-9]/) {

my $openai = ONO::Lib::AI::OpenAI::ToolBox->detect_openai();

my $TOKEN = ONO::IO->load("etc/keys/openai.txt");
$TOKEN =~ s~[^A-Za-z0-9\-]~~g;

if ($openai && $TOKEN) {

$ANSWER .= `echo '$question' | $openai complete - -t '$TOKEN'`;

} else {

$ANSWER .= "ERROR: OpenAI not installed, or empty token/key, please check...";

}

}

} else {

$ANSWER = "ERROR: ONO OpenAI token/key is missing, please install...";

}

}

my $whilecounter;
if ($whilecounter < 255 && ($ANSWER =~ s~^(\n|\r|\t| )~~ || $ANSWER =~ s~(\n|\r|\t| )$~~)) {
$whilecounter++;

$ANSWER =~ s~^(\n|\r|\t| )~~;
$ANSWER =~ s~(\n|\r|\t| )$~~;

}

}

return $ANSWER;

}

###############################################################################
# end of script
###############################################################################

1;

__END__