Separate method with two outputs into two
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user