-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageIndex.java
More file actions
214 lines (178 loc) · 7.51 KB
/
PageIndex.java
File metadata and controls
214 lines (178 loc) · 7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package com.pageindex;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pageindex.llm.LLMClient;
import com.pageindex.llm.LLMClientFactory;
import com.pageindex.model.Config;
import com.pageindex.model.PageContent;
import com.pageindex.model.TreeNode;
import com.pageindex.parser.PDFParser;
import com.pageindex.tree.TreeBuilder;
import com.pageindex.tree.TreeParser;
import com.pageindex.utils.ConfigLoader;
import com.pageindex.utils.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
/**
* PageIndex 主类,提供文档索引构建功能
*/
public class PageIndex {
private static final Logger logger = LoggerFactory.getLogger(PageIndex.class);
private static final ObjectMapper mapper = new ObjectMapper();
private final Config config;
private final LLMClient llmClient;
private final PDFParser pdfParser;
private final TreeParser treeParser;
public PageIndex(Config config) {
this.config = config;
this.llmClient = LLMClientFactory.createClient(config);
this.pdfParser = new PDFParser(config.getModel());
this.treeParser = new TreeParser(config, llmClient);
}
public PageIndex() {
this(ConfigLoader.loadDefaultConfig());
}
/**
* 从 PDF 文件构建索引
*/
public CompletableFuture<IndexResult> buildIndex(String pdfPath) {
return CompletableFuture.supplyAsync(() -> {
try {
logger.info("Building index for PDF: {}", pdfPath);
// 解析 PDF
List<PageContent> pages = pdfParser.parsePDF(pdfPath);
logger.info("Parsed {} pages, total tokens: {}",
pages.size(),
pages.stream().mapToInt(PageContent::getTokenCount).sum());
// 构建树结构
List<TreeNode> tree = treeParser.parseTreeFromPages(pages).join();
// 添加节点文本(如果需要)
if ("yes".equals(config.getIfAddNodeText())) {
addNodeText(tree, pages);
}
// 生成摘要(如果需要)
if ("yes".equals(config.getIfAddNodeSummary())) {
if (!"yes".equals(config.getIfAddNodeText())) {
addNodeText(tree, pages);
}
generateSummaries(tree).join();
if (!"yes".equals(config.getIfAddNodeText())) {
removeNodeText(tree);
}
}
// 构建结果
IndexResult result = new IndexResult();
result.setDocName(new File(pdfPath).getName());
result.setStructure(tree);
// 生成文档描述(如果需要)
if ("yes".equals(config.getIfAddDocDescription())) {
String description = generateDocDescription(tree);
result.setDocDescription(description);
}
logger.info("Successfully built index for: {}", pdfPath);
return result;
} catch (Exception e) {
logger.error("Failed to build index for: " + pdfPath, e);
throw new RuntimeException(e);
}
});
}
/**
* 为树节点添加文本内容
*/
private void addNodeText(List<TreeNode> nodes, List<PageContent> pages) {
for (TreeNode node : nodes) {
if (node.getStartIndex() != null && node.getEndIndex() != null) {
StringBuilder text = new StringBuilder();
for (int i = node.getStartIndex() - 1; i < node.getEndIndex() && i < pages.size(); i++) {
text.append(pages.get(i).getText()).append("\n");
}
node.setText(text.toString());
}
if (node.hasChildren()) {
addNodeText(node.getNodes(), pages);
}
}
}
/**
* 移除树节点的文本内容
*/
private void removeNodeText(List<TreeNode> nodes) {
for (TreeNode node : nodes) {
node.setText(null);
if (node.hasChildren()) {
removeNodeText(node.getNodes());
}
}
}
/**
* 为树节点生成摘要
*/
private CompletableFuture<Void> generateSummaries(List<TreeNode> nodes) {
List<CompletableFuture<Void>> futures = new java.util.ArrayList<>();
for (TreeNode node : nodes) {
if (node.getText() != null && !node.getText().isEmpty()) {
CompletableFuture<Void> future = llmClient.callAsync(config.getModel(),
buildSummaryPrompt(node.getText()))
.thenAccept(summary -> node.setSummary(summary));
futures.add(future);
}
if (node.hasChildren()) {
futures.add(generateSummaries(node.getNodes()));
}
}
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
}
private String buildSummaryPrompt(String text) {
return String.format("""
You are given a part of a document, your task is to generate a description of the partial document
about what are main points covered in the partial document.
Partial Document Text: %s
Directly return the description, do not include any other text.
""", text);
}
/**
* 生成文档描述
*/
private String generateDocDescription(List<TreeNode> tree) {
String structureJson = JsonUtils.toJsonString(tree);
String prompt = String.format("""
You are an expert in generating descriptions for a document.
You are given a structure of a document. Your task is to generate a one-sentence description for the document,
which makes it easy to distinguish the document from other documents.
Document Structure: %s
Directly return the description, do not include any other text.
""", structureJson);
return llmClient.call(config.getModel(), prompt, null);
}
/**
* 索引结果
*/
public static class IndexResult {
private String docName;
private String docDescription;
private List<TreeNode> structure;
public String getDocName() {
return docName;
}
public void setDocName(String docName) {
this.docName = docName;
}
public String getDocDescription() {
return docDescription;
}
public void setDocDescription(String docDescription) {
this.docDescription = docDescription;
}
public List<TreeNode> getStructure() {
return structure;
}
public void setStructure(List<TreeNode> structure) {
this.structure = structure;
}
}
}