diff --git a/LMS.Repository/Forward/SimpleTransferModel.cs b/LMS.Repository/Forward/SimpleTransferModel.cs new file mode 100644 index 0000000..4aaa430 --- /dev/null +++ b/LMS.Repository/Forward/SimpleTransferModel.cs @@ -0,0 +1,19 @@ +namespace LMS.Repository.Forward; + +public class SimpleTransferModel +{ + /// + /// GPT的完整地址 + /// + public string url { get; set; } + + /// + /// 对应的API站的使用的APIkey + /// + public string APIKey { get; set; } + + /// + /// 实际的GPT请求的数据字符串,直接序列化再传递就行 + /// + public string dataString { get; set; } +} diff --git a/LMS.service/Controllers/ForwardController.cs b/LMS.service/Controllers/ForwardController.cs index 4f97bbe..805d646 100644 --- a/LMS.service/Controllers/ForwardController.cs +++ b/LMS.service/Controllers/ForwardController.cs @@ -11,11 +11,13 @@ namespace LMS.service.Controllers; [Route("lms/[controller]/[action]")] [ApiController] -// ceshi public class ForwardController(ForwardWordService forwardWordService) : ControllerBase { private readonly ForwardWordService _forwardWordService = forwardWordService; + + #region 非流转发接口,需要系统数据 + /// /// 转发OpenAi格式的请求 /// @@ -31,6 +33,10 @@ public class ForwardController(ForwardWordService forwardWordService) : Controll return await _forwardWordService.ForwardWord(request); } + #endregion + + + #region 流式转发接口,需要系统数据 /// /// 流式转发 /// @@ -72,4 +78,26 @@ public class ForwardController(ForwardWordService forwardWordService) : Controll return propertyName.ToLower(); } } + + #endregion + + + #region 普通转发接口,直接转发,不要系统数据 + + /// + /// 转发所有的OpenAI格式的AI请求 + /// + /// + /// + [HttpPost] + public async Task>> SimpleTransfer([FromBody] SimpleTransferModel request) + { + if (!ModelState.IsValid) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.ParameterError); + } + return await _forwardWordService.SimpleTransfer(request); + } + + #endregion } diff --git a/LMS.service/Service/ForwardWordService.cs b/LMS.service/Service/ForwardWordService.cs index b67a376..674fdcf 100644 --- a/LMS.service/Service/ForwardWordService.cs +++ b/LMS.service/Service/ForwardWordService.cs @@ -18,6 +18,8 @@ namespace LMS.service.Service; public class ForwardWordService(ApplicationDbContext context) { private readonly ApplicationDbContext _context = context; + + #region 非流转发接口,需要系统数据 /// /// 转发OpenAi格式的请求 非流 /// @@ -94,6 +96,15 @@ public class ForwardWordService(ApplicationDbContext context) } } + #endregion + + #region 流式转发接口,需要系统数据 + /// + /// 流式转发 + /// + /// + /// + /// public async IAsyncEnumerable ForwardWordStream(ForwardModel request) { @@ -144,4 +155,52 @@ public class ForwardWordService(ApplicationDbContext context) } } + + #endregion + + #region 简单的AI转发,OpenAI格式 + /// + /// 简单的转发接口 + /// + /// + /// + /// + public async Task>> SimpleTransfer(SimpleTransferModel request) + { + try + { + // 开始拼接请求体 + using HttpClient client = new(); + client.DefaultRequestHeaders.Add("Authorization", "Bearer " + request.APIKey); + + // 判断请求的url是不是满足条件 + if (string.IsNullOrEmpty(request.url)) + { + throw new Exception("请求的url为空"); + } + if (!request.url.StartsWith("https://ark.cn-beijing.volces.com") && !request.url.StartsWith("https://api.moonshot.cn") && !request.url.StartsWith("https://laitool.net") && !request.url.StartsWith("https://api.laitool.cc") && !request.url.StartsWith("https://laitool.cc")) + { + throw new Exception("请求的url不合法"); + } + client.Timeout = Timeout.InfiniteTimeSpan; + var response = await client.PostAsync(request.url, new StringContent(request.dataString, Encoding.UTF8, "application/json")); + + // 判断返回的状态码 + if (response.StatusCode != HttpStatusCode.OK) + { + // 读取响应体 + string responseContent = await response.Content.ReadAsStringAsync(); + return APIResponseModel.CreateErrorResponseModel(ResponseCode.ForwardWordFail, responseContent, "请求失败"); + } + var content = await response.Content.ReadAsStringAsync(); + // 序列化一下 + return APIResponseModel.CreateSuccessResponseModel(content); + } + catch (Exception e) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.SystemError, e.Message); + } + } + + #endregion }