Skip to content

Support caching of chat cogs #860

Description

@ric2b

While developing a new workflow it is common to have to re-run it multiple times for testing.

Chat cogs result in both higher costs and slower iteration.

I'm currently using this short monkeypatch which can serve as example, but a real implementation would need to support sessions (and maybe other parameters I haven't considered) and should ideally be configurable per chat cog, instead of globally:

require 'digest'

module CachedChat
  def execute(input)
    cache_dir = '/tmp/roast_chat_cache'

    chat_digest = Digest::SHA256.hexdigest(input.valid_prompt!)
    cache_key = "#{config.valid_provider!}:#{config.valid_model}:#{chat_digest}"
    cache_file = File.join(cache_dir, "chat_#{cache_key}.txt")

    if File.exist?(cache_file)
      cached_response = File.read(cache_file)
      empty_session = Roast::Cogs::Chat::Session.new([])
      return Roast::Cogs::Chat::Output.new(empty_session, cached_response)
    end

    result = super
    FileUtils.mkdir_p(cache_dir)
    File.write(cache_file, result.response)
    result
  end
end

Roast::Cogs::Chat.prepend(CachedChat)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions