ai-chat 0.0.6 → 0.0.8

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f4875edb9f6b70da87fbdabfc11fa25be2e76d51496a0ba369e2cf360511abd5
4
- data.tar.gz: a062324e299eea25b68696d1d292f2f0b9fe217b3adaa7068020b6db7d198e53
3
+ metadata.gz: f3f66755a7b1d08ff7fe7e502e69140e11e193dfd238a83abcd032ca83bbd37d
4
+ data.tar.gz: 4bea8f4f0d9c08ed7f42163e6ab16198f217cf67452075c0e119a0b76144b011
5
5
  SHA512:
6
- metadata.gz: fd196bf575fdb10389c93d52e29596bfe68c5a48c49e81b2f5efe377ab91c55b4a2533bb0d09912d06c2400bee688f98f76605161dca9fcf4cab34ebad3067e5
7
- data.tar.gz: d973e4d78ef6fe30333f494d60cd590d523727bf1a175c6fb6ce1832c26dd03dd52bbff8681ad15aec4a19afda2b5616ac9fdda21499ec86e635a106381af9d1
6
+ metadata.gz: 5b9108a61768cc64b5027bab0a918022bb4fba3641406d6ee3ef67f8bec7a0718510392240f72eb56e39a11c412660d25301436b9f625020326d86a845ab7f07
7
+ data.tar.gz: bf82ac462f4879fc6c923d616377cd4d402b2b1fa1b02c62064f29b764e2246a669e5b57f51af262eb5e66f3130be48625a78576dbfb1fb2034057fcbbae6a07
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.0.7] - 2025-04-25
4
+
5
+ - Added support for reasoning effort parameter (`reasoning_effort`) with the OpenAI Responses API
6
+ - Support for reasoning models like o3, o4-mini
7
+ - Updated documentation with examples
8
+
9
+ ## [0.0.6] - 2025-04-25
10
+
11
+ - Fixed minor bugs
12
+ - Improved error handling
13
+
3
14
  ## [0.0.5] - 2025-04-25
4
15
 
5
16
  - Updated to use OpenAI's Responses API instead of Chat Completions API
data/README.md CHANGED
@@ -154,12 +154,45 @@ Useful if you are reconstructing a chat that has already happened.
154
154
  - You can call `.messages` to get an array containing the conversation so far.
155
155
  - TODO: Setting `.messages` will replace the conversation with the provided array.
156
156
 
157
+ ## Testing with Real API Calls
158
+
159
+ While this gem includes specs, they use mocked API responses. To test with real API calls:
160
+
161
+ 1. Navigate to the test program directory: `cd test_program`
162
+ 2. Create a `.env` file in the test_program directory with your API credentials:
163
+ ```
164
+ # Your OpenAI API key
165
+ OPENAI_API_KEY=your_openai_api_key_here
166
+ ```
167
+ 3. Install dependencies: `bundle install`
168
+ 4. Run the test program: `ruby test_ai_chat.rb`
169
+
170
+ This test program runs through all the major features of the gem, making real API calls to OpenAI.
171
+
172
+ ## Reasoning Effort
173
+
174
+ When using reasoning models like `o3` or `o4-mini`, you can specify a reasoning effort level to control how much reasoning the model does before producing its final response:
175
+
176
+ ```ruby
177
+ x = AI::Chat.new
178
+ x.model = "o4-mini"
179
+ x.reasoning_effort = "medium" # Can be "low", "medium", or "high"
180
+
181
+ x.user("Write a bash script that transposes a matrix represented as '[1,2],[3,4],[5,6]'")
182
+ x.assistant!
183
+ ```
184
+
185
+ The `reasoning_effort` parameter guides the model on how many reasoning tokens to generate before creating a response to the prompt. Options are:
186
+ - `"low"`: Favors speed and economical token usage
187
+ - `"medium"`: (Default) Balances speed and reasoning accuracy
188
+ - `"high"`: Favors more complete reasoning
189
+
190
+ Setting to `nil` disables the reasoning parameter.
191
+
157
192
  ## TODOs
158
193
 
159
- - Add a `reasoning_effort` parameter.
160
194
  - Add the ability to set all messages at once, ideally with an ActiveRecord Relation.
161
195
  - Add a way to access the whole API response body (rather than just the message content).
162
- - Add specs.
163
196
 
164
197
  ## Contributing
165
198
 
data/ai_chat.gemspec CHANGED
@@ -33,7 +33,7 @@ Gem::Specification.new do |spec|
33
33
 
34
34
  # Register dependencies of the gem
35
35
  spec.add_runtime_dependency "mime-types", "~> 3.0"
36
- spec.add_runtime_dependency "base64" # Works for all Ruby versions
36
+ spec.add_runtime_dependency "base64", "~> 0.1" # Works for all Ruby versions
37
37
 
38
38
  # Development dependencies
