Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor Bug fix with ollama #556

Merged
merged 3 commits into from
Feb 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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