| 1 | package AI::Embedding; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 3 | ▶ | use HTTP::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | ▶ | use JSON::PP; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use Data::CosineSimilarity; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 3 | our $VERSION = '1.01'; | |||||||||
| 11 | 3 | $VERSION = eval $VERSION; | |||||||||
| 12 | |||||||||||
| 13 | 3 | my $http = HTTP::Tiny->new; | |||||||||
| 14 | |||||||||||
| 15 | # Create Embedding object | ||||||||||
| 16 | sub new { | ||||||||||
| 17 | 4 | ▶ | my $class = shift; | ||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 18 | 4 | my %attr = @_; | |||||||||
| 19 | |||||||||||
| 20 | 4 | $attr{'error'} = ''; | |||||||||
| 21 | |||||||||||
| 22 | 4 | ▶ | $attr{'api'} = 'OpenAI' unless $attr{'api'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 4 | ▶ | $attr{'error'} = 'Invalid API' unless $attr{'api'} eq 'OpenAI'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 4 | ▶ | $attr{'error'} = 'API Key missing' unless $attr{'key'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | |||||||||||
| 26 | 4 | ▶ | $attr{'model'} = 'text-embedding-ada-002' unless $attr{'model'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | |||||||||||
| 28 | 4 | return bless \%attr, $class; | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | # Define endpoints for APIs | ||||||||||
| 32 | 3 | my %url = ( | |||||||||
| 33 | 'OpenAI' => 'https://api.openai.com/v1/embeddings', | ||||||||||
| 34 | ); | ||||||||||
| 35 | |||||||||||
| 36 | # Define HTTP Headers for APIs | ||||||||||
| 37 | 3 | my %header = ( | |||||||||
| 38 | 'OpenAI' => &_get_header_openai, | ||||||||||
| 39 | ); | ||||||||||
| 40 | |||||||||||
| 41 | # Returns true if last operation was success | ||||||||||
| 42 | sub success { | ||||||||||
| 43 | 6 | ▶ | my $self = shift; | ||||||||
|
Subroutine
success: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 44 | 6 | return !$self->{'error'}; | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | # Returns error if last operation failed | ||||||||||
| 48 | sub error { | ||||||||||
| 49 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 50 | 1 | return $self->{'error'}; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | # Header for calling OpenAI | ||||||||||
| 54 | sub _get_header_openai { | ||||||||||
| 55 | 3 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_get_header_openai: called CC=2 SCAR=7.1
| |||||||||||
| 56 | 3 | ▶ | $self->{'key'} = '' unless defined $self->{'key'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | return { | ||||||||||
| 58 | 3 | 'Authorization' => 'Bearer ' . $self->{'key'}, | |||||||||
| 59 | 'Content-type' => 'application/json' | ||||||||||
| 60 | }; | ||||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | # Fetch Embedding response | ||||||||||
| 64 | sub _get_embedding { | ||||||||||
| 65 | 0 | ▶ | my ($self, $text) = @_; | ||||||||
|
Subroutine
_get_embedding: not called CC=1 SCAR=6.9
| |||||||||||
| 66 | |||||||||||
| 67 | return $http->post($url{$self->{'api'}}, { | ||||||||||
| 68 | 'headers' => { | ||||||||||
| 69 | 'Authorization' => 'Bearer ' . $self->{'key'}, | ||||||||||
| 70 | 'Content-type' => 'application/json' | ||||||||||
| 71 | }, | ||||||||||
| 72 | content => encode_json { | ||||||||||
| 73 | input => $text, | ||||||||||
| 74 | 0 | model => $self->{'model'}, | |||||||||
| 75 | } | ||||||||||
| 76 | }); | ||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | # TODO: | ||||||||||
| 80 | # Make 'headers' use $header{$self->{'api'}} | ||||||||||
| 81 | # Currently hard coded to OpenAI | ||||||||||
| 82 | |||||||||||
| 83 | # Added purely for testing - IGNORE! | ||||||||||
| 84 | sub _test { | ||||||||||
| 85 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_test: not called CC=1 SCAR=6.9
| |||||||||||
| 86 | # return $self->{'api'}; | ||||||||||
| 87 | 0 | return $header{$self->{'api'}}; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | # Return Embedding as a CSV string | ||||||||||
| 91 | sub embedding { | ||||||||||
| 92 | 0 | ▶ | my ($self, $text, $verbose) = @_; | ||||||||
|
Subroutine
embedding: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 93 | |||||||||||
| 94 | 0 | my $response = $self->_get_embedding($text); | |||||||||
| 95 | 0 | ▶ | if ($response->{'success'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 0 | my $embedding = decode_json($response->{'content'}); | |||||||||
| 97 | 0 | return join (',', @{$embedding->{'data'}[0]->{'embedding'}}); | |||||||||
| 98 | } | ||||||||||
| 99 | 0 | $self->{'error'} = 'HTTP Error - ' . $response->{'reason'}; | |||||||||
| 100 | 0 | ▶ | return $response if defined $verbose; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 0 | return undef; | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | # Return Embedding as an array | ||||||||||
| 105 | sub raw_embedding { | ||||||||||
| 106 | 0 | ▶ | my ($self, $text, $verbose) = @_; | ||||||||
|
Subroutine
raw_embedding: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 107 | |||||||||||
| 108 | 0 | my $response = $self->_get_embedding($text); | |||||||||
| 109 | 0 | ▶ | if ($response->{'success'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 0 | my $embedding = decode_json($response->{'content'}); | |||||||||
| 111 | 0 | return @{$embedding->{'data'}[0]->{'embedding'}}; | |||||||||
| 112 | } | ||||||||||
| 113 | 0 | $self->{'error'} = 'HTTP Error - ' . $response->{'reason'}; | |||||||||
| 114 | 0 | ▶ | return $response if defined $verbose; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 0 | return undef; | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | # Return Test Embedding | ||||||||||
| 119 | sub test_embedding { | ||||||||||
| 120 | 3 | ▶ | my ($self, $text, $dimension) = @_; | ||||||||
|
Subroutine
test_embedding: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||
| 121 | 3 | $self->{'error'} = ''; | |||||||||
| 122 | |||||||||||
| 123 | 3 | ▶ | $dimension = 1536 unless defined $dimension; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | |||||||||||
| 125 | 3 | ▶ | if ($text) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | 3 | srand scalar split /\s+/, $text; | |||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | 3 | my @vector; | |||||||||
| 130 | 3 | for (1...$dimension) { | |||||||||
| 131 | 4608 | push @vector, rand(2) - 1; | |||||||||
| 132 | } | ||||||||||
| 133 | 3 | return join ',', @vector; | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | # Convert a CSV Embedding into a hashref | ||||||||||
| 137 | sub _make_vector { | ||||||||||
| 138 | 6 | ▶ | my ($self, $embed_string) = @_; | ||||||||
|
Subroutine
_make_vector: called CC=2 SCAR=6.9
| |||||||||||
| 139 | |||||||||||
| 140 | 6 | my %vector; | |||||||||
| 141 | 6 | my @embed = split /,/, $embed_string; | |||||||||
| 142 | 6 | ▶ | for (my $i = 0; $i < @embed; $i++) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 143 | 55 | $vector{'feature' . $i} = $embed[$i]; | |||||||||
| 144 | } | ||||||||||
| 145 | 6 | return \%vector; | |||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | # Return a comparator to compare to a set vector | ||||||||||
| 149 | sub comparator { | ||||||||||
| 150 | 1 | ▶ | my($self, $embed) = @_; | ||||||||
|
Subroutine
comparator: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 151 | 1 | $self->{'error'} = ''; | |||||||||
| 152 | |||||||||||
| 153 | 1 | my $vector1 = $self->_make_vector($embed); | |||||||||
| 154 | return sub { | ||||||||||
| 155 | 1 | ▶ | my($embed2) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 156 | 1 | my $vector2 = $self->_make_vector($embed2); | |||||||||
| 157 | 1 | return $self->_compare_vector($vector1, $vector2); | |||||||||
| 158 | 1 | }; | |||||||||
| 159 | } | ||||||||||
| 160 | |||||||||||
| 161 | # Compare 2 Embeddings | ||||||||||
| 162 | sub compare { | ||||||||||
| 163 | 2 | ▶ | my ($self, $embed1, $embed2) = @_; | ||||||||
|
Subroutine
compare: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||
| 164 | |||||||||||
| 165 | 2 | my $vector1 = $self->_make_vector($embed1); | |||||||||
| 166 | 2 | my $vector2; | |||||||||
| 167 | 2 | ▶ | if (defined $embed2) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 168 | 2 | $vector2 = $self->_make_vector($embed2); | |||||||||
| 169 | } else { | ||||||||||
| 170 | 0 | $vector2 = $self->{'comparator'}; | |||||||||
| 171 | } | ||||||||||
| 172 | |||||||||||
| 173 | 2 | ▶ | if (!defined $vector2) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 174 | 0 | $self->{'error'} = 'Nothing to compare!'; | |||||||||
| 175 | 0 | return; | |||||||||
| 176 | } | ||||||||||
| 177 | |||||||||||
| 178 | 2 | ▶ | if (scalar keys %$vector1 != scalar keys %$vector2) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 179 | 1 | $self->{'error'} = 'Embeds are unequal length'; | |||||||||
| 180 | 1 | return; | |||||||||
| 181 | } | ||||||||||
| 182 | |||||||||||
| 183 | 1 | return $self->_compare_vector($vector1, $vector2); | |||||||||
| 184 | } | ||||||||||
| 185 | |||||||||||
| 186 | # Compare 2 Vectors | ||||||||||
| 187 | sub _compare_vector { | ||||||||||
| 188 | 2 | ▶ | my ($self, $vector1, $vector2) = @_; | ||||||||
|
Subroutine
_compare_vector: called CC=1 SCAR=0.0
| |||||||||||
| 189 | 2 | my $cs = Data::CosineSimilarity->new; | |||||||||
| 190 | 2 | $cs->add( label1 => $vector1 ); | |||||||||
| 191 | 2 | $cs->add( label2 => $vector2 ); | |||||||||
| 192 | 2 | return $cs->similarity('label1', 'label2')->cosine; | |||||||||
| 193 | } | ||||||||||
| 194 | |||||||||||
| 195 | 3 | 1; | |||||||||
| 196 | |||||||||||
| 197 | __END__ | ||||||||||