### Raix-Rails Development Setup and Commands Source: https://github.com/olympiaai/raix-rails/blob/main/README.md Commands for setting up the development environment, running tests, and interacting with the gem. ```shell bin/setup ``` ```shell rake spec ``` ```shell bin/console ``` ```shell bundle exec rake install ``` ```shell bundle exec rake release ``` -------------------------------- ### Install Raix-Rails Gem Source: https://github.com/olympiaai/raix-rails/blob/main/README.md Commands to install the raix-rails gem using Bundler or directly with gem install. ```shell $ bundle add raix-rails ``` ```shell $ gem install raix-rails ``` -------------------------------- ### Implement AI Function Dispatch with Raix in Ruby Source: https://github.com/olympiaai/raix-rails/blob/main/README.md Shows how to integrate `Raix::FunctionDispatch` alongside `Raix::ChatCompletion` to declare and execute functions based on AI discretion. The example defines a `check_weather` function and uses a declarative DSL. ```ruby class WhatIsTheWeather include Raix::ChatCompletion include Raix::FunctionDispatch function :check_weather, "Check the weather for a location", location: { type: "string" } do |arguments| "The weather in #{arguments[:location]} is hot and sunny" end end ``` -------------------------------- ### Test Raix Function Dispatch with RSpec Source: https://github.com/olympiaai/raix-rails/blob/main/README.md Provides an RSpec example for testing a Ruby class that utilizes `Raix::ChatCompletion` and `Raix::FunctionDispatch`. It verifies that the AI can call a declared function and loop to provide a text response. ```rspec RSpec.describe WhatIsTheWeather do subject { described_class.new } it "can call a function and loop to provide text response" do subject.transcript << { user: "What is the weather in Zipolite, Oaxaca?" } response = subject.chat_completion(openai: "gpt-4o", loop: true) expect(response).to include("hot and sunny") end end ``` -------------------------------- ### Raix AI Function Dispatch and Prompt DSL Source: https://github.com/olympiaai/raix-rails/blob/main/README.md API documentation for Raix modules related to AI interaction. Covers the `function` DSL for defining callable AI tools and the `prompt` DSL for declarative prompt chaining. Includes details on parameters, return values, and usage patterns. ```APIDOC Raix::FunctionDispatch Module: Provides a DSL for defining functions that can be called by AI models. function :name, "description", param1: { type: "type" }, param2: { type: "type", optional: true } do |arguments| # Implementation of the function # The return value is added to the transcript as a function result. end Example: function :fetch_url, "Fetches content from a given URL", url: { type: "string" } do |arguments| # ... fetch logic ... end Raix::ChatCompletion Module: Enables interaction with chat-based AI models. chat_completion(loop: true, **options) Initiates a chat completion. If `loop: true`, the AI can call functions multiple times until `stop_looping!` is invoked. `options` can include model names (e.g., `openai: "gpt-4o"`, `anthropic: "claude-3-haiku"`). stop_looping! Terminates an ongoing AI processing loop initiated with `chat_completion(loop: true)`. Raix::PromptDeclarations Module: Allows declarative definition of prompt sequences (prompt chains). prompt call: CallableClass Includes a callable object as a step in the prompt chain. prompt text: -> { "string" }, stream: -> { StreamObject.new(self) }, until: -> { condition } Defines a text-based prompt step. Can specify streaming behavior and termination conditions. Example Prompt Chain: prompt call: PreprocessorStep prompt text: -> { user_message.content } prompt call: PostprocessorStep Context Passing: Callable objects (e.g., `FetchUrlCheck`) can receive context from the calling object via their initializer. This allows for building complex, nested prompt structures. class MyPromptStep def initialize(context) @context = context # context can be the object calling the prompt chain end # ... end prompt call: MyPromptStep Function Call Handling: The `function` DSL automatically handles the creation of function calls and results within the AI's transcript. Methods like `conversation.function_call!` and `conversation.function_result!` are used internally. Example within a function: parent = conversation.function_call!("tool_name", arguments, parent: user_message) conversation.function_result!("tool_name", result, parent: parent) ``` -------------------------------- ### Declaring Prompt Chains with `Raix::PromptDeclarations` Source: https://github.com/olympiaai/raix-rails/blob/main/README.md Illustrates using `Raix::PromptDeclarations` to define a sequence of AI prompts, including callable objects and text-based prompts. It shows how to integrate `Raix::ChatCompletion` and `Raix::FunctionDispatch` for dynamic function calls within a prompt chain. ```ruby class PromptSubscriber include Raix::ChatCompletion include Raix::PromptDeclarations attr_accessor :conversation, :bot_message, :user_message # many other declarations ommitted... prompt call: FetchUrlCheck prompt call: MemoryScan prompt text: -> { user_message.content }, stream: -> { ReplyStream.new(self) }, until: -> { bot_message.complete? } def initialize(conversation) self.conversation = conversation end def message_created(user_message) self.user_message = user_message self.bot_message = conversation.bot_message!(responding_to: user_message) chat_completion(loop: true, openai: "gpt-4o") end ... end class FetchUrlCheck include ChatCompletion include FunctionDispatch REGEX = %r{\b(?:http(s)?:\/\/)?(?:www\.)?[a-zA-Z0-9-]+(\.[a-zA-Z]{2,})+(?:/[^\s]*)?\b} attr_accessor :context, :conversation delegate :user_message, to: :context delegate :content, to: :user_message def initialize(context) self.context = context self.conversation = context.conversation self.model = "anthropic/claude-3-haiku" end def call return unless content&.match?(REGEX) transcript << { system: "Call the `fetch` function if the user mentions a website, otherwise say nil" } transcript << { user: content } chat_completion # TODO: consider looping to fetch more than one URL per user message end function :fetch, "Gets the plain text contents of a web page", url: { type: "string" } do |arguments| Tools::FetchUrl.fetch(arguments[:url]).tap do |result| parent = conversation.function_call!("fetch_url", arguments, parent: user_message) conversation.function_result!("fetch_url", result, parent: parent) end end end ``` -------------------------------- ### Use Raix ChatCompletion Module in Ruby Source: https://github.com/olympiaai/raix-rails/blob/main/README.md Demonstrates how to include the `Raix::ChatCompletion` module in a Ruby class to enable AI chat functionalities. It shows adding messages to the transcript using both abbreviated and standard OpenAI message formats. ```ruby class MeaningOfLife include Raix::ChatCompletion end >> ai = MeaningOfLife.new >> ai.transcript << { user: "What is the meaning of life?" } >> ai.chat_completion => "The question of the meaning of life is one of the most profound and enduring inquiries in philosophy, religion, and science. Different perspectives offer various answers..." # Abbreviated transcript format: transcript << { user: "What is the meaning of life?" } # Standard OpenAI message hash format: transcript << { role: "user", content: "What is the meaning of life?" } ``` -------------------------------- ### Manually Stopping an AI Loop with `stop_looping!` Source: https://github.com/olympiaai/raix-rails/blob/main/README.md Demonstrates how to control AI processing loops in a Ruby class by including `Raix::ChatCompletion` and `Raix::FunctionDispatch`. The `finished_processing` method illustrates calling `stop_looping!` to terminate the loop after completing tasks. ```ruby class OrderProcessor include Raix::ChatCompletion include Raix::FunctionDispatch SYSTEM_DIRECTIVE = "You are an order processor, tasked with order validation, inventory check, payment processing, and shipping." attr_accessor :order def initialize(order) self.order = order transcript << { system: SYSTEM_DIRECTIVE } transcript << { user: order.to_json } end def perform # will continue looping until `stop_looping!` is called chat_completion(loop: true) end # implementation of functions that can be called by the AI # entirely at its discretion, depending on the needs of the order. # The return value of each `perform` method will be added to the # transcript of the conversation as a function result. function :validate_order do OrderValidationWorker.perform(@order) end function :check_inventory do InventoryCheckWorker.perform(@order) end function :process_payment do PaymentProcessingWorker.perform(@order) end function :schedule_shipping do ShippingSchedulerWorker.perform(@order) end function :send_confirmation do OrderConfirmationWorker.perform(@order) end function :finished_processing do order.update!(transcript: transcript, processed_at: Time.current) stop_looping! end end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.