ai-lite 1.0.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +25 -4
- data/README.md +382 -24
- data/lib/ai_lite/gemini/chat.rb +8 -5
- data/lib/ai_lite/gemini/configuration.rb +11 -1
- data/lib/ai_lite/gemini/embeddings.rb +58 -0
- data/lib/ai_lite/gemini/images.rb +56 -0
- data/lib/ai_lite/gemini/models.rb +3 -1
- data/lib/ai_lite/gemini/speech.rb +108 -0
- data/lib/ai_lite/gemini/streaming.rb +34 -5
- data/lib/ai_lite/gemini/tokens.rb +53 -0
- data/lib/ai_lite/gemini/tool_calling.rb +66 -0
- data/lib/ai_lite/gemini/transcription.rb +66 -0
- data/lib/ai_lite/gemini.rb +38 -3
- data/lib/ai_lite/openai/streaming.rb +11 -7
- data/lib/ai_lite/openai/tokens.rb +38 -0
- data/lib/ai_lite/openai/tool_calling.rb +71 -0
- data/lib/ai_lite/openai.rb +22 -15
- data/lib/ai_lite/version.rb +1 -1
- data/test/ai_lite_test.rb +5 -5
- data/test/gemini_embeddings_test.rb +122 -0
- data/test/gemini_images_test.rb +154 -0
- data/test/gemini_models_test.rb +3 -2
- data/test/gemini_speech_test.rb +199 -0
- data/test/gemini_test.rb +1 -1
- data/test/gemini_transcription_test.rb +123 -0
- data/test/package_test.rb +51 -0
- data/test/token_counting_test.rb +173 -0
- data/test/tool_calling_test.rb +270 -0
- metadata +23 -6
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
class AiLite
|
|
2
|
+
class OpenAI
|
|
3
|
+
module ToolCalling
|
|
4
|
+
private
|
|
5
|
+
|
|
6
|
+
def apply_chat_tools(payload, tools)
|
|
7
|
+
payload["tools"] = tools unless tools.nil?
|
|
8
|
+
if payload.key?("tools") && !(payload["tools"].is_a?(Array) && payload["tools"].all? { |tool| tool.is_a?(Hash) })
|
|
9
|
+
raise ArgumentError, "tools must be an array of provider tool definitions"
|
|
10
|
+
end
|
|
11
|
+
payload
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def tool_result_input(message, results, previous_id)
|
|
15
|
+
return message if results.nil?
|
|
16
|
+
raise ArgumentError, "Use tool_results without a message" unless message.nil?
|
|
17
|
+
unless previous_id.is_a?(String) && !previous_id.strip.empty?
|
|
18
|
+
raise ArgumentError, "tool_results requires previous_response_id; use native input for stateless history"
|
|
19
|
+
end
|
|
20
|
+
unless results.is_a?(Array) && !results.empty?
|
|
21
|
+
raise ArgumentError, "tool_results must be a nonempty array"
|
|
22
|
+
end
|
|
23
|
+
ids = []
|
|
24
|
+
results.map do |item|
|
|
25
|
+
raise ArgumentError, "Each tool result must be a hash" unless item.is_a?(Hash)
|
|
26
|
+
entry = item.each_with_object({}) { |(key, value), hash| hash[key.to_s] = value }
|
|
27
|
+
unless %w[id name].all? { |key| entry[key].is_a?(String) && !entry[key].strip.empty? } && entry.key?("output")
|
|
28
|
+
raise ArgumentError, "Each tool result requires id, name, and output"
|
|
29
|
+
end
|
|
30
|
+
raise ArgumentError, "Duplicate tool result ID" if ids.include?(entry["id"])
|
|
31
|
+
ids << entry["id"]
|
|
32
|
+
output = entry["output"].is_a?(String) ? entry["output"] : JSON.generate(entry["output"])
|
|
33
|
+
{ "type" => "function_call_output", "call_id" => entry["id"], "output" => output }
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def extract_tool_calls(raw)
|
|
38
|
+
calls = Array(raw["output"]).select { |item| item.is_a?(Hash) && item["type"] == "function_call" }
|
|
39
|
+
if !calls.empty? && raw["status"] && raw["status"] != "completed"
|
|
40
|
+
raise "Response #{raw['status']}: function calls are not complete"
|
|
41
|
+
end
|
|
42
|
+
ids = []
|
|
43
|
+
calls.map do |call|
|
|
44
|
+
if call["status"] && call["status"] != "completed"
|
|
45
|
+
raise "Function call #{call['status']}: arguments are not complete"
|
|
46
|
+
end
|
|
47
|
+
id = call["call_id"]
|
|
48
|
+
name = call["name"]
|
|
49
|
+
arguments = call["arguments"]
|
|
50
|
+
arguments = JSON.parse(arguments) if arguments.is_a?(String)
|
|
51
|
+
unless id.is_a?(String) && !id.empty? && name.is_a?(String) && !name.empty? && arguments.is_a?(Hash)
|
|
52
|
+
raise "Invalid function call: expected ID, name, and object arguments"
|
|
53
|
+
end
|
|
54
|
+
raise "Invalid function call: duplicate ID" if ids.include?(id)
|
|
55
|
+
ids << id
|
|
56
|
+
{ "id" => id, "name" => name, "arguments" => arguments }
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def attach_tool_calls(result, raw)
|
|
61
|
+
return result if result["error"]
|
|
62
|
+
calls = extract_tool_calls(raw)
|
|
63
|
+
result["tool_calls"] = calls unless calls.empty?
|
|
64
|
+
result
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
include ToolCalling
|
|
69
|
+
private_constant :ToolCalling
|
|
70
|
+
end
|
|
71
|
+
end
|
data/lib/ai_lite/openai.rb
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
require "base64"
|
|
2
1
|
require "json"
|
|
3
2
|
require "net/http"
|
|
4
3
|
require "securerandom"
|
|
5
4
|
require "uri"
|
|
6
5
|
require_relative "version"
|
|
7
6
|
require_relative "openai/configuration"
|
|
7
|
+
require_relative "openai/tokens"
|
|
8
|
+
require_relative "openai/tool_calling"
|
|
8
9
|
require_relative "openai/streaming"
|
|
9
10
|
require_relative "openai/models"
|
|
10
11
|
|
|
@@ -31,6 +32,10 @@ class AiLite
|
|
|
31
32
|
@client ||= new
|
|
32
33
|
end
|
|
33
34
|
|
|
35
|
+
def count_tokens(input, **kwargs)
|
|
36
|
+
client.count_tokens(input, **kwargs)
|
|
37
|
+
end
|
|
38
|
+
|
|
34
39
|
def chat(message, **kwargs)
|
|
35
40
|
client.chat(message, **kwargs)
|
|
36
41
|
end
|
|
@@ -85,15 +90,16 @@ class AiLite
|
|
|
85
90
|
}
|
|
86
91
|
end
|
|
87
92
|
|
|
88
|
-
def chat(message, model: nil, instructions: nil, previous_response_id: nil, max_output_tokens: nil, debug: false, options: {})
|
|
89
|
-
payload = options.
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
payload[
|
|
96
|
-
|
|
93
|
+
def chat(message, model: nil, instructions: nil, previous_response_id: nil, max_output_tokens: nil, tools: nil, tool_results: nil, debug: false, options: {})
|
|
94
|
+
payload = options.each_with_object({}) { |(key, value), hash| hash[key.to_s] = value }
|
|
95
|
+
previous_id = previous_response_id || payload["previous_response_id"]
|
|
96
|
+
input = tool_result_input(message, tool_results, previous_id)
|
|
97
|
+
raise ArgumentError, "Missing chat input" if input.nil?
|
|
98
|
+
payload.merge!("model" => model || self.model, "input" => input,
|
|
99
|
+
"max_output_tokens" => max_output_tokens || self.max_output_tokens)
|
|
100
|
+
payload["instructions"] = instructions if instructions
|
|
101
|
+
payload["previous_response_id"] = previous_response_id if previous_response_id
|
|
102
|
+
apply_chat_tools(payload, tools)
|
|
97
103
|
extract_content(post(payload), debug: debug)
|
|
98
104
|
rescue => e
|
|
99
105
|
prettify_data(status: "unknown", error: e.message, raw: nil, debug: debug)
|
|
@@ -257,7 +263,7 @@ class AiLite
|
|
|
257
263
|
reason = parsed_response.dig("incomplete_details", "reason")
|
|
258
264
|
["Response #{parsed_response['status']}", reason].compact.join(": ")
|
|
259
265
|
end
|
|
260
|
-
prettify_data(
|
|
266
|
+
result = prettify_data(
|
|
261
267
|
status: status,
|
|
262
268
|
content: content,
|
|
263
269
|
error: error,
|
|
@@ -265,10 +271,11 @@ class AiLite
|
|
|
265
271
|
raw: parsed_response,
|
|
266
272
|
debug: debug
|
|
267
273
|
)
|
|
274
|
+
attach_tool_calls(result, parsed_response)
|
|
268
275
|
rescue JSON::ParserError => e
|
|
269
276
|
prettify_data(status: response_status(response), error: e.message, raw: response&.body, debug: debug)
|
|
270
277
|
rescue => e
|
|
271
|
-
prettify_data(status: response_status(response), error: e.message, raw:
|
|
278
|
+
prettify_data(status: response_status(response), error: e.message, raw: parsed_response, debug: debug)
|
|
272
279
|
end
|
|
273
280
|
|
|
274
281
|
def extract_moderation(response, debug: false)
|
|
@@ -459,7 +466,7 @@ class AiLite
|
|
|
459
466
|
|
|
460
467
|
def image_data_url(path)
|
|
461
468
|
mime_type = image_mime_type(path)
|
|
462
|
-
"data:#{mime_type};base64,#{
|
|
469
|
+
"data:#{mime_type};base64,#{[File.binread(path)].pack("m0")}"
|
|
463
470
|
end
|
|
464
471
|
|
|
465
472
|
def image_mime_type(path)
|
|
@@ -509,11 +516,11 @@ class AiLite
|
|
|
509
516
|
def write_image_output(path, content)
|
|
510
517
|
raise "No image data returned" if content.to_s.empty?
|
|
511
518
|
|
|
512
|
-
File.binwrite(path,
|
|
519
|
+
File.binwrite(path, content.unpack1("m"))
|
|
513
520
|
end
|
|
514
521
|
|
|
515
522
|
def speech_content(audio, output_path:, base64:, response_format:)
|
|
516
|
-
return
|
|
523
|
+
return [audio].pack("m0") if base64
|
|
517
524
|
return audio unless output_path
|
|
518
525
|
|
|
519
526
|
{
|
data/lib/ai_lite/version.rb
CHANGED
data/test/ai_lite_test.rb
CHANGED
|
@@ -619,7 +619,7 @@ class AiLiteTest < Minitest::Test
|
|
|
619
619
|
|
|
620
620
|
def test_image_sends_post_to_images_with_default_payload
|
|
621
621
|
client = provider.new(api_key: "token-abc")
|
|
622
|
-
image_data =
|
|
622
|
+
image_data = ["fake image"].pack("m0")
|
|
623
623
|
|
|
624
624
|
with_stubbed_http(image_response(b64_json: image_data)) do |captured, _response|
|
|
625
625
|
result = client.image("A small ruby gem logo")
|
|
@@ -689,7 +689,7 @@ class AiLiteTest < Minitest::Test
|
|
|
689
689
|
|
|
690
690
|
def test_image_writes_decoded_content_to_output_path
|
|
691
691
|
client = provider.new(api_key: "token-abc")
|
|
692
|
-
image_data =
|
|
692
|
+
image_data = ["fake image"].pack("m0")
|
|
693
693
|
|
|
694
694
|
Tempfile.create(["generated", ".png"]) do |file|
|
|
695
695
|
with_stubbed_http(image_response(b64_json: image_data)) do |_captured, _response|
|
|
@@ -703,7 +703,7 @@ class AiLiteTest < Minitest::Test
|
|
|
703
703
|
|
|
704
704
|
def test_image_debug_true_returns_raw_usage
|
|
705
705
|
client = provider.new(api_key: "token-abc")
|
|
706
|
-
image_data =
|
|
706
|
+
image_data = ["fake image"].pack("m0")
|
|
707
707
|
|
|
708
708
|
with_stubbed_http(image_response(b64_json: image_data)) do |_captured, _response|
|
|
709
709
|
result = client.image("A debuggable image", debug: true)
|
|
@@ -825,7 +825,7 @@ class AiLiteTest < Minitest::Test
|
|
|
825
825
|
with_stubbed_http(speech_response("fake audio")) do |_captured, _response|
|
|
826
826
|
result = client.speak("Encode this", base64: true)
|
|
827
827
|
|
|
828
|
-
assert_equal
|
|
828
|
+
assert_equal ["fake audio"].pack("m0"), result["content"]
|
|
829
829
|
assert_nil result["raw"]
|
|
830
830
|
end
|
|
831
831
|
end
|
|
@@ -1155,7 +1155,7 @@ class AiLiteTest < Minitest::Test
|
|
|
1155
1155
|
FakeResponse.new("200", body)
|
|
1156
1156
|
end
|
|
1157
1157
|
|
|
1158
|
-
def image_response(b64_json:
|
|
1158
|
+
def image_response(b64_json: ["fake image"].pack("m0"))
|
|
1159
1159
|
image = {}
|
|
1160
1160
|
image["b64_json"] = b64_json if b64_json
|
|
1161
1161
|
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
require_relative "test_helper"
|
|
2
|
+
|
|
3
|
+
class GeminiEmbeddingsTest < Minitest::Test
|
|
4
|
+
Response = Struct.new(:code, :body)
|
|
5
|
+
class Http
|
|
6
|
+
attr_accessor :open_timeout, :read_timeout
|
|
7
|
+
attr_reader :request_received
|
|
8
|
+
def initialize(response)
|
|
9
|
+
@response = response
|
|
10
|
+
end
|
|
11
|
+
def request(request)
|
|
12
|
+
@request_received = request
|
|
13
|
+
raise @response if @response.is_a?(Exception)
|
|
14
|
+
@response
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def setup
|
|
19
|
+
AiLite::Gemini.reset_configuration!
|
|
20
|
+
AiLite::OpenAI.reset_configuration!
|
|
21
|
+
@client = AiLite::Gemini.new(api_key: "gemini-key", timeout: 9)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def with_http(raw = {}, status = "200")
|
|
25
|
+
response = raw.is_a?(Exception) ? raw : Response.new(status, raw.is_a?(String) ? raw : JSON.generate(raw))
|
|
26
|
+
http = Http.new(response)
|
|
27
|
+
original = Net::HTTP.method(:start)
|
|
28
|
+
Net::HTTP.define_singleton_method(:start) do |host, port, use_ssl:, &block|
|
|
29
|
+
raise "Wrong provider host" unless host == "generativelanguage.googleapis.com" && port == 443 && use_ssl
|
|
30
|
+
block.call(http)
|
|
31
|
+
end
|
|
32
|
+
yield http
|
|
33
|
+
ensure
|
|
34
|
+
Net::HTTP.define_singleton_method(:start, original)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def test_single_embedding_transport_and_envelope
|
|
38
|
+
with_http({ "embedding" => { "values" => [0.1, -0.2] } }) do |http|
|
|
39
|
+
result = @client.embed("Hello")
|
|
40
|
+
assert_equal({ "content" => [0.1, -0.2], "status" => 200, "error" => nil,
|
|
41
|
+
"raw" => nil, "response_id" => nil }, result)
|
|
42
|
+
assert_equal "/v1beta/models/gemini-embedding-001:embedContent", http.request_received.path
|
|
43
|
+
assert_equal "gemini-key", http.request_received["x-goog-api-key"]
|
|
44
|
+
assert_equal "application/json", http.request_received["Content-Type"]
|
|
45
|
+
assert_equal 9, http.read_timeout
|
|
46
|
+
assert_equal 9, http.open_timeout
|
|
47
|
+
assert_equal({ "model" => "models/gemini-embedding-001", "content" => { "parts" => [{ "text" => "Hello" }] } }, JSON.parse(http.request_received.body))
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def test_batch_order_options_dimensions_and_debug
|
|
52
|
+
options = { taskType: "RETRIEVAL_DOCUMENT", title: "Guide", "outputDimensionality" => 8 }
|
|
53
|
+
original = Marshal.dump(options)
|
|
54
|
+
raw = { "embeddings" => [{ "values" => [1, 2] }, { "values" => [3, 4] }], "usageMetadata" => { "totalTokenCount" => 4 } }
|
|
55
|
+
with_http(raw) do |http|
|
|
56
|
+
result = @client.embed(["First", "Second"], model: "models/custom", dimensions: 2, encoding_format: "float", options: options, debug: true)
|
|
57
|
+
assert_equal [[1, 2], [3, 4]], result["content"]
|
|
58
|
+
assert_equal raw, result["raw"]
|
|
59
|
+
assert_equal "/v1beta/models/custom:batchEmbedContents", http.request_received.path
|
|
60
|
+
requests = JSON.parse(http.request_received.body)["requests"]
|
|
61
|
+
assert_equal ["First", "Second"], requests.map { |r| r.dig("content", "parts", 0, "text") }
|
|
62
|
+
requests.each do |request|
|
|
63
|
+
assert_equal "models/custom", request["model"]
|
|
64
|
+
assert_equal 2, request["outputDimensionality"]
|
|
65
|
+
assert_equal "RETRIEVAL_DOCUMENT", request["taskType"]
|
|
66
|
+
assert_equal "Guide", request["title"]
|
|
67
|
+
refute request.key?("encoding_format")
|
|
68
|
+
end
|
|
69
|
+
assert_equal original, Marshal.dump(options)
|
|
70
|
+
end
|
|
71
|
+
with_http({ "embeddings" => [{ "values" => [1] }] }) do
|
|
72
|
+
assert_equal [[1]], @client.embed(["Only one"])["content"]
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def test_configuration_delegation_and_model_checks
|
|
77
|
+
AiLite::Gemini.configure { |c| c.api_key = "key"; c.embedding_model = "configured" }
|
|
78
|
+
assert_equal "configured", AiLite::Gemini.client.embedding_model
|
|
79
|
+
assert_equal "gemini-embedding-001", @client.embedding_model
|
|
80
|
+
assert_equal "override", AiLite::Gemini.new(embedding_model: "override").embedding_model
|
|
81
|
+
snapshot = AiLite::Gemini.configured_models
|
|
82
|
+
snapshot["embedding"].replace("changed")
|
|
83
|
+
assert_equal "configured", AiLite::Gemini.configuration.embedding_model
|
|
84
|
+
with_http({ "embedding" => { "values" => [1] } }) do |http|
|
|
85
|
+
assert_equal [1], AiLite::Gemini.embed("Hello")["content"]
|
|
86
|
+
assert_equal "/v1beta/models/configured:embedContent", http.request_received.path
|
|
87
|
+
end
|
|
88
|
+
with_http({ "models" => [{ "name" => "models/configured" }] }) do
|
|
89
|
+
assert_equal true, AiLite::Gemini.check_configured_models.dig("content", "embedding", "available")
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def test_invalid_inputs_do_not_make_requests
|
|
94
|
+
with_http do |http|
|
|
95
|
+
[nil, [], "", ["ok", nil], { text: "hi" }].each { |input| refute_nil @client.embed(input)["error"] }
|
|
96
|
+
[{ dimensions: 0 }, { dimensions: "2" }, { encoding_format: "base64" }, { model: "bad/path" }].each do |kwargs|
|
|
97
|
+
refute_nil @client.embed("Hello", **kwargs)["error"]
|
|
98
|
+
end
|
|
99
|
+
assert_nil http.request_received
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def test_api_transport_and_malformed_response_errors
|
|
104
|
+
with_http({ "error" => { "message" => "Denied" } }, "403") do
|
|
105
|
+
result = @client.embed("Hello", debug: true)
|
|
106
|
+
assert_equal 403, result["status"]
|
|
107
|
+
assert_equal "Denied", result["error"]
|
|
108
|
+
refute_nil result["raw"]
|
|
109
|
+
end
|
|
110
|
+
["invalid json", [], {}, { "embedding" => { "values" => ["bad"] } }, IOError.new("Disconnected")].each do |raw|
|
|
111
|
+
with_http(raw) do
|
|
112
|
+
result = @client.embed("Hello")
|
|
113
|
+
refute_nil result["error"]
|
|
114
|
+
assert_nil result["content"]
|
|
115
|
+
assert_nil result["raw"]
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
with_http({ "embeddings" => [{ "values" => [1] }] }) do
|
|
119
|
+
assert_match(/count/, @client.embed(["one", "two"])["error"])
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
require_relative "test_helper"
|
|
2
|
+
require "tmpdir"
|
|
3
|
+
|
|
4
|
+
class GeminiImagesTest < Minitest::Test
|
|
5
|
+
Response = Struct.new(:code, :body)
|
|
6
|
+
class Http
|
|
7
|
+
attr_accessor :open_timeout, :read_timeout
|
|
8
|
+
attr_reader :request_received
|
|
9
|
+
def initialize(response)
|
|
10
|
+
@response = response
|
|
11
|
+
end
|
|
12
|
+
def request(request)
|
|
13
|
+
@request_received = request
|
|
14
|
+
raise @response if @response.is_a?(Exception)
|
|
15
|
+
@response
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def setup
|
|
20
|
+
AiLite::Gemini.reset_configuration!
|
|
21
|
+
AiLite::OpenAI.reset_configuration!
|
|
22
|
+
@client = AiLite::Gemini.new(api_key: "gemini-key", timeout: 9)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def with_http(raw = {}, status = "200")
|
|
26
|
+
response = raw.is_a?(Exception) ? raw : Response.new(status, raw.is_a?(String) ? raw : JSON.generate(raw))
|
|
27
|
+
http = Http.new(response)
|
|
28
|
+
original = Net::HTTP.method(:start)
|
|
29
|
+
Net::HTTP.define_singleton_method(:start) do |host, port, use_ssl:, &block|
|
|
30
|
+
raise "Wrong provider host" unless host == "generativelanguage.googleapis.com" && port == 443 && use_ssl
|
|
31
|
+
block.call(http)
|
|
32
|
+
end
|
|
33
|
+
yield http
|
|
34
|
+
ensure
|
|
35
|
+
Net::HTTP.define_singleton_method(:start, original)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
PNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aX1sAAAAASUVORK5CYII=".unpack1("m0")
|
|
39
|
+
|
|
40
|
+
def image_body
|
|
41
|
+
{ "id" => "image-123", "status" => "completed", "steps" => [
|
|
42
|
+
{ "type" => "thought", "content" => [{ "type" => "image", "data" => ["hidden"].pack("m0") }] },
|
|
43
|
+
{ "type" => "model_output", "content" => [
|
|
44
|
+
{ "type" => "text", "text" => "Here is your image" },
|
|
45
|
+
{ "type" => "image", "mime_type" => "image/png", "data" => [PNG].pack("m0") },
|
|
46
|
+
{ "type" => "image", "mime_type" => "image/png", "data" => ["second"].pack("m0") }
|
|
47
|
+
] }
|
|
48
|
+
] }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def test_default_transport_first_image_and_envelope
|
|
52
|
+
with_http(image_body) do |http|
|
|
53
|
+
result = @client.image("A cat")
|
|
54
|
+
assert_equal({ "content" => [PNG].pack("m0"), "status" => 200,
|
|
55
|
+
"error" => nil, "response_id" => "image-123", "raw" => nil }, result)
|
|
56
|
+
request = http.request_received
|
|
57
|
+
assert_equal "/v1beta/interactions", request.path
|
|
58
|
+
assert_equal "gemini-key", request["x-goog-api-key"]
|
|
59
|
+
assert_equal "application/json", request["Content-Type"]
|
|
60
|
+
assert_equal({ "model" => AiLite::Gemini::DEFAULT_IMAGE_MODEL, "input" => "A cat" }, JSON.parse(request.body))
|
|
61
|
+
assert_equal 9, http.open_timeout
|
|
62
|
+
assert_equal 9, http.read_timeout
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def test_native_options_precedence_debug_and_file_output
|
|
67
|
+
options = { model: "ignored", "input" => "ignored", response_format: { type: "image", aspect_ratio: "16:9", image_size: "2K" }, store: false }
|
|
68
|
+
original = Marshal.dump(options)
|
|
69
|
+
Dir.mktmpdir do |dir|
|
|
70
|
+
path = File.join(dir, "cat.png")
|
|
71
|
+
with_http(image_body) do |http|
|
|
72
|
+
result = @client.image("A cat", model: "custom-image", output_path: path, options: options, debug: true)
|
|
73
|
+
assert_nil result["error"]
|
|
74
|
+
assert_equal PNG, File.binread(path)
|
|
75
|
+
assert_equal [PNG].pack("m0"), result["content"]
|
|
76
|
+
assert_equal image_body, result["raw"]
|
|
77
|
+
payload = JSON.parse(http.request_received.body)
|
|
78
|
+
assert_equal "custom-image", payload["model"]
|
|
79
|
+
assert_equal "A cat", payload["input"]
|
|
80
|
+
assert_equal false, payload["store"]
|
|
81
|
+
assert_equal "16:9", payload.dig("response_format", "aspect_ratio")
|
|
82
|
+
assert_equal "2K", payload.dig("response_format", "image_size")
|
|
83
|
+
refute payload.key?("generation_config")
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
assert_equal original, Marshal.dump(options)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def test_configuration_delegation_and_model_checks
|
|
90
|
+
AiLite::Gemini.configure { |c| c.api_key = "key"; c.image_model = "configured-image" }
|
|
91
|
+
assert_equal "configured-image", AiLite::Gemini.client.image_model
|
|
92
|
+
assert_equal AiLite::Gemini::DEFAULT_IMAGE_MODEL, @client.image_model
|
|
93
|
+
assert_equal "override", AiLite::Gemini.new(image_model: "override").image_model
|
|
94
|
+
original = AiLite::OpenAI.configured_models
|
|
95
|
+
snapshot = AiLite::Gemini.configured_models
|
|
96
|
+
snapshot["image"].replace("changed")
|
|
97
|
+
assert_equal "configured-image", AiLite::Gemini.configuration.image_model
|
|
98
|
+
with_http(image_body) do |http|
|
|
99
|
+
assert_nil AiLite::Gemini.image("A cat")["error"]
|
|
100
|
+
assert_equal "configured-image", JSON.parse(http.request_received.body)["model"]
|
|
101
|
+
end
|
|
102
|
+
with_http({ "models" => [{ "name" => "models/configured-image" }] }) do
|
|
103
|
+
assert_equal true, AiLite::Gemini.check_configured_models.dig("content", "image", "available")
|
|
104
|
+
end
|
|
105
|
+
assert_equal original, AiLite::OpenAI.configured_models
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def test_invalid_inputs_and_execution_modes_do_not_send_requests
|
|
109
|
+
with_http do |http|
|
|
110
|
+
[nil, " ", [], {}].each { |prompt| refute_nil @client.image(prompt)["error"] }
|
|
111
|
+
[{ stream: true }, { "background" => true }].each do |options|
|
|
112
|
+
refute_nil @client.image("A cat", options: options)["error"]
|
|
113
|
+
end
|
|
114
|
+
assert_nil http.request_received
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def test_errors_do_not_overwrite_output
|
|
119
|
+
malformed = image_body
|
|
120
|
+
malformed["steps"][1]["content"][1]["data"] = "invalid base64!"
|
|
121
|
+
no_image = image_body.merge("steps" => [{ "type" => "model_output", "content" => [{ "type" => "text", "text" => "Cannot generate" }] }])
|
|
122
|
+
responses = [malformed, no_image, "invalid JSON", [], {}, IOError.new("Disconnected")]
|
|
123
|
+
%w[failed incomplete cancelled requires_action queued in_progress].each do |state|
|
|
124
|
+
responses << image_body.merge("status" => state)
|
|
125
|
+
end
|
|
126
|
+
Dir.mktmpdir do |dir|
|
|
127
|
+
path = File.join(dir, "existing.png")
|
|
128
|
+
File.binwrite(path, "original")
|
|
129
|
+
responses.each do |raw|
|
|
130
|
+
with_http(raw) do
|
|
131
|
+
result = @client.image("A cat", output_path: path)
|
|
132
|
+
refute_nil result["error"]
|
|
133
|
+
assert_nil result["content"]
|
|
134
|
+
assert_nil result["raw"]
|
|
135
|
+
assert_equal "original", File.binread(path)
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
with_http(image_body) do
|
|
139
|
+
result = @client.image("A cat", output_path: File.join(dir, "missing", "cat.png"), debug: true)
|
|
140
|
+
refute_nil result["error"]
|
|
141
|
+
assert_equal "image-123", result["response_id"]
|
|
142
|
+
assert_equal image_body, result["raw"]
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
["403", "200"].each do |status|
|
|
146
|
+
with_http({ "error" => { "message" => "Denied" } }, status) do
|
|
147
|
+
result = @client.image("A cat", debug: true)
|
|
148
|
+
assert_equal "Denied", result["error"]
|
|
149
|
+
assert_equal status.to_i, result["status"]
|
|
150
|
+
refute_nil result["raw"]
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
end
|
data/test/gemini_models_test.rb
CHANGED
|
@@ -65,7 +65,7 @@ class GeminiModelsTest < Minitest::Test
|
|
|
65
65
|
end
|
|
66
66
|
def test_local_configuration_and_single_paginated_check
|
|
67
67
|
with_pages do |http|
|
|
68
|
-
assert_equal({ "chat" => AiLite::Gemini::DEFAULT_MODEL }, AiLite::Gemini.configured_models)
|
|
68
|
+
assert_equal({ "chat" => AiLite::Gemini::DEFAULT_MODEL, "embedding" => AiLite::Gemini::DEFAULT_EMBEDDING_MODEL, "image" => AiLite::Gemini::DEFAULT_IMAGE_MODEL, "speech" => AiLite::Gemini::DEFAULT_SPEECH_MODEL, "transcription" => AiLite::Gemini::DEFAULT_TRANSCRIPTION_MODEL }, AiLite::Gemini.configured_models)
|
|
69
69
|
snapshot = @client.configured_models
|
|
70
70
|
snapshot["chat"].replace("changed")
|
|
71
71
|
assert_equal "models/test-model", @client.model
|
|
@@ -74,7 +74,8 @@ class GeminiModelsTest < Minitest::Test
|
|
|
74
74
|
with_pages(page(["other"], "next"), page(["test-model"])) do |http|
|
|
75
75
|
result = @client.check_configured_models
|
|
76
76
|
assert_equal 2, http.requests.size
|
|
77
|
-
assert_equal({ "chat" => { "model" => "models/test-model", "usage" => "chat", "available" => true, "error" => nil } }, result["content"])
|
|
77
|
+
assert_equal({ "chat" => { "model" => "models/test-model", "usage" => "chat", "available" => true, "error" => nil } }, result["content"].slice("chat"))
|
|
78
|
+
assert_equal false, result.dig("content", "embedding", "available")
|
|
78
79
|
assert_nil result["raw"]
|
|
79
80
|
end
|
|
80
81
|
end
|