Skip to content

Commit

Permalink
统一命名
Browse files Browse the repository at this point in the history
  • Loading branch information
DHclly committed Jul 15, 2024
1 parent 8446a6f commit 7f184c6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 49 deletions.
8 changes: 7 additions & 1 deletion Thor.sln
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Thor.LocalMemory.Cache", "s
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Thor.RedisMemory.Cache", "src\framework\Thor.RedisMemory.Cache\Thor.RedisMemory.Cache.csproj", "{84C27CF3-BD3C-46D6-A774-967DB5D3F060}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Thor.Moonshot", "src\extensions\Thor.Moonshot\Thor.Moonshot.csproj", "{4D98C74B-D071-430C-9956-3A9B8CF642DD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -119,6 +121,10 @@ Global
{84C27CF3-BD3C-46D6-A774-967DB5D3F060}.Debug|Any CPU.Build.0 = Debug|Any CPU
{84C27CF3-BD3C-46D6-A774-967DB5D3F060}.Release|Any CPU.ActiveCfg = Release|Any CPU
{84C27CF3-BD3C-46D6-A774-967DB5D3F060}.Release|Any CPU.Build.0 = Release|Any CPU
{4D98C74B-D071-430C-9956-3A9B8CF642DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4D98C74B-D071-430C-9956-3A9B8CF642DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4D98C74B-D071-430C-9956-3A9B8CF642DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4D98C74B-D071-430C-9956-3A9B8CF642DD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -142,7 +148,7 @@ Global
{F78FCCB8-19E6-43E9-91EB-765257722DD5} = {1035B36B-8194-43D4-8A4A-992D962CD1D8}
{42402B28-DDC4-4294-A444-6D8B3C43B934} = {1035B36B-8194-43D4-8A4A-992D962CD1D8}
{84C27CF3-BD3C-46D6-A774-967DB5D3F060} = {1035B36B-8194-43D4-8A4A-992D962CD1D8}
{969CB243-CFD0-4635-8663-3892008BB916} = {294B7A34-48F5-43AB-A1D3-033480559E3A}
{4D98C74B-D071-430C-9956-3A9B8CF642DD} = {294B7A34-48F5-43AB-A1D3-033480559E3A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8A80931C-B951-4EF2-AC79-457E73118E5F}
Expand Down
7 changes: 4 additions & 3 deletions src/Thor.Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using Thor.Service.Options;
using Thor.Service.Service;
using Thor.SparkDesk.Extensions;
using Thor.Abstractions.Chats.Dtos;

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -673,7 +674,7 @@ await service.ShareAsync(userId, context))

// 对话补全请求
app.MapPost("/v1/chat/completions",
async (ChatService service, HttpContext httpContext, ChatCompletionCreateRequest request) =>
async (ChatService service, HttpContext httpContext, ThorChatCompletionsRequest request) =>
await service.ChatCompletionsAsync(httpContext, request))
.WithGroupName("OpenAI")
.WithDescription("Get completions from OpenAI")
Expand All @@ -693,8 +694,8 @@ await embeddingService.EmbeddingAsync(context))
.WithOpenApi();

app.MapPost("/v1/images/generations",
async (ChatService imageService, HttpContext context, ImageCreateRequest module) =>
await imageService.CreateImageAsync(context, module))
async (ChatService imageService, HttpContext context, ImageCreateRequest request) =>
await imageService.CreateImageAsync(context, request))
.WithDescription("OpenAI")
.WithDescription("Image")
.WithOpenApi();
Expand Down
54 changes: 27 additions & 27 deletions src/Thor.Service/Service/ChatService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,39 +89,39 @@ public sealed class ChatService(
};


