| 
									
										
										
										
											2023-06-27 12:26:33 -03:00
										 |  |  | # 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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-11 15:08:54 -03:00
										 |  |  |         def summarize(content, &on_partial_blk) | 
					
						
							| 
									
										
										
										
											2023-06-27 12:26:33 -03:00
										 |  |  |           opts = content.except(:contents) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-13 17:05:41 -03:00
										 |  |  |           chunks = split_into_chunks(content[:contents]) | 
					
						
							| 
									
										
										
										
											2023-06-27 12:26:33 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-13 17:05:41 -03:00
										 |  |  |           if chunks.length == 1
 | 
					
						
							| 
									
										
										
										
											2023-08-11 15:08:54 -03:00
										 |  |  |             { | 
					
						
							|  |  |  |               summary: | 
					
						
							|  |  |  |                 completion_model.summarize_single(chunks.first[:summary], opts, &on_partial_blk), | 
					
						
							|  |  |  |               chunks: [], | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-07-13 17:05:41 -03:00
										 |  |  |           else | 
					
						
							|  |  |  |             summaries = completion_model.summarize_in_chunks(chunks, opts) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-11 15:08:54 -03:00
										 |  |  |             { | 
					
						
							|  |  |  |               summary: completion_model.concatenate_summaries(summaries, &on_partial_blk), | 
					
						
							|  |  |  |               chunks: summaries, | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-07-13 17:05:41 -03:00
										 |  |  |           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 | 
					
						
							| 
									
										
										
										
											2023-06-27 12:26:33 -03:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |