Rewrite: Use different prompts for first and next continuations;

This commit is contained in:
extio-de
2024-12-27 12:25:59 +01:00
parent 674c47dfe1
commit 436e996e5d

View File

@@ -6,7 +6,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Supplier; import java.util.function.Function;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -30,9 +30,13 @@ public class Rewrite implements DatasetTool {
private static final String SYSTEM_PROMPT = "You are a helpful assistant with great authoring skills."; private static final String SYSTEM_PROMPT = "You are a helpful assistant with great authoring skills.";
private static final String IMPROVEMENT_PROMPT = "Improve the following text without changing the existing sentences, improve the text in a nuanced and understated way. Return the improved text, 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:"; private static final String IMPROVEMENT_PROMPT = "Improve %s without changing the existing sentences, improve the text in a nuanced and understated way. Return the improved text, 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:";
private static final String ENHANCE_PROMPT = "Improve the following text. %s without changing the existing sentences, focusing on enriching the description of the setting in a nuanced and understated way. Return the improved text, 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:"; private static final String ENHANCE_PROMPT = "Improve %s. %s without changing the existing sentences, focusing on enriching the description of the setting in a nuanced and understated way. Return the improved text, 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:";
private static final String FIRST_PROMPT = "the following text";
private static final String NEXT_PROMPT = "the next paragraph";
private static final List<String> ENHANCEMENTS = List.of( private static final List<String> ENHANCEMENTS = List.of(
"Infuse this paragraph with subtle sensory details, focusing on the texture of objects and the atmosphere of the setting", "Infuse this paragraph with subtle sensory details, focusing on the texture of objects and the atmosphere of the setting",
@@ -66,24 +70,24 @@ public class Rewrite implements DatasetTool {
final List<Runnable> tasks = new ArrayList<>(); final List<Runnable> tasks = new ArrayList<>();
for (int i = 0; i < Integer.parseInt(properties.getProperty("rewrite.improve")); i++) { for (int i = 0; i < Integer.parseInt(properties.getProperty("rewrite.improve")); i++) {
final int fi = i; final int fi = i;
tasks.add(() -> this.rewriteFile(properties, packet, fi, "impr", () -> IMPROVEMENT_PROMPT)); tasks.add(() -> this.rewriteFile(properties, packet, fi, "impr", first -> String.format(IMPROVEMENT_PROMPT, first ? FIRST_PROMPT : NEXT_PROMPT)));
} }
for (int i = 0; i < Integer.parseInt(properties.getProperty("rewrite.enhance")); i++) { for (int i = 0; i < Integer.parseInt(properties.getProperty("rewrite.enhance")); i++) {
final int fi = i; final int fi = i;
tasks.add(() -> this.rewriteFile(properties, packet, fi, "enh", () -> String.format(ENHANCE_PROMPT, ENHANCEMENTS.get(ThreadLocalRandom.current().nextInt(ENHANCEMENTS.size()))))); tasks.add(() -> this.rewriteFile(properties, packet, fi, "enh", first -> String.format(ENHANCE_PROMPT, first ? FIRST_PROMPT : NEXT_PROMPT, ENHANCEMENTS.get(ThreadLocalRandom.current().nextInt(ENHANCEMENTS.size())))));
} }
return tasks; return tasks;
}); });
} }
private void rewriteFile(final Properties properties, final WorkPacket packet, final int variation, final String identifier, final Supplier<String> promptSupplier) { private void rewriteFile(final Properties properties, final WorkPacket packet, final int variation, final String identifier, final Function<Boolean, String> promptSupplier) {
final var out = Execution.suffixFilename(packet.file().getFileName(), final var out = Execution.suffixFilename(packet.file().getFileName(),
"rewr", "rewr",
properties.getProperty("rewrite.model"), properties.getProperty("rewrite.model"),
identifier, identifier,
String.valueOf(variation)); String.valueOf(variation));
LOGGER.info("Rewriting " + packet.file().getFileName() + " " + identifier + " " + " " + variation); LOGGER.info("Rewriting " + packet.file().getFileName() + " " + identifier + " " + variation);
Execution.streamOut(out, "rewrite.destination", properties, fos -> { Execution.streamOut(out, "rewrite.destination", properties, fos -> {
final var client = this.getClient(properties, this.clientService); final var client = this.getClient(properties, this.clientService);
@@ -101,13 +105,13 @@ public class Rewrite implements DatasetTool {
Conversation conversation; Conversation conversation;
if (first) { if (first) {
first = false; first = false;
turn = promptSupplier.get() + "\n" + splits.get(i); turn = promptSupplier.apply(Boolean.TRUE) + "\n" + splits.get(i);
conversation = Conversation.create(SYSTEM_PROMPT, turn); conversation = Conversation.create(SYSTEM_PROMPT, turn);
} }
else { else {
conversation = Conversation.create(SYSTEM_PROMPT, turn); conversation = Conversation.create(SYSTEM_PROMPT, turn);
conversation.addTurn(new Turn(TurnType.ASSISTANT, response)); conversation.addTurn(new Turn(TurnType.ASSISTANT, response));
turn = promptSupplier.get() + "\n" + splits.get(i); turn = promptSupplier.apply(Boolean.FALSE) + "\n" + splits.get(i);
conversation.addTurn(new Turn(TurnType.USER, turn)); conversation.addTurn(new Turn(TurnType.USER, turn));
fos.write(PARAGRAPH); fos.write(PARAGRAPH);