Coverage for vorlagellm/prompts.py: 100.00%
25 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-10-24 03:22 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2025-10-24 03:22 +0000
1from langchain.prompts import ChatPromptTemplate
3SYSTEM_MESSAGE = "You are a text critic who is an expert in {apparatus_language} and {doc_language}."
5def readings_list_to_str(readings:list[str])->str:
6 result = ""
7 for i, reading in enumerate(readings):
8 reading = reading if reading else "OMISSION"
9 result += f"{i+1}. {reading}\n"
10 return result
14def build_prompt(initiate_response:bool=False, **kwargs):
15 messages = [
16 ("system", SYSTEM_MESSAGE),
17 ("user",
18 "You are to read the following text in {doc_language} "
19 "and then choose which readings in {apparatus_language} which could have been the likely source of the translation into {doc_language}. "
20 "You may choose more than one {apparatus_language} reading if more than one may have been the source. "
21 "Just give the number of each {apparatus_language} reading, separated by a comma. "
22 "If none could have been the source of the {doc_language} text, then you should answer 'NONE'\n\n"
23 "You will also be penalized if you do not select the reading that was the source. You will be penalized if you select more readings than necessary.\n"
24 "After you give the numbers for the readings, print 5 hyphens '-----' and then give a justification for why those readings are possible sources for the tranlation into {doc_language} considering the translation technique.\n\n"
25 "Use the examples of translation technique to inform your decision. For example, if you see examples of the {doc_language} text translating strictly word-for-word, then you can infer that the source {apparatus_language} should be very close and omitted words or phrases in the translation were probably missing in the source. "
26 "If in the translation technique you see examples of {doc_language} text translating the concepts of the source {apparatus_language} in the examples, then any {apparatus_language} text could be the source of the {doc_language} so long as the same concepts are conveyed. "
27 "If the translation technique looks like it preserves word order in certain circumstances and you see the same circumstances in the current text, then you prefer a source {apparatus_language} reading that matches the word order. "
28 "But if the translation technique is inconsistent in preserving word order, then you should not consider word order in your decision. "
29 "Cite the sentence IDs (given in square brackets) of relevant example sentences in your justification to explain why you decided which was the likely source of the translation. "
30 "In your justification, cite phrases from {apparatus_language} readings themselves instead of the reading ID numbers. "
32 # "For example, here is an Arabic text:\n"
33 # ""
35 # Here a list of {apparatus_language} texts:
36 # 1] {{apparatus_language}_example_1}
37 # 2] {{apparatus_language}_example_2}
38 # 3] {{apparatus_language}_example_3}
40 # Your response should be:
41 # The {apparatus_language} texts which could be translated into the {doc_language} '{{doc_language}_example}' are: 2,3
43 "Here is the {doc_language} text to analyze:\n '{text}'\n"
45 "Here is list of {apparatus_language} readings:\n{readings}\n"
47 "{similar_verse_examples}"
49 "Now list the {apparatus_language} readings which could have been the source of the {doc_language} text given the translation technique."
50 ),
51 ]
52 if initiate_response:
53 messages.append(
54 ("ai", "The {apparatus_language} texts which could be translated into the {doc_language} '{text}' are:")
55 )
56 return ChatPromptTemplate.from_messages(messages=messages).partial(**kwargs)
59def build_source_prompt(initiate_response:bool=False, **kwargs):
60 if 'notes' not in kwargs:
61 kwargs['notes'] = ""
63 messages = [
64 ("system", SYSTEM_MESSAGE),
65 ("user",
66 "You are to read the following text in {doc_language} "
67 "and then choose which readings in {apparatus_language} which plausibly could have been the source of the translation into {doc_language}. "
68 "You may choose more than one {apparatus_language} reading if more than one may have been the source. "
69 "Just give the number of each {apparatus_language} reading, separated by a comma. "
70 "If none could have been the source of the {doc_language} text, then you should answer 'NONE'\n\n"
71 "You will also be penalized if you do not select the reading that was the source. Try not to select more readings than necessary. If you are uncertain, then err on the side of selecting more possible readings so you do not exclude the actual source.\n"
72 "After you give the numbers for the readings, print 5 hyphens '-----' and then give a justification for why those readings are possible sources for the tranlation into {doc_language} considering the translation technique.\n\n"
73 "Use the examples of translation technique to inform your decision. For example, if you see examples of the {doc_language} text translating strictly word-for-word, then you can infer that the source {apparatus_language} should be very close and omitted words or phrases in the translation were probably missing in the source. "
74 "If in the translation technique you see examples of {doc_language} text translating the concepts of the source {apparatus_language} in the examples, then any {apparatus_language} text could be the source of the {doc_language} so long as the same concepts are conveyed. "
75 "If the translation technique looks like it preserves word order in certain circumstances and you see the same circumstances in the current text, then you prefer a source {apparatus_language} reading that matches the word order. "
76 "But if the translation technique is inconsistent in preserving word order, then you should not consider word order in your decision. "
77 "{notes}\n"
78 "Cite the IDs of relevant example sentences in your justification to explain why you decided which was the likely source of the translation. "
79 "In your justification, cite phrases from {apparatus_language} readings themselves instead of the reading ID numbers. "
81 "Here is the {doc_language} text to analyze:\n{doc_verse_text}\nIn particular, focus on the words '{doc_corresponding_text}'.\n"
82 "Here is the source {apparatus_language} text to analyze with the textual variant in brackets like this: ⸂ ⸃:\n{apparatus_verse_text}\n\n"
83 "Here are the potential {apparatus_language} readings that go between the brackets that could be the source of '{doc_corresponding_text}':\n{readings}\n\n"
85 "{similar_verse_examples}"
87 "Now list the numbers of the {apparatus_language} readings which could plausibly have been the source of the {doc_language} text given the translation technique."
88 ),
89 ]
90 if initiate_response:
91 messages.append(
92 ("ai", "The {apparatus_language} readings which plausibly could be translated into the {doc_language} '{doc_corresponding_text}' are:")
93 )
94 return ChatPromptTemplate.from_messages(messages=messages).partial(**kwargs)
97def build_corresponding_text_prompt(initiate_response:bool=False, **kwargs):
98 messages = [
99 ("system", SYSTEM_MESSAGE),
100 ("user",
101 "You are to read the following translated text in {doc_language} "
102 "and find the corresponding phrase that correspond to a textual variant in a {apparatus_language} source. "
103 "The textual variant text will be marked in brackets like this ⸂ ⸃. "
104 "The actual {apparatus_language} source reading is unknown. You will be given all potential readings that could have been the source of the translation. "
105 "You are to print the {doc_language} text which best corresponds to the {apparatus_language} text in brackets ⸂ ⸃ with whatever reading was likely to be the original source. "
106 "Only print the {doc_language} text which correspond to the {apparatus_language} text in brackets ⸂ ⸃. "
107 "If the {doc_language} text agrees an omission in {apparatus_language}, then just then print 'OMISSION'. "
108 "Print the {doc_language} text on a single line without line breaks. When finished the {doc_language} text, print a new line and then 5 hyphens '-----' and stop. "
109 "Do not give any other information in your response.\n\n"
111 "For example, if the source Greek readings were ⸂πᾶσι⸃ and ⸂δαῖτα⸃ in the following contexts:\n"
112 "ἡρώων, αὐτοὺς δὲ ἑλώρια τεῦχε κύνεσσιν οἰωνοῖσί τε ⸂πᾶσι⸃, Διὸς δ᾽ ἐτελείετο βουλή,\n"
113 "ἡρώων, αὐτοὺς δὲ ἑλώρια τεῦχε κύνεσσιν οἰωνοῖσί τε ⸂δαῖτα⸃, Διὸς δ᾽ ἐτελείετο βουλή,\n"
114 "And if the English translated text was 'of heroes, and made them prey for dogs and for birds feast, and the will of Zeus was being fulfilled'\n"
115 "Then you would reply with the text: 'feast'\n\n"
117 "For example, if the source English readings were ⸂it was the worst of times⸃ and ⸂OMISSION⸃ in the following contexts:\n"
118 "It was the best of times, ⸂it was the worst of times⸃\n"
119 "It was a good time, ⸂it was the worst of times⸃\n"
120 "It was the best of times,\n"
121 "It was a good time,\n"
122 "And if the German translated text was 'Es war die beste aller Zeiten,'\n"
123 "Then you would reply with the text: 'OMISSION'\n\n"
126 "Here is the {doc_language} text to analyze:\n{doc_verse_text}\n\n"
127 "Here are the possible readings at the variation unit: {reading_list}\n\n"
128 "Here are the potential readings in context. The location of the variation unit is indicated with brackets: ⸂ ⸃:\n{permutations}\n\n"
129 ),
130 ]
131 if initiate_response:
132 messages.append(
133 ("ai", "The {doc_language} word(s) from '{doc_verse_text}' which best correspond to the text in the brackets (i.e. {reading_list}) are:")
134 )
135 return ChatPromptTemplate.from_messages(messages=messages).partial(**kwargs)