omniai-google 1.9.2 → 1.9.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -0
- data/lib/omniai/google/client.rb +17 -0
- data/lib/omniai/google/embed.rb +64 -0
- data/lib/omniai/google/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 80e08395bc3710a72cc59b7c110cc50b1ba4a1dc64e15c622fcacb39448c1265
|
4
|
+
data.tar.gz: cccb64b57c0430caf449ebb1de2620e597409de0179379ef0cb26588d41418a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cf8a4e78c06a9808930300c1aa05b45211ab0f733a79bddc22bd5ac3ca2bfff1a00d075ca596122295c82aeb909372d2a9c7a66bc27566386d529428209beda7
|
7
|
+
data.tar.gz: 7324a4d99c3ce3a890129eb5a3091459facbd0acf5818b7ff0624d168d2fee94613656db1fe1c1091f8f001c6c34cc34c66a835b69fb7d90191075bde455de5b
|
data/README.md
CHANGED
@@ -120,3 +120,12 @@ end
|
|
120
120
|
```
|
121
121
|
|
122
122
|
[Google API Reference `stream`](https://ai.google.dev/gemini-api/docs/api-overview#stream)
|
123
|
+
|
124
|
+
### Embed
|
125
|
+
|
126
|
+
Text can be converted into a vector embedding for similarity comparison usage via:
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
response = client.embed('The quick brown fox jumps over a lazy dog.')
|
130
|
+
response.embedding # [0.0, ...]
|
131
|
+
```
|
data/lib/omniai/google/client.rb
CHANGED
@@ -68,6 +68,23 @@ module OmniAI
|
|
68
68
|
def upload(io)
|
69
69
|
Upload.process!(client: self, io:)
|
70
70
|
end
|
71
|
+
|
72
|
+
# @raise [OmniAI::Error]
|
73
|
+
#
|
74
|
+
# @param input [String, Array<String>, Array<Integer>] required
|
75
|
+
# @param model [String] optional
|
76
|
+
def embed(input, model: Embed::DEFAULT_MODEL)
|
77
|
+
Embed.process!(input, model:, client: self)
|
78
|
+
end
|
79
|
+
|
80
|
+
# @return [String]
|
81
|
+
def path
|
82
|
+
if @project_id
|
83
|
+
"/#{@version}/projects/#{@project_id}/locations/#{@location}/publishers/google"
|
84
|
+
else
|
85
|
+
"/#{@version}"
|
86
|
+
end
|
87
|
+
end
|
71
88
|
end
|
72
89
|
end
|
73
90
|
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OmniAI
|
4
|
+
module Google
|
5
|
+
# An Google embed implementation.
|
6
|
+
#
|
7
|
+
# Usage:
|
8
|
+
#
|
9
|
+
# input = "..."
|
10
|
+
# response = OmniAI::Google::Embed.process!(input, client: client)
|
11
|
+
# response.embedding [0.0, ...]
|
12
|
+
class Embed < OmniAI::Embed
|
13
|
+
module Model
|
14
|
+
TEXT_EMBEDDING_004 = 'text-embedding-004'
|
15
|
+
TEXT_MULTILINGUAL_EMBEDDING_002 = 'text-multilingual-embedding-002'
|
16
|
+
EMBEDDING = TEXT_EMBEDDING_004
|
17
|
+
MULTILINGUAL_EMBEDDING = TEXT_MULTILINGUAL_EMBEDDING_002
|
18
|
+
end
|
19
|
+
|
20
|
+
DEFAULT_MODEL = Model::EMBEDDING
|
21
|
+
|
22
|
+
EMBEDDINGS_DESERIALIZER = proc do |data, *|
|
23
|
+
data['embeddings'].map { |embedding| embedding['values'] }
|
24
|
+
end
|
25
|
+
|
26
|
+
# @return [Context]
|
27
|
+
CONTEXT = Context.build do |context|
|
28
|
+
context.deserializers[:embeddings] = EMBEDDINGS_DESERIALIZER
|
29
|
+
end
|
30
|
+
|
31
|
+
protected
|
32
|
+
|
33
|
+
# @param response [HTTP::Response]
|
34
|
+
# @return [Response]
|
35
|
+
def parse!(response:)
|
36
|
+
Response.new(data: response.parse, context: CONTEXT)
|
37
|
+
end
|
38
|
+
|
39
|
+
# @return [Array<Hash<{ text: String }>]
|
40
|
+
def requests
|
41
|
+
arrayify(@input).map do |text|
|
42
|
+
{
|
43
|
+
model: "models/#{@model}",
|
44
|
+
content: { parts: [{ text: text }] },
|
45
|
+
}
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# @return [Hash]
|
50
|
+
def payload
|
51
|
+
{ requests: }
|
52
|
+
end
|
53
|
+
|
54
|
+
# @return [String]
|
55
|
+
def path
|
56
|
+
"/#{@client.path}/models/#{@model}:batchEmbedContents?key=#{@client.api_key}"
|
57
|
+
end
|
58
|
+
|
59
|
+
def arrayify(input)
|
60
|
+
input.is_a?(Array) ? input : [input]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: omniai-google
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.9.
|
4
|
+
version: 1.9.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Sylvestre
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-11-
|
11
|
+
date: 2024-11-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: event_stream_parser
|
@@ -76,6 +76,7 @@ files:
|
|
76
76
|
- lib/omniai/google/chat/usage_serializer.rb
|
77
77
|
- lib/omniai/google/client.rb
|
78
78
|
- lib/omniai/google/config.rb
|
79
|
+
- lib/omniai/google/embed.rb
|
79
80
|
- lib/omniai/google/upload.rb
|
80
81
|
- lib/omniai/google/upload/file.rb
|
81
82
|
- lib/omniai/google/version.rb
|