Skip to content

Commit 6538087

Browse files
committed
Merge branch 'main' into feature/i18n
2 parents 3dea850 + da51172 commit 6538087

File tree

7 files changed

+215
-45
lines changed

7 files changed

+215
-45
lines changed

api.py

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from biz.gitlab.webhook_handler import slugify_url
1414
from biz.queue.worker import handle_merge_request_event, handle_push_event, handle_github_pull_request_event, handle_github_push_event
1515
from biz.service.review_service import ReviewService
16+
from biz.utils.config_checker import check_config
1617
from biz.utils.im import notifier
1718
from biz.utils.log import logger
1819
from biz.utils.queue import handle_queue
@@ -22,6 +23,7 @@
2223
api_app = Flask(__name__)
2324

2425
from biz.utils.i18n import get_translator
26+
2527
_ = get_translator()
2628

2729
PUSH_REVIEW_ENABLED = os.environ.get('PUSH_REVIEW_ENABLED', '0') == '1'
@@ -124,6 +126,7 @@ def handle_webhook():
124126
else:
125127
return jsonify({'message': _('Invalid data format')}), 400
126128

129+
127130
def handle_github_webhook(event_type, data):
128131
# 获取GitHub配置
129132
github_token = os.getenv('GITHUB_ACCESS_TOKEN') or request.headers.get('X-GitHub-Token')
@@ -156,6 +159,7 @@ def handle_github_webhook(event_type, data):
156159
logger.error(error_message)
157160
return jsonify(error_message), 400
158161

162+
159163
def handle_gitlab_webhook(data):
160164
object_kind = data.get("object_kind")
161165

@@ -207,7 +211,9 @@ def handle_gitlab_webhook(data):
207211
logger.error(error_message)
208212
return jsonify(error_message), 400
209213

214+
210215
if __name__ == '__main__':
216+
check_config()
211217
# 启动定时任务调度器
212218
setup_scheduler()
213219

biz/llm/client/base.py

+10
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@
22
from typing import List, Dict, Optional
33

44
from biz.llm.types import NotGiven, NOT_GIVEN
5+
from biz.utils.log import logger
56

67

78
class BaseClient:
89
""" Base class for chat models client. """
910

