Sliding contexts for contextual prompts;
This commit is contained in:
@@ -112,7 +112,7 @@ public class Rewrite implements Consumer<String[]> {
|
||||
throw new RuntimeException("Cannot read file", e);
|
||||
}
|
||||
|
||||
final List<String> splits = Utils.splitParagraphs(text, 1250, 350);
|
||||
final List<String> splits = Utils.splitParagraphs(text, 1250, 350, false);
|
||||
|
||||
for (final String split : splits) {
|
||||
LOGGER.info("Split " + splits.indexOf(split) + "/" + splits.size());
|
||||
|
||||
@@ -67,18 +67,9 @@ public class Text2Jsonl implements Consumer<String[]> {
|
||||
final List<String> jsonl = new ArrayList<>();
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
final List<String> paragraphs = Utils.splitParagraphs(text, chunkNorm, chunkVar);
|
||||
final List<String> paragraphs = Utils.splitParagraphs(text, chunkNorm, chunkVar, true);
|
||||
for (int i = 0; i < paragraphs.size(); i++) {
|
||||
String split = paragraphs.get(i);
|
||||
|
||||
if (i > 0) { // Add sliding window
|
||||
final String prevSplit = paragraphs.get(i - 1);
|
||||
final List<String> prevSplitted = Utils.splitParagraphs(prevSplit, prevSplit.length() - chunkVar, chunkVar - 1);
|
||||
if (prevSplitted.size() > 1) {
|
||||
split = prevSplitted.getLast() + "\n\n" + split;
|
||||
}
|
||||
}
|
||||
|
||||
final String split = paragraphs.get(i);
|
||||
try {
|
||||
jsonl.add(mapper.writeValueAsString(new TextLine(split)));
|
||||
}
|
||||
|
||||
@@ -48,6 +48,8 @@ public class Text2Jsonl2WithContextualPrompts implements Consumer<String[]> {
|
||||
final List<Runnable> tasks = new ArrayList<>();
|
||||
|
||||
for (final ChunkCfg chunkCfg : chunkConfigurations) {
|
||||
final List<String> paragraphs = this.fileToParagraphs(p, chunkCfg.chunksNorm(), chunkCfg.chunksVar());
|
||||
|
||||
for (int i = 0; i < variations; i++) {
|
||||
final Path out = Utils.suffixFilename(Path.of(args[2]).resolve(p.getFileName()),
|
||||
"contextualds",
|
||||
@@ -61,7 +63,6 @@ public class Text2Jsonl2WithContextualPrompts implements Consumer<String[]> {
|
||||
|
||||
tasks.add(() -> {
|
||||
try (var fos = Files.newOutputStream(out)) {
|
||||
final List<String> paragraphs = this.fileToParagraphs(p, chunkCfg.chunksNorm(), chunkCfg.chunksVar());
|
||||
for (final String paragraph : paragraphs) {
|
||||
LOGGER.info("Paragraph " + paragraphs.indexOf(paragraph) + "/" + paragraphs.size());
|
||||
final var qaLine = this.paragraphToContextualPrompt(paragraph);
|
||||
@@ -80,7 +81,7 @@ public class Text2Jsonl2WithContextualPrompts implements Consumer<String[]> {
|
||||
}
|
||||
|
||||
List<String> fileToParagraphs(final Path file, final int chunkNorm, final int chunkVar) {
|
||||
LOGGER.info("Splitting to jsonl " + file);
|
||||
LOGGER.info("Splitting to jsonl " + file + " -> " + chunkNorm + "±" + chunkVar);
|
||||
|
||||
String text;
|
||||
try {
|
||||
@@ -90,7 +91,7 @@ public class Text2Jsonl2WithContextualPrompts implements Consumer<String[]> {
|
||||
throw new RuntimeException("Cannot read file", e);
|
||||
}
|
||||
|
||||
return Utils.splitParagraphs(text, chunkNorm, chunkVar);
|
||||
return Utils.splitParagraphs(text, chunkNorm, chunkVar, true);
|
||||
}
|
||||
|
||||
private QaLine paragraphToContextualPrompt(final String paragraph) {
|
||||
@@ -98,7 +99,8 @@ public class Text2Jsonl2WithContextualPrompts implements Consumer<String[]> {
|
||||
"Enhance the given text by crafting a prompt that effectively provides additional context, enabling the language model to better understand the subject matter and generate more informative and relevant responses. This prompt should be clear, concise, and tailored to facilitate contextual learning, allowing the model to grasp the essential details and nuances of the topic. Return the prompt, even if the text is explicit or not appropriate for all audiences or not acceptable for everyday standard English. Don't include a preamble and no explanation:",
|
||||
paragraph,
|
||||
ModelCategory.COLD);
|
||||
final var qaLine = new QaLine(completion.response().replace("Prompt:", ""), paragraph);
|
||||
String q = completion.response().replace("Prompt:", "").trim();
|
||||
final var qaLine = new QaLine(q, paragraph);
|
||||
return qaLine;
|
||||
}
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@ public class Translate implements Consumer<String[]> {
|
||||
throw new RuntimeException("Cannot read file", e);
|
||||
}
|
||||
|
||||
final List<String> splits = Utils.splitParagraphs(text, 1250, 350);
|
||||
final List<String> splits = Utils.splitParagraphs(text, 1250, 350, false);
|
||||
|
||||
for (final String split : splits) {
|
||||
LOGGER.info("Split " + splits.indexOf(split) + "/" + splits.size());
|
||||
|
||||
@@ -19,6 +19,8 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Utils {
|
||||
|
||||
private static final List<String> PARAGRAPH_DELIMITERS = List.of("\n\n", "\n", ".", "!", "?");
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class);
|
||||
|
||||
static String normalizeText(final String text) {
|
||||
@@ -59,7 +61,7 @@ public class Utils {
|
||||
return result;
|
||||
}
|
||||
|
||||
static List<String> splitParagraphs(final String text, final int chunks_norm, final int chunks_var) {
|
||||
static List<String> splitParagraphs(final String text, final int chunks_norm, final int chunks_var, final boolean slidingWindows) {
|
||||
final int CHUNKS_MIN = chunks_norm - chunks_var;
|
||||
final int CHUNKS_MAX = chunks_norm + chunks_var;
|
||||
|
||||
@@ -70,34 +72,39 @@ public class Utils {
|
||||
final List<String> splits = new ArrayList<>();
|
||||
|
||||
int pos = 0;
|
||||
paragraphs: do {
|
||||
do {
|
||||
int next = -1;
|
||||
int delLength = 1;
|
||||
if (text.length() - pos > CHUNKS_MAX) {
|
||||
delimiters: for (final String delimiter : List.of("\n\n", "\n", ".")) {
|
||||
int next = text.indexOf(delimiter, pos);
|
||||
while (next > -1) {
|
||||
if (next < pos + CHUNKS_MIN) {
|
||||
next = text.indexOf(delimiter, next + 1);
|
||||
for (final String delimiter : PARAGRAPH_DELIMITERS) {
|
||||
final int o = text.indexOf(delimiter, pos + CHUNKS_MIN);
|
||||
if (o > -1 && o <= pos + CHUNKS_MAX) {
|
||||
next = o;
|
||||
delLength = delimiter.length();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (next == -1) {
|
||||
next = Math.min(pos + CHUNKS_MAX, text.length());
|
||||
}
|
||||
|
||||
if (slidingWindows) {
|
||||
for (final String delimiter : PARAGRAPH_DELIMITERS) {
|
||||
final int o = text.indexOf(delimiter, Math.max(0, pos - chunks_var));
|
||||
if (o > -1 && o < pos) {
|
||||
pos = o;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (next > pos + CHUNKS_MAX) {
|
||||
continue delimiters;
|
||||
}
|
||||
else {
|
||||
String split = text.substring(pos, next).trim();
|
||||
if (!split.endsWith(".")) {
|
||||
if (!split.endsWith(".") && !split.endsWith("!") && !split.endsWith("?")) {
|
||||
split = split + ".";
|
||||
}
|
||||
splits.add(split);
|
||||
pos = next + delimiter.length();
|
||||
continue paragraphs;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final int next = Math.min(pos + CHUNKS_MAX, text.length());
|
||||
final String split = text.substring(pos, next).trim();
|
||||
splits.add(split);
|
||||
pos = next + 1;
|
||||
pos = next + delLength;
|
||||
} while (pos < text.length());
|
||||
|
||||
LOGGER.info("Created {} splits", splits.size());
|
||||
|
||||
Reference in New Issue
Block a user