-
Notifications
You must be signed in to change notification settings - Fork 0
/
Embedding.java
169 lines (145 loc) · 7.19 KB
/
Embedding.java
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
package RAGCON;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.Scanner;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.embedding.AllMiniLmL6V2EmbeddingModel;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.neo4j.Neo4jEmbeddingStore;
public class Embedding {
public static void embedString(String text, String dbUrl, String dbUser, String dbPass) throws IOException {
if (text != null && !text.trim().isEmpty()) {
EmbeddingStore<TextSegment> embeddingStore = connectNeo4j(dbUrl, dbUser, dbPass);
AllMiniLmL6V2EmbeddingModel embeddingModel = new AllMiniLmL6V2EmbeddingModel();
String[] sentences = text.split("(?<=[.?!])\\s+");
for (String sentence : sentences) {
String trimmedSentence = sentence.trim();
if (!trimmedSentence.isEmpty()) {
try {
TextSegment segment = TextSegment.from(trimmedSentence);
dev.langchain4j.data.embedding.Embedding embedding = embeddingModel.embed(segment).content();
embeddingStore.add(embedding, segment);
System.out.println("Embedded sentence: " + trimmedSentence);
} catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage() + " for sentence: " + trimmedSentence);
}
}
}
} else {
System.out.println("Error: Provided text is null or empty.");
}
}
public static void embedUrl(String urlString, String dbUrl, String dbUser, String dbPass) {
try {
System.out.println("Fetching content from URL: " + urlString);
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
int responseCode = conn.getResponseCode();
if (responseCode != 200) {
System.out.println("Error: Failed to fetch content from URL, response code: " + responseCode);
return;
}
Scanner scanner = new Scanner(conn.getInputStream());
scanner.useDelimiter("\\A");
String content = scanner.hasNext() ? scanner.next() : "";
scanner.close();
// Using JSoup to parse the HTML and extract <p> elements
Document doc = Jsoup.parse(content);
Elements paragraphs = doc.select("p");
StringBuilder textBuilder = new StringBuilder();
for (Element paragraph : paragraphs) {
textBuilder.append(paragraph.text()).append("\n\n");
}
String extractedText = textBuilder.toString().trim();
if (!extractedText.isEmpty()) {
embedString(extractedText, dbUrl, dbUser, dbPass);
} else {
System.out.println("No valid text found in <p> elements.");
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error: Failed to fetch content from URL");
}
}
public static void embedFiles(String path, String dbUrl, String dbUser, String dbPass) {
File fileOrFolder = new File(path);
if (!fileOrFolder.exists()) {
System.out.println("Error: File or folder not found");
return;
}
EmbeddingStore<TextSegment> embeddingStore = connectNeo4j(dbUrl, dbUser, dbPass);
if (fileOrFolder.isDirectory()) {
File[] files = fileOrFolder.listFiles();
if (files != null) {
Arrays.stream(files)
.filter(file -> file.getName().endsWith(".pdf"))
.forEach(file -> embedFile(file, embeddingStore));
} else {
System.out.println("Error: No files found in directory");
}
} else {
System.out.println("Error: Not a directory");
}
}
private static EmbeddingStore<TextSegment> connectNeo4j(String dbUrl, String dbUser, String dbPass) {
try {
return Neo4jEmbeddingStore.builder()
.withBasicAuth(dbUrl, dbUser, dbPass)
.dimension(384)
.build();
} catch (Exception e) {
System.out.println("Error connecting to Neo4j: " + e.getMessage());
return null;
}
}
private static void embedFile(File file, EmbeddingStore<TextSegment> embeddingStore) {
if (file.getName().endsWith(".pdf")) {
System.out.println("Processing file: " + file.getName());
try (PDDocument document = PDDocument.load(file)) {
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(document);
// Log the extracted text
System.out.println("Extracted text: ");
System.out.println(text);
if (text != null && !text.trim().isEmpty()) {
AllMiniLmL6V2EmbeddingModel embeddingModel = new AllMiniLmL6V2EmbeddingModel();
String[] paragraphs = text.split("\\n\\n+");
for (String paragraph : paragraphs) {
String[] sentences = paragraph.split("(?<=[.?!])\\s+");
for (String sentence : sentences) {
String trimmedSentence = sentence.trim();
if (!trimmedSentence.isEmpty()) {
try {
TextSegment segment = TextSegment.from(trimmedSentence);
dev.langchain4j.data.embedding.Embedding embedding = embeddingModel.embed(segment).content();
embeddingStore.add(embedding, segment);
} catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage() + " for sentence: " + trimmedSentence);
}
} else {
System.out.println("Skipping empty or blank sentence.");
}
}
}
} else {
System.out.println("No valid text found in file: " + file.getName());
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("Failed to process file: " + file.getName());
}
} else {
System.out.println("Error: Not a PDF file: " + file.getName());
}
}
}