fix: verify provider connection online
This commit is contained in:
@@ -142,7 +142,7 @@ async def delete_provider(provider_id: str, db: AsyncSession = Depends(get_db)):
|
|||||||
|
|
||||||
@router.post("/{provider_id}/test", summary="测试提供商连接")
|
@router.post("/{provider_id}/test", summary="测试提供商连接")
|
||||||
async def test_provider(provider_id: str, db: AsyncSession = Depends(get_db)):
|
async def test_provider(provider_id: str, db: AsyncSession = Depends(get_db)):
|
||||||
"""测试提供商连接"""
|
"""通过真实模型接口测试提供商连接"""
|
||||||
result = await db.execute(
|
result = await db.execute(
|
||||||
select(LLMProvider).where(
|
select(LLMProvider).where(
|
||||||
LLMProvider.id == provider_id,
|
LLMProvider.id == provider_id,
|
||||||
@@ -153,36 +153,42 @@ async def test_provider(provider_id: str, db: AsyncSession = Depends(get_db)):
|
|||||||
if not provider:
|
if not provider:
|
||||||
raise HTTPException(status_code=404, detail="提供商不存在")
|
raise HTTPException(status_code=404, detail="提供商不存在")
|
||||||
|
|
||||||
# 创建提供商实例
|
api_base = provider.api_base or ''
|
||||||
|
instance_kwargs = {}
|
||||||
|
if provider.provider_type == 'ollama' and provider.ollama_host:
|
||||||
|
api_base = provider.ollama_host
|
||||||
|
instance_kwargs['ollama_host'] = provider.ollama_host
|
||||||
|
|
||||||
provider_instance = ProviderRegistry.create_instance(
|
provider_instance = ProviderRegistry.create_instance(
|
||||||
provider_type=provider.provider_type,
|
provider_type=provider.provider_type,
|
||||||
api_key=provider.api_key,
|
api_key=provider.api_key,
|
||||||
api_base=provider.api_base,
|
api_base=api_base,
|
||||||
ollama_host=provider.ollama_host,
|
**instance_kwargs,
|
||||||
)
|
)
|
||||||
|
|
||||||
if not provider_instance:
|
if not provider_instance:
|
||||||
raise HTTPException(status_code=400, detail=f"不支持的提供商类型: {provider.provider_type}")
|
raise HTTPException(status_code=400, detail=f"不支持的提供商类型: {provider.provider_type}")
|
||||||
|
|
||||||
# 验证配置
|
|
||||||
if not provider_instance.validate_config():
|
if not provider_instance.validate_config():
|
||||||
raise HTTPException(status_code=400, detail="配置无效,请检查 API Key")
|
raise HTTPException(status_code=400, detail="配置无效,请检查 API Key")
|
||||||
|
|
||||||
# 尝试获取模型列表(如果支持)
|
models = await provider_instance.fetch_models_from_api()
|
||||||
try:
|
if not models:
|
||||||
models = provider_instance.get_available_models()
|
logger.warning(
|
||||||
return {
|
"Provider online test failed: no models returned "
|
||||||
"success": True,
|
f"[provider={provider.name}, type={provider.provider_type}, api_base={api_base or '(empty)'}]"
|
||||||
"message": "连接成功",
|
)
|
||||||
"models": models,
|
raise HTTPException(
|
||||||
}
|
status_code=400,
|
||||||
except Exception as e:
|
detail="连接失败:模型接口未返回可用模型,请检查 Base URL、API Key 或网络连通性",
|
||||||
logger.warning(f"获取模型列表失败: {e}")
|
)
|
||||||
return {
|
|
||||||
"success": True,
|
return {
|
||||||
"message": "连接成功(无法获取模型列表)",
|
"success": True,
|
||||||
"models": [],
|
"message": f"连接成功,已从模型接口拉取到 {len(models)} 个模型",
|
||||||
}
|
"source": "api",
|
||||||
|
"model_count": len(models),
|
||||||
|
"models": models[:20],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{provider_id}/default-models", summary="获取默认模型列表")
|
@router.get("/{provider_id}/default-models", summary="获取默认模型列表")
|
||||||
|
|||||||
@@ -391,7 +391,9 @@ export async function deleteProviderApi(id: string) {
|
|||||||
export async function testProviderApi(id: string) {
|
export async function testProviderApi(id: string) {
|
||||||
return requestClient.post<{
|
return requestClient.post<{
|
||||||
message: string;
|
message: string;
|
||||||
models: any[];
|
model_count: number;
|
||||||
|
models: DefaultModel[];
|
||||||
|
source: 'api';
|
||||||
success: boolean;
|
success: boolean;
|
||||||
}>(`${BASE_URL}/provider/${id}/test`);
|
}>(`${BASE_URL}/provider/${id}/test`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -332,7 +332,14 @@ async function handleTestProvider(row: ProviderListItem) {
|
|||||||
try {
|
try {
|
||||||
const res = await testProviderApi(row.id);
|
const res = await testProviderApi(row.id);
|
||||||
if (res.success) {
|
if (res.success) {
|
||||||
ElMessage.success(res.message);
|
const sampleModels = res.models
|
||||||
|
.slice(0, 3)
|
||||||
|
.map((model) => model.model_name)
|
||||||
|
.join(', ');
|
||||||
|
const sampleText = sampleModels ? `: ${sampleModels}` : '';
|
||||||
|
ElMessage.success(
|
||||||
|
`连接成功,已从模型接口拉取到 ${res.model_count} 个模型${sampleText}`,
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
ElMessage.error($t('ui.actionMessage.operationFailed'));
|
ElMessage.error($t('ui.actionMessage.operationFailed'));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user