OpenAIGPT4 is a Perl module that enables developers to interface with the OpenAI GPT API. With this module, you can easily generate natural language text.
This module depends on the following Perl modules:
- LWP::UserAgent
- LWP::Protocol::https
- HTTP::Request::Common
- JSON
- Generate text that sounds like natural language with the OpenAI GPT API.
- Debug mode for detailed logging of HTTP requests and responses to and from the OpenAI API.
The module is self-contained and can be included directly into your project. Save it as OpenAIGPT4.pm and ensure that Perl's @INC array can reference its location.
perl Makefile.PL
make
make test
make install
With CPAN:
cpan OpenAIGPT4
You will need an API key to use the OpenAI API. You can get an API key from OpenAI's API key page.
use OpenAIGPT4;
my $gpt = OpenAIGPT4->new('YOUR_OPENAI_API_KEY');
Replace 'YOUR_OPENAI_API_KEY' with your API key obtained from the OpenAI dashboard.
Use the generate_text method to generate text. This method takes a prompt and returns the model's response.
my $response = $gpt->generate_text('Tell me a joke');
print $response;
The generate_text method can also accept a model name and temperature parameter as options. By default, if not specified, the model name is set to 'gpt-3.5-turbo-0613' and temperature to 0.7.
my $response = $gpt->generate_text('Tell me a joke', 'gpt-3.5-turbo-0613', 0.8);
print $response;
If you need debugging, set the debug flag in the new method.
my $gpt = OpenAIGPT4->new('YOUR_OPENAI_API_KEY', undef, 1);
With this flag set, details of the requests and responses will be outputted.
You can also create an interactive conversation with gpt-3.5-turbo-0613 using a loop. Here's an example:
use OpenAIGPT4;
my $gpt = OpenAIGPT4->new('YOUR_OPENAI_API_KEY');
print "ChatGPT: Hello! Let's start a conversation.\n";
while (1) {
print "User: ";
my $user_input = <STDIN>;
chomp $user_input;
# Send the user's input to the API and receive a response
my $response = $gpt->generate_text($user_input);
# Display the response
print "ChatGPT: $response\n";
# Check for exit condition (e.g., input of the keyword "exit")
if ($user_input eq 'exit') {
last; # Exit the loop to end the conversation
}
}
Using the OpenAI GPT API involves rate limits and costs, dictated by your subscription plan. Exceeding your quota will result in an error message and possible delays or temporary suspension of API requests. For detailed information, visit OpenAI's Pricing page.
The following YouTube link includes a usage demo for this module:
Kawamura Shingo (pannakoota@gmail.com)
This module is distributed under the terms of the Artistic License 2.0.
Please see LICENSE for more details.
LocalAI is an OpenAI API compatible system for locally hosting models