DEV Community

Cover image for Writing a CPAN module that talks to ChatGPT
Dave Cross
Dave Cross

Posted on • Originally published at perlhacks.com on

Writing a CPAN module that talks to ChatGPT

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:

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;
Enter fullscreen mode Exit fullscreen mode

To use this module in your Perl code, you can create a new ChatGPT object and call the send_prompt method like this:

use ChatGPT;

my $chatgpt = ChatGPT->new(host => 'localhost', port => 8080);
my $response = $chatgpt->send_prompt('What is your name?');
print "Response: $response\n";
Enter fullscreen mode Exit fullscreen mode

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:

cpanm Moose
Enter fullscreen mode Exit fullscreen mode

With Moose installed, you can modify the class like this:

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;
Enter fullscreen mode Exit fullscreen mode

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:

use ChatGPT;

my $chatgpt = ChatGPT->new(host => 'localhost', port => 8080);

my $response = $chatgpt->send_prompt('What is your name?');

print "Response: $response\n";
Enter fullscreen mode Exit fullscreen mode

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:

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;
Enter fullscreen mode Exit fullscreen mode

With these changes, the class is now called WebService::ChatGPT and can be used like this:

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";
Enter fullscreen mode Exit fullscreen mode

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):

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();
Enter fullscreen mode Exit fullscreen mode

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:

prove -l t/chatgpt.t
Enter fullscreen mode Exit fullscreen mode

I haven’t uploaded it to CPAN yet. Do you think I should?

The post Writing a CPAN module that talks to ChatGPT appeared first on Perl Hacks.

Top comments (3)

Collapse
 
john_napiorkowski_00db306 profile image
john napiorkowski

upload it please. If you're not certain about the approach just say its beta subject to change. Also maybe use Moo instead of Moose to reduce the dependency count. You're not using anything here that is especially Moose restricted.

Collapse
 
john_napiorkowski_00db306 profile image
john napiorkowski

You're awesome for writing this, more of us Perl programmers need to blog this well.

Collapse
 
johnse8899 profile image
johnse8899

Share with everyone a very convenient support tool that is ChatGPT for Search Engines. Can directly chat on Extension without accessing on Openai
Image description