Skip to content

Commit

Permalink
Merge pull request #639 from hellofinch/bug-fix
Browse files Browse the repository at this point in the history
fix: 🐛 address issues in Ollama service response and improve translation logic
  • Loading branch information
awwaawwa authored Feb 17, 2025
2 parents a625b8f + b217997 commit c73c8cf
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .github/ISSUE_TEMPLATE/问题反馈_cn.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ body:
required: true
- label: 我已经认真且完整的阅读了 wiki
required: true
- label: 我已经认真检查了问题和网络环境无关
- label: 我已经认真检查了问题和网络环境无关(包括但不限于Google不可用,模型下载失败)
required: true
- type: markdown
attributes:
value: |
感谢您使用本项目并反馈!
请再次确认上述复选框所述的内容已经认真执行!
- type: textarea
id: environment
attributes:
Expand Down
2 changes: 1 addition & 1 deletion docs/ADVANCED.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ We've provided a detailed table on the required [environment variables](https://
|**Grok**|`grok`| `GORK_API_KEY`, `GORK_MODEL` | `[Your GORK_API_KEY]`, `grok-2-1212` |See [Grok](https://docs.x.ai/docs/overview)|
|**Groq**|`groq`| `GROQ_API_KEY`, `GROQ_MODEL` | `[Your GROQ_API_KEY]`, `llama-3-3-70b-versatile` |See [Groq](https://console.groq.com/docs/models)|
|**DeepSeek**|`deepseek`| `DEEPSEEK_API_KEY`, `DEEPSEEK_MODEL` | `[Your DEEPSEEK_API_KEY]`, `deepseek-chat` |See [DeepSeek](https://www.deepseek.com/)|
|**OpenAI-Liked**|`openai-liked`| `OPENAILIKED_BASE_URL`, `OPENAILIKED_API_KEY`, `OPENAILIKED_MODEL` | `url`, `[Your Key]`, `model name` | None |
|**OpenAI-Liked**|`openailiked`| `OPENAILIKED_BASE_URL`, `OPENAILIKED_API_KEY`, `OPENAILIKED_MODEL` | `url`, `[Your Key]`, `model name` | None |
|**Ali Qwen Translation**|`qwen-mt`| `ALI_MODEL`, `ALI_API_KEY`, `ALI_DOMAINS` | `qwen-mt-turbo`, `[Your Key]`, `scientific paper` | Tranditional Chinese are not yet supported, it will be translated into Simplified Chinese. More see [Qwen MT](https://bailian.console.aliyun.com/?spm=5176.28197581.0.0.72e329a4HRxe99#/model-market/detail/qwen-mt-turbo) |

For large language models that are compatible with the OpenAI API but not listed in the table above, you can set environment variables using the same method outlined for OpenAI in the table.
Expand Down
2 changes: 1 addition & 1 deletion docs/README_ja-JP.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pdf2zh example.pdf -li en -lo ja
|**Argos Translate**|`argos`| | |See [argos-translate](https://github.com/argosopentech/argos-translate)|
|**Grok**|`grok`| `GORK_API_KEY`, `GORK_MODEL` | `[Your GORK_API_KEY]`, `grok-2-1212` |See [Grok](https://docs.x.ai/docs/overview)|
|**DeepSeek**|`deepseek`| `DEEPSEEK_API_KEY`, `DEEPSEEK_MODEL` | `[Your DEEPSEEK_API_KEY]`, `deepseek-chat` |See [DeepSeek](https://www.deepseek.com/)|
|**OpenAI-Liked**|`openai-liked`| `OPENAILIKE_BASE_URL`, `OPENAILIKE_API_KEY`, `OPENAILIKE_MODEL` | `url`, `[Your Key]`, `model name` | None |
|**OpenAI-Liked**|`openailiked`| `OPENAILIKED_BASE_URL`, `OPENAILIKED_API_KEY`, `OPENAILIKED_MODEL` | `url`, `[Your Key]`, `model name` | None |
(need Japenese translation)
For large language models that are compatible with the OpenAI API but not listed in the table above, you can set environment variables using the same method outlined for OpenAI in the table.
Expand Down
2 changes: 1 addition & 1 deletion docs/README_ko-KR.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ pdf2zh example.pdf -li en -lo ko
| **Argos Translate** | `argos` | | | [argos-translate](https://github.com/argosopentech/argos-translate) 참조 |
| **Grok** | `grok` | `GORK_API_KEY`, `GORK_MODEL` | `[Your GORK_API_KEY]`, `grok-2-1212` | [Grok](https://docs.x.ai/docs/overview) 참조 |
| **DeepSeek** | `deepseek` | `DEEPSEEK_API_KEY`, `DEEPSEEK_MODEL` | `[Your DEEPSEEK_API_KEY]`, `deepseek-chat` | [DeepSeek](https://www.deepseek.com/) 참조 |
| **OpenAI-Liked** | `openai-liked` | `OPENAILIKE_BASE_URL`, `OPENAILIKE_API_KEY`, `OPENAILIKE_MODEL` | `url`, `[Your Key]`, `model name` | 없음 |
| **OpenAI-Liked** | `openailiked` | `OPENAILIKED_BASE_URL`, `OPENAILIKED_API_KEY`, `OPENAILIKED_MODEL` | `url`, `[Your Key]`, `model name` | 없음 |

위 표에 없는 OpenAI API와 호환되는 대형 언어 모델의 경우, 표의 OpenAI와 동일한 방식으로 환경 변수를 설정할 수 있습니다.

Expand Down
2 changes: 1 addition & 1 deletion docs/README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ pdf2zh example.pdf -li en -lo ja
|**Argos Translate**|`argos`| | |See [argos-translate](https://github.com/argosopentech/argos-translate)|
|**Grok**|`grok`| `GORK_API_KEY`, `GORK_MODEL` | `[Your GORK_API_KEY]`, `grok-2-1212` |See [Grok](https://docs.x.ai/docs/overview)|
|**DeepSeek**|`deepseek`| `DEEPSEEK_API_KEY`, `DEEPSEEK_MODEL` | `[Your DEEPSEEK_API_KEY]`, `deepseek-chat` |See [DeepSeek](https://www.deepseek.com/)|
|**OpenAI-Liked**|`openai-liked`| `OPENAILIKE_BASE_URL`, `OPENAILIKE_API_KEY`, `OPENAILIKE_MODEL` | `url`, `[Your Key]`, `model name` | None |
|**OpenAI-Liked**|`openailiked`| `OPENAILIKED_BASE_URL`, `OPENAILIKED_API_KEY`, `OPENAILIKED_MODEL` | `url`, `[Your Key]`, `model name` | None |
对于未在上述表格中的,并且兼容 OpenAI api 的大语言模型,可使用表格中的 OpenAI 的方式进行环境变量的设置。
Expand Down
2 changes: 1 addition & 1 deletion docs/README_zh-TW.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ pdf2zh example.pdf -li en -lo ja
|**Argos Translate**|`argos`| | |參閱 [argos-translate](https://github.com/argosopentech/argos-translate)|
|**Grok**|`grok`| `GORK_API_KEY`, `GORK_MODEL` | `[Your GORK_API_KEY]`, `grok-2-1212` |參閱 [Grok](https://docs.x.ai/docs/overview)|
|**DeepSeek**|`deepseek`| `DEEPSEEK_API_KEY`, `DEEPSEEK_MODEL` | `[Your DEEPSEEK_API_KEY]`, `deepseek-chat` |參閱 [DeepSeek](https://www.deepseek.com/)|
|**OpenAI-Liked**|`openai-liked`| `OPENAILIKE_BASE_URL`, `OPENAILIKE_API_KEY`, `OPENAILIKE_MODEL` | `url`, `[Your Key]`, `model name` ||
|**OpenAI-Liked**|`openailiked`| `OPENAILIKED_BASE_URL`, `OPENAILIKED_API_KEY`, `OPENAILIKED_MODEL` | `url`, `[Your Key]`, `model name` ||

對於不在上述表格中,但兼容 OpenAI API 的大語言模型,可以使用與 OpenAI 相同的方式設定環境變數。

Expand Down
42 changes: 15 additions & 27 deletions pdf2zh/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,35 +279,25 @@ def __init__(self, lang_in, lang_out, model, envs=None, prompt=None):
self.add_cache_impact_parameters("temperature", self.options["temperature"])

def do_translate(self, text):
maxlen = max(2000, len(text) * 5)
for model in self.model.split(";"):
try:
response = ""
stream = self.client.chat(
model=model,
response = self.client.chat(
model=self.model,
options=self.options,
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"]
# 只在 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()
response = response["message"]["content"].strip()
if (
"deepseek-r1" in model
and "<think>" in response["message"]["content"].strip()
and "</think>" in response["message"]["content"].strip()
):
response = re.sub(
r"^<think>.+?</think>",
"",
response["message"]["content"].strip(),
)
return response
except Exception as e:
print(e)
raise Exception("All models failed")
Expand Down Expand Up @@ -402,9 +392,7 @@ def do_translate(self, text) -> str:
)
if not response.choices:
if hasattr(response, "error"):
raise ValueError("Empty response from OpenAI API", response.error)
else:
raise ValueError("Empty response from OpenAI API")
raise ValueError("Error response from Service", response.error)
return response.choices[0].message.content.strip()

def get_formular_placeholder(self, id: int):
Expand Down

0 comments on commit c73c8cf

Please sign in to comment.