Skip to content

Commit 50c9589

Browse files
committed
Optimize context prompt
1 parent 9f0d3b9 commit 50c9589

File tree

4 files changed

+29
-23
lines changed

4 files changed

+29
-23
lines changed

src/main/scala/tools/AnthropicCompletions.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
*/
2121
public class AnthropicCompletions {
2222
private static final Logger LOGGER = LoggerFactory.getLogger(AnthropicCompletions.class);
23+
// Add your API key
2324
public static final String API_KEY = "***";
25+
26+
// Note that sonnet 4 has a different output style
27+
// https://docs.anthropic.com/en/docs/about-claude/models/overview#model-comparison-table
2428
public static final String MODEL_NAME = "claude-3-7-sonnet-20250219";
2529

2630
private final ChatModel model;
@@ -79,12 +83,12 @@ public ImmutablePair<String, Integer> runCompletions(String prompt) {
7983
}
8084

8185
public static void main(String[] args) {
82-
String toTranslate = CompletionsUtil.createTranslationPrompt("This is fun.", "English", "German");
86+
String prompt = CompletionsUtil.createTranslationPrompt("This is fun.", "English", "German");
8387

84-
ImmutablePair<String, Integer> result = new AnthropicCompletions().runCompletions(toTranslate);
88+
ImmutablePair<String, Integer> result = new AnthropicCompletions().runCompletions(prompt);
8589
CompletionsUtil.logCompletionResult(result.getLeft(), result.getRight(), "Translation");
8690

87-
ImmutablePair<String, Integer> resultWithContext = AnthropicCompletions.withContext("The Hangover", 2009).runCompletions(toTranslate);
91+
ImmutablePair<String, Integer> resultWithContext = AnthropicCompletions.withContext("The Hangover", 2009).runCompletions(prompt);
8892
CompletionsUtil.logCompletionResult(resultWithContext.getLeft(), resultWithContext.getRight(), "Translation with context");
8993
}
9094

src/main/scala/tools/CompletionsUtil.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,22 @@ public static String createTranslationPrompt(String text, String sourceLanguage,
2828
}
2929

3030
/**
31-
* Creates a movie context prompt
31+
* Creates a movie context prompt optimized for subtitle translation.
32+
* Focuses on elements most relevant to understanding dialogue and cultural context.
3233
*
3334
* @param movieTitle The title of the movie
3435
* @param movieReleaseYear The release year of the movie
35-
* @return Formatted movie context prompt
36+
* @return Formatted movie context prompt optimized for subtitle translation
3637
*/
3738
public static String createMovieContextPrompt(String movieTitle, int movieReleaseYear) {
3839
return String.format(
39-
"Find movie metadata about the movie '%s' released in %d\n" +
40-
"If you don't know this movie or if the title or release year is ambiguous: Respond with \"N/A\", do not apologize." +
41-
"Structure of the response:\n" +
42-
"Plot summary: [Max 100 words]" +
43-
"Locations: [Max 3 key locations]" +
44-
"Key characters: [Max 5 main character names, no actor names]" +
45-
"Themes: [Max 3 main themes]" +
46-
"Notable scenes: [Max 3 brief descriptions of memorable scenes]",
40+
"Provide context for subtitle translation of '%s' (%d).\n" +
41+
"If unknown/ambiguous: respond \"N/A\".\n" +
42+
"Genre: [1-2 genres]\n" +
43+
"Setting: [Time period, main location]\n" +
44+
"Key characters: [Max 4 main character names with brief role]\n" +
45+
"Cultural context: [Important cultural/historical references for translation, Max 100 words]\n" +
46+
"Language style: [Formal/informal, period-specific terms, slang]",
4747
movieTitle, movieReleaseYear);
4848
}
4949

src/main/scala/tools/OpenAICompletions.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class OpenAICompletions {
2222
private static final Logger LOGGER = LoggerFactory.getLogger(OpenAICompletions.class);
2323
// Add your API key, see: https://platform.openai.com/api-keys
2424
public static final String API_KEY = "***";
25+
2526
public static final String MODEL_NAME = "gpt-4o";
2627

2728
private final ChatModel model;
@@ -78,12 +79,12 @@ public ImmutablePair<String, Integer> runCompletions(String prompt) {
7879
}
7980

8081
public static void main(String[] args) {
81-
String toTranslate = CompletionsUtil.createTranslationPrompt("This is fun.", "English", "German");
82+
String prompt = CompletionsUtil.createTranslationPrompt("This is fun.", "English", "German");
8283

83-
ImmutablePair<String, Integer> result = new OpenAICompletions().runCompletions(toTranslate);
84+
ImmutablePair<String, Integer> result = new OpenAICompletions().runCompletions(prompt);
8485
CompletionsUtil.logCompletionResult(result.getLeft(), result.getRight(), "Translation");
8586

86-
ImmutablePair<String, Integer> resultWithContext = OpenAICompletions.withContext("The Hangover", 2009).runCompletions(toTranslate);
87+
ImmutablePair<String, Integer> resultWithContext = OpenAICompletions.withContext("The Hangover", 2009).runCompletions(prompt);
8788
CompletionsUtil.logCompletionResult(resultWithContext.getLeft(), resultWithContext.getRight(), "Translation with context");
8889
}
8990

src/main/scala/tools/SubtitleTranslator.scala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ import scala.util.{Failure, Success}
2121
* - Continuously write translated blocks to target file
2222
* *
2323
* Usage:
24-
* - Wire Params
25-
* - Add API_KEY in [[AnthropicCompletions]], [[OpenAICompletions]] and then run this class
24+
* - Wire Params, eg sourceFilePath
25+
* - Decide, whether to use context info to streamline translation
26+
* - Add API_KEY in [[AnthropicCompletions]], [[OpenAICompletions]] and then run this class
2627
* - Scan log for WARN log messages and improve corresponding blocks in target file manually
2728
*
2829
* Remarks:
2930
* - Numerical block headers in the .srt files are not interpreted, only timestamps matter
30-
* . - Default params are just example values
3131
*
3232
* Similar to: [[sample.stream.SessionWindow]]
3333
*/
@@ -37,14 +37,15 @@ object SubtitleTranslator extends App {
3737
implicit val executionContext: ExecutionContextExecutor = system.dispatcher
3838

3939
// Params
40-
private val sourceFilePath = "Killers.Of.The.Flower.Moon.2023.720p.WEBRip.x264.AAC-[YTS.MX].srt"
41-
private val targetFilePath = "DE_NEW_Killers.Of.The.Flower.Moon.2023.720p.WEBRip.x264.AAC-[YTS.MX].srt"
40+
private val sourceFilePath = "EN_Killers.Of.The.Flower.Moon.srt"
41+
private val targetFilePath = "DE_Killers.Of.The.Flower.Moon.srt"
4242
private val targetLanguage = "German"
4343
private val movieTitle = "Killers of the Flower Moon"
4444
private val movieReleaseYear = 2023
4545

46-
private val defaultModel = OpenAICompletions.withContext(movieTitle, movieReleaseYear)
47-
private val fallbackModel = AnthropicCompletions.withContext(movieTitle, movieReleaseYear)
46+
private val useContext = true
47+
private val defaultModel = if (useContext) OpenAICompletions.withContext(movieTitle, movieReleaseYear) else new OpenAICompletions()
48+
private val fallbackModel = if (useContext) AnthropicCompletions.withContext(movieTitle, movieReleaseYear) else new AnthropicCompletions()
4849

4950
private val maxGapSeconds = 1 // gap time between two scenes (= session windows)
5051
private val endLineTag = "\n"

0 commit comments

Comments
 (0)