@Summary 测试博查AI搜索功能 @Description 根据平台设置ID测试博查AI搜索功能是否正常 @Tags 能力平台设置 @Accept json @Produce json @Security BearerAuth @Param id path int true "平台设置ID" @Success 200 {object} model.CommonResponse @Router /api/platform-settings/{id}/test-bochaai-search [post]
(c *gin.Context)
| 308 | // @Success 200 {object} model.CommonResponse |
| 309 | // @Router /api/platform-settings/{id}/test-bochaai-search [post] |
| 310 | func TestBochaAISearch(c *gin.Context) { |
| 311 | id, _ := strconv.Atoi(c.Param("id")) |
| 312 | platformSetting, err := model.GetPlatformSettingByID(int64(id)) |
| 313 | |
| 314 | if err != nil || platformSetting.Eid != config.GetEID(c) { |
| 315 | c.JSON(http.StatusNotFound, model.NotFound.ToResponse(nil)) |
| 316 | return |
| 317 | } |
| 318 | |
| 319 | // 检查平台设置是否为博查AI类型 |
| 320 | if platformSetting.PlatformKey != model.PLATFORM_BOCHAAI { |
| 321 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(fmt.Errorf("平台设置类型不匹配,期望: %s, 实际: %s", model.PLATFORM_BOCHAAI, platformSetting.PlatformKey))) |
| 322 | return |
| 323 | } |
| 324 | |
| 325 | // 解析设置中的API密钥 |
| 326 | var apiKeySetting BochaAIKeySetting |
| 327 | if err := json.Unmarshal([]byte(platformSetting.Setting), &apiKeySetting); err != nil { |
| 328 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(fmt.Errorf("解析API密钥失败: %v", err))) |
| 329 | return |
| 330 | } |
| 331 | |
| 332 | if apiKeySetting.APIKey == "" { |
| 333 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(fmt.Errorf("API密钥不能为空"))) |
| 334 | return |
| 335 | } |
| 336 | |
| 337 | // 创建博查AI服务实例 |
| 338 | bochaAIService := service.NewBochaAIService(apiKeySetting.APIKey) |
| 339 | |
| 340 | // 创建搜索请求,使用简单的查询词,只请求1个结果 |
| 341 | request := service.SearchRequest{ |
| 342 | Query: "人工智能", |
| 343 | Count: 1, |
| 344 | Summary: true, |
| 345 | } |
| 346 | |
| 347 | // 执行搜索 |
| 348 | response, err := bochaAIService.Search(request) |
| 349 | if err != nil { |
| 350 | c.JSON(http.StatusInternalServerError, model.ParamError.ToResponse(fmt.Errorf("搜索请求失败: %v", err))) |
| 351 | return |
| 352 | } |
| 353 | |
| 354 | // 检查响应状态码 |
| 355 | codeStr, ok := response.Code.(string) |
| 356 | if ok { |
| 357 | if codeStr != "200" { |
| 358 | c.JSON(http.StatusInternalServerError, model.ParamError.ToResponse(fmt.Errorf("搜索响应错误,状态码: %s", codeStr))) |
| 359 | return |
| 360 | } |
| 361 | } else { |
| 362 | codeNum, ok := response.Code.(float64) // JSON数字默认解析为float64 |
| 363 | if ok { |
| 364 | if codeNum != 200 { |
| 365 | c.JSON(http.StatusInternalServerError, model.ParamError.ToResponse(fmt.Errorf("搜索响应错误,状态码: %f", codeNum))) |
| 366 | return |
| 367 | } |
nothing calls this directly
no test coverage detected