11+
def ping(self) -> bool:
12+
"""Ping the model to check connectivity."""
13+
try:
14+
result = self.completions(messages=[{"role": "user", "content": '请仅返回 "ok"。'}])
15+
return result and result == 'ok'
16+
except Exception:
17+
logger.error("尝试连接LLM失败, {e}")
18+
return False
19+
1020
@abstractmethod
1121
def completions(self,
1222
messages: List[Dict[str, str]],

biz/llm/factory.py

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ def getClient(provider: str = None) -> BaseClient:
2424

2525
provider_func = chat_model_providers.get(provider)
2626
if provider_func:
27-
logger.info(_("Successfully loaded LLM provider: {provider}").format(provider=provider))
2827
return provider_func()
2928
else:
3029
raise Exception(_('Unknown chat model provider: {provider}'.format(provider=provider)))

biz/utils/config_checker.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import os
2+
3+
from dotenv import load_dotenv
4+
5+
from biz.llm.factory import Factory
6+
from biz.utils.log import logger
7+
8+
# 指定环境变量文件路径
9+
ENV_FILE_PATH = "conf/.env"
10+
load_dotenv(ENV_FILE_PATH)
11+
12+
from i18n import get_translator
13+
14+
_ = get_translator()
15+
16+
REQUIRED_ENV_VARS = [
17+
"LLM_PROVIDER",
18+
]
19+
20+
# 允许的 LLM 供应商
21+
LLM_PROVIDERS = {"zhipuai", "openai", "deepseek", "ollama"}
22+
23+
# 每种供应商必须配置的键
24+
LLM_REQUIRED_KEYS = {
25+
"zhipuai": ["ZHIPUAI_API_KEY", "ZHIPUAI_API_MODEL"],
26+
"openai": ["OPENAI_API_KEY", "OPENAI_API_MODEL"],
27+
"deepseek": ["DEEPSEEK_API_KEY", "DEEPSEEK_API_MODEL"],
28+
"ollama": ["OLLAMA_API_BASE_URL", "OLLAMA_API_MODEL"],
29+
}
30+
31+
32+
def check_env_vars():
33+
"""检查环境变量"""
34+
missing_vars = [var for var in REQUIRED_ENV_VARS if var not in os.environ]
35+
if missing_vars:
36+
logger.warning(_("缺少环境变量: {}").format(", ".join(missing_vars)))
37+
else:
38+
logger.info(_("所有必要的环境变量均已设置。"))
39+
40+
41+
def check_llm_provider():
42+
"""检查 LLM 供应商的配置"""
43+
llm_provider = os.getenv("LLM_PROVIDER")
44+
45+
if not llm_provider:
46+
logger.error(_("LLM_PROVIDER 未设置!"))
47+
return
48+
49+
if llm_provider not in LLM_PROVIDERS:
50+
logger.error(_("LLM_PROVIDER 值错误,应为 {} 之一。").format(LLM_PROVIDERS))
51+
return
52+
53+
required_keys = LLM_REQUIRED_KEYS.get(llm_provider, [])
54+
missing_keys = [key for key in required_keys if not os.getenv(key)]
55+
56+
if missing_keys:
57+
logger.error(_("当前 LLM 供应商为 {},但缺少必要的环境变量: {}").format(llm_provider, ', '.join(missing_keys)))
58+
else:
59+
logger.info(_("LLM 供应商 {} 的配置项已设置。").format(llm_provider))
60+
61+
62+
def check_llm_connectivity():
63+
client = Factory().getClient()
64+
logger.info(_("正在检查 LLM 供应商的连接..."))
65+
if client.ping():
66+
logger.info(_("LLM 可以连接成功。"))
67+
else:
68+
logger.error(_("LLM连接可能有问题,请检查配置项。"))
69+
70+
71+
def check_config():
72+
"""主检查入口"""
73+
logger.info(_("开始检查配置项..."))
74+
check_env_vars()
75+
check_llm_provider()
76+
check_llm_connectivity()
77+
logger.info(_("配置项检查完成。"))

locales/en_US/LC_MESSAGES/messages.mo

1.04 KB
Binary file not shown.

locales/en_US/LC_MESSAGES/messages.po

+61-22
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ msgid ""
77
msgstr ""
88
"Project-Id-Version: PACKAGE VERSION\n"
99
"Report-Msgid-Bugs-To: \n"
10-
"POT-Creation-Date: 2025-03-28 17:40+0100\n"
10+
"POT-Creation-Date: 2025-03-29 15:24+0100\n"
1111
"PO-Revision-Date: 2025-03-18 15:05+0100\n"
1212
"Last-Translator: Automatically generated\n"
1313
"Language-Team: none\n"
@@ -16,67 +16,67 @@ msgstr ""
1616
"Content-Type: text/plain; charset=UTF-8\n"
1717
"Content-Transfer-Encoding: 8bit\n"
1818

19-
#: api.py:55 api.py:56
19+
#: api.py:57 api.py:58
2020
msgid "No data to process."
2121
msgstr "No data to process."
2222

23-
#: api.py:66
23+
#: api.py:68
2424
msgid "代码提交日报"
2525
msgstr "Code Submission Daily Report"
2626

27-
#: api.py:71
27+
#: api.py:73
2828
msgid "Failed to generate daily report: {}"
2929
msgstr "Failed to generate daily report: {}"
3030

31-
#: api.py:99
31+
#: api.py:101
3232
msgid "Scheduler started successfully."
3333
msgstr "Scheduler started successfully."
3434

35-
#: api.py:104
35+
#: api.py:106
3636
msgid "Error setting up scheduler: {}"
3737
msgstr "Error setting up scheduler: {}"
3838

39-
#: api.py:115
39+
#: api.py:117
4040
msgid "Invalid JSON"
4141
msgstr "Invalid JSON"
4242

43-
#: api.py:125
43+
#: api.py:127
4444
msgid "Invalid data format"
4545
msgstr "Invalid data format"
4646

47-
#: api.py:131 api.py:167 api.py:170
47+
#: api.py:134 api.py:171 api.py:174
4848
msgid "Missing GitLab URL"
4949
msgstr "Missing GitLab URL"
5050

51-
#: api.py:137 api.py:186
51+
#: api.py:140 api.py:190
5252
msgid "Received event: {}"
5353
msgstr "Received event: {}"
5454

55-
#: api.py:138 api.py:187
55+
#: api.py:141 api.py:191
5656
msgid "Payload: {}"
5757
msgstr "Payload: {}"
5858

59-
#: api.py:144 api.py:150
59+
#: api.py:147 api.py:153
6060
msgid "GitHub request received(event_type={}), will process asynchronously."
6161
msgstr "GitHub request received(event_type={}), will process asynchronously."
6262

63-
#: api.py:154
63+
#: api.py:157
6464
msgid ""
6565
"Only pull_request and push events are supported for GitHub webhook, but "
6666
"received:: {}."
6767
msgstr ""
6868
"Only pull_request and push events are supported for GitHub webhook, but "
6969
"received:: {}."
7070

71-
#: api.py:181
71+
#: api.py:185
7272
msgid "Missing GitLab access token"
7373
msgstr "Missing GitLab access token"
7474

75-
#: api.py:194 api.py:201
75+
#: api.py:198 api.py:205
7676
msgid "Request received(object_kind={}), will process asynchronously."
7777
msgstr "Request received(object_kind={}), will process asynchronously."
7878

79-
#: api.py:205
79+
#: api.py:209
8080
msgid ""
8181
"Only merge_request and push events are supported (both Webhook and System "
8282
"Hook), but received: {}."
@@ -325,12 +325,7 @@ msgstr ""
325325
msgid "调用DeepSeek API时出错: {e}"
326326
msgstr "Error occurred while calling DeepSeek API: {e}"
327327

328-
#: biz/llm/factory.py:27
329-
#, python-brace-format
330-
msgid "Successfully loaded LLM provider: {provider}"
331-
msgstr "Successfully loaded LLM provider: {provider}"
332-
333-
#: biz/llm/factory.py:30
328+
#: biz/llm/factory.py:29
334329
#, python-brace-format
335330
msgid "Unknown chat model provider: {provider}"
336331
msgstr "Unknown chat model provider: {provider}"
@@ -429,6 +424,50 @@ msgstr "Code is empty"
429424
msgid "总分[::]\\s*\\**(\\d+)分?"
430425
msgstr "Total [sS]core[::]\\s*\\**(\\d+) points"
431426

427+
#: biz/utils/config_checker.py:36
428+
msgid "缺少环境变量: {}"
429+
msgstr "Missing environment variables: {}"
430+
431+
#: biz/utils/config_checker.py:38
432+
msgid "所有必要的环境变量均已设置。"
433+
msgstr "All necessary environment variables are set."
434+
435+
#: biz/utils/config_checker.py:46
436+
msgid "LLM_PROVIDER 未设置!"
437+
msgstr "LLM_PROVIDER not set!"
438+
439+
#: biz/utils/config_checker.py:50
440+
msgid "LLM_PROVIDER 值错误,应为 {} 之一。"
441+
msgstr "Wrong LLM_PROVIDER value, should be one of {}."
442+
443+
#: biz/utils/config_checker.py:57
444+
msgid "当前 LLM 供应商为 {},但缺少必要的环境变量: {}"
445+
msgstr "The current LLM provider is {}, but the required environment variables are missing: {}"
446+
447+
#: biz/utils/config_checker.py:59
448+
msgid "LLM 供应商 {} 的配置项已设置。"
449+
msgstr "Configuration item for LLM vendor {} has been set."
450+
451+
#: biz/utils/config_checker.py:64
452+
msgid "正在检查 LLM 供应商的连接..."
453+
msgstr "Checking LLM supplier connectivity..."
454+
455+
#: biz/utils/config_checker.py:66
456+
msgid "LLM 可以连接成功。"
457+
msgstr "LLM can connect successfully."
458+
459+
#: biz/utils/config_checker.py:68
460+
msgid "LLM连接可能有问题,请检查配置项。"
461+
msgstr "There may be a problem with the LLM connection. Please check the configuration items."
462+
463+
#: biz/utils/config_checker.py:73
464+
msgid "开始检查配置项..."
465+
msgstr "Starting to check configuration items..."
466+
467+
#: biz/utils/config_checker.py:77
468+
msgid "配置项检查完成。"
469+
msgstr "The configuration item check is complete."
470+
432471
#: biz/utils/im/dingtalk.py:30
433472
msgid "未提供项目名称,且未设置默认的钉钉 Webhook URL。"
434473
msgstr "Project name not provided, and no default DingTalk Webhook URL set."

0 commit comments

Comments
 (0)