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