ruby_llm 0.1.0.pre20 → 0.1.0.pre21
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/README.md +22 -3
- data/lib/ruby_llm/error.rb +54 -0
- data/lib/ruby_llm/provider.rb +1 -1
- data/lib/ruby_llm/providers/anthropic.rb +4 -0
- data/lib/ruby_llm/providers/openai.rb +4 -0
- data/lib/ruby_llm/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: 5e413abd8ebadc4f30b5dc080113d9e40405117128bc8156bb7d9b0086f76956
|
4
|
+
data.tar.gz: 9a6971f4f5d7f709a4783b9f816589ca2b7e11199d1189bd5c946a8154d2a529
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bdc7ad9b3e63755d398cfcc514103532d065a72bbf0eb7219496fc4c278e62b80cede1c9cadc75f3dbdb194cb655fb843d04a77d9860fd5665f5af34419f68eb
|
7
|
+
data.tar.gz: c4056980199b29e41993e1f7b092d760328b21e6327c427ad1a9ef09f4e5521482bb9a3cc9c1d0eba3380b18127541b583ebbd7d4780057df1e838c9bba48793
|
data/README.md
CHANGED
@@ -19,9 +19,7 @@ Or install it yourself:
|
|
19
19
|
gem install ruby_llm
|
20
20
|
```
|
21
21
|
|
22
|
-
##
|
23
|
-
|
24
|
-
RubyLLM makes it dead simple to start chatting with AI models:
|
22
|
+
## Configuration
|
25
23
|
|
26
24
|
```ruby
|
27
25
|
require 'ruby_llm'
|
@@ -31,7 +29,13 @@ RubyLLM.configure do |config|
|
|
31
29
|
config.openai_api_key = ENV['OPENAI_API_KEY']
|
32
30
|
config.anthropic_api_key = ENV['ANTHROPIC_API_KEY']
|
33
31
|
end
|
32
|
+
```
|
33
|
+
|
34
|
+
## Quick Start
|
35
|
+
|
36
|
+
RubyLLM makes it dead simple to start chatting with AI models:
|
34
37
|
|
38
|
+
```ruby
|
35
39
|
# Start a conversation
|
36
40
|
chat = RubyLLM.chat
|
37
41
|
chat.ask "What's the best way to learn Ruby?"
|
@@ -169,6 +173,21 @@ chat.ask "What's 123 * 456?"
|
|
169
173
|
# D, -- RubyLLM: Tool calculator returned: "56088"
|
170
174
|
```
|
171
175
|
|
176
|
+
## Error Handling
|
177
|
+
|
178
|
+
RubyLLM wraps provider errors in clear Ruby exceptions:
|
179
|
+
|
180
|
+
```ruby
|
181
|
+
begin
|
182
|
+
chat = RubyLLM.chat
|
183
|
+
chat.ask "Hello world!"
|
184
|
+
rescue RubyLLM::UnauthorizedError
|
185
|
+
puts "Check your API credentials"
|
186
|
+
rescue RubyLLM::BadRequestError => e
|
187
|
+
puts "Something went wrong: #{e.message}"
|
188
|
+
end
|
189
|
+
```
|
190
|
+
|
172
191
|
## Rails Integration
|
173
192
|
|
174
193
|
RubyLLM comes with built-in Rails support that makes it dead simple to persist your chats and messages. Just create your tables and hook it up:
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RubyLLM
|
4
|
+
# Custom error class that wraps API errors from different providers
|
5
|
+
# into a consistent format with helpful error messages.
|
6
|
+
#
|
7
|
+
# Example:
|
8
|
+
# begin
|
9
|
+
# chat.ask "What's 2+2?"
|
10
|
+
# rescue RubyLLM::Error => e
|
11
|
+
# puts "Couldn't chat with AI: #{e.message}"
|
12
|
+
# end
|
13
|
+
class Error < StandardError
|
14
|
+
attr_reader :response
|
15
|
+
|
16
|
+
def initialize(response = nil, message = nil)
|
17
|
+
@response = response
|
18
|
+
super(message || response&.body.to_s)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class UnauthorizedError < Error; end
|
23
|
+
class BadRequestError < Error; end
|
24
|
+
class RateLimitError < Error; end
|
25
|
+
class ServerError < Error; end
|
26
|
+
|
27
|
+
# Faraday middleware that maps provider-specific API errors to RubyLLM errors.
|
28
|
+
# Uses provider's parse_error method to extract meaningful error messages.
|
29
|
+
class ErrorMiddleware < Faraday::Middleware
|
30
|
+
def initialize(app, provider: nil)
|
31
|
+
super(app)
|
32
|
+
@provider = provider
|
33
|
+
end
|
34
|
+
|
35
|
+
def call(env) # rubocop:disable Metrics/MethodLength
|
36
|
+
@app.call(env).on_complete do |response|
|
37
|
+
message = @provider&.parse_error(response)
|
38
|
+
|
39
|
+
case response.status
|
40
|
+
when 400
|
41
|
+
raise BadRequestError.new(response, message)
|
42
|
+
when 401
|
43
|
+
raise UnauthorizedError.new(response, 'Invalid API key - check your credentials')
|
44
|
+
when 429
|
45
|
+
raise RateLimitError.new(response, 'Rate limit exceeded - please wait a moment')
|
46
|
+
when 500..599
|
47
|
+
raise ServerError.new(response, 'API server error - please try again')
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
Faraday::Middleware.register_middleware(llm_errors: RubyLLM::ErrorMiddleware)
|
data/lib/ruby_llm/provider.rb
CHANGED
@@ -69,7 +69,7 @@ module RubyLLM
|
|
69
69
|
f.request :json
|
70
70
|
f.response :json
|
71
71
|
f.adapter Faraday.default_adapter
|
72
|
-
f.use
|
72
|
+
f.use :llm_errors, provider: self
|
73
73
|
f.response :logger, RubyLLM.logger, { headers: false, bodies: true, errors: true, log_level: :debug }
|
74
74
|
end
|
75
75
|
end
|
data/lib/ruby_llm/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_llm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0.
|
4
|
+
version: 0.1.0.pre21
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Carmine Paolino
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-02-
|
11
|
+
date: 2025-02-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: event_stream_parser
|
@@ -350,6 +350,7 @@ files:
|
|
350
350
|
- lib/ruby_llm/chunk.rb
|
351
351
|
- lib/ruby_llm/configuration.rb
|
352
352
|
- lib/ruby_llm/embedding.rb
|
353
|
+
- lib/ruby_llm/error.rb
|
353
354
|
- lib/ruby_llm/message.rb
|
354
355
|
- lib/ruby_llm/model_capabilities/anthropic.rb
|
355
356
|
- lib/ruby_llm/model_capabilities/openai.rb
|