22
22
import java .util .StringTokenizer ;
23
23
import java .util .concurrent .atomic .AtomicInteger ;
24
24
import java .util .stream .Collectors ;
25
+ import java .util .stream .Stream ;
25
26
26
27
import org .slf4j .Logger ;
27
28
import org .slf4j .LoggerFactory ;
@@ -70,6 +71,12 @@ public class DocumentService {
70
71
If unsure, simply state that you don't know.\n \n " + " {prompt} \n \n " + "DOCUMENTS:\n " + "{documents}
71
72
""" ;
72
73
74
+ private final String bookPrompt = """
75
+ You're a language expert for creating a summary of a text.\n
76
+ Create a concise summary of the text provided. Concentrate on the most important points in the text.\n
77
+ TEXT: {text}
78
+ """ ;
79
+
73
80
@ Value ("${embedding-token-limit:1000}" )
74
81
private Integer embeddingTokenLimit ;
75
82
@ Value ("${document-token-limit:1000}" )
@@ -90,23 +97,38 @@ public void init() {
90
97
LOGGER .info ("Profile: {}" , this .activeProfile );
91
98
}
92
99
93
- public String summarizeBook (Book book , List <ChapterPages > chapters ) {
100
+ public String summarizeBook (Book book , List <ChapterPages > chapters ) {
94
101
var tikaDocuments = new TikaDocumentReader (new ByteArrayResource (book .getBookFile ())).get ();
95
102
var atomicInt = new AtomicInteger (0 );
96
103
var myChapters = chapters .stream ()
97
- .flatMap (myChapter -> tikaDocuments .stream ().skip (myChapter .startPage ()).limit (myChapter .endPage ())).map (myDoc -> {
98
- var result = new Chapter ();
99
- result .setBook (book );
100
- result .setChapterText (myDoc .getContent ());
101
- result .setTitle ("Chapter " +atomicInt .addAndGet (1 ));
102
- return result ;
103
- }) .toList ();
104
+ .map (myChapter -> tikaDocuments .stream ().skip (myChapter .startPage ()).limit (myChapter .endPage ()).toList ())
105
+ .flatMap (myDocuments -> createChapter (book , atomicInt , myDocuments ))
106
+ .toList ();
104
107
book .getChapters ().addAll (myChapters );
105
- LOGGER .info (myChapters .getLast ().getChapterText ());
106
-
107
- //book.setSummary("");
108
- this .bookRepository .save (book );
109
- return "" ;
108
+ // LOGGER.info(myChapters.getLast().getChapterText());
109
+ myChapters = myChapters .stream ().map (myChapter -> {
110
+ Message systemMessage = new SystemPromptTemplate (this .bookPrompt )
111
+ .createMessage (Map .of ("text" , myChapter .getChapterText ()));
112
+ myChapter .setSummary (systemMessage .getContent ());
113
+ return myChapter ;
114
+ }).toList ();
115
+ // LOGGER.info(myChapters.getLast().getSummary());
116
+ var summaries = myChapters .stream ().map (myChapter -> myChapter .getChapterText ())
117
+ .reduce ((acc , myChapter ) -> acc + "\n " + myChapter );
118
+ book .setSummary (
119
+ new SystemPromptTemplate (this .bookPrompt ).createMessage (Map .of ("text" , summaries )).getContent ());
120
+ var myBook = this .bookRepository .save (book );
121
+ // LOGGER.info(myBook.getSummary());
122
+ return myBook .getSummary ();
123
+ }
124
+
125
+ private Stream <? extends Chapter > createChapter (Book book , AtomicInteger atomicInt ,
126
+ List <org .springframework .ai .document .Document > myDocuments ) {
127
+ var result = new Chapter ();
128
+ result .setTitle ("Chapter " + atomicInt .addAndGet (1 ));
129
+ result .setBook (book );
130
+ result .setChapterText (myDocuments .stream ().map (myDoc -> myDoc .getFormattedContent ()).collect (Collectors .joining ("\n " )));
131
+ return Stream .of (result );
110
132
}
111
133
112
134
public Long storeDocument (Document document ) {
0 commit comments