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
137 lines
5.4 KiB
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Shentun.Peis.Enums;
|
|
using Shentun.Peis.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Json;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Volo.Abp;
|
|
using Volo.Abp.Application.Services;
|
|
using Volo.Abp.Domain.Repositories;
|
|
|
|
namespace Shentun.Peis.AIMessages
|
|
{
|
|
/// <summary>
|
|
/// AI对话
|
|
/// </summary>
|
|
[ApiExplorerSettings(GroupName = "Work")]
|
|
[Authorize]
|
|
public class AIMessageAppService : ApplicationService
|
|
{
|
|
private readonly IRepository<ThirdInterface, Guid> _thirdInterfaceRepository;
|
|
public AIMessageAppService(
|
|
IRepository<ThirdInterface, Guid> thirdInterfaceRepository
|
|
)
|
|
{
|
|
_thirdInterfaceRepository = thirdInterfaceRepository;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取Ai回复内容
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="UserFriendlyException"></exception>
|
|
[HttpPost("api/app/AIMessage/GetAIMessageResult")]
|
|
public async Task<GetAIMessageResultDto> GetAIMessageResultAsync(GetAIMessageResultInputDto input)
|
|
{
|
|
var messageDto = new GetAIMessageResultDto();
|
|
|
|
if (string.IsNullOrWhiteSpace(input.Message))
|
|
{
|
|
throw new UserFriendlyException("请求内容不能为空");
|
|
}
|
|
|
|
var thirdInterface = await _thirdInterfaceRepository.FirstOrDefaultAsync(f => f.ThirdInterfaceType == ThirdInterfaceTypeFlag.WebAI);
|
|
|
|
if (thirdInterface == null)
|
|
{
|
|
throw new UserFriendlyException("未配置第三方AI接口");
|
|
}
|
|
|
|
if (thirdInterface.IsActive != 'Y')
|
|
{
|
|
throw new UserFriendlyException("该接口已禁用");
|
|
}
|
|
|
|
var parmValue = thirdInterface.ParmValue;
|
|
var configurationBuilder = new ConfigurationBuilder()
|
|
.AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue)));
|
|
var config = configurationBuilder.Build();
|
|
var apiBaseAddress = config.GetSection("Interface").GetSection("BaseAddress").Value;
|
|
var apiKey = config.GetSection("Interface").GetSection("ApiKey").Value;
|
|
var aiType = config.GetSection("Interface").GetSection("AIType").Value;
|
|
var modelValue = config.GetSection("Interface").GetSection("ModelValue").Value;
|
|
|
|
if (aiType == AITypeFlag.DeepSeek)
|
|
{
|
|
using (HttpClient client = new HttpClient())
|
|
{
|
|
client.BaseAddress = new Uri(apiBaseAddress);
|
|
// 设置API密钥或其他认证信息(如果有的话)
|
|
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
|
|
//client.DefaultRequestHeaders.Add("Accept", "text/html");
|
|
try
|
|
{
|
|
var requestBody = new
|
|
{
|
|
model = modelValue,
|
|
messages = new[] { new { role = "user", content = input.Message } }
|
|
//response_format = "html"
|
|
};
|
|
|
|
var response = await client.PostAsJsonAsync("chat/completions", requestBody);
|
|
var result = await response.Content.ReadFromJsonAsync<DeepSeekResponse>();
|
|
string data = result.Choices.First().Message.Content;
|
|
|
|
//string dataHtml = data.Replace("### ", "").Replace("---", "").Replace("-", "");
|
|
//var dataHtmlList = dataHtml.Split("**", StringSplitOptions.RemoveEmptyEntries).ToList();
|
|
//StringBuilder stringBuilder = new StringBuilder();
|
|
//foreach (var item in dataHtmlList)
|
|
//{
|
|
// var sindex = dataHtmlList.IndexOf(item) + 1;
|
|
// if (sindex > 1)
|
|
// {
|
|
// if (sindex % 2 == 0)
|
|
// {
|
|
|
|
// stringBuilder.Append("<strong>" + item);
|
|
// }
|
|
// else
|
|
// {
|
|
|
|
// stringBuilder.Append("</strong>" + item);
|
|
// }
|
|
// }
|
|
// else
|
|
// {
|
|
// stringBuilder.Append(item);
|
|
// }
|
|
//}
|
|
//messageDto.Result = stringBuilder.ToString();
|
|
|
|
string dataHtml = data.Replace("### ", "").Replace("---", "").Replace("-", "").Replace("**", "");
|
|
|
|
messageDto.Result = dataHtml;
|
|
|
|
}
|
|
catch (HttpRequestException e)
|
|
{
|
|
throw new UserFriendlyException($"获取异常:{e.Message}");
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
throw new UserFriendlyException("AI接口类型不正确");
|
|
}
|
|
return messageDto;
|
|
}
|
|
}
|
|
}
|