raix 0.7 → 0.7.1

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: f6783ed9671864edec5d734f8003822accee2dd7b89d0453a2c6973d35ecf2bf
4
- data.tar.gz: 90eac19ba012daaeed4ce898a2be3476d1dac9c6df2cbb66fbe88a5620b971c6
3
+ metadata.gz: 9a2990e8225edcfbcc5007aae41a0dd69987bff18bd085bb9c486561e5597044
4
+ data.tar.gz: 60ef3efa8eb6cbe1b445afd09fc65a4b1d6fc62c872588956baee46b511d79d3
5
5
  SHA512:
6
- metadata.gz: 6753d494dd65e960373308189d7324e973c347f627ed82463b9ae3d7079446a87b7e012949c5a69adf734a1e9f37e8684b51a04f3a7b39b6b9772b501908949a
7
- data.tar.gz: 07f8d142ca423979013c17da2f8758a7113fc583fc2ee47b10cf64072a7be12aa7b53447821340239033866e8c108b1bea76ffc202fcd24e02cf814607df5989
6
+ metadata.gz: dfa0a7dfed2782d49c95f749c847b024ef41097b4ab318b34f9eabbfd9f754cf49c6eb859c1b14534332efea930d707df457e2d24a4c1f30f043d7dacb3c801c
7
+ data.tar.gz: 2e291a98b2ed5e25c2381a30d609e4277cb94458531c03e77b79fc95e70ee92e40c0d9cacb31809a1f7acca860550e4d02698856321869144dc56a4d794ea3d8
data/CHANGELOG.md CHANGED
@@ -16,7 +16,6 @@
16
16
  - adds support for `execute_ai_request` in `PromptDeclarations` to handle API calls
17
17
  - adds support for `chat_completion_from_superclass` in `PromptDeclarations` to handle superclass calls
18
18
  - adds support for `model`, `temperature`, and `max_tokens` in `PromptDeclarations` to access prompt parameters
19
- - fixes function return values in `FunctionDispatch` to properly return results from tool calls (thanks @ttilberg)
20
19
  - Make automatic JSON parsing available to non-OpenAI providers that don't support the response_format parameter by scanning for json XML tags
21
20
 
22
21
  ## [0.6.0] - 2024-11-12
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- raix (0.7)
4
+ raix (0.7.1)
5
5
  activesupport (>= 6.0)
6
6
  open_router (~> 0.2)
7
7
  ruby-openai (~> 7.0)
data/README.md CHANGED
@@ -78,6 +78,26 @@ Note that there is a limit of four breakpoints, and the cache will expire within
78
78
  },
79
79
  ```
80
80
 
81
+ ### JSON Mode
82
+
83
+ Raix supports JSON mode for chat completions, which ensures that the AI model's response is valid JSON. This is particularly useful when you need structured data from the model.
84
+
85
+ When using JSON mode with OpenAI models, Raix will automatically set the `response_format` parameter on requests accordingly, and attempt to parse the entire response body as JSON.
86
+ When using JSON mode with other models (e.g. Anthropic) that don't support `response_format`, Raix will look for JSON content inside of <json> XML tags in the response, before
87
+ falling back to parsing the entire response body. Make sure you tell the AI to reply with JSON inside of XML tags.
88
+
89
+ ```ruby
90
+ >> my_class.chat_completion(json: true)
91
+ => { "key": "value" }
92
+ ```
93
+
94
+ When using JSON mode with non-OpenAI providers, Raix automatically sets the `require_parameters` flag to ensure proper JSON formatting. You can also combine JSON mode with other parameters:
95
+
96
+ ```ruby
97
+ >> my_class.chat_completion(json: true, openai: "gpt-4o")
98
+ => { "key": "value" }
99
+ ```
100
+
81
101
  ### Use of Tools/Functions
82
102
 
83
103
  The second (optional) module that you can add to your Ruby classes after `ChatCompletion` is `FunctionDispatch`. It lets you declare and implement functions to be called at the AI's discretion as part of a chat completion "loop" in a declarative, Rails-like "DSL" fashion.
@@ -109,44 +129,39 @@ Note that for security reasons, dispatching functions only works with functions
109
129
 
110
130
  #### Multiple Tool Calls
111
131
 
112
- Some AI models (like GPT-4) can make multiple tool calls in a single response. When this happens, Raix will automatically handle all the function calls sequentially and return an array of their results. Here's an example:
132
+ Some AI models (like GPT-4) can make multiple tool calls in a single response. When this happens, Raix will automatically handle all the function calls sequentially.
133
+ If you need to capture the arguments to the function calls, do so in the block passed to `function`. The response from `chat_completion` is always the final text
134
+ response from the assistant, and is not affected by function calls.
113
135
 
114
136
  ```ruby
115
137
  class MultipleToolExample
116
138
  include Raix::ChatCompletion
117
139
  include Raix::FunctionDispatch
118
140
 
141
+ attr_reader :invocations
142
+
119
143
  function :first_tool do |arguments|
144
+ @invocations << :first
120
145
  "Result from first tool"
121
146
  end
122
147
 
123
148
  function :second_tool do |arguments|
149
+ @invocations << :second
124
150
  "Result from second tool"
125
151
  end
152
+
153
+ def initialize
154
+ @invocations = []
155
+ end
126
156
  end
127
157
 
128
158
  example = MultipleToolExample.new
129
159
  example.transcript << { user: "Please use both tools" }
130
- results = example.chat_completion(openai: "gpt-4o")
131
- # => ["Result from first tool", "Result from second tool"]
132
- ```
133
-
134
- Note that as of version 0.6.1, function return values are properly returned from tool calls, whether in single or multiple tool call scenarios. This means you can use the return values from your functions in your application logic.
135
-
136
- ```ruby
137
- class DiceRoller
138
- include Raix::ChatCompletion
139
- include Raix::FunctionDispatch
140
-
141
- function :roll_dice, "Roll a die with specified number of faces", faces: { type: :integer } do |arguments|
142
- rand(1..arguments[:faces])
143
- end
144
- end
160
+ example.chat_completion(openai: "gpt-4o")
161
+ # => "I used both tools, as requested"
145
162
 
146
- roller = DiceRoller.new
147
- roller.transcript << { user: "Roll a d20 twice" }
148
- results = roller.chat_completion(openai: "gpt-4o")
149
- # => [15, 7] # Actual random dice roll results
163
+ example.invocations
164
+ # => [:first, :second]
150
165
  ```
151
166
 
152
167
  #### Manually Stopping a Loop
@@ -70,7 +70,7 @@ module Raix
70
70
  }
71
71
  ]
72
72
  }
73
- result = instance_exec(arguments, &block).tap do |content|
73
+ instance_exec(arguments, &block).tap do |content|
74
74
  transcript << {
75
75
  role: "tool",
76
76
  tool_call_id: id,
@@ -81,10 +81,6 @@ module Raix
81
81
  end
82
82
 
83
83
  chat_completion(**chat_completion_args) if loop
84
-
85
- # Return the result of the function call in case that's what the caller wants
86
- # https://github.com/OlympiaAI/raix/issues/16
87
- result
88
84
  end
89
85
  end
90
86
  end
data/lib/raix/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Raix
4
- VERSION = "0.7"
4
+ VERSION = "0.7.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: raix
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.7'
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Obie Fernandez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-04-13 00:00:00.000000000 Z
11
+ date: 2025-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport