新增一个openAI格式的转发接口,直接转发整个请求体
This commit is contained in:
parent
3934088e43
commit
01055762ca
19
LMS.Repository/Forward/SimpleTransferModel.cs
Normal file
19
LMS.Repository/Forward/SimpleTransferModel.cs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
namespace LMS.Repository.Forward;
|
||||||
|
|
||||||
|
public class SimpleTransferModel
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// GPT的完整地址
|
||||||
|
/// </summary>
|
||||||
|
public string url { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 对应的API站的使用的APIkey
|
||||||
|
/// </summary>
|
||||||
|
public string APIKey { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实际的GPT请求的数据字符串,直接序列化再传递就行
|
||||||
|
/// </summary>
|
||||||
|
public string dataString { get; set; }
|
||||||
|
}
|
||||||
@ -11,11 +11,13 @@ namespace LMS.service.Controllers;
|
|||||||
[Route("lms/[controller]/[action]")]
|
[Route("lms/[controller]/[action]")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
|
|
||||||
// ceshi
|
|
||||||
public class ForwardController(ForwardWordService forwardWordService) : ControllerBase
|
public class ForwardController(ForwardWordService forwardWordService) : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly ForwardWordService _forwardWordService = forwardWordService;
|
private readonly ForwardWordService _forwardWordService = forwardWordService;
|
||||||
|
|
||||||
|
|
||||||
|
#region 非流转发接口,需要系统数据
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 转发OpenAi格式的请求
|
/// 转发OpenAi格式的请求
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -31,6 +33,10 @@ public class ForwardController(ForwardWordService forwardWordService) : Controll
|
|||||||
return await _forwardWordService.ForwardWord(request);
|
return await _forwardWordService.ForwardWord(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region 流式转发接口,需要系统数据
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 流式转发
|
/// 流式转发
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -72,4 +78,26 @@ public class ForwardController(ForwardWordService forwardWordService) : Controll
|
|||||||
return propertyName.ToLower();
|
return propertyName.ToLower();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region 普通转发接口,直接转发,不要系统数据
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 转发所有的OpenAI格式的AI请求
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<ActionResult<APIResponseModel<object>>> SimpleTransfer([FromBody] SimpleTransferModel request)
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
return APIResponseModel<object>.CreateErrorResponseModel(ResponseCode.ParameterError);
|
||||||
|
}
|
||||||
|
return await _forwardWordService.SimpleTransfer(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,6 +18,8 @@ namespace LMS.service.Service;
|
|||||||
public class ForwardWordService(ApplicationDbContext context)
|
public class ForwardWordService(ApplicationDbContext context)
|
||||||
{
|
{
|
||||||
private readonly ApplicationDbContext _context = context;
|
private readonly ApplicationDbContext _context = context;
|
||||||
|
|
||||||
|
#region 非流转发接口,需要系统数据
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 转发OpenAi格式的请求 非流
|
/// 转发OpenAi格式的请求 非流
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -94,6 +96,15 @@ public class ForwardWordService(ApplicationDbContext context)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 流式转发接口,需要系统数据
|
||||||
|
/// <summary>
|
||||||
|
/// 流式转发
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="Exception"></exception>
|
||||||
public async IAsyncEnumerable<string> ForwardWordStream(ForwardModel request)
|
public async IAsyncEnumerable<string> ForwardWordStream(ForwardModel request)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -144,4 +155,52 @@ public class ForwardWordService(ApplicationDbContext context)
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 简单的AI转发,OpenAI格式
|
||||||
|
/// <summary>
|
||||||
|
/// 简单的转发接口
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="NotImplementedException"></exception>
|
||||||
|
public async Task<ActionResult<APIResponseModel<object>>> 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<object>.CreateErrorResponseModel(ResponseCode.ForwardWordFail, responseContent, "请求失败");
|
||||||
|
}
|
||||||
|
var content = await response.Content.ReadAsStringAsync();
|
||||||
|
// 序列化一下
|
||||||
|
return APIResponseModel<object>.CreateSuccessResponseModel(content);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return APIResponseModel<object>.CreateErrorResponseModel(ResponseCode.SystemError, e.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user