using Microsoft.AspNetCore.Mvc; using OASystem.API.OAMethodLib.HunYuanAPI; using TencentCloud.Hunyuan.V20230901.Models; namespace OASystem.API.Controllers { /// /// AI测试控制器 /// [Route("api/[controller]")] public class AITestController : ControllerBase { private readonly IHunyuanService _hunyuanService; private readonly ILogger _logger; public AITestController(IHunyuanService hunyuanService, ILogger logger) { _hunyuanService = hunyuanService; _logger = logger; } #region 混元 AI /// /// 基础对话示例 /// [HttpPost("chat")] public async Task> BasicChat(string question) { try { var response = await _hunyuanService.ChatCompletionsHunyuan_t1_latestAsync(question); return Ok(response); } catch (Exception ex) { _logger.LogError(ex, "调用腾讯云混元API失败。"); return StatusCode(500, new { Message = "调用腾讯云API失败,请检查配置或网络。", Detail = ex.Message }); } } /// /// 模拟“根据文件提问”的API端点 /// 注意:此示例中,文件内容通过请求体传入。 /// 实际场景中,文件内容可能来自用户上传并解析(如PDF、TXT解析为文本)后的结果。 /// [HttpPost("ask-with-file")] public async Task> AskBasedOnFile([FromBody] AskWithFileRequest request) { if (string.IsNullOrEmpty(request.FileContent) || string.IsNullOrEmpty(request.Question)) { return BadRequest(new { Message = "FileContent和Question字段不能为空。" }); } try { var answer = await _hunyuanService.AskWithFileContextAsync(request.FileContent, request.Question, request.Model); return Ok(answer); } catch (Exception ex) { _logger.LogError(ex, "处理基于文件的提问失败。"); return StatusCode(500, new { Message = "处理请求失败。", Detail = ex.Message }); } } /// /// 用于测试的GET端点,快速验证服务可用性(使用示例数据) /// [HttpGet("test-file-query")] public async Task> TestFileQuery() { // 示例文件内容和问题 var sampleFileContent = "在软件开发中,依赖注入(Dependency Injection)是一种设计模式,用于实现控制反转(Inversion of Control, IoC)。它允许在类外部创建依赖对象,并通过构造函数、属性或方法将其‘注入’到类中,从而降低类之间的耦合度。"; var sampleQuestion = "依赖注入的主要目的是什么?"; var model = "hunyuan-lite"; // 可使用 "hunyuan-pro" 等 try { var answer = await _hunyuanService.AskWithFileContextAsync(sampleFileContent, sampleQuestion, model); return Ok($"测试成功。问题:'{sampleQuestion}'\n回答:{answer}"); } catch (Exception ex) { _logger.LogError(ex, "测试文件提问失败。"); return StatusCode(500, new { Message = "测试失败。", Detail = ex.Message }); } } /// /// 用于“根据文件提问”的请求体 /// public class AskWithFileRequest { public string FileContent { get; set; } = string.Empty; public string Question { get; set; } = string.Empty; public string Model { get; set; } = "hunyuan-lite"; } #endregion } }