39
39
  spec.add_development_dependency "rake", "~> 13.0"
@@ -2,6 +2,6 @@
2
2
 
3
3
  module AI
4
4
  class Chat
5
- VERSION = "0.0.6"
5
+ VERSION = "0.0.8"
6
6
  end
7
7
  end
data/lib/ai/chat.rb CHANGED
@@ -5,11 +5,31 @@
5
5
  module AI
6
6
  class Chat
7
7
  attr_accessor :messages, :schema, :model
8
+ attr_reader :reasoning_effort
9
+
10
+ VALID_REASONING_EFFORTS = [:low, :medium, :high].freeze
8
11
 
9
12
  def initialize(api_key: nil)
10
13
  @api_key = api_key || ENV.fetch("OPENAI_API_KEY")
11
14
  @messages = []
12
15
  @model = "gpt-4.1-mini"
16
+ @reasoning_effort = nil
17
+ end
18
+
19
+ def reasoning_effort=(value)
20
+ if value.nil?
21
+ @reasoning_effort = nil
22
+ else
23
+ # Convert string to symbol if needed
24
+ symbol_value = value.is_a?(String) ? value.to_sym : value
25
+
26
+ if VALID_REASONING_EFFORTS.include?(symbol_value)
27
+ @reasoning_effort = symbol_value
28
+ else
29
+ valid_values = VALID_REASONING_EFFORTS.map { |v| ":#{v} or \"#{v}\"" }.join(", ")
30
+ raise ArgumentError, "Invalid reasoning_effort value: '#{value}'. Must be one of: #{valid_values}"
31
+ end
32
+ end
13
33
  end
14
34
 
15
35
  def system(content)
@@ -99,6 +119,14 @@ module AI
99
119
  "input" => messages
100
120
  }
101
121
 
122
+ # Add reasoning parameter if specified
123
+ if !@reasoning_effort.nil?
124
+ # Convert symbol back to string for the API request
125
+ request_body_hash["reasoning"] = {
126
+ "effort" => @reasoning_effort.to_s
127
+ }
128
+ end
129
+
102
130
  # Handle structured output (JSON schema)
103
131
  if !schema.nil?
104
132
  # Parse the schema and use it with Structured Output (json_schema)
@@ -172,7 +200,7 @@ module AI
172
200
  end
173
201
 
174
202
  def inspect
175
- "#<#{self.class.name} @messages=#{messages.inspect} @model=#{@model.inspect} @schema=#{@schema.inspect}>"
203
+ "#<#{self.class.name} @messages=#{messages.inspect} @model=#{@model.inspect} @schema=#{@schema.inspect} @reasoning_effort=#{@reasoning_effort.inspect}>"
176
204
  end
177
205
 
178
206
  private
@@ -112,4 +112,46 @@ a.user("Tell me a joke.")
112
112
  response = a.assistant!
113
113
  puts "Assistant response after manual message: #{response}"
114
114
 
115
+ # Reasoning Effort Test
116
+ puts "\nReasoning Effort Test:"
117
+ puts "---------------------"
118
+ begin
119
+ r = AI::Chat.new
120
+ r.model = "o4-mini" # Use a reasoning model
121
+ r.reasoning_effort = "medium"
122
+ r.user("Write a short bash script that counts the number of unique words in a text file.")
123
+ response = r.assistant!
124
+ puts "Response with reasoning effort 'medium': #{response}"
125
+ rescue => e
126
+ puts "Reasoning effort test error: #{e.message}"
127
+ end
128
+
129
+ # Reasoning Effort Validation Test
130
+ puts "\nReasoning Effort Validation Test:"
131
+ puts "-------------------------------"
132
+ begin
133
+ r = AI::Chat.new
134
+ puts "Setting valid reasoning effort string 'low'..."
135
+ r.reasoning_effort = "low"
136
+ puts "Success: reasoning_effort = #{r.reasoning_effort}"
137
+
138
+ puts "Setting valid reasoning effort symbol :medium..."
139
+ r.reasoning_effort = :medium
140
+ puts "Success: reasoning_effort = #{r.reasoning_effort}"
141
+
142
+ puts "Setting valid reasoning effort string 'high'..."
143
+ r.reasoning_effort = "high"
144
+ puts "Success: reasoning_effort = #{r.reasoning_effort}"
145
+
146
+ puts "Setting nil reasoning effort..."
147
+ r.reasoning_effort = nil
148
+ puts "Success: reasoning_effort = #{r.reasoning_effort}"
149
+
150
+ puts "Setting invalid reasoning effort 'extreme'..."
151
+ r.reasoning_effort = "extreme"
152
+ puts "This line should not be reached"
153
+ rescue ArgumentError => e
154
+ puts "Expected error caught: #{e.message}"
155
+ end
156
+
115
157
  puts "\nTests completed!"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ai-chat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Raghu Betina
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: base64
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '0.1'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '0.1'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement