Skip to content

Commit

Permalink
Merge pull request #556 from damaoooo/main
Browse files Browse the repository at this point in the history
  • Loading branch information
Byaidu authored Feb 1, 2025
2 parents 72b6414 + 6a886a3 commit b79b6e9
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions pdf2zh/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def prompt(self, text, prompt):
return [
{
"role": "system",
"content": "You are a professional,authentic machine translation engine.",
"content": "You are a professional,authentic machine translation engine. Only Output the translated text, do not include any other text.",
},
{
"role": "user",
Expand Down Expand Up @@ -289,9 +289,22 @@ def do_translate(self, text):
messages=self.prompt(text, self.prompttext),
stream=True,
)
in_think_block = False
is_deepseek_r1 = "deepseek-r1" in model
for chunk in stream:
chunk = chunk["message"]["content"]
response += chunk
# 只在 deepseek-r1 模型下检查 <think> 块
if is_deepseek_r1:
if "<think>" in chunk:
in_think_block = True
chunk = chunk.split("<think>")[0]
if "</think>" in chunk:
in_think_block = False
chunk = chunk.split("</think>")[1]
if not in_think_block:
response += chunk
else:
response += chunk
if len(response) > maxlen:
raise Exception("Response too long")
return response.strip()
Expand Down

0 comments on commit b79b6e9

Please sign in to comment.