Skip to content

Commit

Permalink
Support for generating single / multiple embedding
Browse files Browse the repository at this point in the history
  • Loading branch information
ksylvest committed Oct 21, 2024
1 parent ddb4707 commit 7736f7b
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 89 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
voyageai (1.0.1)
voyageai (1.1.0)
http
zeitwerk

Expand Down
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ gem install voyageai

## Usage

### Generating Single Embedding

```ruby
require 'voyageai'

input = 'A quick brown fox jumps over the lazy dog.'

voyageai = VoyageAI::Client.new(api_key: 'pa-...') # or configure ENV['VOYAGEAI_API_KEY']

embed = voyageai.emed(input)
embed.model # "..."
embed.usage # "#<VoyageAI::Usage total_tokens=...>"
embed.embedding # [0.0, ...]
```

### Generating Multiple Embeddings

```ruby
require 'voyageai'

Expand All @@ -22,10 +39,8 @@ input = [

voyageai = VoyageAI::Client.new(api_key: 'pa-...') # or configure ENV['VOYAGEAI_API_KEY']

result = voyageai.embed(input)
embed = voyageai.embed(input)
embed.model # "..."
embed.usage # "#<VoyageAI::Usage total_tokens=...>"
embed.embeddings.each do |embedding|
embedding.index # "#<VoyageAI::Embedding index=... embedding=...>
end
embed.embeddings # [[0.0, ...], ...]
```
8 changes: 7 additions & 1 deletion lib/voyageai/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def inspect
#
# @return [Embedding]
def embed(input, model: Model::VOYAGE)
payload = { input: input, model: model }
payload = { input: arrayify(input), model: model }
response = HTTP
.accept(:json)
.auth("Bearer #{@api_key}")
Expand All @@ -43,5 +43,11 @@ def embed(input, model: Model::VOYAGE)

Embed.parse(data: response.parse)
end

private

def arrayify(input)
input.is_a?(Array) ? input : [input]
end
end
end
13 changes: 9 additions & 4 deletions lib/voyageai/embed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@ class Embed
attr_accessor :usage

# @!attribute [rw] embeddings
# @return [Array<Embedding>]
# @return [Array<Array<Float>>]
attr_accessor :embeddings

# @param data [Hash]
# @return [Embed]
def self.parse(data:)
model = data["model"]
usage = Usage.parse(data: data["usage"])
embeddings = data["data"].map do |embedding_data|
Embedding.parse(data: embedding_data)
end
embeddings = data["data"].map { |embedding_data| embedding_data["embedding"] }

Embed.new(model: model, usage: usage, embeddings: embeddings)
end
Expand All @@ -43,5 +41,12 @@ def initialize(model:, usage:, embeddings:)
def inspect
"#<#{self.class.name} model=#{@model.inspect} embeddings=#{@embeddings.inspect} usage=#{@usage.inspect}>"
end

# @param index [Integer] optional
#
# @return [Array<Float>]
def embedding(index: 0)
@embeddings[index]
end
end
end
37 changes: 0 additions & 37 deletions lib/voyageai/embedding.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/voyageai/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module VoyageAI
VERSION = "1.0.1"
VERSION = "1.1.0"
end
2 changes: 1 addition & 1 deletion spec/factories/embed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

model { VoyageAI::Model::VOYAGE }
usage
embeddings { [] }
embeddings { [[0.0]] }
end
end
10 changes: 0 additions & 10 deletions spec/factories/embedding.rb

This file was deleted.

30 changes: 28 additions & 2 deletions spec/voyageai/embed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"data" => [
{
"object" => "embedding",
"embedding" => [0.0, 1.0, 2.0, 3.0],
"embedding" => [0.0],
"index" => 0,
},
],
Expand All @@ -22,15 +22,41 @@
it "parses a hash and returns an instance" do
expect(parse).to be_a(described_class)
end

it "sets embeddings to an array of arrays" do
expect(parse.embeddings).to eql [[0.0]]
end
end

describe "#inspect" do
subject(:inspect) { embed.inspect }

let(:embed) { build(:embed) }
let(:embed) { build(:embed, embeddings: []) }

it "returns a string" do
expect(inspect).to eql '#<VoyageAI::Embed model="voyage-3" embeddings=[] usage=#<VoyageAI::Usage total_tokens=0>>'
end
end

describe "#embedding" do
subject(:embedding) { embed.embedding(index: index) }

let(:embed) { build(:embed) }

context "with an index that exists" do
let(:index) { 0 }

it "returns an array" do
expect(embedding).to eql [0.0]
end
end

context "when the index that does not exist" do
let(:index) { 2 }

it "returns nil" do
expect(embedding).to be_nil
end
end
end
end
28 changes: 0 additions & 28 deletions spec/voyageai/embedding_spec.rb

This file was deleted.

0 comments on commit 7736f7b

Please sign in to comment.