diff --git a/app/models/event/activity_summary.rb b/app/models/event/activity_summary.rb index b275bc455..4a68f2dc9 100644 --- a/app/models/event/activity_summary.rb +++ b/app/models/event/activity_summary.rb @@ -8,11 +8,11 @@ class Event::ActivitySummary < ApplicationRecord key = key_for(events) # Outside to avoid holding the transaction during the LLM request - summary, cost_in_microcents = Event::Summarizer.new(events).summarize + summarizer = Event::Summarizer.new(events) create_or_find_by!(key: key) do |record| - record.content = summary - record.cost_in_microcents = cost_in_microcents + record.content = summarizer.summarized_content + record.cost_in_microcents = summarizer.cost.in_microcents end end diff --git a/app/models/event/summarizer.rb b/app/models/event/summarizer.rb index 6c9ff20d2..f4d8c77d4 100644 --- a/app/models/event/summarizer.rb +++ b/app/models/event/summarizer.rb @@ -27,9 +27,12 @@ class Event::Summarizer @llm_model = llm_model end - def summarize - response = chat.ask join_prompts("Summarize the following content:", summarizable_content) - [ response.content, Ai::UsageCost.from_llm_response(response).in_microcents ] + def summarized_content + llm_response.content + end + + def cost + Ai::UsageCost.from_llm_response(llm_response) end def summarizable_content @@ -39,6 +42,10 @@ class Event::Summarizer private attr_reader :prompt, :llm_model + def llm_response + @llm_response ||= chat.ask join_prompts("Summarize the following content:", summarizable_content) + end + def chat chat = RubyLLM.chat(model: llm_model) chat.with_instructions(join_prompts(prompt, domain_model_prompt, user_data_injection_prompt))