public async ValueTask CreateImageAsync(HttpContext context, ImageCreateRequest module)
public async ValueTask CreateImageAsync(HttpContext context, ImageCreateRequest request)
{
try
{
var (token, user) = await tokenService.CheckTokenAsync(context);

if (module?.Model.IsNullOrEmpty() == true) module.Model = "dall-e-2";
if (request?.Model.IsNullOrEmpty() == true) request.Model = "dall-e-2";

await rateLimitModelService.CheckAsync(module.Model, context, serviceCache);
await rateLimitModelService.CheckAsync(request.Model, context, serviceCache);

var imageCostRatio = GetImageCostRatio(module);
var imageCostRatio = GetImageCostRatio(request);

var rate = SettingService.PromptRate[module.Model];
var rate = SettingService.PromptRate[request.Model];

module.N ??= 1;
request.N ??= 1;

var quota = (int)(rate * imageCostRatio * 1000) * module.N;
var quota = (int)(rate * imageCostRatio * 1000) * request.N;

if (module == null) throw new Exception("模型校验异常");
if (request == null) throw new Exception("模型校验异常");

if (quota > user.ResidualCredit) throw new InsufficientQuotaException("账号余额不足请充值");

// 获取渠道 通过算法计算权重
var channel = CalculateWeight(await channelService.GetChannelsContainsModelAsync(module.Model));
var channel = CalculateWeight(await channelService.GetChannelsContainsModelAsync(request.Model));

if (channel == null) throw new NotModelException(module.Model);
if (channel == null) throw new NotModelException(request.Model);

// 获取渠道指定的实现类型的服务
var openService = GetKeyedService<IThorImageService>(channel.Type);

if (openService == null) throw new Exception($"并未实现:{channel.Type} 的服务");

var response = await openService.CreateImage(module, new ThorPlatformOptions
var response = await openService.CreateImage(request, new ThorPlatformOptions
{
ApiKey = channel.Key,
Address = channel.Address,
Expand All @@ -135,11 +135,11 @@ await context.Response.WriteAsJsonAsync(new ThorImageCreateResponse
successful = response.Successful
});

await loggerService.CreateConsumeAsync(string.Format(ConsumerTemplate, rate, 0), module.Model,
await loggerService.CreateConsumeAsync(string.Format(ConsumerTemplate, rate, 0), request.Model,
0, 0, quota ?? 0, token?.Name, user?.UserName, user?.Id, channel.Id,
channel.Name);

await userService.ConsumeAsync(user!.Id, quota ?? 0, 0, token?.Key, channel.Id, module.Model);
await userService.ConsumeAsync(user!.Id, quota ?? 0, 0, token?.Key, channel.Id, request.Model);
}
catch (RateLimitException)
{
Expand Down Expand Up @@ -344,20 +344,20 @@ await userService.ConsumeAsync(user!.Id, (long)quota, requestToken, token?.Key,
return (requestToken, responseToken);
}

public async ValueTask ChatCompletionsAsync(HttpContext context, ChatCompletionCreateRequest module)
public async ValueTask ChatCompletionsAsync(HttpContext context, ThorChatCompletionsRequest request)
{
try
{
await rateLimitModelService.CheckAsync(module!.Model, context, serviceCache);
await rateLimitModelService.CheckAsync(request!.Model, context, serviceCache);

var (token, user) = await tokenService.CheckTokenAsync(context);

// 获取渠道通过算法计算权重
var channel = CalculateWeight(await channelService.GetChannelsContainsModelAsync(module.Model));
var channel = CalculateWeight(await channelService.GetChannelsContainsModelAsync(request.Model));

if (channel == null)
{
throw new NotModelException(module.Model);
throw new NotModelException(request.Model);
}

// 获取渠道指定的实现类型的服务
Expand All @@ -368,42 +368,42 @@ public async ValueTask ChatCompletionsAsync(HttpContext context, ChatCompletionC
throw new Exception($"并未实现:{channel.Type} 的服务");
}

if (SettingService.PromptRate.TryGetValue(module.Model, out var rate))
if (SettingService.PromptRate.TryGetValue(request.Model, out var rate))
{
int requestToken;
var responseToken = 0;

if (module.Stream == true)
if (request.Stream == true)
{
(requestToken, responseToken) =
await StreamChatCompletionsHandlerAsync(context, module, channel, chatCompletionsService, user, rate);
await StreamChatCompletionsHandlerAsync(context, request, channel, chatCompletionsService, user, rate);
}
else
{
(requestToken, responseToken) =
await ChatCompletionsHandlerAsync(context, module, channel, chatCompletionsService, user, rate);
await ChatCompletionsHandlerAsync(context, request, channel, chatCompletionsService, user, rate);

}
var quota = requestToken * rate;

var completionRatio = GetCompletionRatio(module.Model);
var completionRatio = GetCompletionRatio(request.Model);
quota += responseToken * rate * completionRatio;

// 将quota 四舍五入
quota = Math.Round(quota, 0, MidpointRounding.AwayFromZero);

await loggerService.CreateConsumeAsync(string.Format(ConsumerTemplate, rate, completionRatio),
module.Model,
request.Model,
requestToken, responseToken, (int)quota, token?.Name, user?.UserName, user?.Id, channel.Id,
channel.Name);

await userService.ConsumeAsync(user!.Id, (long)quota, requestToken, token?.Key, channel.Id,
module.Model);
request.Model);
}
else
{
context.Response.StatusCode = 200;
if (module.Stream == true)
if (request.Stream == true)
await context.WriteStreamErrorAsync("当前模型未设置倍率");
else
await context.WriteErrorAsync("当前模型未设置倍率");
Expand All @@ -420,15 +420,15 @@ await userService.ConsumeAsync(user!.Id, (long)quota, requestToken, token?.Key,
catch (OpenAIErrorException error)
{
context.Response.StatusCode = 400;
if (module.Stream == true)
if (request.Stream == true)
await context.WriteStreamErrorAsync(error.Message, error.Code);
else
await context.WriteErrorAsync(error.Message, error.Code);
}
catch (Exception e)
{
GetLogger<ChatService>().LogError(e.Message);
if (module.Stream == true)
if (request.Stream == true)
await context.WriteStreamErrorAsync(e.Message);
else
await context.WriteErrorAsync(e.Message);
Expand Down
30 changes: 12 additions & 18 deletions src/extensions/Thor.MetaGLM/Chats/MetaGLMChatCompletionsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
using Thor.Abstractions;
using Thor.Abstractions.Chats;
using Thor.Abstractions.Chats.Dtos;
using Thor.Abstractions.ObjectModels.ObjectModels.ResponseModels;
using Thor.MetaGLM.Models.RequestModels;
using Thor.MetaGLM.Models.RequestModels.FunctionModels;
using ChatCompletionsResponse = Thor.Abstractions.ObjectModels.ObjectModels.ResponseModels.ChatCompletionsResponse;
using ChatCompletionCreateResponse =
Thor.Abstractions.ObjectModels.ObjectModels.ResponseModels.ChatCompletionCreateResponse;

namespace Thor.MetaGLM.Chats;

Expand Down Expand Up @@ -101,29 +100,24 @@ public async Task<ChatCompletionsResponse> ChatCompletionsAsync(ThorChatCompleti
Type = x.type,
Function = new ThorChatMessageFunction()
{
Id = x.id,
Type = x.type,
FunctionCall = new FunctionCall()
{
Arguments = x.function?.arguments,
Name = x.function?.name,
}
}));
}
Arguments = x.function?.arguments,
Name = x.function?.name,
}
}));
}

return new ChatCompletionsResponse()
{
Choices =
[
new()
{
Delta = ThorChatMessage.CreateAssistantMessage(result.choices.FirstOrDefault()?.message.content ?? string.Empty,
null, tools),
Message = ThorChatMessage.CreateAssistantMessage(result.choices.FirstOrDefault()?.message.content ?? string.Empty, null, tools),
FinishReason = "stop",
Index = 0,
}
{
Delta = ThorChatMessage.CreateAssistantMessage(result.choices.FirstOrDefault()?.message.content ?? string.Empty,
null, tools),
Message = ThorChatMessage.CreateAssistantMessage(result.choices.FirstOrDefault()?.message.content ?? string.Empty, null, tools),
FinishReason = "stop",
Index = 0,
}
],
Error = new Error
{
Expand Down

0 comments on commit 7f184c6

Please sign in to comment.