cache the summary

This commit is contained in:
Pranav
2026-01-29 11:45:36 -08:00
parent 137f0e726b
commit f45e82016d
14 changed files with 245 additions and 108 deletions
+37 -8
View File
@@ -1,14 +1,10 @@
class Captain::SummaryService < Captain::BaseTaskService
pattr_initialize [:account!, :conversation_display_id!]
pattr_initialize [:account!, :conversation_display_id!, { force_regenerate: false }]
def perform
make_api_call(
model: GPT_MODEL,
messages: [
{ role: 'system', content: prompt_from_file('summary') },
{ role: 'user', content: conversation.to_llm_text(include_contact_details: false) }
]
)
return cached_response if use_cache?
generate_and_cache_summary
end
private
@@ -16,4 +12,37 @@ class Captain::SummaryService < Captain::BaseTaskService
def event_name
'summarize'
end
def use_cache?
return false if force_regenerate
return false if conversation.cached_summary.blank?
return false if conversation.cached_summary_at.blank?
conversation.cached_summary_at >= conversation.last_activity_at
end
def cached_response
{ message: conversation.cached_summary }
end
def generate_and_cache_summary
result = make_api_call(
model: GPT_MODEL,
messages: [
{ role: 'system', content: prompt_from_file('summary') },
{ role: 'user', content: conversation.to_llm_text(include_contact_details: false) }
]
)
cache_summary(result[:message]) if result[:message].present? && result[:error].blank?
result
end
def cache_summary(summary)
conversation.update(
cached_summary: summary,
cached_summary_at: Time.current
)
end
end