Merge pull request #985 from basecamp/refactor-summarizer

Refactor: Separate method with two outputs into two
This commit is contained in:
Jorge Manrubia
2025-08-27 14:33:24 +02:00
committed by GitHub
2 changed files with 13 additions and 6 deletions
+3 -3
View File
@@ -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
+10 -3
View File
@@ -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))