raix 0.8.2 → 0.8.4
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 +3 -0
- data/Gemfile.lock +1 -1
- data/README.md +32 -0
- data/lib/raix/chat_completion.rb +4 -3
- data/lib/raix/function_dispatch.rb +33 -27
- data/lib/raix/version.rb +1 -1
- metadata +2 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 77197d6de1d77373260029d955fd0110990b7c562a279d887130ee4f95909377
         | 
| 4 | 
            +
              data.tar.gz: d811d21316bba7c92797dff98c044871ec4382998b77090f29348217156698df
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 9a513c9c82ef57ba4beb9681a02111a5207c9418b6f0b26015e9ad299d889b8a65644d594c11246ff7c70a5354419fc3932a8a776072757e615b8afbfc48ca98
         | 
| 7 | 
            +
              data.tar.gz: ab0743ed3d99efd20886053b2d7648fc6dec7b551d48c5f86fee9689441dfbda0280e0c693766fa8f03c944f7dbfc459c8301f2b3bb969de47e3f4bbfbf23d0b
         | 
    
        data/CHANGELOG.md
    CHANGED
    
    | @@ -1,3 +1,6 @@ | |
| 1 | 
            +
            ## [0.8.3] - 2025-04-30
         | 
| 2 | 
            +
            - Adds optional ActiveSupport Cache parameter to `dispatch_tool_function` for caching tool calls
         | 
| 3 | 
            +
             | 
| 1 4 | 
             
            ## [0.8.2] - 2025-04-29
         | 
| 2 5 | 
             
            - Extracts function call dispatch into a public `dispatch_tool_function` that can be overridden in subclasses
         | 
| 3 6 | 
             
            - Uses `public_send` instead of `send` for better security and explicitness
         | 
    
        data/Gemfile.lock
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | @@ -228,6 +228,38 @@ class CustomDispatchExample | |
| 228 228 | 
             
            end
         | 
| 229 229 | 
             
            ```
         | 
| 230 230 |  | 
| 231 | 
            +
            #### Function Call Caching
         | 
| 232 | 
            +
             | 
| 233 | 
            +
            You can use ActiveSupport's Cache to cache function call results, which can be particularly useful for expensive operations or external API calls that don't need to be repeated frequently.
         | 
| 234 | 
            +
             | 
| 235 | 
            +
            ```ruby
         | 
| 236 | 
            +
            class CachedFunctionExample
         | 
| 237 | 
            +
              include Raix::ChatCompletion
         | 
| 238 | 
            +
              include Raix::FunctionDispatch
         | 
| 239 | 
            +
             | 
| 240 | 
            +
              function :expensive_operation do |arguments|
         | 
| 241 | 
            +
                "Result of expensive operation with #{arguments}"
         | 
| 242 | 
            +
              end
         | 
| 243 | 
            +
             | 
| 244 | 
            +
              # Override dispatch_tool_function to enable caching for all functions
         | 
| 245 | 
            +
              def dispatch_tool_function(function_name, arguments)
         | 
| 246 | 
            +
                # Pass the cache to the superclass implementation
         | 
| 247 | 
            +
                super(function_name, arguments, cache: Rails.cache)
         | 
| 248 | 
            +
              end
         | 
| 249 | 
            +
            end
         | 
