Skip to content

Commit

Permalink
添加注释
Browse files Browse the repository at this point in the history
  • Loading branch information
DHclly committed Jul 16, 2024
1 parent 7d3c93d commit 8955c2c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Thor.Abstractions.Chats.Consts
/// <summary>
/// 支持图片识别的消息体内容类型
/// </summary>
internal class ThorMessageContentTypeConst
public class ThorMessageContentTypeConst
{
/// <summary>
/// 文本内容
Expand All @@ -20,5 +20,10 @@ internal class ThorMessageContentTypeConst
/// 图片 Url 类型
/// </summary>
public static string ImageUrl => "image_url";

/// <summary>
/// 图片 Url 类型
/// </summary>
public static string Image => "image";
}
}
11 changes: 11 additions & 0 deletions src/Thor.Abstractions/ThorPlatformOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ namespace Thor.Abstractions;
/// </summary>
public class ThorPlatformOptions
{
public ThorPlatformOptions()
{

}
public ThorPlatformOptions(string address, string apiKey, string? other = null)
{
this.Address = address;
this.ApiKey = apiKey;
this.Other = other;
}

/// <summary>
/// 对话平台基地址,如月之暗面的 https://api.moonshot.cn
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/Thor.Service/Infrastructure/Helper/TokenHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ static TokenHelper()
GptEncoding ??= GptEncoding.GetEncodingForModel("gpt-4");
}

/// <summary>
///
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public static int GetTotalTokens(params string[] content)
{
return content.Sum(item => GptEncoding.Encode(item).Count);
Expand Down
56 changes: 35 additions & 21 deletions src/Thor.Service/Service/ChatService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text;
using System.Text.Json;
using Thor.Abstractions.Chats;
using Thor.Abstractions.Chats.Consts;
using Thor.Abstractions.Chats.Dtos;
using Thor.Abstractions.Embeddings;
using Thor.Abstractions.Embeddings.Dtos;
Expand Down Expand Up @@ -344,6 +345,13 @@ await userService.ConsumeAsync(user!.Id, (long)quota, requestToken, token?.Key,
return (requestToken, responseToken);
}

/// <summary>
/// 对话补全调用
/// </summary>
/// <param name="context"></param>
/// <param name="request"></param>
/// <returns></returns>
/// <exception cref="NotModelException"></exception>
public async ValueTask ChatCompletionsAsync(HttpContext context, ThorChatCompletionsRequest request)
{
try
Expand Down Expand Up @@ -384,6 +392,7 @@ public async ValueTask ChatCompletionsAsync(HttpContext context, ThorChatComplet
await ChatCompletionsHandlerAsync(context, request, channel, chatCompletionsService, user, rate);

}

var quota = requestToken * rate;

var completionRatio = GetCompletionRatio(request.Model);
Expand Down Expand Up @@ -439,34 +448,34 @@ await userService.ConsumeAsync(user!.Id, (long)quota, requestToken, token?.Key,
/// 对话补全服务处理
/// </summary>
/// <param name="context"></param>
/// <param name="input"></param>
/// <param name="request"></param>
/// <param name="channel"></param>
/// <param name="openService"></param>
/// <param name="user"></param>
/// <param name="rate"></param>
/// <returns></returns>
/// <exception cref="InsufficientQuotaException"></exception>
private async ValueTask<(int requestToken, int responseToken)> ChatCompletionsHandlerAsync(HttpContext context, ThorChatCompletionsRequest input,
ChatChannel channel, IThorChatCompletionsService openService, User user, decimal rate)
private async ValueTask<(int requestToken, int responseToken)> ChatCompletionsHandlerAsync(
HttpContext context,
ThorChatCompletionsRequest request,
ChatChannel channel,
IThorChatCompletionsService openService,
User user,
decimal rate)
{
int requestToken;
int responseToken;

var setting = new ThorPlatformOptions
{
ApiKey = channel.Key,
Address = channel.Address,
Other = channel.Other
};
var setting = new ThorPlatformOptions(channel.Key, channel.Address, channel.Other);

// 这里应该用其他的方式来判断是否是vision模型,目前先这样处理
if (input.Messages.Any(x => x.Contents != null))
if (request.Messages.Any(x => x.Contents != null))
{
requestToken = TokenHelper.GetTotalTokens(input?.Messages.SelectMany(x => x.Contents)
requestToken = TokenHelper.GetTotalTokens(request.Messages.SelectMany(x => x.Contents)
.Where(x => x.Type == "text").Select(x => x.Text).ToArray());

// 解析图片
foreach (var message in input.Messages.SelectMany(x => x.Contents)
foreach (var message in request.Messages.SelectMany(x => x.Contents)
.Where(x => x.Type is "image" or "image_url"))
{
var imageUrl = message.ImageUrl;
Expand All @@ -493,22 +502,23 @@ await userService.ConsumeAsync(user!.Id, (long)quota, requestToken, token?.Key,
// 判断请求token数量是否超过额度
if (quota > user.ResidualCredit) throw new InsufficientQuotaException("账号余额不足请充值");

var result = await openService.ChatCompletionsAsync(input, setting);
var result = await openService.ChatCompletionsAsync(request, setting);

await context.Response.WriteAsJsonAsync(result);

responseToken = TokenHelper.GetTokens(result.Choices.FirstOrDefault()?.Delta.Content ?? string.Empty);
}
else
{
requestToken = TokenHelper.GetTotalTokens(input?.Messages.Select(x => x.Content).ToArray());
var contentArray = request.Messages.Select(x => x.Content).ToArray();
requestToken = TokenHelper.GetTotalTokens(contentArray);

var quota = requestToken * rate;

// 判断请求token数量是否超过额度
if (quota > user.ResidualCredit) throw new InsufficientQuotaException("账号余额不足请充值");

var result = await openService.ChatCompletionsAsync(input, setting);
var result = await openService.ChatCompletionsAsync(request, setting);

await context.Response.WriteAsJsonAsync(result);

Expand Down Expand Up @@ -631,20 +641,24 @@ await userService.ConsumeAsync(user!.Id, (long)quota, requestToken, token?.Key,
/// <returns></returns>
private static ChatChannel CalculateWeight(IEnumerable<ChatChannel> channel)
{
var chatChannels = channel as ChatChannel[] ?? channel.ToArray();
var total = chatChannels.Sum(x => x.Order);

if (chatChannels.Length == 0)
var chatChannels = channel.ToList();
if (chatChannels.Count == 0)
{
throw new NotModelException("模型未找到可用的渠道");
}

var value = Random.Shared.NextDouble() * total;
// 所有权重值之和
var total = chatChannels.Sum(x => x.Order);

var value = Convert.ToInt32(Random.Shared.NextDouble() * total);

foreach (var chatChannel in chatChannels)
{
value -= chatChannel.Order;
if (value <= 0) return chatChannel;
if (value <= 0)
{
return chatChannel;
}
}

return chatChannels.Last();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public async Task<ThorChatCompletionsResponse> ChatCompletionsAsync(
{
var client = HttpClient;

var response = await client.PostJsonAsync((options?.Address?.TrimEnd('/') ?? "") + "/api/chat", new OllamaChatCompletionsRequest()
var url = (options?.Address?.TrimEnd('/') ?? "") + "/api/chat";

var response = await client.PostJsonAsync(url, new OllamaChatCompletionsRequest()
{
stream = false,
model = request.Model ?? "",
Expand Down

0 comments on commit 8955c2c

Please sign in to comment.