ChatGPT exposes an API, but there’s no CPAN module for taking advantage of that. Let’s put that right (with help from ChatGPT)…
Write a Perl CPAN module for connecting to a ChatGPT server
To create a Perl CPAN module for connecting to a ChatGPT server, you will need to have Perl and the necessary dependencies installed on your system.
To begin, create a new directory for your module and create a file called “ChatGPT.pm” with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package ChatGPT; use strict; use warnings; use Net::WebSocket::Client; use Net::WebSocket::Server; use HTTP::Async; sub new { my ($class, %params) = @_; my $self = { host => $params{host}, port => $params{port}, }; bless $self, $class; return $self; } sub send_prompt { my ($self, $prompt) = @_; my $http = HTTP::Async->new; my $client = Net::WebSocket::Client->new( url => "ws://$self->{host}:$self->{port}" ); $client->connect; $client->send_utf8($prompt); my $response = $client->recv; $client->disconnect; return $response; } 1; |
To use this module in your Perl code, you can create a new ChatGPT object and call the send_prompt
method like this:
1 2 3 4 5 |
use ChatGPT; my $chatgpt = ChatGPT->new(host => 'localhost', port => 8080); my $response = $chatgpt->send_prompt('What is your name?'); print "Response: $response\n"; |
This will send the text prompt “What is your name?” to the ChatGPT server running on localhost at port 8080, and print the response to the console.
Note: This is just a basic example of how you can create a Perl CPAN module for connecting to a ChatGPT server. There are many other features and considerations that you may want to add to your module, such as error handling and customization options.
To make your module available on CPAN, you will need to follow the guidelines for creating and distributing CPAN modules, which can be found in the Perl documentation and online resources.
Use Moose to create the class
To use Moose to create the ChatGPT
class, you will need to have Moose installed on your system. You can install Moose by running the following command:
1 |
cpanm Moose |
With Moose installed, you can modify the class like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package ChatGPT; use Moose; use Net::WebSocket::Client; use Net::WebSocket::Server; use HTTP::Async; has host => ( is => 'ro', isa => 'Str', required => 1, ); has port => ( is => 'ro', isa => 'Int', required => 1, ); sub send_prompt { my ($self, $prompt) = @_; my $http = HTTP::Async->new; my $client = Net::WebSocket::Client->new( url => "ws://$self->{host}:$self->{port}" ); $client->connect; $client->send_utf8($prompt); my $response = $client->recv; $client->disconnect; return $response; } 1; |
In this version of the class, the host
and port
attributes are defined using Moose’s has
keyword. These attributes are required and must be specified when creating a new ChatGPT
object. The send_prompt
method is unchanged from the previous example.
To create a new ChatGPT
object, you can use the new
method provided by Moose like this:
1 2 3 4 5 6 7 |
use ChatGPT; my $chatgpt = ChatGPT->new(host => 'localhost', port => 8080); my $response = $chatgpt->send_prompt('What is your name?'); print "Response: $response\n"; |
This will create a new ChatGPT
object with the specified host
and port
attributes, and send the text prompt “What is your name?” to the ChatGPT server running on localhost at port 8080. The response will be printed to the console.
Note: This is just one way to use Moose to create the ChatGPT
class. There are many other features and customization options available in Moose that you may want to consider adding to your class, such as type constraints, default values, and methods.
Call the class WebService::ChatGPT
To rename the ChatGPT
class to WebService::ChatGPT
, you will need to update the package name at the top of the file and update the use
statements to refer to the new package name.
Here is how you can update the ChatGPT
class to use the new package name:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package WebService::ChatGPT; use Moose; use Net::WebSocket::Client; use Net::WebSocket::Server; use HTTP::Async; has host => ( is => 'ro', isa => 'Str', required => 1, ); has port => ( is => 'ro', isa => 'Int', required => 1, ); sub send_prompt { my ($self, $prompt) = @_; my $http = HTTP::Async->new; my $client = Net::WebSocket::Client->new( url => "ws://$self->{host}:$self->{port}" ); $client->connect; $client->send_utf8($prompt); my $response = $client->recv; $client->disconnect; return $response; } 1; |
With these changes, the class is now called WebService::ChatGPT
and can be used like this:
1 2 3 4 5 6 |
use WebService::ChatGPT; my $chatgpt = WebService::ChatGPT->new(host => 'localhost', port => 8080); my $response = $chatgpt->send_prompt('What is your name?'); print "Response: $response\n"; |
This will create a new WebService::ChatGPT
object with the specified host
and port
attributes, and send the text prompt “What is your name?” to the ChatGPT server running on localhost at port 8080. The response will be printed to the console.,/p>
Note: This is just one way to rename the ChatGPT
class to WebService::ChatGPT
. You may also need to update any other references to the class name in your code and documentation, as well as update the module file name and directory structure to reflect the new package name.
Suggest some tests using the standard Perl testing framework
Here are some examples of tests that you could write for the WebService::ChatGPT
module using the standard Perl testing framework (Test::More):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
use Test::More; use WebService::ChatGPT; # Test creating a new ChatGPT object my $chatgpt = WebService::ChatGPT->new(host => 'localhost', port => 8080); isa_ok($chatgpt, 'WebService::ChatGPT', 'Object is a ChatGPT'); # Test sending a prompt and receiving a response my $response = $chatgpt->send_prompt('What is your name?'); is($response, 'My name is ChatGPT.', 'Response is correct'); # Test sending an invalid prompt eval { $chatgpt->send_prompt('') }; like($@, qr/Invalid prompt/, 'Invalid prompt error is thrown'); done_testing(); |
The first test uses the isa_ok
function to check that the object created by the new
method is an instance of the WebService::ChatGPT
class.
The second test uses the is
function to compare the expected response to the actual response returned by the send_prompt
method.
The third test uses the eval
function to catch any exceptions thrown by the send_prompt
method when an invalid prompt is provided, and the like
function to check that the exception message matches the expected error message.
These tests can be run using the prove
command:
1 |
prove -l t/chatgpt.t |
I haven’t uploaded it to CPAN yet. Do you think I should?
Any particular reason you have HTTP::Async use’d but not used? Doesn’t appear appropriate for a module that has blocking as the desired behavior even if it was used.
It wasn’t me who wrote that code – it was ChatGPT (sorry if that wasn’t clear).
The code is full of errors. That’s only one of them. I’m not sure why ChatGPT is so bad at generating Perl code (when it generally seems to do a lot better with, say, Javascript).
Yes!! Awesome work π
I can not find Net::WebSocket::Client. Do you know what CPAN module contains it?
It seems it doesn’t exist. ChatGPT hallucinated it.