Skip to content

Commit 4599c84

Browse files
committed
Freeze prompt string to avoid realloc
1 parent 6440b3a commit 4599c84

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

app/services/discourse_ai/translator.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
module DiscourseAi
44
class Translator
5+
PROMPT_TEMPLATE = <<~TEXT.freeze
6+
You are a highly skilled translator with expertise in many languages.
7+
Your task is to identify the language of the text I provide and accurately translate it into this language locale "%{target_language}" while preserving the meaning, tone, and nuance of the original text.
8+
The text may also contain html tags, which should be preserved in the translation.
9+
Please maintain proper grammar, spelling, and punctuation in the translated version.
10+
Wrap the translated text in a <translation> tag.
11+
TEXT
12+
513
def initialize(text, target_language)
614
@text = text
715
@target_language = target_language
@@ -27,13 +35,7 @@ def translate
2735
private
2836

2937
def build_prompt(target_language)
30-
<<~TEXT
31-
You are a highly skilled translator with expertise in many languages.
32-
Your task is to identify the language of the text I provide and accurately translate it into this language locale "#{target_language}" while preserving the meaning, tone, and nuance of the original text.
33-
The text may also contain html tags, which should be preserved in the translation.
34-
Please maintain proper grammar, spelling, and punctuation in the translated version.
35-
Wrap the translated text in a <translation> tag.
36-
TEXT
38+
PROMPT_TEMPLATE % { target_language: target_language }
3739
end
3840
end
3941
end

0 commit comments

Comments
 (0)