Writing a CPAN module that talks to ChatGPT

ChatGPT code

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:

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

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:

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

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:

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:

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

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

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:


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

5 thoughts on “Writing a CPAN module that talks to ChatGPT

  1. 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.

    1. 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).

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.