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)
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: