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

feat: 将 groq 翻译重构为 openai 通用翻译 #371

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 6 additions & 3 deletions core/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ aip_secret_key =
add_label_to_cover = no

[Translate]
# 翻译引擎,可选: google, bing, baidu, claude(haiku), groq(llama 3.1-70b) (Google可以直接免费使用。留空表示禁用翻译功能)
# 翻译引擎,可选: google, bing, baidu, claude(haiku), openai (Google可以直接免费使用。留空表示禁用翻译功能)
# 进阶功能的文档 https://github.com/Yuukiy/JavSP/wiki/Translation-%7C-%E7%BF%BB%E8%AF%91
engine =
# 是否翻译标题
Expand All @@ -132,8 +132,11 @@ baidu_key =
bing_key =
# Claude的密钥 (使用haiku模型)
claude_key =
# Groq的密钥 (使用llama 3.1-70b模型)
groq_key =
# OpenAI API(默认使用 Groq,可替换成任何兼容 OpenAI 的第三方 API)
openai_url = https://api.groq.com/openai/v1/chat/completions
openai_key =
# 要使用的模型(默认使用 Groq 的 llama-3.1-70b-versatile 模型,若使用 OpenAI 官方 API 的话一般模型为 gpt-3.5-turbo)
openai_model = llama-3.1-70b-versatile

[NFO]
# 是否添加自定义分类(genre)
Expand Down
10 changes: 6 additions & 4 deletions core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@ def validate_translation(cfg: Config):
trans.baidu_key = os.getenv('JAVSP_BAIDU_KEY', trans.baidu_key)
trans.bing_key = os.getenv('JAVSP_BING_KEY', trans.bing_key)
trans.claude_key = os.getenv('JAVSP_CLAUDE_KEY', trans.claude_key)
trans.groq_key = os.getenv('JAVSP_GROQ_KEY', trans.groq_key)
trans.openai_url = os.getenv('JAVSP_OPENAI_URL', trans.openai_url)
trans.openai_key = os.getenv('JAVSP_OPENAI_KEY', trans.openai_key)
trans.openai_model = os.getenv('JAVSP_OPENAI_MODEL', trans.openai_model)
# 先获取访问凭据再判断翻译引擎,这样的话即使配置文件中未启用翻译也可以调试翻译功能
if trans.engine == '':
return
Expand All @@ -337,11 +339,11 @@ def validate_translation(cfg: Config):
cfg.Translate.engine = engine_name
else:
logger.error('启用Claude时,key不能留空')
elif engine_name == 'groq':
if trans.groq_key:
elif engine_name == 'openai':
if trans.openai_url and trans.openai_key and trans.openai_model:
cfg.Translate.engine = engine_name
else:
logger.error('启用Groq时,key不能留空')
logger.error('启用 OpenAI 时,url、key、model 不能留空')
else:
logger.error('无效的翻译引擎: ' + engine_name)

Expand Down
14 changes: 7 additions & 7 deletions web/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ def translate(texts, engine='google', actress=[]):
err_msg = "{}: {}: {}".format(engine, result['error_code'], result['error_msg'])
except Exception as e:
err_msg = "{}: {}: Exception: {}".format(engine, -2, repr(e))
elif engine == 'groq':
elif engine == 'openai':
try:
result = groq_translate(texts)
result = openai_translate(texts)
if 'error_code' not in result:
rtn = {'trans': result}
else:
Expand Down Expand Up @@ -215,12 +215,12 @@ def claude_translate(texts, to="zh_CN"):
}
return result

def groq_translate(texts, to="zh_CN"):
"""使用Groq翻译文本(默认翻译为简体中文)"""
api_url = "https://api.groq.com/openai/v1/chat/completions"
def openai_translate(texts, to="zh_CN"):
"""使用 OpenAI 翻译文本(默认翻译为简体中文)"""
api_url = cfg.Translate.openai_url
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {cfg.Translate.groq_key}",
"Authorization": f"Bearer {cfg.Translate.openai_key}",
}
data = {
"messages": [
Expand All @@ -233,7 +233,7 @@ def groq_translate(texts, to="zh_CN"):
"content": texts
}
],
"model": "llama-3.1-70b-versatile",
"model": cfg.Translate.openai_model,
"temperature": 0,
"max_tokens": 1024,
}
Expand Down