(
self, text: str, prompt_template: Template | None = None
)
| 111 | raise NotImplementedError |
| 112 | |
| 113 | def prompt( |
| 114 | self, text: str, prompt_template: Template | None = None |
| 115 | ) -> list[dict[str, str]]: |
| 116 | try: |
| 117 | return [ |
| 118 | { |
| 119 | "role": "user", |
| 120 | "content": cast(Template, prompt_template).safe_substitute( |
| 121 | { |
| 122 | "lang_in": self.lang_in, |
| 123 | "lang_out": self.lang_out, |
| 124 | "text": text, |
| 125 | } |
| 126 | ), |
| 127 | } |
| 128 | ] |
| 129 | except AttributeError: # `prompt_template` is None |
| 130 | pass |
| 131 | except Exception: |
| 132 | logging.exception("Error parsing prompt, use the default prompt.") |
| 133 | |
| 134 | return [ |
| 135 | { |
| 136 | "role": "user", |
| 137 | "content": ( |
| 138 | "You are a professional, authentic machine translation engine. " |
| 139 | "Only Output the translated text, do not include any other text." |
| 140 | "\n\n" |
| 141 | f"Translate the following markdown source text to {self.lang_out}. " |
| 142 | "Keep the formula notation {v*} unchanged. " |
| 143 | "Output translation directly without any additional text." |
| 144 | "\n\n" |
| 145 | f"Source Text: {text}" |
| 146 | "\n\n" |
| 147 | "Translated Text:" |
| 148 | ), |
| 149 | }, |
| 150 | ] |
| 151 | |
| 152 | def __str__(self): |
| 153 | return f"{self.name} {self.lang_in} {self.lang_out} {self.model}" |
no outgoing calls