add test connectivity
This commit is contained in:
@@ -7,7 +7,13 @@ LLM 调用模块
|
||||
- call_llm_with_template: 使用模板调用(最常用)
|
||||
"""
|
||||
|
||||
from .client import call_llm, call_llm_json, call_llm_with_template, call_llm_with_task_name
|
||||
from .client import (
|
||||
call_llm,
|
||||
call_llm_json,
|
||||
call_llm_with_template,
|
||||
call_llm_with_task_name,
|
||||
test_connectivity
|
||||
)
|
||||
from .config import LLMMode, get_task_mode
|
||||
from .exceptions import LLMError, ParseError, ConfigError
|
||||
|
||||
@@ -16,10 +22,10 @@ __all__ = [
|
||||
"call_llm_json",
|
||||
"call_llm_with_template",
|
||||
"call_llm_with_task_name",
|
||||
"test_connectivity",
|
||||
"LLMMode",
|
||||
"get_task_mode",
|
||||
"LLMError",
|
||||
"ParseError",
|
||||
"ConfigError",
|
||||
]
|
||||
|
||||
|
||||
@@ -172,3 +172,30 @@ async def call_llm_with_task_name(
|
||||
|
||||
return await call_llm_with_template(template_path, infos, mode, max_retries)
|
||||
|
||||
|
||||
def test_connectivity(mode: LLMMode = LLMMode.NORMAL) -> bool:
|
||||
"""
|
||||
测试 LLM 服务连通性 (同步版本)
|
||||
|
||||
Args:
|
||||
mode: 测试使用的模式 (NORMAL/FAST)
|
||||
|
||||
Returns:
|
||||
bool: 连接成功返回 True,失败返回 False
|
||||
"""
|
||||
try:
|
||||
config = LLMConfig.from_mode(mode)
|
||||
if HAS_LITELLM:
|
||||
# 使用 litellm 同步接口
|
||||
litellm.completion(
|
||||
model=config.model_name,
|
||||
messages=[{"role": "user", "content": "你好"}],
|
||||
api_key=config.api_key,
|
||||
base_url=config.base_url,
|
||||
)
|
||||
else:
|
||||
# 直接调用 requests 实现
|
||||
_call_with_requests(config, "test")
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
Reference in New Issue
Block a user