feat: 新增gemini支持
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Text.Json;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Volo.Abp.Application.Services;
|
||||
@@ -201,7 +202,7 @@ public class OpenApiService : ApplicationService
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 响应-Openai新规范 (尊享服务专用)
|
||||
/// </summary>
|
||||
@@ -240,9 +241,9 @@ public class OpenApiService : ApplicationService
|
||||
//ai网关代理httpcontext
|
||||
if (input.Stream == true)
|
||||
{
|
||||
await _aiGateWayManager.OpenAiResponsesStreamForStatisticsAsync(_httpContextAccessor.HttpContext,
|
||||
input,
|
||||
userId, null, tokenId, cancellationToken);
|
||||
await _aiGateWayManager.OpenAiResponsesStreamForStatisticsAsync(_httpContextAccessor.HttpContext,
|
||||
input,
|
||||
userId, null, tokenId, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -253,7 +254,63 @@ public class OpenApiService : ApplicationService
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 生成-Gemini (尊享服务专用)
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <param name="modelId"></param>
|
||||
/// <param name="alt"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
[HttpPost("openApi/v1beta/models/{modelId}:generateContent")]
|
||||
public async Task GenerateContentAsync([FromBody] JsonElement input,
|
||||
[FromRoute] string modelId,
|
||||
[FromQuery] string? alt, CancellationToken cancellationToken)
|
||||
{
|
||||
//前面都是校验,后面才是真正的调用
|
||||
var httpContext = this._httpContextAccessor.HttpContext;
|
||||
var tokenValidation = await _tokenManager.ValidateTokenAsync(GetTokenByHttpContext(httpContext), modelId);
|
||||
var userId = tokenValidation.UserId;
|
||||
var tokenId = tokenValidation.TokenId;
|
||||
await _aiBlacklistManager.VerifiyAiBlacklist(userId);
|
||||
|
||||
// 验证用户是否为VIP
|
||||
var userInfo = await _accountService.GetAsync(null, null, userId);
|
||||
if (userInfo == null)
|
||||
{
|
||||
throw new UserFriendlyException("用户信息不存在");
|
||||
}
|
||||
|
||||
// 检查是否为VIP(使用RoleCodes判断)
|
||||
if (!userInfo.RoleCodes.Contains(AiHubConst.VipRole) && userInfo.User.UserName != "cc")
|
||||
{
|
||||
throw new UserFriendlyException("该接口为尊享服务专用,需要VIP权限才能使用");
|
||||
}
|
||||
|
||||
// 检查尊享token包用量
|
||||
var availableTokens = await _premiumPackageManager.GetAvailableTokensAsync(userId);
|
||||
if (availableTokens <= 0)
|
||||
{
|
||||
throw new UserFriendlyException("尊享token包用量不足,请先购买尊享token包");
|
||||
}
|
||||
|
||||
//ai网关代理httpcontext
|
||||
if (alt == "sse")
|
||||
{
|
||||
// await _aiGateWayManager.OpenAiResponsesStreamForStatisticsAsync(_httpContextAccessor.HttpContext,
|
||||
// input,
|
||||
// userId, null, tokenId, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _aiGateWayManager.GeminiGenerateContentAsyncForStatisticsAsync(_httpContextAccessor.HttpContext,
|
||||
modelId, input,
|
||||
userId,
|
||||
null, tokenId,
|
||||
cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
#region 私有
|
||||
|
||||
private string? GetTokenByHttpContext(HttpContext httpContext)
|
||||
|
||||
Reference in New Issue
Block a user