feat: 支持未登录用户统计
This commit is contained in:
@@ -94,7 +94,7 @@ public class AiChatService : ApplicationService
|
|||||||
/// <param name="input"></param>
|
/// <param name="input"></param>
|
||||||
/// <param name="sessionId"></param>
|
/// <param name="sessionId"></param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
public async Task PostSendAsync([FromBody] ThorChatCompletionsRequest input, [FromRoute] Guid sessionId,
|
public async Task PostSendAsync([FromBody] ThorChatCompletionsRequest input, [FromRoute] Guid? sessionId,
|
||||||
CancellationToken cancellationToken)
|
CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
//除了免费模型,其他的模型都要校验
|
//除了免费模型,其他的模型都要校验
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ public class MessageAggregateRoot : FullAuditedAggregateRoot<Guid>
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public MessageAggregateRoot(Guid userId, Guid? sessionId, string content, string role, string modelId,
|
public MessageAggregateRoot(Guid? userId, Guid? sessionId, string content, string role, string modelId,
|
||||||
ThorUsageResponse? tokenUsage)
|
ThorUsageResponse? tokenUsage)
|
||||||
{
|
{
|
||||||
UserId = userId;
|
UserId = userId;
|
||||||
@@ -48,7 +48,7 @@ public class MessageAggregateRoot : FullAuditedAggregateRoot<Guid>
|
|||||||
this.MessageType = sessionId is null ? MessageTypeEnum.Api : MessageTypeEnum.Web;
|
this.MessageType = sessionId is null ? MessageTypeEnum.Api : MessageTypeEnum.Web;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Guid UserId { get; set; }
|
public Guid? UserId { get; set; }
|
||||||
public Guid? SessionId { get; set; }
|
public Guid? SessionId { get; set; }
|
||||||
|
|
||||||
[SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString)]
|
[SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString)]
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ public class UsageStatisticsAggregateRoot : FullAuditedAggregateRoot<Guid>
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public UsageStatisticsAggregateRoot(Guid userId, string modelId)
|
public UsageStatisticsAggregateRoot(Guid? userId, string modelId)
|
||||||
{
|
{
|
||||||
UserId = userId;
|
UserId = userId;
|
||||||
ModelId = modelId;
|
ModelId = modelId;
|
||||||
@@ -22,7 +22,7 @@ public class UsageStatisticsAggregateRoot : FullAuditedAggregateRoot<Guid>
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 用户id
|
/// 用户id
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Guid UserId { get; set; }
|
public Guid? UserId { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 哪个模型
|
/// 哪个模型
|
||||||
|
|||||||
@@ -242,25 +242,23 @@ public class AiGateWayManager : DomainService
|
|||||||
|
|
||||||
await outputTask;
|
await outputTask;
|
||||||
|
|
||||||
if (userId is not null)
|
|
||||||
{
|
|
||||||
await _aiMessageManager.CreateUserMessageAsync(userId.Value, sessionId,
|
|
||||||
new MessageInputDto
|
|
||||||
{
|
|
||||||
Content = request.Messages?.LastOrDefault()?.Content ?? string.Empty,
|
|
||||||
ModelId = request.Model,
|
|
||||||
TokenUsage = tokenUsage,
|
|
||||||
});
|
|
||||||
|
|
||||||
await _aiMessageManager.CreateSystemMessageAsync(userId.Value, sessionId,
|
await _aiMessageManager.CreateUserMessageAsync(userId, sessionId,
|
||||||
new MessageInputDto
|
new MessageInputDto
|
||||||
{
|
{
|
||||||
Content = backupSystemContent.ToString(),
|
Content = request.Messages?.LastOrDefault()?.Content ?? string.Empty,
|
||||||
ModelId = request.Model,
|
ModelId = request.Model,
|
||||||
TokenUsage = tokenUsage
|
TokenUsage = tokenUsage,
|
||||||
});
|
});
|
||||||
|
|
||||||
await _usageStatisticsManager.SetUsageAsync(userId.Value, request.Model, tokenUsage);
|
await _aiMessageManager.CreateSystemMessageAsync(userId, sessionId,
|
||||||
}
|
new MessageInputDto
|
||||||
|
{
|
||||||
|
Content = backupSystemContent.ToString(),
|
||||||
|
ModelId = request.Model,
|
||||||
|
TokenUsage = tokenUsage
|
||||||
|
});
|
||||||
|
|
||||||
|
await _usageStatisticsManager.SetUsageAsync(userId, request.Model, tokenUsage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -23,7 +23,7 @@ public class AiMessageManager : DomainService
|
|||||||
/// <param name="userId"></param>
|
/// <param name="userId"></param>
|
||||||
/// <param name="input"></param>
|
/// <param name="input"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task CreateSystemMessageAsync(Guid userId, Guid? sessionId, MessageInputDto input)
|
public async Task CreateSystemMessageAsync(Guid? userId, Guid? sessionId, MessageInputDto input)
|
||||||
{
|
{
|
||||||
input.Role = "system";
|
input.Role = "system";
|
||||||
var message = new MessageAggregateRoot(userId, sessionId, input.Content, input.Role, input.ModelId,input.TokenUsage);
|
var message = new MessageAggregateRoot(userId, sessionId, input.Content, input.Role, input.ModelId,input.TokenUsage);
|
||||||
@@ -37,7 +37,7 @@ public class AiMessageManager : DomainService
|
|||||||
/// <param name="userId"></param>
|
/// <param name="userId"></param>
|
||||||
/// <param name="input"></param>
|
/// <param name="input"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task CreateUserMessageAsync(Guid userId, Guid? sessionId, MessageInputDto input)
|
public async Task CreateUserMessageAsync(Guid? userId, Guid? sessionId, MessageInputDto input)
|
||||||
{
|
{
|
||||||
input.Role = "user";
|
input.Role = "user";
|
||||||
var message = new MessageAggregateRoot(userId, sessionId, input.Content, input.Role, input.ModelId,input.TokenUsage);
|
var message = new MessageAggregateRoot(userId, sessionId, input.Content, input.Role, input.ModelId,input.TokenUsage);
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ public class UsageStatisticsManager : DomainService
|
|||||||
private IDistributedLockProvider DistributedLock =>
|
private IDistributedLockProvider DistributedLock =>
|
||||||
LazyServiceProvider.LazyGetRequiredService<IDistributedLockProvider>();
|
LazyServiceProvider.LazyGetRequiredService<IDistributedLockProvider>();
|
||||||
|
|
||||||
public async Task SetUsageAsync(Guid userId, string modelId, ThorUsageResponse? tokenUsage)
|
public async Task SetUsageAsync(Guid? userId, string modelId, ThorUsageResponse? tokenUsage)
|
||||||
{
|
{
|
||||||
long inputTokenCount = tokenUsage?.PromptTokens
|
long inputTokenCount = tokenUsage?.PromptTokens
|
||||||
?? tokenUsage.InputTokens
|
?? tokenUsage.InputTokens
|
||||||
@@ -28,7 +28,7 @@ public class UsageStatisticsManager : DomainService
|
|||||||
?? tokenUsage.OutputTokens
|
?? tokenUsage.OutputTokens
|
||||||
?? 0;
|
?? 0;
|
||||||
|
|
||||||
await using (await DistributedLock.AcquireLockAsync($"UsageStatistics:{userId.ToString()}"))
|
await using (await DistributedLock.AcquireLockAsync($"UsageStatistics:{userId?.ToString()}"))
|
||||||
{
|
{
|
||||||
var entity = await _repository._DbQueryable.FirstAsync(x => x.UserId == userId && x.ModelId == modelId);
|
var entity = await _repository._DbQueryable.FirstAsync(x => x.UserId == userId && x.ModelId == modelId);
|
||||||
//存在数据,更细
|
//存在数据,更细
|
||||||
|
|||||||
Reference in New Issue
Block a user