voyageai 1.3.0 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +84 -0
- data/lib/voyageai/client.rb +5 -3
- data/lib/voyageai/model.rb +1 -0
- data/lib/voyageai/version.rb +1 -1
- metadata +7 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8afba7d9e268ee2d7c8f606bb8dc13794839c960d31a0dada531dc92002d1122
|
4
|
+
data.tar.gz: 29a4d077a33abda9c88eb95c17abeb1e785cde693ba5a85fc2e54967028c358b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3defa2dcaaee1f8da7e4fcd492d082d9ac63d1e5c22994b03ca1bff091327b84c883ef88dba1b1722bda1c7169eb6403227661fb649bac25aa2281c4188ec94a
|
7
|
+
data.tar.gz: 0371ccd81feb78fb19566c9be5bb71c43306c395d47002a917cd314af2985dd92967126f69179a1ca976dae750fc787393e442f74d0134a42f4e112575d3cb4f
|
data/README.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
# VoyageAI
|
2
2
|
|
3
|
+
[![LICENSE](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/ksylvest/voyageai/blob/main/LICENSE)
|
4
|
+
[![RubyGems](https://img.shields.io/gem/v/voyageai)](https://rubygems.org/gems/voyageai)
|
5
|
+
[![GitHub](https://img.shields.io/badge/github-repo-blue.svg)](https://github.com/ksylvest/voyageai)
|
6
|
+
[![Yard](https://img.shields.io/badge/docs-site-blue.svg)](https://voyageai.ksylvest.com)
|
7
|
+
[![CircleCI](https://img.shields.io/circleci/build/github/ksylvest/voyageai)](https://circleci.com/gh/ksylvest/voyageai)
|
8
|
+
|
3
9
|
`voyageai` is a ruby client for [VoyageAI](https://www.voyageai.com)
|
4
10
|
|
5
11
|
## Installation
|
@@ -82,3 +88,81 @@ VoyageAI.configure do |config|
|
|
82
88
|
config.logger = Logger.new(STDOUT)
|
83
89
|
end
|
84
90
|
```
|
91
|
+
|
92
|
+
## Examples
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
require "voyageai"
|
96
|
+
|
97
|
+
Entry = Data.define(:document, :embedding)
|
98
|
+
|
99
|
+
VOYAGEAI = VoyageAI::Client.new
|
100
|
+
|
101
|
+
DOCUMENTS = [
|
102
|
+
"John is a musician.",
|
103
|
+
"Paul is a plumber.",
|
104
|
+
"George is a teacher.",
|
105
|
+
"Ringo is a doctor.",
|
106
|
+
"Lisa is a lawyer.",
|
107
|
+
"Stuart is a painter.",
|
108
|
+
"Brian is a writer.",
|
109
|
+
"Jane is a chef.",
|
110
|
+
"Bill is a nurse.",
|
111
|
+
"Susan is a carpenter.",
|
112
|
+
].freeze
|
113
|
+
|
114
|
+
embeddings = VOYAGEAI.embed(DOCUMENTS, input_type: "document").embeddings
|
115
|
+
|
116
|
+
ENTRIES = DOCUMENTS.zip(embeddings).map do |document, embedding|
|
117
|
+
Entry.new(document:, embedding:)
|
118
|
+
end
|
119
|
+
|
120
|
+
# @param src [Array<Float>]
|
121
|
+
# @param dst [Array<Float>]
|
122
|
+
#
|
123
|
+
# @return [Float]
|
124
|
+
def euclidean_distance(src, dst)
|
125
|
+
Math.sqrt(src.zip(dst).map { |a, b| (a - b)**2 }.reduce(:+))
|
126
|
+
end
|
127
|
+
|
128
|
+
# @param query [String]
|
129
|
+
# @param limit [Integer]
|
130
|
+
#
|
131
|
+
# @return [Array<String>]
|
132
|
+
def nearest_documents(query:, limit: 4)
|
133
|
+
embedding = VOYAGEAI.embed(query, input_type: "query").embedding
|
134
|
+
|
135
|
+
ENTRIES
|
136
|
+
.sort_by { |entry| euclidean_distance(entry.embedding, embedding) }
|
137
|
+
.first(limit)
|
138
|
+
.map(&:document)
|
139
|
+
end
|
140
|
+
|
141
|
+
# @param query [String]
|
142
|
+
def search(query:)
|
143
|
+
documents = nearest_documents(query:)
|
144
|
+
|
145
|
+
results = VOYAGEAI.rerank(query:, documents:, top_k: 2).results
|
146
|
+
|
147
|
+
puts "query=#{query.inspect}"
|
148
|
+
results.each do |reranking|
|
149
|
+
document = documents[reranking.index]
|
150
|
+
puts("document=#{document.inspect} relevance_score=#{reranking.relevance_score}")
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
search(query: "What do George and Ringo do?")
|
155
|
+
search(query: "Who works in the medical field?")
|
156
|
+
```
|
157
|
+
|
158
|
+
```
|
159
|
+
query="What do George and Ringo do?"
|
160
|
+
document="Ringo is a doctor." relevance_score=0.67968755
|
161
|
+
document="George is a teacher." relevance_score=0.58593755
|
162
|
+
```
|
163
|
+
|
164
|
+
```
|
165
|
+
query="Who works in the medical field?"
|
166
|
+
document="Bill is a nurse." relevance_score=0.55078125
|
167
|
+
document="Ringo is a doctor." relevance_score=0.50390625
|
168
|
+
```
|
data/lib/voyageai/client.rb
CHANGED
@@ -37,12 +37,14 @@ module VoyageAI
|
|
37
37
|
"#<#{self.class.name} api_key=#{masked_api_key.inspect} host=#{@host.inspect} version=#{@version.inspect}>"
|
38
38
|
end
|
39
39
|
|
40
|
-
# @param model [String] optional (e.g. VoyageAI::Model::VOYAGE or "voyage-3")
|
41
40
|
# @param input [String, Array<String>] required (e.g. "Sample" or ["Sample 1", "Sample 2", ...])
|
41
|
+
# @param model [String] optional (e.g. VoyageAI::Model::VOYAGE or "voyage-3")
|
42
|
+
# @param input_type [Symbol] optional (e.g. :query or :document)
|
43
|
+
# @param truncation [Boolean] optional
|
42
44
|
#
|
43
45
|
# @return [Embedding]
|
44
|
-
def embed(input, model: Model::VOYAGE)
|
45
|
-
payload = { input: arrayify(input), model: model }
|
46
|
+
def embed(input, model: Model::VOYAGE, input_type: nil, truncation: nil)
|
47
|
+
payload = { input: arrayify(input), model: model, truncation:, input_type: }.compact
|
46
48
|
response = http.accept(:json).post("/#{@version}/embeddings", json: payload)
|
47
49
|
|
48
50
|
raise RequestError.new(response:) unless response.status.ok?
|
data/lib/voyageai/model.rb
CHANGED
data/lib/voyageai/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: voyageai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Sylvestre
|
8
|
-
autorequire:
|
9
8
|
bindir: bin
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 2025-01-08 00:00:00.000000000 Z
|
12
11
|
dependencies:
|
13
12
|
- !ruby/object:Gem::Dependency
|
14
13
|
name: http
|
@@ -63,10 +62,11 @@ homepage: https://github.com/ksylvest/voyageai
|
|
63
62
|
licenses:
|
64
63
|
- MIT
|
65
64
|
metadata:
|
66
|
-
homepage_uri: https://github.com/ksylvest/voyageai
|
67
|
-
changelog_uri: https://github.com/ksylvest/voyageai/releases
|
68
65
|
rubygems_mfa_required: 'true'
|
69
|
-
|
66
|
+
homepage_uri: https://github.com/ksylvest/voyageai
|
67
|
+
source_code_uri: https://github.com/ksylvest/voyageai/tree/v1.6.0
|
68
|
+
changelog_uri: https://github.com/ksylvest/voyageai/releases/tag/v1.6.0
|
69
|
+
documentation_uri: https://voyageai.ksylvest.com/
|
70
70
|
rdoc_options: []
|
71
71
|
require_paths:
|
72
72
|
- lib
|
@@ -81,8 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
requirements: []
|
84
|
-
rubygems_version: 3.
|
85
|
-
signing_key:
|
84
|
+
rubygems_version: 3.6.2
|
86
85
|
specification_version: 4
|
87
86
|
summary: A client for voyageai.com.
|
88
87
|
test_files: []
|