Skip to content

Compatibility with RubyLLM activerecord #2

@jordanyeo

Description

@jordanyeo

First off, love the library, thanks so much- SO clean!

I'm building a rails engine and use RubyLLM's activerecord integration so I create accompanying records. In order to get it to work, I had to set this in an initializer:

# Ensure AR chat models (acts_as_chat) can call `with_template`
      if defined?(RubyLLM::ActiveRecord::ActsAs::ChatMethods)
        mod = RubyLLM::ActiveRecord::ActsAs::ChatMethods
        unless mod.instance_methods(false).include?(:with_template)
          mod.module_eval do
            # Return the underlying RubyLLM::Chat so chaining goes to it
            def with_template(template_name, context = {})
              to_llm.with_template(template_name, context)
            end
          end
        end
      end

and this on my Chat model thats using acts_as_chat:

    def with_template(template_name, context = {})
      to_llm.with_template(template_name, context)
    end

FWIW, gpt5 says:

 Clean, upstream fix in the gem:
      - Add an AR integration module and include it when acts_as_chat is present:
      - lib/ruby_llm/template/active_record.rb
        module RubyLLM
          module Template
            module ActiveRecordChatExtension
              def with_template(template_name, context = {})
                to_llm.with_template(template_name, context)
                self
              end
            end
          end
        end
      - In the gem’s Railtie or main init:
        initializer "ruby_llm_template.active_record" do
          if defined?(RubyLLM::ActiveRecord::ActsAs::ChatMethods)
            RubyLLM::ActiveRecord::ActsAs::ChatMethods.include(
              RubyLLM::Template::ActiveRecordChatExtension
            )
          end
        end

  #This makes .with_template available on any AR chat model automatically, matching the Rails integration style of ruby_llm.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions