Skip to content

Commit bfe9e1c

Browse files
committed
Change prompt
1 parent cb98657 commit bfe9e1c

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

mathtranslate/chatgpt.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ def __init__(self):
1616

1717

1818
def format_prompt(self, text, language_to, language_from):
19-
PROMPT_PROTOTYPE = 'As an academic expert with specialized knowledge in various fields, please provide a proficient and precise translation translation from {} to {} of the academic text enclosed in 🔤. It is crucial to maintaining the original phrase or sentence and ensure accuracy while utilizing the appropriate language. Please provide only the translated result without any additional explanation and remove 🔤. Do not modify or delete any word contains "/XMATHX_" such as /XMATHX_0, /XMATHX_1, /XMATHX_3_4. The text is as follows: 🔤 {} 🔤 '
19+
PROMPT_PROTOTYPE = 'As an academic expert with specialized knowledge in various fields, please provide a proficient and precise translation translation from {} to {} of the academic text between 🔤 and 🔠. It is crucial to maintaining the original phrase or sentence and ensure accuracy while utilizing the appropriate language. Please provide only the translated result without any additional explanation or punctuation. Please remove \"🔤\" and \"🔠\". Do not modify or delete any word contains \"#XMATHX_\" such as #XMATHX_0, #XMATHX_1, #XMATHX_3_4. The text is as follows: 🔤{}🔠'
2020
#prompt prototype changed from https://github.com/windingwind/zotero-pdf-translate
21-
SYSTEM_PROMPT_PROTOTYPE = 'You are an academic translator with specialized knowledge in various fields, please provide a proficient and precise translation translation from {} to {} of the academic text enclosed in 🔤.Do not modify or delete any word contains "/XMATHX_" such as /XMATHX_0, /XMATHX_1, /XMATHX_3_4.'
21+
SYSTEM_PROMPT_PROTOTYPE = 'You are an academic translator with specialized knowledge in various fields, please provide a proficient and precise translation translation from {} to {} of the academic text enclosed in 🔤 and 🔠.Please provide only the translated result without any additional explanation or punctuation. Do not modify or delete any word contains "#XMATHX_" such as #XMATHX_0, #XMATHX_1, #XMATHX_3_4. '
2222
return {'system':SYSTEM_PROMPT_PROTOTYPE.format(language_from,language_to),'user':PROMPT_PROTOTYPE.format(language_from,language_to,text)}
2323

2424
def get_server_errormsg(self,error):
@@ -34,7 +34,7 @@ def find_all_mathmask(self,text):
3434

3535
def is_gpt_output_valid(self,masks,text_translated):
3636
masks_translated = self.find_all_mathmask(text_translated)
37-
return (masks_translated==masks)
37+
return (masks_translated==masks) and ('🔤' not in text_translated) and ('🔠' not in text_translated)
3838

3939
def is_text_all_mask(self,masks,text):
4040
for mask in masks:
@@ -65,7 +65,8 @@ def call_openai_api(self,prompt):
6565
print('OpenAI api Authentication failed ({}). please check your api setting by:\n translate_tex --setgpt'.format(self.get_server_errormsg(e)))
6666
sys.exit(-1)
6767
except openai.APIError as e:
68-
print('Api requests failed with error:{}. please check your server status'.format(e.message))
68+
print('Api requests failed with error:{}. please check your service status'.format(e.message))
69+
raise e
6970

7071

7172

@@ -74,10 +75,14 @@ def translate(self, text, language_to, language_from):
7475
masks = self.find_all_mathmask(text)
7576
if self.is_text_all_mask(masks,text):
7677
return text
78+
text = text.lstrip('\n')
7779
while True:
7880
result = self.call_openai_api(self.format_prompt(text, language_to, language_from))
79-
content_translated = result.choices[0].message.content.replace('🔤','')
81+
content_translated = result.choices[0].message.content
8082
if self.is_gpt_output_valid(masks,content_translated):
83+
if content_translated.startswith('"') and content_translated.endswith('"'):
84+
content_translated=content_translated.lstrip('"').rstrip('"')
85+
#remove unexpect " " added by gpt
8186
return content_translated
8287

8388

0 commit comments

Comments
 (0)