You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
5.4 KiB

3 months ago
3 months ago
3 months ago
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Configuration;
  4. using Shentun.Peis.Enums;
  5. using Shentun.Peis.Models;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Net.Http;
  11. using System.Net.Http.Json;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using Volo.Abp;
  15. using Volo.Abp.Application.Services;
  16. using Volo.Abp.Domain.Repositories;
  17. namespace Shentun.Peis.AIMessages
  18. {
  19. /// <summary>
  20. /// AI对话
  21. /// </summary>
  22. [ApiExplorerSettings(GroupName = "Work")]
  23. [Authorize]
  24. public class AIMessageAppService : ApplicationService
  25. {
  26. private readonly IRepository<ThirdInterface, Guid> _thirdInterfaceRepository;
  27. public AIMessageAppService(
  28. IRepository<ThirdInterface, Guid> thirdInterfaceRepository
  29. )
  30. {
  31. _thirdInterfaceRepository = thirdInterfaceRepository;
  32. }
  33. /// <summary>
  34. /// 获取Ai回复内容
  35. /// </summary>
  36. /// <param name="input"></param>
  37. /// <returns></returns>
  38. /// <exception cref="UserFriendlyException"></exception>
  39. [HttpPost("api/app/AIMessage/GetAIMessageResult")]
  40. public async Task<GetAIMessageResultDto> GetAIMessageResultAsync(GetAIMessageResultInputDto input)
  41. {
  42. var messageDto = new GetAIMessageResultDto();
  43. if (string.IsNullOrWhiteSpace(input.Message))
  44. {
  45. throw new UserFriendlyException("请求内容不能为空");
  46. }
  47. var thirdInterface = await _thirdInterfaceRepository.FirstOrDefaultAsync(f => f.ThirdInterfaceType == ThirdInterfaceTypeFlag.WebAI);
  48. if (thirdInterface == null)
  49. {
  50. throw new UserFriendlyException("未配置第三方AI接口");
  51. }
  52. if (thirdInterface.IsActive != 'Y')
  53. {
  54. throw new UserFriendlyException("该接口已禁用");
  55. }
  56. var parmValue = thirdInterface.ParmValue;
  57. var configurationBuilder = new ConfigurationBuilder()
  58. .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue)));
  59. var config = configurationBuilder.Build();
  60. var apiBaseAddress = config.GetSection("Interface").GetSection("BaseAddress").Value;
  61. var apiKey = config.GetSection("Interface").GetSection("ApiKey").Value;
  62. var aiType = config.GetSection("Interface").GetSection("AIType").Value;
  63. var modelValue = config.GetSection("Interface").GetSection("ModelValue").Value;
  64. if (aiType == AITypeFlag.DeepSeek)
  65. {
  66. using (HttpClient client = new HttpClient())
  67. {
  68. client.BaseAddress = new Uri(apiBaseAddress);
  69. // 设置API密钥或其他认证信息(如果有的话)
  70. client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
  71. //client.DefaultRequestHeaders.Add("Accept", "text/html");
  72. try
  73. {
  74. var requestBody = new
  75. {
  76. model = modelValue,
  77. messages = new[] { new { role = "user", content = input.Message } }
  78. //response_format = "html"
  79. };
  80. var response = await client.PostAsJsonAsync("chat/completions", requestBody);
  81. var result = await response.Content.ReadFromJsonAsync<DeepSeekResponse>();
  82. string data = result.Choices.First().Message.Content;
  83. //string dataHtml = data.Replace("### ", "").Replace("---", "").Replace("-", "");
  84. //var dataHtmlList = dataHtml.Split("**", StringSplitOptions.RemoveEmptyEntries).ToList();
  85. //StringBuilder stringBuilder = new StringBuilder();
  86. //foreach (var item in dataHtmlList)
  87. //{
  88. // var sindex = dataHtmlList.IndexOf(item) + 1;
  89. // if (sindex > 1)
  90. // {
  91. // if (sindex % 2 == 0)
  92. // {
  93. // stringBuilder.Append("<strong>" + item);
  94. // }
  95. // else
  96. // {
  97. // stringBuilder.Append("</strong>" + item);
  98. // }
  99. // }
  100. // else
  101. // {
  102. // stringBuilder.Append(item);
  103. // }
  104. //}
  105. //messageDto.Result = stringBuilder.ToString();
  106. string dataHtml = data.Replace("### ", "").Replace("---", "").Replace("-", "").Replace("**", "");
  107. messageDto.Result = dataHtml;
  108. }
  109. catch (HttpRequestException e)
  110. {
  111. throw new UserFriendlyException($"获取异常:{e.Message}");
  112. }
  113. }
  114. }
  115. else
  116. {
  117. throw new UserFriendlyException("AI接口类型不正确");
  118. }
  119. return messageDto;
  120. }
  121. }
  122. }