| 1 | package WebService::Chroma::Embeddings::Ollama; | ||
| 2 | |||
| 3 | use Moo; | ||
| 4 | use LWP::UserAgent; | ||
| 5 | use JSON; | ||
| 6 | |||
| 7 | has ua => ( | ||
| 8 | is => 'ro', | ||
| 9 | default => sub { | ||
| 10 | LWP::UserAgent->new(); | ||
| 11 | } | ||
| 12 | ); | ||
| 13 | |||
| 14 | has json => ( | ||
| 15 | is => 'ro', | ||
| 16 | default => sub { | ||
| 17 | JSON->new; | ||
| 18 | } | ||
| 19 | ); | ||
| 20 | |||
| 21 | has base_url => ( | ||
| 22 | is => 'rw', | ||
| 23 | default => 'http://localhost:11434' | ||
| 24 | ); | ||
| 25 | |||
| 26 | has model => ( | ||
| 27 | is => 'rw', | ||
| 28 | default => sub { 'nomic-embed-text' } | ||
| 29 | ); | ||
| 30 | |||
| 31 | sub get { | ||
| 32 | my ($self, $data) = @_; | ||
| 33 | |||
| 34 | my $url = URI->new($self->base_url . '/api/embeddings'); | ||
| 35 | |||
| 36 | my $res = $self->ua->post( | ||
| 37 | $url, | ||
| 38 | content => $self->json->encode({ | ||
| 39 | model => $self->model, | ||
| 40 | prompt => $data | ||
| 41 | }) | ||
| 42 | ); | ||
| 43 | |||
| 44 | if ($res->is_success) { | ||
| 45 | my $embedding = $self->json->decode($res->decoded_content)->{embedding}; | ||
| 46 | |||
| 47 | if ($embedding && scalar @{$embedding}) { | ||
| 48 | return $embedding; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | die 'error retrieving embeddings'; | ||
| 53 | } | ||
| 54 | |||
| 55 | 1; |