AITestController.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using Microsoft.AspNetCore.Mvc;
  2. using OASystem.API.OAMethodLib.HunYuanAPI;
  3. using OASystem.API.OAMethodLib.QiYeWeChatAPI;
  4. using OASystem.Domain.ViewModels.QiYeWeChat;
  5. using TencentCloud.Hunyuan.V20230901.Models;
  6. namespace OASystem.API.Controllers
  7. {
  8. /// <summary>
  9. /// AI测试控制器
  10. /// </summary>
  11. [Route("api/[controller]")]
  12. public class AITestController : ControllerBase
  13. {
  14. private readonly IHunyuanService _hunyuanService;
  15. private readonly ILogger<AITestController> _logger;
  16. private readonly IQiYeWeChatApiService _qiYeWeChatApiService;
  17. public AITestController(IHunyuanService hunyuanService, ILogger<AITestController> logger, IQiYeWeChatApiService qiYeWeChatApiService)
  18. {
  19. _hunyuanService = hunyuanService;
  20. _logger = logger;
  21. _qiYeWeChatApiService = qiYeWeChatApiService;
  22. }
  23. #region 企业微信发送邮件测试
  24. /// <summary>
  25. /// 企业微信发送邮件测试
  26. /// </summary>
  27. [HttpPost("sendEmail")]
  28. public async Task<ActionResult<string>> SendEmail([FromForm] IFormFile[] feils)
  29. {
  30. try
  31. {
  32. var req = new EmailRequestDto() {
  33. ToEmails = new List<string> { "johnny.yang@pan-american-intl.com" },
  34. CcEmails = new List<string> { "Roy.lei@pan-american-intl.com" },
  35. BccEmails = new List<string> { "Roy.lei@pan-american-intl.com" },
  36. Subject = "测试邮件 - 来自企业微信API",
  37. Body = "这是一封通过企业微信API发送的测试邮件,包含附件。",
  38. Files = feils
  39. };
  40. var response = await _qiYeWeChatApiService.EmailSendAsync(req);
  41. return Ok(response);
  42. }
  43. catch (Exception ex)
  44. {
  45. _logger.LogError(ex, "调用企业微信邮件API失败。");
  46. return StatusCode(500, new { Message = "调用企业微信邮件API失败,请检查配置或网络。", Detail = ex.Message });
  47. }
  48. }
  49. #endregion
  50. #region 混元 AI
  51. /// <summary>
  52. /// 基础对话示例
  53. /// </summary>
  54. [HttpPost("chat")]
  55. public async Task<ActionResult<string>> BasicChat(string question)
  56. {
  57. try
  58. {
  59. var response = await _hunyuanService.ChatCompletionsHunyuan_t1_latestAsync(question);
  60. return Ok(response);
  61. }
  62. catch (Exception ex)
  63. {
  64. _logger.LogError(ex, "调用腾讯云混元API失败。");
  65. return StatusCode(500, new { Message = "调用腾讯云API失败,请检查配置或网络。", Detail = ex.Message });
  66. }
  67. }
  68. /// <summary>
  69. /// 模拟“根据文件提问”的API端点
  70. /// 注意:此示例中,文件内容通过请求体传入。
  71. /// 实际场景中,文件内容可能来自用户上传并解析(如PDF、TXT解析为文本)后的结果。
  72. /// </summary>
  73. [HttpPost("ask-with-file")]
  74. public async Task<ActionResult<string>> AskBasedOnFile([FromBody] AskWithFileRequest request)
  75. {
  76. if (string.IsNullOrEmpty(request.FileContent) || string.IsNullOrEmpty(request.Question))
  77. {
  78. return BadRequest(new { Message = "FileContent和Question字段不能为空。" });
  79. }
  80. try
  81. {
  82. var answer = await _hunyuanService.AskWithFileContextAsync(request.FileContent, request.Question, request.Model);
  83. return Ok(answer);
  84. }
  85. catch (Exception ex)
  86. {
  87. _logger.LogError(ex, "处理基于文件的提问失败。");
  88. return StatusCode(500, new { Message = "处理请求失败。", Detail = ex.Message });
  89. }
  90. }
  91. /// <summary>
  92. /// 用于测试的GET端点,快速验证服务可用性(使用示例数据)
  93. /// </summary>
  94. [HttpGet("test-file-query")]
  95. public async Task<ActionResult<string>> TestFileQuery()
  96. {
  97. // 示例文件内容和问题
  98. var sampleFileContent = "在软件开发中,依赖注入(Dependency Injection)是一种设计模式,用于实现控制反转(Inversion of Control, IoC)。它允许在类外部创建依赖对象,并通过构造函数、属性或方法将其‘注入’到类中,从而降低类之间的耦合度。";
  99. var sampleQuestion = "依赖注入的主要目的是什么?";
  100. var model = "hunyuan-lite"; // 可使用 "hunyuan-pro" 等
  101. try
  102. {
  103. var answer = await _hunyuanService.AskWithFileContextAsync(sampleFileContent, sampleQuestion, model);
  104. return Ok($"测试成功。问题:'{sampleQuestion}'\n回答:{answer}");
  105. }
  106. catch (Exception ex)
  107. {
  108. _logger.LogError(ex, "测试文件提问失败。");
  109. return StatusCode(500, new { Message = "测试失败。", Detail = ex.Message });
  110. }
  111. }
  112. /// <summary>
  113. /// 用于“根据文件提问”的请求体
  114. /// </summary>
  115. public class AskWithFileRequest
  116. {
  117. public string FileContent { get; set; } = string.Empty;
  118. public string Question { get; set; } = string.Empty;
  119. public string Model { get; set; } = "hunyuan-lite";
  120. }
  121. #endregion
  122. }
  123. }