| 1 | package OpenAIGPT4; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | our $VERSION = '0.06'; | ||
| 7 | |||
| 8 | # ABSTRACT: Interact with the OpenAI GPT-4 API | ||
| 9 | |||
| 10 | 1 | ▶ | use LWP::UserAgent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | use LWP::Protocol::https | ||
| 12 | use HTTP::Request::Common qw(POST); | ||
| 13 | use JSON; | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | OpenAIGPT4 - Interact with the OpenAI GPT-4 API | ||
| 18 | |||
| 19 | =head1 VERSION | ||
| 20 | |||
| 21 | Version 0.06 | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | use OpenAIGPT4; | ||
| 26 | |||
| 27 | my $gpt = OpenAIGPT4->new('<your_api_key>'); | ||
| 28 | my $response = $gpt->generate_text('Hello, how are you?'); | ||
| 29 | print $response; | ||
| 30 | |||
| 31 | =head1 DESCRIPTION | ||
| 32 | |||
| 33 | This module provides a Perl interface to the OpenAI GPT-4 API. It currently supports generating text given a prompt. | ||
| 34 | |||
| 35 | =head1 METHODS | ||
| 36 | |||
| 37 | =head2 new | ||
| 38 | |||
| 39 | my $gpt = OpenAIGPT4->new('<your_api_key>'); | ||
| 40 | |||
| 41 | This constructor returns a new OpenAIGPT4 object. You must pass your OpenAI API key as the argument. | ||
| 42 | |||
| 43 | =head2 generate_text | ||
| 44 | |||
| 45 | my $response = $gpt->generate_text('Hello, how are you?'); | ||
| 46 | |||
| 47 | This method generates text given a prompt. The argument should be a string containing the prompt. It returns the generated text. | ||
| 48 | |||
| 49 | =head1 AUTHOR | ||
| 50 | |||
| 51 | Kawamura Shingo, <pannakoota@gmail.com> | ||
| 52 | |||
| 53 | =head1 LICENSE AND COPYRIGHT | ||
| 54 | |||
| 55 | Copyright 2023 Kawamura Shingo. | ||
| 56 | |||
| 57 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 58 | |||
| 59 | =head1 SYNOPSIS | ||
| 60 | |||
| 61 | use OpenAIGPT4; | ||
| 62 | |||
| 63 | my $gpt = OpenAIGPT4->new('<your_api_key>'); | ||
| 64 | my $response = $gpt->generate_text('Hello, how are you?'); | ||
| 65 | print $response; | ||
| 66 | |||
| 67 | # Or for a more interactive example: | ||
| 68 | |||
| 69 | my $gpt4 = OpenAIGPT4->new('<your_api_key>'); | ||
| 70 | print "ChatGPT: Hello! Let's start a conversation.\n"; | ||
| 71 | |||
| 72 | while (1) { | ||
| 73 | print "User: "; | ||
| 74 | my $user_input = <STDIN>; | ||
| 75 | chomp $user_input; | ||
| 76 | |||
| 77 | # Send the user's input to the API and receive a response | ||
| 78 | my $response = $gpt4->generate_text($user_input); | ||
| 79 | |||
| 80 | # Display the response | ||
| 81 | print "ChatGPT: $response\n"; | ||
| 82 | |||
| 83 | # Check for exit condition (e.g., input of the keyword "exit") | ||
| 84 | if ($user_input eq 'exit') { | ||
| 85 | last; # Exit the loop to end the conversation | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub new { | ||
| 92 | my ($class, $api_key) = @_; | ||
| 93 | |||
| 94 | my $self = { | ||
| 95 | api_key => $api_key, | ||
| 96 | ua => LWP::UserAgent->new, | ||
| 97 | history => [], # Keep track of conversation history | ||
| 98 | }; | ||
| 99 | |||
| 100 | return bless $self, $class; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub generate_text { | ||
| 104 | my ($self, $prompt) = @_; | ||
| 105 | |||
| 106 | push @{$self->{history}}, {role => 'user', content => $prompt}; | ||
| 107 | |||
| 108 | my $req = POST 'https://api.openai.com/v1/chat/completions', | ||
| 109 | Content_Type => 'application/json', | ||
| 110 | Content => to_json({ | ||
| 111 | messages => $self->{history}, | ||
| 112 | model => 'gpt-3.5-turbo', | ||
| 113 | temperature => 0.7 | ||
| 114 | }), | ||
| 115 | Authorization => 'Bearer ' . $self->{api_key}; | ||
| 116 | |||
| 117 | my $res = $self->{ua}->request($req); | ||
| 118 | |||
| 119 | if ($res->is_success) { | ||
| 120 | my $data = from_json($res->decoded_content); | ||
| 121 | my $reply = $data->{choices}[0]{message}{content}; | ||
| 122 | |||
| 123 | # Add the model's reply to the conversation history | ||
| 124 | push @{$self->{history}}, {role => 'system', content => $reply}; | ||
| 125 | |||
| 126 | return $reply; | ||
| 127 | } | ||
| 128 | else { | ||
| 129 | die $res->status_line; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | 1; | ||