Writing a CPAN module that talks to ChatGPT
Tags: programming, ai, chatgpt, cpan
Published: 2022-12-20 16:19:19

---
title: Writing a CPAN module that talks to ChatGPT
published: true
date: 2022-12-20 16:12:38 UTC
tags: Programming,ai,chatgpt,cpan
canonical_url: https://perlhacks.com/2022/12/writing-a-cpan-module-that-talks-to-chatgpt/
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5fovpjfpty7arp3r0sg2.png
---

[ChatGPT](https://chat.openai.com/chat) 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:

```perl
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:

```perl
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:

```shell
cpanm Moose
```

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

```perl
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:

```perl
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:

```perl
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:

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

```perl
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:

```shell
prove -l t/chatgpt.t
```

* * *

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

The post [Writing a CPAN module that talks to ChatGPT](https://perlhacks.com/2022/12/writing-a-cpan-module-that-talks-to-chatgpt/) appeared first on [Perl Hacks](https://perlhacks.com).