11 changed files with 286 additions and 31 deletions
-
71src/Shentun.Peis.Application.Contracts/AIMessages/DeepSeekResponse.cs
-
11src/Shentun.Peis.Application.Contracts/AIMessages/GetAIMessageResultDto.cs
-
11src/Shentun.Peis.Application.Contracts/AIMessages/GetAIMessageResultInputDto.cs
-
5src/Shentun.Peis.Application.Contracts/PeisReports/GetPatientRegisterReportRequestDto.cs
-
10src/Shentun.Peis.Application.Contracts/PhoneFollowUps/GetPatientRegisterCriticalListInputDto.cs
-
107src/Shentun.Peis.Application/AIMessages/AIMessageAppService.cs
-
2src/Shentun.Peis.Application/PeisApplicationModule.cs
-
7src/Shentun.Peis.Application/PeisReports/PeisReportAppService.cs
-
71src/Shentun.Peis.Application/PhoneFollowUps/PhoneFollowUpAppService.cs
-
19src/Shentun.Peis.Domain.Shared/Enums/AITypeFlag.cs
-
3src/Shentun.Peis.Domain.Shared/Enums/ThirdInterfaceTypeFlag.cs
@ -0,0 +1,71 @@ |
|||||
|
using Newtonsoft.Json; |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Text; |
||||
|
|
||||
|
namespace Shentun.Peis.AIMessages |
||||
|
{ |
||||
|
public class DeepSeekResponse |
||||
|
{ |
||||
|
// 唯一请求标识符
|
||||
|
[JsonProperty("id")] |
||||
|
public string Id { get; set; } |
||||
|
|
||||
|
// 响应创建时间戳(Unix时间)
|
||||
|
[JsonProperty("created")] |
||||
|
public long Created { get; set; } |
||||
|
|
||||
|
// 使用的模型名称(如deepseek-chat)
|
||||
|
[JsonProperty("model")] |
||||
|
public string Model { get; set; } |
||||
|
|
||||
|
// 包含对话结果的集合
|
||||
|
[JsonProperty("choices")] |
||||
|
public List<Choice> Choices { get; set; } |
||||
|
|
||||
|
// Token使用统计(可选字段)
|
||||
|
[JsonProperty("usage")] |
||||
|
public UsageInfo Usage { get; set; } |
||||
|
} |
||||
|
|
||||
|
public class Choice |
||||
|
{ |
||||
|
// 结果序号(多结果时区分)
|
||||
|
[JsonProperty("index")] |
||||
|
public int Index { get; set; } |
||||
|
|
||||
|
// 消息内容封装
|
||||
|
[JsonProperty("message")] |
||||
|
public Message Message { get; set; } |
||||
|
|
||||
|
// 终止原因(如"stop"表示正常结束)
|
||||
|
[JsonProperty("finish_reason")] |
||||
|
public string FinishReason { get; set; } |
||||
|
} |
||||
|
|
||||
|
public class Message |
||||
|
{ |
||||
|
// 角色标识(user/assistant)
|
||||
|
[JsonProperty("role")] |
||||
|
public string Role { get; set; } |
||||
|
|
||||
|
// AI返回的文本内容
|
||||
|
[JsonProperty("content")] |
||||
|
public string Content { get; set; } |
||||
|
} |
||||
|
|
||||
|
public class UsageInfo |
||||
|
{ |
||||
|
// 输入消耗的Token数
|
||||
|
[JsonProperty("prompt_tokens")] |
||||
|
public int PromptTokens { get; set; } |
||||
|
|
||||
|
// 输出消耗的Token数
|
||||
|
[JsonProperty("completion_tokens")] |
||||
|
public int CompletionTokens { get; set; } |
||||
|
|
||||
|
// 总Token消耗
|
||||
|
[JsonProperty("total_tokens")] |
||||
|
public int TotalTokens { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Text; |
||||
|
|
||||
|
namespace Shentun.Peis.AIMessages |
||||
|
{ |
||||
|
public class GetAIMessageResultDto |
||||
|
{ |
||||
|
public string Result { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Text; |
||||
|
|
||||
|
namespace Shentun.Peis.AIMessages |
||||
|
{ |
||||
|
public class GetAIMessageResultInputDto |
||||
|
{ |
||||
|
public string Message { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,107 @@ |
|||||
|
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}"); |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
var requestBody = new |
||||
|
{ |
||||
|
model = modelValue, |
||||
|
messages = new[] { new { role = "user", content = input.Message } } |
||||
|
}; |
||||
|
|
||||
|
var response = await client.PostAsJsonAsync("chat/completions", requestBody); |
||||
|
var result = await response.Content.ReadFromJsonAsync<DeepSeekResponse>(); |
||||
|
string data = result.Choices.First().Message.Content; |
||||
|
messageDto.Result = data; |
||||
|
|
||||
|
} |
||||
|
catch (HttpRequestException e) |
||||
|
{ |
||||
|
throw new UserFriendlyException($"获取异常:{e.Message}"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
throw new UserFriendlyException("AI接口类型不正确"); |
||||
|
} |
||||
|
return messageDto; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.ComponentModel; |
||||
|
using System.Text; |
||||
|
|
||||
|
namespace Shentun.Peis.Enums |
||||
|
{ |
||||
|
public static class AITypeFlag |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// DeepSeek
|
||||
|
/// </summary>
|
||||
|
[Description("DeepSeek")] |
||||
|
public const string DeepSeek = "01"; |
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue