Sliding contexts for contextual prompts;

This commit is contained in:
extio-de
2024-11-01 15:08:26 +01:00
parent 7232387134
commit 65e50e1dcc
5 changed files with 40 additions and 40 deletions

View File

@@ -112,7 +112,7 @@ public class Rewrite implements Consumer<String[]> {
throw new RuntimeException("Cannot read file", e); 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) { for (final String split : splits) {
LOGGER.info("Split " + splits.indexOf(split) + "/" + splits.size()); LOGGER.info("Split " + splits.indexOf(split) + "/" + splits.size());

View File

@@ -67,18 +67,9 @@ public class Text2Jsonl implements Consumer<String[]> {
final List<String> jsonl = new ArrayList<>(); final List<String> jsonl = new ArrayList<>();
final ObjectMapper mapper = new ObjectMapper(); 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++) { for (int i = 0; i < paragraphs.size(); i++) {
String split = paragraphs.get(i); final 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;
}
}
try { try {
jsonl.add(mapper.writeValueAsString(new TextLine(split))); jsonl.add(mapper.writeValueAsString(new TextLine(split)));
} }

View File

@@ -48,6 +48,8 @@ public class Text2Jsonl2WithContextualPrompts implements Consumer<String[]> {
final List<Runnable> tasks = new ArrayList<>(); final List<Runnable> tasks = new ArrayList<>();
for (final ChunkCfg chunkCfg : chunkConfigurations) { for (final ChunkCfg chunkCfg : chunkConfigurations) {
final List<String> paragraphs = this.fileToParagraphs(p, chunkCfg.chunksNorm(), chunkCfg.chunksVar());
for (int i = 0; i < variations; i++) { for (int i = 0; i < variations; i++) {
final Path out = Utils.suffixFilename(Path.of(args[2]).resolve(p.getFileName()), final Path out = Utils.suffixFilename(Path.of(args[2]).resolve(p.getFileName()),
"contextualds", "contextualds",
@@ -61,7 +63,6 @@ public class Text2Jsonl2WithContextualPrompts implements Consumer<String[]> {
tasks.add(() -> { tasks.add(() -> {
try (var fos = Files.newOutputStream(out)) { try (var fos = Files.newOutputStream(out)) {
final List<String> paragraphs = this.fileToParagraphs(p, chunkCfg.chunksNorm(), chunkCfg.chunksVar());
for (final String paragraph : paragraphs) { for (final String paragraph : paragraphs) {
LOGGER.info("Paragraph " + paragraphs.indexOf(paragraph) + "/" + paragraphs.size()); LOGGER.info("Paragraph " + paragraphs.indexOf(paragraph) + "/" + paragraphs.size());
final var qaLine = this.paragraphToContextualPrompt(paragraph); 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) { 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; String text;
try { try {
@@ -90,7 +91,7 @@ public class Text2Jsonl2WithContextualPrompts implements Consumer<String[]> {
throw new RuntimeException("Cannot read file", e); 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) { 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:", "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, paragraph,
ModelCategory.COLD); 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; return qaLine;
} }

View File

@@ -73,7 +73,7 @@ public class Translate implements Consumer<String[]> {
throw new RuntimeException("Cannot read file", e); 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) { for (final String split : splits) {
LOGGER.info("Split " + splits.indexOf(split) + "/" + splits.size()); LOGGER.info("Split " + splits.indexOf(split) + "/" + splits.size());

View File

@@ -19,6 +19,8 @@ import org.slf4j.LoggerFactory;
public class Utils { public class Utils {
private static final List<String> PARAGRAPH_DELIMITERS = List.of("\n\n", "\n", ".", "!", "?");
private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class); private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class);
static String normalizeText(final String text) { static String normalizeText(final String text) {
@@ -59,7 +61,7 @@ public class Utils {
return result; 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_MIN = chunks_norm - chunks_var;
final int CHUNKS_MAX = 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<>(); final List<String> splits = new ArrayList<>();
int pos = 0; int pos = 0;
paragraphs: do { do {
int next = -1;
int delLength = 1;
if (text.length() - pos > CHUNKS_MAX) { if (text.length() - pos > CHUNKS_MAX) {
delimiters: for (final String delimiter : List.of("\n\n", "\n", ".")) { for (final String delimiter : PARAGRAPH_DELIMITERS) {
int next = text.indexOf(delimiter, pos); final int o = text.indexOf(delimiter, pos + CHUNKS_MIN);
while (next > -1) { if (o > -1 && o <= pos + CHUNKS_MAX) {
if (next < pos + CHUNKS_MIN) { next = o;
next = text.indexOf(delimiter, next + 1); 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(); String split = text.substring(pos, next).trim();
if (!split.endsWith(".")) { if (!split.endsWith(".") && !split.endsWith("!") && !split.endsWith("?")) {
split = split + "."; split = split + ".";
} }
splits.add(split); splits.add(split);
pos = next + delimiter.length();
continue paragraphs;
}
}
}
}
final int next = Math.min(pos + CHUNKS_MAX, text.length()); pos = next + delLength;
final String split = text.substring(pos, next).trim();
splits.add(split);
pos = next + 1;
} while (pos < text.length()); } while (pos < text.length());
LOGGER.info("Created {} splits", splits.size()); LOGGER.info("Created {} splits", splits.size());