| 250 | 
            +
            ```
         | 
| 251 | 
            +
             | 
| 252 | 
            +
            The caching mechanism works by:
         | 
| 253 | 
            +
            1. Passing the cache object through `dispatch_tool_function` to the function implementation
         | 
| 254 | 
            +
            2. Using the function name and arguments as cache keys
         | 
| 255 | 
            +
            3. Automatically fetching from cache when available or executing the function when not cached
         | 
| 256 | 
            +
             | 
| 257 | 
            +
            This is particularly useful for:
         | 
| 258 | 
            +
            - Expensive database operations
         | 
| 259 | 
            +
            - External API calls
         | 
| 260 | 
            +
            - Resource-intensive computations
         | 
| 261 | 
            +
            - Functions with deterministic outputs for the same inputs
         | 
| 262 | 
            +
             | 
| 231 263 | 
             
            #### Manually Stopping a Loop
         | 
| 232 264 |  | 
| 233 265 | 
             
            To loop AI components that don't interact with end users, at least one function block should invoke `stop_looping!` whenever you're ready to stop processing.
         | 
    
        data/lib/raix/chat_completion.rb
    CHANGED
    
    | @@ -140,7 +140,7 @@ module Raix | |
| 140 140 | 
             
                      content = res.dig("choices", 0, "message", "content")
         | 
| 141 141 |  | 
| 142 142 | 
             
                      transcript << { assistant: content } if save_response
         | 
| 143 | 
            -
                      content = content. | 
| 143 | 
            +
                      content = content.strip
         | 
| 144 144 |  | 
| 145 145 | 
             
                      if json
         | 
| 146 146 | 
             
                        # Make automatic JSON parsing available to non-OpenAI providers that don't support the response_format parameter
         | 
| @@ -194,9 +194,10 @@ module Raix | |
| 194 194 | 
             
                #
         | 
| 195 195 | 
             
                # @param function_name [String] The name of the function to call
         | 
| 196 196 | 
             
                # @param arguments [Hash] The arguments to pass to the function
         | 
| 197 | 
            +
                # @param cache [ActiveSupport::Cache] Optional cache object
         | 
| 197 198 | 
             
                # @return [Object] The result of the function call
         | 
| 198 | 
            -
                def dispatch_tool_function(function_name, arguments)
         | 
| 199 | 
            -
                  public_send(function_name, arguments)
         | 
| 199 | 
            +
                def dispatch_tool_function(function_name, arguments, cache: nil)
         | 
| 200 | 
            +
                  public_send(function_name, arguments, cache)
         | 
| 200 201 | 
             
                end
         | 
| 201 202 |  | 
| 202 203 | 
             
                private
         | 
| @@ -54,35 +54,41 @@ module Raix | |
| 54 54 | 
             
                      end
         | 
| 55 55 | 
             
                    end
         | 
| 56 56 |  | 
| 57 | 
            -
                    define_method(name) do |arguments|
         | 
| 57 | 
            +
                    define_method(name) do |arguments, cache|
         | 
| 58 58 | 
             
                      id = SecureRandom.uuid[0, 23]
         | 
| 59 | 
            -
             | 
| 60 | 
            -
             | 
| 61 | 
            -
             | 
| 62 | 
            -
             | 
| 63 | 
            -
             | 
| 64 | 
            -
             | 
| 65 | 
            -
             | 
| 66 | 
            -
             | 
| 67 | 
            -
             | 
| 68 | 
            -
             | 
| 69 | 
            -
             | 
| 70 | 
            -
             | 
| 71 | 
            -
             | 
| 72 | 
            -
             | 
| 73 | 
            -
             | 
| 59 | 
            +
             | 
| 60 | 
            +
                      content = if cache.present?
         | 
| 61 | 
            +
                                  cache.fetch([name, arguments]) do
         | 
| 62 | 
            +
                                    instance_exec(arguments, &block)
         | 
| 63 | 
            +
                                  end
         | 
| 64 | 
            +
                                else
         | 
| 65 | 
            +
                                  instance_exec(arguments, &block)
         | 
| 66 | 
            +
                                end
         | 
| 67 | 
            +
             | 
| 68 | 
            +
                      # add in one operation to prevent race condition and potential wrong
         | 
| 69 | 
            +
                      # interleaving of tool calls in multi-threaded environments
         | 
| 70 | 
            +
                      transcript << [
         | 
| 71 | 
            +
                        {
         | 
| 72 | 
            +
                          role: "assistant",
         | 
| 73 | 
            +
                          content: nil,
         | 
| 74 | 
            +
                          tool_calls: [
         | 
| 75 | 
            +
                            {
         | 
| 76 | 
            +
                              id:,
         | 
| 77 | 
            +
                              type: "function",
         | 
| 78 | 
            +
                              function: {
         | 
| 79 | 
            +
                                name:,
         | 
| 80 | 
            +
                                arguments: arguments.to_json
         | 
| 74 81 | 
             
                              }
         | 
| 75 | 
            -
                             | 
| 76 | 
            -
                           | 
| 77 | 
            -
             | 
| 78 | 
            -
             | 
| 79 | 
            -
             | 
| 80 | 
            -
             | 
| 81 | 
            -
             | 
| 82 | 
            -
                           | 
| 83 | 
            -
                         | 
| 84 | 
            -
             | 
| 85 | 
            -
                      end
         | 
| 82 | 
            +
                            }
         | 
| 83 | 
            +
                          ]
         | 
| 84 | 
            +
                        },
         | 
| 85 | 
            +
                        {
         | 
| 86 | 
            +
                          role: "tool",
         | 
| 87 | 
            +
                          tool_call_id: id,
         | 
| 88 | 
            +
                          name:,
         | 
| 89 | 
            +
                          content: content.to_s
         | 
| 90 | 
            +
                        }
         | 
| 91 | 
            +
                      ]
         | 
| 86 92 |  | 
| 87 93 | 
             
                      chat_completion(**chat_completion_args) if loop
         | 
| 88 94 | 
             
                    end
         | 
    
        data/lib/raix/version.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,13 +1,13 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: raix
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.8. | 
| 4 | 
            +
              version: 0.8.4
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Obie Fernandez
         | 
| 8 8 | 
             
            bindir: exe
         | 
| 9 9 | 
             
            cert_chain: []
         | 
| 10 | 
            -
            date: 2025- | 
| 10 | 
            +
            date: 2025-05-07 00:00:00.000000000 Z
         | 
| 11 11 | 
             
            dependencies:
         | 
| 12 12 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 13 13 | 
             
              name: activesupport
         |