ai-lite 1.2.0 → 1.3.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 +6 -0
- data/README.md +101 -13
- data/lib/ai_lite/claude/chat.rb +73 -0
- data/lib/ai_lite/claude/configuration.rb +22 -0
- data/lib/ai_lite/claude.rb +82 -0
- data/lib/ai_lite/version.rb +1 -1
- data/lib/ai_lite.rb +1 -0
- data/test/claude_test.rb +156 -0
- data/test/package_test.rb +5 -4
- metadata +11 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9b745990d626099d4241a118b3d1b86492c3e5f7ee6d2462ce9354c6e5f7d4a5
|
|
4
|
+
data.tar.gz: 29410ac136fe3eb8c07aa1a00b62f0a47f91693030ffec657a90bdd73c81415b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: caf3d92566985f294ea04a53014c03ce9b9d5d57290e59a3b9fcbd8b7314dc7762ffa9cda830b680a671f0f6605abbcef15616ce8f682c55a452130fb492bc74
|
|
7
|
+
data.tar.gz: 5e1ab3f82a09b7f8e356b575b1ac6e4331296419a65e019f063ced9a8f295f9942006b23599e70e32c9073d0e73a40e3a35493641615c1d9e87cfc26bd5a13d9
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.3.0 (2026-11-10)
|
|
4
|
+
|
|
5
|
+
- Begin Claude support with independent configuration, stdlib Messages transport,
|
|
6
|
+
synchronous chat, explicit history, replayable assistant messages, and tests.
|
|
7
|
+
Streaming, tool helpers, token counting, and model helpers remain pending.
|
|
8
|
+
|
|
3
9
|
## 1.2.0 (2026-11-02)
|
|
4
10
|
|
|
5
11
|
- Add `tools:` and `tool_results:` to OpenAI and Gemini chat and streaming,
|
data/README.md
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
# AI Lite
|
|
2
2
|
|
|
3
|
-
AI Lite is a lightweight, pure Ruby client for OpenAI and
|
|
4
|
-
|
|
5
|
-
checks through simple interfaces and a consistent result format.
|
|
3
|
+
AI Lite is a lightweight, pure Ruby client for OpenAI, Gemini, and Claude, built
|
|
4
|
+
entirely on the Ruby standard library with no runtime gem dependencies.
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
OpenAI and Gemini support chat, streaming, function calling, embeddings, image
|
|
7
|
+
generation, speech, transcription, token counting, and model availability checks.
|
|
8
|
+
Claude currently supports synchronous chat and explicit message history through
|
|
9
|
+
the same consistent result format. Claude support is available starting in v1.3.0.
|
|
10
|
+
|
|
11
|
+
Built for Rails apps and plain Ruby projects that want straightforward AI
|
|
12
|
+
integration without a larger SDK or framework. AI Lite handles the API calls
|
|
13
|
+
while you control your application's architecture.
|
|
14
|
+
|
|
15
|
+
Supports Ruby 2.6 through 4.0, with no Rails dependency or runtime gem dependencies.
|
|
10
16
|
|
|
11
17
|
This gem is intentionally small:
|
|
12
18
|
|
|
@@ -22,9 +28,11 @@ require "ai_lite"
|
|
|
22
28
|
|
|
23
29
|
openai = AiLite::OpenAI.new # Reads OPENAI_API_KEY
|
|
24
30
|
gemini = AiLite::Gemini.new # Reads GEMINI_API_KEY
|
|
31
|
+
claude = AiLite::Claude.new # Reads ANTHROPIC_API_KEY
|
|
25
32
|
|
|
26
33
|
openai.chat("Say hello")
|
|
27
34
|
gemini.chat("Say hello")
|
|
35
|
+
claude.chat("Say hello")
|
|
28
36
|
|
|
29
37
|
openai.embed("Text to vectorize")
|
|
30
38
|
gemini.embed("Text to vectorize")
|
|
@@ -39,19 +47,99 @@ media input and a YouTube helper. The table below lists the available methods.
|
|
|
39
47
|
|
|
40
48
|
## Provider support
|
|
41
49
|
|
|
42
|
-
| Method | OpenAI | Gemini |
|
|
43
|
-
| --- | --- | --- |
|
|
44
|
-
| `chat
|
|
45
|
-
|
|
|
46
|
-
|
|
|
47
|
-
| `
|
|
48
|
-
| `
|
|
50
|
+
| Method | OpenAI | Gemini | Claude |
|
|
51
|
+
| --- | --- | --- | --- |
|
|
52
|
+
| `chat` | Yes | Yes | Yes; tool helpers pending |
|
|
53
|
+
| `chat_stream`, `count_tokens` | Yes | Yes | Not implemented |
|
|
54
|
+
| Four model helpers | Yes | Yes | Not implemented |
|
|
55
|
+
| `youtube` | No | Yes | Not implemented |
|
|
56
|
+
| `embed`, `image`, `speak`, `transcribe` | Yes | Yes | Not implemented |
|
|
57
|
+
| `moderate` | Yes | Not implemented | Not implemented |
|
|
49
58
|
|
|
50
59
|
Unsupported provider methods are absent and raise Ruby's `NoMethodError` if called.
|
|
51
60
|
Configure providers during application startup; do not mutate shared configuration
|
|
52
61
|
or client headers while requests are running. The gem does not provide automatic
|
|
53
62
|
retries, worker threads, or a background-job queue.
|
|
54
63
|
|
|
64
|
+
## Claude
|
|
65
|
+
|
|
66
|
+
`AiLite::Claude` supports synchronous chat and explicit message history starting
|
|
67
|
+
in v1.3.0. Claude streaming, normalized tool calls/results, token
|
|
68
|
+
counting, and model helpers are still pending.
|
|
69
|
+
|
|
70
|
+
```ruby
|
|
71
|
+
require "ai_lite/claude" # Also loaded by require "ai_lite"
|
|
72
|
+
|
|
73
|
+
claude = AiLite::Claude.new # Reads ANTHROPIC_API_KEY; does not load .env
|
|
74
|
+
result = claude.chat("Hello", max_output_tokens: 32,
|
|
75
|
+
options: { thinking: { type: "disabled" }, output_config: { effort: "low" } })
|
|
76
|
+
raise result["error"] if result["error"]
|
|
77
|
+
puts result["content"]
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Defaults: `claude-haiku-5-5`, API version `2026-10-21`, 120-second timeout, and
|
|
81
|
+
2,000 maximum output tokens. Thinking settings are provider defaults unless
|
|
82
|
+
supplied through `options:`. The short example disables thinking and limits output
|
|
83
|
+
for inexpensive development checks; those options depend on the selected model.
|
|
84
|
+
|
|
85
|
+
The example above uses the default Haiku model. For `claude-sonnet-5-5`, use
|
|
86
|
+
`thinking: { type: "between_tools" }` instead of `disabled`:
|
|
87
|
+
|
|
88
|
+
```ruby
|
|
89
|
+
result = claude.chat("Reply only OK.", model: "claude-sonnet-5-5",
|
|
90
|
+
max_output_tokens: 32,
|
|
91
|
+
options: { thinking: { type: "between_tools" }, output_config: { effort: "low" } })
|
|
92
|
+
raise result["error"] if result["error"]
|
|
93
|
+
puts result["content"]
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
AI Lite passes these native options through to Claude; it does not translate
|
|
97
|
+
thinking settings between models. The API returns an error for unsupported settings.
|
|
98
|
+
|
|
99
|
+
```ruby
|
|
100
|
+
AiLite::Claude.configure do |config|
|
|
101
|
+
config.api_key = ENV["ANTHROPIC_API_KEY"]
|
|
102
|
+
config.model = "claude-haiku-5-5"
|
|
103
|
+
config.max_output_tokens = 256
|
|
104
|
+
# config.workspace_id = "wrkspc_..." # For keys that require workspace selection
|
|
105
|
+
end
|
|
106
|
+
AiLite::Claude.chat("Hello")
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Constructor keywords override configuration, and `api_key:` falls back to
|
|
110
|
+
`ANTHROPIC_API_KEY`. Other providers' keys are never used. `api_version` and
|
|
111
|
+
`workspace_id` are configurable in both places. Missing credentials raise
|
|
112
|
+
`ArgumentError`; request, transport, and response failures use the normal error
|
|
113
|
+
envelope. `instructions:` maps to `system`, and `max_output_tokens:` maps to
|
|
114
|
+
`max_tokens`. Native options pass through without mutating the caller's hashes.
|
|
115
|
+
|
|
116
|
+
Claude's standard Messages API requires history on each turn:
|
|
117
|
+
|
|
118
|
+
```ruby
|
|
119
|
+
history = [{ role: "user", content: "My favorite color is blue." }]
|
|
120
|
+
first = claude.chat(history)
|
|
121
|
+
raise first["error"] if first["error"]
|
|
122
|
+
history << first["assistant_message"]
|
|
123
|
+
history << { role: "user", content: "What is my favorite color?" }
|
|
124
|
+
second = claude.chat(history)
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
The usual envelope is preserved, with `assistant_message` added for replay.
|
|
128
|
+
It contains the original assistant content blocks, including any thinking blocks
|
|
129
|
+
and signatures, even when `raw` is nil. Treat it as provider conversation data;
|
|
130
|
+
`content` remains the displayable text (or parsed JSON). `response_id` identifies
|
|
131
|
+
the message but cannot continue the conversation: `previous_response_id:` returns
|
|
132
|
+
an error. No conversation is stored inside the client.
|
|
133
|
+
|
|
134
|
+
Use `debug: true` for the full native response and usage. Truncation, refusals,
|
|
135
|
+
paused turns, and tool requests currently return a descriptive error while
|
|
136
|
+
preserving available text and assistant content. Only `end_turn` and
|
|
137
|
+
`stop_sequence` are handled as completed answers in this initial implementation.
|
|
138
|
+
Do not treat HTTP 200 alone as success. Native content blocks can be supplied in
|
|
139
|
+
message arrays; the gem does not upload or convert media.
|
|
140
|
+
|
|
141
|
+
Reference: [Claude Messages API](https://platform.claude.com/docs/en/api/messages/create).
|
|
142
|
+
|
|
55
143
|
## Function calling (OpenAI and Gemini)
|
|
56
144
|
|
|
57
145
|
Pass native tool definitions through `tools:` on `chat` or `chat_stream`.
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
class AiLite
|
|
2
|
+
class Claude
|
|
3
|
+
module Chat
|
|
4
|
+
def chat(message, model: nil, instructions: nil, max_output_tokens: nil, previous_response_id: nil, debug: false, options: {})
|
|
5
|
+
response = raw = response_id = nil
|
|
6
|
+
raise ArgumentError, "Claude requires explicit message history, not previous_response_id" unless previous_response_id.nil?
|
|
7
|
+
raise ArgumentError, "options must be a hash" unless options.is_a?(Hash)
|
|
8
|
+
payload = options.each_with_object({}) { |(key, value), result| result[key.to_s] = value }
|
|
9
|
+
if payload.key?("previous_response_id") || payload.key?("previous_interaction_id")
|
|
10
|
+
raise ArgumentError, "Claude requires explicit message history, not a previous response ID"
|
|
11
|
+
end
|
|
12
|
+
raise ArgumentError, "Claude streaming is not implemented yet" if payload["stream"]
|
|
13
|
+
unless instructions.nil? || instructions.is_a?(String)
|
|
14
|
+
raise ArgumentError, "instructions must be a string; use options[:system] for native blocks"
|
|
15
|
+
end
|
|
16
|
+
messages = message.is_a?(String) ? [{ "role" => "user", "content" => message }] : message
|
|
17
|
+
unless messages.is_a?(Array) && !messages.empty? && messages.all? { |item| item.is_a?(Hash) }
|
|
18
|
+
raise ArgumentError, "message must be a string or a nonempty array of Claude messages"
|
|
19
|
+
end
|
|
20
|
+
messages.each do |item|
|
|
21
|
+
role = item["role"] || item[:role]
|
|
22
|
+
content = item.key?("content") ? item["content"] : item[:content]
|
|
23
|
+
unless %w[user assistant system].include?(role) && (content.is_a?(String) || content.is_a?(Array))
|
|
24
|
+
raise ArgumentError, "Each Claude message requires a native role and string or array content"
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
limit = max_output_tokens || payload["max_tokens"] || self.max_output_tokens
|
|
28
|
+
raise ArgumentError, "max_output_tokens must be a nonnegative integer" unless limit.is_a?(Integer) && limit >= 0
|
|
29
|
+
payload.merge!("model" => model || self.model, "messages" => messages, "max_tokens" => limit)
|
|
30
|
+
payload["system"] = instructions unless instructions.nil?
|
|
31
|
+
response = post_json(payload, endpoint: "#{API_BASE_URL}/messages")
|
|
32
|
+
raw = response.body
|
|
33
|
+
raw = JSON.parse(raw)
|
|
34
|
+
raise "Invalid Claude response: expected an object" unless raw.is_a?(Hash)
|
|
35
|
+
response_id = raw["id"]
|
|
36
|
+
unless response.code.to_i.between?(200, 299) && !raw["error"]
|
|
37
|
+
return result_envelope(status: response.code.to_i, error: error_message(raw), response_id: response_id, raw: raw, debug: debug)
|
|
38
|
+
end
|
|
39
|
+
blocks = raw["content"]
|
|
40
|
+
unless raw["type"] == "message" && raw["role"] == "assistant" && blocks.is_a?(Array) && blocks.all? { |part| part.is_a?(Hash) }
|
|
41
|
+
raise "Invalid Claude response: expected an assistant message with content blocks"
|
|
42
|
+
end
|
|
43
|
+
text = blocks.select { |part| part["type"] == "text" }.map do |part|
|
|
44
|
+
raise "Invalid Claude text block" unless part["text"].is_a?(String)
|
|
45
|
+
part["text"]
|
|
46
|
+
end.join.strip
|
|
47
|
+
content = text.empty? ? nil : text
|
|
48
|
+
if content
|
|
49
|
+
begin
|
|
50
|
+
content = JSON.parse(text)
|
|
51
|
+
rescue JSON::ParserError
|
|
52
|
+
# Preserve ordinary text, matching the other providers.
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
reason = raw["stop_reason"]
|
|
56
|
+
error = unless %w[end_turn stop_sequence].include?(reason)
|
|
57
|
+
"Claude stopped with #{reason || 'no stop reason'}; inspect raw with debug: true"
|
|
58
|
+
end
|
|
59
|
+
result = result_envelope(status: response.code.to_i, content: content, error: error,
|
|
60
|
+
response_id: response_id, raw: raw, debug: debug)
|
|
61
|
+
# Preserve every content block for stateless replay, including thinking signatures.
|
|
62
|
+
result["assistant_message"] = { "role" => "assistant", "content" => blocks }
|
|
63
|
+
result
|
|
64
|
+
rescue StandardError => e
|
|
65
|
+
result_envelope(status: response ? response.code.to_i : "unknown", error: e.message,
|
|
66
|
+
response_id: response_id, raw: raw, debug: debug)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
include Chat
|
|
71
|
+
private_constant :Chat
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
class AiLite
|
|
2
|
+
class Claude
|
|
3
|
+
API_BASE_URL = "https://api.anthropic.com/v1".freeze
|
|
4
|
+
DEFAULT_MODEL = "claude-haiku-5-5".freeze
|
|
5
|
+
DEFAULT_API_VERSION = "2026-10-21".freeze
|
|
6
|
+
DEFAULT_TIMEOUT = 120
|
|
7
|
+
DEFAULT_MAX_OUTPUT_TOKENS = 2000
|
|
8
|
+
|
|
9
|
+
class Configuration
|
|
10
|
+
attr_accessor :api_key, :model, :api_version, :workspace_id, :timeout, :max_output_tokens
|
|
11
|
+
|
|
12
|
+
def initialize
|
|
13
|
+
@api_key = nil
|
|
14
|
+
@model = DEFAULT_MODEL
|
|
15
|
+
@api_version = DEFAULT_API_VERSION
|
|
16
|
+
@workspace_id = nil
|
|
17
|
+
@timeout = DEFAULT_TIMEOUT
|
|
18
|
+
@max_output_tokens = DEFAULT_MAX_OUTPUT_TOKENS
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "net/http"
|
|
3
|
+
require "uri"
|
|
4
|
+
require_relative "version"
|
|
5
|
+
require_relative "claude/configuration"
|
|
6
|
+
require_relative "claude/chat"
|
|
7
|
+
|
|
8
|
+
class AiLite
|
|
9
|
+
class Claude
|
|
10
|
+
class << self
|
|
11
|
+
def configuration
|
|
12
|
+
@configuration ||= Configuration.new
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def configure
|
|
16
|
+
yield configuration
|
|
17
|
+
reset_client!
|
|
18
|
+
configuration
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def reset_configuration!
|
|
22
|
+
@configuration = Configuration.new
|
|
23
|
+
reset_client!
|
|
24
|
+
configuration
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def client
|
|
28
|
+
@client ||= new
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def reset_client!
|
|
32
|
+
@client = nil
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def chat(message, **kwargs)
|
|
36
|
+
client.chat(message, **kwargs)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
attr_reader :api_key, :model, :api_version, :workspace_id, :timeout, :max_output_tokens, :headers
|
|
41
|
+
|
|
42
|
+
def initialize(api_key: nil, model: nil, api_version: nil, workspace_id: nil, timeout: nil, max_output_tokens: nil)
|
|
43
|
+
config = self.class.configuration
|
|
44
|
+
@api_key = api_key || config.api_key || ENV["ANTHROPIC_API_KEY"]
|
|
45
|
+
raise ArgumentError, "Missing Anthropic API key" if @api_key.to_s.strip.empty?
|
|
46
|
+
|
|
47
|
+
@model = model || config.model
|
|
48
|
+
@api_version = api_version || config.api_version
|
|
49
|
+
@workspace_id = workspace_id || config.workspace_id
|
|
50
|
+
@timeout = timeout || config.timeout
|
|
51
|
+
@max_output_tokens = max_output_tokens || config.max_output_tokens
|
|
52
|
+
@headers = { "Authorization" => "Bearer #{@api_key}", "anthropic-version" => @api_version,
|
|
53
|
+
"Content-Type" => "application/json" }
|
|
54
|
+
@headers["anthropic-workspace-id"] = @workspace_id if @workspace_id
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def post_json(payload, endpoint:)
|
|
60
|
+
uri = URI.parse(endpoint)
|
|
61
|
+
request = Net::HTTP::Post.new(uri)
|
|
62
|
+
headers.each { |key, value| request[key] = value }
|
|
63
|
+
request.body = JSON.generate(payload)
|
|
64
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
|
|
65
|
+
http.open_timeout = timeout
|
|
66
|
+
http.read_timeout = timeout
|
|
67
|
+
http.request(request)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def result_envelope(status:, content: nil, error: nil, response_id: nil, raw: nil, debug: false)
|
|
72
|
+
{ "content" => content, "response_id" => response_id, "status" => status,
|
|
73
|
+
"error" => error, "raw" => debug ? raw : nil }
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def error_message(raw)
|
|
77
|
+
return raw.to_s unless raw.is_a?(Hash)
|
|
78
|
+
error = raw["error"]
|
|
79
|
+
error.is_a?(Hash) ? (error["message"] || error["type"] || error.to_s) : (error || raw["message"] || raw.to_s)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
data/lib/ai_lite/version.rb
CHANGED
data/lib/ai_lite.rb
CHANGED
data/test/claude_test.rb
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
require_relative "test_helper"
|
|
2
|
+
|
|
3
|
+
class ClaudeTest < 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::Claude.reset_configuration!
|
|
20
|
+
@old_key = ENV.delete("ANTHROPIC_API_KEY")
|
|
21
|
+
@client = AiLite::Claude.new(api_key: "claude-test", timeout: 7)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def teardown
|
|
25
|
+
ENV["ANTHROPIC_API_KEY"] = @old_key
|
|
26
|
+
AiLite::Claude.reset_configuration!
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def body(text = "Hello", reason = "end_turn")
|
|
30
|
+
{ "id" => "msg_123", "type" => "message", "role" => "assistant", "stop_reason" => reason,
|
|
31
|
+
"content" => [{ "type" => "thinking", "thinking" => "private", "signature" => "sig" },
|
|
32
|
+
{ "type" => "text", "text" => text }], "usage" => { "input_tokens" => 3 } }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def with_http(raw = body, status = "200")
|
|
36
|
+
http = Http.new(raw.is_a?(Exception) ? raw : Response.new(status, raw.is_a?(String) ? raw : JSON.generate(raw)))
|
|
37
|
+
original = Net::HTTP.method(:start)
|
|
38
|
+
Net::HTTP.define_singleton_method(:start) do |host, port, use_ssl:, &block|
|
|
39
|
+
raise "Wrong Claude endpoint" unless host == "api.anthropic.com" && port == 443 && use_ssl
|
|
40
|
+
block.call(http)
|
|
41
|
+
end
|
|
42
|
+
yield http
|
|
43
|
+
ensure
|
|
44
|
+
Net::HTTP.define_singleton_method(:start, original)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def test_configuration_key_precedence_and_provider_isolation
|
|
48
|
+
assert_raises(ArgumentError) { AiLite::Claude.new }
|
|
49
|
+
ENV["ANTHROPIC_API_KEY"] = "env-key"
|
|
50
|
+
assert_equal "env-key", AiLite::Claude.new.api_key
|
|
51
|
+
previous = AiLite::OpenAI.configuration.model
|
|
52
|
+
AiLite::Claude.configure { |c| c.api_key = "configured"; c.model = "custom"; c.workspace_id = "wrkspc_test"; c.api_version = "custom-version" }
|
|
53
|
+
assert_equal "configured", AiLite::Claude.new.api_key
|
|
54
|
+
assert_equal "explicit", AiLite::Claude.new(api_key: "explicit").api_key
|
|
55
|
+
assert_equal "custom", AiLite::Claude.client.model
|
|
56
|
+
assert_equal "wrkspc_test", AiLite::Claude.client.headers["anthropic-workspace-id"]
|
|
57
|
+
assert_equal "custom-version", AiLite::Claude.client.headers["anthropic-version"]
|
|
58
|
+
old_client = AiLite::Claude.client
|
|
59
|
+
AiLite::Claude.configure { |c| c.model = "other" }
|
|
60
|
+
refute_same old_client, AiLite::Claude.client
|
|
61
|
+
assert_equal previous, AiLite::OpenAI.configuration.model
|
|
62
|
+
AiLite::Claude.reset_configuration!
|
|
63
|
+
assert_equal "claude-haiku-5-5", AiLite::Claude.new.model
|
|
64
|
+
%i[chat_stream embed speak transcribe image moderate count_tokens available_models].each { |method| refute_respond_to @client, method }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def test_chat_transport_and_replay_message
|
|
68
|
+
with_http do |http|
|
|
69
|
+
result = @client.chat("Hi")
|
|
70
|
+
assert_equal "Hello", result["content"]
|
|
71
|
+
assert_equal "msg_123", result["response_id"]
|
|
72
|
+
assert_equal 200, result["status"]
|
|
73
|
+
assert_nil result["error"]
|
|
74
|
+
assert_nil result["raw"]
|
|
75
|
+
assert_equal({ "role" => "assistant", "content" => body["content"] }, result["assistant_message"])
|
|
76
|
+
request = http.request_received
|
|
77
|
+
assert_equal "/v1/messages", request.path
|
|
78
|
+
assert_equal "Bearer claude-test", request["Authorization"]
|
|
79
|
+
assert_equal "2026-10-21", request["anthropic-version"]
|
|
80
|
+
assert_nil request["x-goog-api-key"]
|
|
81
|
+
assert_equal 7, http.open_timeout
|
|
82
|
+
assert_equal 7, http.read_timeout
|
|
83
|
+
assert_equal({ "model" => "claude-haiku-5-5", "messages" => [{ "role" => "user", "content" => "Hi" }], "max_tokens" => 2000 }, JSON.parse(request.body))
|
|
84
|
+
history = [{ role: "user", content: "Hi" }, result["assistant_message"], { role: "user", content: "Continue" }]
|
|
85
|
+
@client.chat(history)
|
|
86
|
+
assert_equal JSON.parse(JSON.generate(history)), JSON.parse(http.request_received.body)["messages"]
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def test_options_native_media_precedence_and_no_mutation
|
|
91
|
+
history = [{ role: "user", content: [{ type: "image", source: { type: "base64", media_type: "image/png", data: "AAAA" } }] }]
|
|
92
|
+
options = { "model" => "ignored", messages: [], max_tokens: 10, system: "ignored", thinking: { type: "disabled" } }
|
|
93
|
+
original = Marshal.dump([history, options])
|
|
94
|
+
with_http(body('{"ok":true}')) do |http|
|
|
95
|
+
result = @client.chat(history, model: "selected", instructions: "Brief", max_output_tokens: 20, options: options, debug: true)
|
|
96
|
+
assert_equal({ "ok" => true }, result["content"])
|
|
97
|
+
assert_equal body('{"ok":true}'), result["raw"]
|
|
98
|
+
payload = JSON.parse(http.request_received.body)
|
|
99
|
+
assert_equal "selected", payload["model"]
|
|
100
|
+
assert_equal "Brief", payload["system"]
|
|
101
|
+
assert_equal 20, payload["max_tokens"]
|
|
102
|
+
assert_equal({ "type" => "disabled" }, payload["thinking"])
|
|
103
|
+
assert_equal JSON.parse(JSON.generate(history)), payload["messages"]
|
|
104
|
+
end
|
|
105
|
+
assert_equal original, Marshal.dump([history, options])
|
|
106
|
+
with_http do |http|
|
|
107
|
+
@client.chat("Hi", options: { max_tokens: 12, system: [{ type: "text", text: "Native" }] })
|
|
108
|
+
assert_equal 12, JSON.parse(http.request_received.body)["max_tokens"]
|
|
109
|
+
assert_equal [{ "type" => "text", "text" => "Native" }], JSON.parse(http.request_received.body)["system"]
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def test_class_delegation_and_text_only_output
|
|
114
|
+
AiLite::Claude.configure { |c| c.api_key = "configured" }
|
|
115
|
+
with_http(body("Hello", "stop_sequence")) { assert_equal "Hello", AiLite::Claude.chat("Hi")["content"] }
|
|
116
|
+
with_http(body.merge("content" => [])) { assert_nil @client.chat("Hi")["content"] }
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def test_stop_reasons_and_malformed_responses
|
|
120
|
+
%w[max_tokens refusal model_context_window_exceeded pause_turn tool_use unknown].each do |reason|
|
|
121
|
+
with_http(body("Partial", reason)) do
|
|
122
|
+
result = @client.chat("Hi")
|
|
123
|
+
assert_equal "Partial", result["content"]
|
|
124
|
+
assert_includes result["error"], reason
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
[[], {}, body.merge("content" => nil), body.merge("role" => "user"), body.merge("content" => [{ "type" => "text", "text" => 1 }]), "not json"].each do |raw|
|
|
128
|
+
with_http(raw) do
|
|
129
|
+
result = @client.chat("Hi", debug: true)
|
|
130
|
+
refute_nil result["error"]
|
|
131
|
+
assert_equal 200, result["status"]
|
|
132
|
+
assert_equal raw, result["raw"]
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
["200", "401", "429", "529"].each do |status|
|
|
136
|
+
with_http({ "error" => { "type" => "overloaded_error", "message" => "Unavailable" } }, status) do
|
|
137
|
+
result = @client.chat("Hi")
|
|
138
|
+
assert_equal "Unavailable", result["error"]
|
|
139
|
+
assert_equal status.to_i, result["status"]
|
|
140
|
+
assert_nil result["raw"]
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
with_http(IOError.new("Disconnected")) { assert_equal "unknown", @client.chat("Hi")["status"] }
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def test_invalid_inputs_never_send_requests
|
|
147
|
+
with_http do |http|
|
|
148
|
+
[nil, 1, {}, [], [1], [{ role: "invalid", content: "Hi" }]].each { |message| refute_nil @client.chat(message)["error"] }
|
|
149
|
+
[{ options: nil }, { previous_response_id: "msg_123" }, { options: { previous_response_id: "msg_123" } },
|
|
150
|
+
{ options: { stream: true } }, { instructions: 1 }, { max_output_tokens: -1 }].each do |kwargs|
|
|
151
|
+
refute_nil @client.chat("Hi", **kwargs)["error"]
|
|
152
|
+
end
|
|
153
|
+
assert_nil http.request_received
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
end
|
data/test/package_test.rb
CHANGED
|
@@ -26,7 +26,7 @@ class PackageTest < Minitest::Test
|
|
|
26
26
|
"--local", "--install-dir", installed, "--no-document", "--ignore-dependencies")
|
|
27
27
|
assert status.success?, output
|
|
28
28
|
|
|
29
|
-
%w[ai_lite ai_lite/openai ai_lite/gemini].each do |entry|
|
|
29
|
+
%w[ai_lite ai_lite/openai ai_lite/gemini ai_lite/claude].each do |entry|
|
|
30
30
|
output, status = Open3.capture2e(
|
|
31
31
|
{ "GEM_HOME" => installed, "GEM_PATH" => installed, "RUBYLIB" => nil, "RUBYOPT" => nil },
|
|
32
32
|
RbConfig.ruby, "-e", <<~'RUBY', entry, installed, chdir: dir
|
|
@@ -34,13 +34,14 @@ class PackageTest < Minitest::Test
|
|
|
34
34
|
spec = Gem.loaded_specs.fetch("ai-lite")
|
|
35
35
|
abort "Loaded outside isolated installation" unless File.realpath(spec.full_gem_path).start_with?(File.realpath(ARGV.fetch(1)) + "/")
|
|
36
36
|
abort "Unexpected runtime dependencies" unless spec.runtime_dependencies.empty?
|
|
37
|
-
[:OpenAI, :Gemini].each do |name|
|
|
37
|
+
[:OpenAI, :Gemini, :Claude].each do |name|
|
|
38
38
|
next unless AiLite.const_defined?(name, false)
|
|
39
39
|
client = AiLite.const_get(name).new(api_key: "package-test-key")
|
|
40
|
-
[:chat, :chat_stream, :embed, :image, :speak, :transcribe, :count_tokens, :configured_models]
|
|
40
|
+
methods = name == :Claude ? [:chat] : [:chat, :chat_stream, :embed, :image, :speak, :transcribe, :count_tokens, :configured_models]
|
|
41
|
+
methods.each do |method|
|
|
41
42
|
abort "Missing #{name}.#{method}" unless client.respond_to?(method)
|
|
42
43
|
end
|
|
43
|
-
abort "Missing chat model" unless client.
|
|
44
|
+
abort "Missing chat model" unless client.model
|
|
44
45
|
end
|
|
45
46
|
RUBY
|
|
46
47
|
)
|
metadata
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ai-lite
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- William Basmayor
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-10 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: |-
|
|
14
|
-
AI Lite is a lightweight Ruby client for OpenAI and
|
|
14
|
+
AI Lite is a lightweight Ruby client for OpenAI, Gemini, and Claude, built for Rails apps and plain Ruby projects. It uses only Ruby's standard library and has no runtime gem dependencies.
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
AI Lite handles the API calls while you control your application's architecture, providing straightforward AI integration without a larger SDK or framework. Supports Ruby 2.6 through 4.0 with no Rails dependency.
|
|
17
|
+
|
|
18
|
+
OpenAI and Gemini support chat, streaming, function calling, embeddings, image generation, speech, transcription, token counting, and model checks. OpenAI also supports moderation. Gemini also supports structured media inputs and a YouTube helper. Claude supports synchronous chat and explicit message history.
|
|
17
19
|
email:
|
|
18
20
|
executables: []
|
|
19
21
|
extensions: []
|
|
@@ -23,6 +25,9 @@ files:
|
|
|
23
25
|
- LICENSE
|
|
24
26
|
- README.md
|
|
25
27
|
- lib/ai_lite.rb
|
|
28
|
+
- lib/ai_lite/claude.rb
|
|
29
|
+
- lib/ai_lite/claude/chat.rb
|
|
30
|
+
- lib/ai_lite/claude/configuration.rb
|
|
26
31
|
- lib/ai_lite/gemini.rb
|
|
27
32
|
- lib/ai_lite/gemini/chat.rb
|
|
28
33
|
- lib/ai_lite/gemini/configuration.rb
|
|
@@ -43,6 +48,7 @@ files:
|
|
|
43
48
|
- lib/ai_lite/openai/tool_calling.rb
|
|
44
49
|
- lib/ai_lite/version.rb
|
|
45
50
|
- test/ai_lite_test.rb
|
|
51
|
+
- test/claude_test.rb
|
|
46
52
|
- test/gemini_embeddings_test.rb
|
|
47
53
|
- test/gemini_images_test.rb
|
|
48
54
|
- test/gemini_models_test.rb
|
|
@@ -84,5 +90,5 @@ requirements: []
|
|
|
84
90
|
rubygems_version: 3.5.22
|
|
85
91
|
signing_key:
|
|
86
92
|
specification_version: 4
|
|
87
|
-
summary: Lightweight Ruby client
|
|
93
|
+
summary: Lightweight Ruby client for OpenAI, Gemini, and Claude.
|
|
88
94
|
test_files: []
|