| 1 | package AI::Embedding; | ||
| 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 | 1 | ▶ | use HTTP::Tiny; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use JSON; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use Data::CosineSimilarity; | ||
| 9 | |||
| 10 | our $VERSION = '0.1_1'; | ||
| 11 | $VERSION = eval $VERSION; | ||
| 12 | |||
| 13 | my $http = HTTP::Tiny->new; | ||
| 14 | |||
| 15 | # Create Embedding object | ||
| 16 | sub new { | ||
| 17 | my $class = shift; | ||
| 18 | my %attr = @_; | ||
| 19 | |||
| 20 | $attr{'error'} = ''; | ||
| 21 | |||
| 22 | $attr{'api'} = 'OpenAI' unless $attr{'api'}; | ||
| 23 | $attr{'error'} = 'Invalid API' unless $attr{'api'} eq 'OpenAI'; | ||
| 24 | $attr{'error'} = 'API Key missing' unless $attr{'key'}; | ||
| 25 | |||
| 26 | $attr{'model'} = 'text-embedding-ada-002' unless $attr{'model'}; | ||
| 27 | |||
| 28 | return bless \%attr, $class; | ||
| 29 | } | ||
| 30 | |||
| 31 | # Define endpoints for APIs | ||
| 32 | my %url = ( | ||
| 33 | 'OpenAI' => 'https://api.openai.com/v1/embeddings', | ||
| 34 | ); | ||
| 35 | |||
| 36 | # Define HTTP Headers for APIs | ||
| 37 | my %header = ( | ||
| 38 | 'OpenAI' => \&_get_header_openai, | ||
| 39 | ); | ||
| 40 | |||
| 41 | # Returns true if last operation was success | ||
| 42 | sub success { | ||
| 43 | my $self = shift; | ||
| 44 | return !$self->{'error'}; | ||
| 45 | } | ||
| 46 | |||
| 47 | # Returns error if last operation failed | ||
| 48 | sub error { | ||
| 49 | my $self = shift; | ||
| 50 | return $self->{'error'}; | ||
| 51 | } | ||
| 52 | |||
| 53 | # Header for calling OpenAI | ||
| 54 | sub _get_header_openai { | ||
| 55 | my $self = shift; | ||
| 56 | return { | ||
| 57 | headers => { | ||
| 58 | 'Authorization' => $self->key, | ||
| 59 | 'Content-type' => 'application/json' | ||
| 60 | } | ||
| 61 | }; | ||
| 62 | } | ||
| 63 | |||
| 64 | # Fetch Embedding response | ||
| 65 | sub _get_embedding { | ||
| 66 | my ($self, $text) = @_; | ||
| 67 | |||
| 68 | return $http->post($url{$self->{'api'}}, { | ||
| 69 | $header{$self->{'api'}}, | ||
| 70 | content => encode_json { | ||
| 71 | input => $text, | ||
| 72 | model => $self->{'model'}, | ||
| 73 | } | ||
| 74 | }); | ||
| 75 | } | ||
| 76 | |||
| 77 | # Return Embedding as a CSV string | ||
| 78 | sub embedding { | ||
| 79 | my ($self, $text) = @_; | ||
| 80 | |||
| 81 | my $response = $self->_get_embedding($text); | ||
| 82 | if ($response->{'success'}) { | ||
| 83 | my $embedding = decode_json($response->{'content'}); | ||
| 84 | return join (',', @{$embedding->{'data'}[0]->{'embedding'}}); | ||
| 85 | } | ||
| 86 | $self->{'error'} = 'HTTP Error - ' . $response->{'reason'}; | ||
| 87 | return $response; | ||
| 88 | } | ||
| 89 | |||
| 90 | # Return Embedding as an array | ||
| 91 | sub raw_embedding { | ||
| 92 | my ($self, $text) = @_; | ||
| 93 | |||
| 94 | my $response = $self->_get_embedding($text); | ||
| 95 | if ($response->{'success'}) { | ||
| 96 | my $embedding = decode_json($response->{'content'}); | ||
| 97 | return @{$embedding->{'data'}[0]->{'embedding'}}; | ||
| 98 | } | ||
| 99 | $self->{'error'} = 'HTTP Error - ' . $response->{'reason'}; | ||
| 100 | return $response; | ||
| 101 | } | ||
| 102 | |||
| 103 | # Convert a CSV Embedding into a hashref | ||
| 104 | sub _make_vector { | ||
| 105 | my ($self, $embed_string) = @_; | ||
| 106 | |||
| 107 | my %vector; | ||
| 108 | my @embed = split /,/, $embed_string; | ||
| 109 | for (my $i = 0; $i < @embed; $i++) { | ||
| 110 | $vector{'feature' . $i} = $embed[$i]; | ||
| 111 | } | ||
| 112 | return \%vector; | ||
| 113 | } | ||
| 114 | |||
| 115 | # Set a vector to compare | ||
| 116 | sub comparator { | ||
| 117 | my ($self, $embed) = @_; | ||
| 118 | |||
| 119 | $self->{'comparator'} = $self->_make_vector($embed); | ||
| 120 | return; | ||
| 121 | } | ||
| 122 | |||
| 123 | # Compare 2 Embeddings | ||
| 124 | sub compare { | ||
| 125 | my ($self, $embed1, $embed2) = @_; | ||
| 126 | |||
| 127 | my $vector1 = $self->_make_vector($embed1); | ||
| 128 | my $vector2; | ||
| 129 | if (defined $embed2) { | ||
| 130 | $vector2 = $self->_make_vector($embed2); | ||
| 131 | } else { | ||
| 132 | $vector2 = $self->{'comparator'}; | ||
| 133 | } | ||
| 134 | |||
| 135 | if (!defined $vector2) { | ||
| 136 | $self->{'error'} = 'Nothing to compare!'; | ||
| 137 | return; | ||
| 138 | } | ||
| 139 | |||
| 140 | if (scalar keys %$vector1 != scalar keys %$vector2) { | ||
| 141 | $self->{'error'} = 'Embeds are unequal length'; | ||
| 142 | return; | ||
| 143 | } | ||
| 144 | |||
| 145 | my $cs = Data::CosineSimilarity->new; | ||
| 146 | $cs->add( label1 => $vector1 ); | ||
| 147 | $cs->add( label2 => $vector2 ); | ||
| 148 | return $cs->similarity('label1', 'label2')->cosine; | ||
| 149 | } | ||
| 150 | |||
| 151 | 1; | ||
| 152 | |||
| 153 | __END__ | ||