mirror of
				https://github.com/discourse/discourse-ai.git
				synced 2025-10-31 06:28:48 +00:00 
			
		
		
		
	Single and multi-chunk summaries end using different prompts for the last summary. This change detects when the summarized content fits in a single chunk and uses a slightly different prompt, which leads to more consistent summary formats. This PR also moves the chunk-splitting step to the `FoldContent` strategy as preparation for implementing streamed summaries.
		
			
				
	
	
		
			65 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			65 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
| # frozen_string_literal: true
 | |
| 
 | |
| module DiscourseAi
 | |
|   module Summarization
 | |
|     module Strategies
 | |
|       class FoldContent < ::Summarization::Base
 | |
|         def initialize(completion_model)
 | |
|           @completion_model = completion_model
 | |
|         end
 | |
| 
 | |
|         attr_reader :completion_model
 | |
| 
 | |
|         delegate :correctly_configured?,
 | |
|                  :display_name,
 | |
|                  :configuration_hint,
 | |
|                  :model,
 | |
|                  to: :completion_model
 | |
| 
 | |
|         def summarize(content)
 | |
|           opts = content.except(:contents)
 | |
| 
 | |
|           chunks = split_into_chunks(content[:contents])
 | |
| 
 | |
|           if chunks.length == 1
 | |
|             { summary: completion_model.summarize_single(chunks.first[:summary], opts), chunks: [] }
 | |
|           else
 | |
|             summaries = completion_model.summarize_in_chunks(chunks, opts)
 | |
| 
 | |
|             { summary: completion_model.concatenate_summaries(summaries), chunks: summaries }
 | |
|           end
 | |
|         end
 | |
| 
 | |
|         private
 | |
| 
 | |
|         def split_into_chunks(contents)
 | |
|           section = { ids: [], summary: "" }
 | |
| 
 | |
|           chunks =
 | |
|             contents.reduce([]) do |sections, item|
 | |
|               new_content = completion_model.format_content_item(item)
 | |
| 
 | |
|               if completion_model.can_expand_tokens?(
 | |
|                    section[:summary],
 | |
|                    new_content,
 | |
|                    completion_model.available_tokens,
 | |
|                  )
 | |
|                 section[:summary] += new_content
 | |
|                 section[:ids] << item[:id]
 | |
|               else
 | |
|                 sections << section
 | |
|                 section = { ids: [item[:id]], summary: new_content }
 | |
|               end
 | |
| 
 | |
|               sections
 | |
|             end
 | |
| 
 | |
|           chunks << section if section[:summary].present?
 | |
| 
 | |
|           chunks
 | |
|         end
 | |
|       end
 | |
|     end
 | |
|   end
 | |
| end
 |