Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
sdcb committed Dec 6, 2024
2 parents 475338a + fae4590 commit 7414e1b
Show file tree
Hide file tree
Showing 71 changed files with 349 additions and 325 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Chats.BE.Services.Conversations.Extensions;
using Chats.BE.Services.ChatServices.Extensions;
using OpenAI.Chat;
using System.Runtime.CompilerServices;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Chats.BE.Controllers.Admin.AdminMessage.Dtos;
using Chats.BE.Controllers.Admin.Common;
using Chats.BE.Controllers.Chats.Conversations.Dtos;
using Chats.BE.Controllers.Chats.Chats.Dtos;
using Chats.BE.Controllers.Common.Dtos;
using Chats.BE.DB;
using Chats.BE.DB.Enums;
using Chats.BE.DB.Jsons;
using Chats.BE.Infrastructure;
using Chats.BE.Services.Conversations;
using Chats.BE.Services.ChatServices;
using Chats.BE.Services.FileServices;
using Chats.BE.Services.UrlEncryption;
using Microsoft.AspNetCore.Mvc;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Chats.BE.Controllers.Chats.Conversations.Dtos;
using Chats.BE.Controllers.Chats.Chats.Dtos;
using Chats.BE.Controllers.Chats.Messages.Dtos;
using Chats.BE.DB;
using Chats.BE.Services.Conversations;
using Chats.BE.Services.ChatServices;
using Chats.BE.Services.FileServices;
using Chats.BE.Services.UrlEncryption;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -39,7 +39,7 @@ public AdminMessageRoot ToDto(AdminMessageBasicItem[] messages)
{
Name = Name,
ModelName = ModelName,
ModelTemperature = Temperature ?? ConversationService.DefaultTemperature,
ModelTemperature = Temperature ?? ChatService.DefaultTemperature,
ModelPrompt = messages.FirstOrDefault(x => x.Role.Equals(DBChatRole.System.ToString(), StringComparison.OrdinalIgnoreCase))?.Content.Text,
Messages = messages.Where(x => !x.Role.Equals(DBChatRole.System.ToString(), StringComparison.OrdinalIgnoreCase)).ToArray(),
};
Expand Down
4 changes: 2 additions & 2 deletions src/BE/Controllers/Admin/AdminModels/AdminModelsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Chats.BE.DB.Jsons;
using Chats.BE.Infrastructure;
using Chats.BE.Services;
using Chats.BE.Services.Conversations;
using Chats.BE.Services.ChatServices;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -182,7 +182,7 @@ public async Task<ActionResult> DeleteModel(short modelId, CancellationToken can
[HttpPost("models/validate")]
public async Task<ActionResult<ModelValidateResult>> ValidateModel(
[FromBody] ValidateModelRequest req,
[FromServices] ConversationFactory conversationFactory,
[FromServices] ChatFactory conversationFactory,
CancellationToken cancellationToken)
{
ModelKey? modelKey = await db.ModelKeys
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Chats.BE.DB;
using Chats.BE.Services.Conversations;
using Chats.BE.Services.ChatServices;
using System.Text.Json.Serialization;

namespace Chats.BE.Controllers.Admin.ModelKeys.Dtos;
Expand Down
4 changes: 2 additions & 2 deletions src/BE/Controllers/Admin/ModelKeys/ModelKeysController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Chats.BE.Controllers.Common;
using Chats.BE.DB;
using Chats.BE.Services.Common;
using Chats.BE.Services.Conversations;
using Chats.BE.Services.ChatServices;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

Expand Down Expand Up @@ -115,7 +115,7 @@ public async Task<ActionResult> DeleteModelKey(short modelKeyId, CancellationTok
}

[HttpPost("{modelKeyId:int}/auto-create-models")]
public async Task<ActionResult<AutoCreateModelResult[]>> AutoCreateModels(short modelKeyId, [FromServices] ConversationFactory conversationFactory, CancellationToken cancellationToken)
public async Task<ActionResult<AutoCreateModelResult[]>> AutoCreateModels(short modelKeyId, [FromServices] ChatFactory conversationFactory, CancellationToken cancellationToken)
{
ModelKey? modelKey = await db
.ModelKeys
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Chats.BE.Controllers.Chats.Conversations.Dtos;
using Chats.BE.Controllers.Chats.Chats.Dtos;
using Chats.BE.Controllers.Common;
using Chats.BE.DB;
using Chats.BE.DB.Jsons;
using Chats.BE.Infrastructure;
using Chats.BE.Services;
using Chats.BE.Services.Conversations;
using Chats.BE.Services.Conversations.Dtos;
using Chats.BE.Services.ChatServices;
using Chats.BE.Services.ChatServices.Dtos;
using Chats.BE.Services.FileServices;
using Chats.BE.Services.UrlEncryption;
using Microsoft.AspNetCore.Authorization;
Expand All @@ -18,16 +18,16 @@
using TencentCloud.Common;
using OpenAIChatMessage = OpenAI.Chat.ChatMessage;

namespace Chats.BE.Controllers.Chats.Conversations;
namespace Chats.BE.Controllers.Chats.Chats;

[Route("api/chats"), Authorize]
public class ConversationController(ChatsDB db, CurrentUser currentUser, ILogger<ConversationController> logger, IUrlEncryptionService idEncryption) : ControllerBase
public class ChatController(ChatsDB db, CurrentUser currentUser, ILogger<ChatController> logger, IUrlEncryptionService idEncryption) : ControllerBase
{
[HttpPost]
public async Task<IActionResult> StartConversationStreamed(
[FromBody] ConversationRequest request,
[FromBody] ChatRequest request,
[FromServices] BalanceService balanceService,
[FromServices] ConversationFactory conversationFactory,
[FromServices] ChatFactory conversationFactory,
[FromServices] UserModelManager userModelManager,
[FromServices] ClientInfoManager clientInfoManager,
[FromServices] FileUrlProvider fileDownloadUrlProvider,
Expand Down Expand Up @@ -155,7 +155,7 @@ ..await GetMessageTree(existingMessages, messageId).ToAsyncEnumerable().SelectAw
throw new InvalidModelException(request.ModelId.ToString());
}

using ConversationService s = conversationFactory.CreateConversationService(userModel.Model);
using ChatService s = conversationFactory.CreateConversationService(userModel.Model);
ChatCompletionOptions cco = new()
{
Temperature = request.UserModelConfig.Temperature,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using Chats.BE.DB.Jsons;
using System.Text.Json.Serialization;

namespace Chats.BE.Controllers.Chats.Conversations.Dtos;
namespace Chats.BE.Controllers.Chats.Chats.Dtos;

public record ConversationRequest
public record ChatRequest
{
[JsonPropertyName("modelId")]
public required short ModelId { get; init; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Chats.BE.DB;
using Chats.BE.DB.Enums;
using Chats.BE.DB.Extensions;
using Chats.BE.Services.Conversations;
using Chats.BE.Services.ChatServices;
using Chats.BE.Services.FileServices;
using OpenAI.Chat;

namespace Chats.BE.Controllers.Chats.Conversations.Dtos;
namespace Chats.BE.Controllers.Chats.Chats.Dtos;

public record MessageLiteDto
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;

namespace Chats.BE.Controllers.Chats.Conversations.Dtos;
namespace Chats.BE.Controllers.Chats.Chats.Dtos;

public record SseResponseLine
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Chats.BE.Services.Conversations;
using Chats.BE.Services.ChatServices;

namespace Chats.BE.Controllers.Chats.Conversations;
namespace Chats.BE.Controllers.Chats.Chats;

public abstract class ChatServiceException(DBFinishReason errorCode) : Exception
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Chats.BE.DB;
using Chats.BE.DB.Jsons;

namespace Chats.BE.Controllers.Chats.Conversations;
namespace Chats.BE.Controllers.Chats.Chats;

public record UserModelBalanceCost(int Counts, int Tokens, decimal Balance, int CostCount, int CostTokens, decimal InputTokenPrice, decimal OutputTokenPrice)
{
Expand Down
2 changes: 1 addition & 1 deletion src/BE/Controllers/Chats/Messages/Dtos/MessageDto.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Chats.BE.DB;
using Chats.BE.DB.Enums;
using Chats.BE.Services.Conversations;
using Chats.BE.Services.ChatServices;
using Chats.BE.Services.FileServices;
using Chats.BE.Services.UrlEncryption;
using System.Text.Json.Serialization;
Expand Down
2 changes: 1 addition & 1 deletion src/BE/Controllers/Chats/Messages/MessagesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Chats.BE.DB;
using Chats.BE.DB.Enums;
using Chats.BE.Infrastructure;
using Chats.BE.Services.Conversations;
using Chats.BE.Services.ChatServices;
using Chats.BE.Services.FileServices;
using Chats.BE.Services.UrlEncryption;
using Microsoft.AspNetCore.Authorization;
Expand Down
6 changes: 3 additions & 3 deletions src/BE/Controllers/Chats/Prompts/PromptsController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Chats.BE.Controllers.Chats.Prompts.Dtos;
using Chats.BE.DB;
using Chats.BE.Infrastructure;
using Chats.BE.Services.Conversations;
using Chats.BE.Services.ChatServices;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -97,8 +97,8 @@ public async Task<ActionResult<PromptDto>> GetDefaultPrompt(CancellationToken ca
IsSystem = consolidated.IsSystem,
} : new PromptDto()
{
Content = ConversationService.DefaultPrompt,
Temperature = ConversationService.DefaultTemperature,
Content = ChatService.DefaultPrompt,
Temperature = ChatService.DefaultTemperature,
Id = -1,
IsDefault = true,
Name = "Default",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Chats.BE.DB;
using Chats.BE.DB.Enums;
using Chats.BE.DB.Jsons;
using Chats.BE.Services.Conversations;
using Chats.BE.Services.ChatServices;
using Chats.BE.Services.UrlEncryption;
using System.Text.Json.Serialization;

namespace Chats.BE.Controllers.Chats.Chats.Dtos;
namespace Chats.BE.Controllers.Chats.UserChats.Dtos;

public record ChatsResponse
{
Expand Down Expand Up @@ -46,9 +46,9 @@ public static ChatsResponse FromDB(Chat chat, IUrlEncryptionService idEncryption
DeploymentName = chat.Model.DeploymentName,
EnableSearch = chat.Model.ModelReference.AllowSearch,
MaxLength = chat.Model.ModelReference.MaxResponseTokens,
Temperature = ConversationService.DefaultTemperature,
Temperature = ChatService.DefaultTemperature,
Version = chat.Model.ModelKey.ModelProvider.Name,
Prompt = ConversationService.DefaultPrompt,
Prompt = ChatService.DefaultPrompt,
},
UserModelConfig = new JsonUserModelConfig { EnableSearch = chat.EnableSearch, Temperature = chat.Temperature },
IsShared = chat.IsShared,
Expand Down Expand Up @@ -87,8 +87,8 @@ public ChatsResponse ToResponse(IUrlEncryptionService idEncryption)
ModelName = ModelName,
ModelConfig = new JsonModelConfig
{
Prompt = ConversationService.DefaultPrompt,
Temperature = Temperature ?? ConversationService.DefaultTemperature,
Prompt = ChatService.DefaultPrompt,
Temperature = Temperature ?? ChatService.DefaultTemperature,
},
UserModelConfig = UserModelConfig,
IsShared = IsShared,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;

namespace Chats.BE.Controllers.Chats.Chats.Dtos;
namespace Chats.BE.Controllers.Chats.UserChats.Dtos;

public record CreateChatsRequest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Chats.BE.Controllers.Chats.Chats.Dtos;
namespace Chats.BE.Controllers.Chats.UserChats.Dtos;

public class UpdateChatsRequest
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Chats.BE.Controllers.Chats.Chats.Dtos;
using Chats.BE.Controllers.Chats.UserChats.Dtos;
using Chats.BE.Controllers.Common;
using Chats.BE.Controllers.Common.Dtos;
using Chats.BE.DB;
Expand All @@ -7,13 +7,13 @@
using Chats.BE.Infrastructure;
using Chats.BE.Services;
using Chats.BE.Services.Common;
using Chats.BE.Services.Conversations;
using Chats.BE.Services.ChatServices;
using Chats.BE.Services.UrlEncryption;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Chats.BE.Controllers.Chats.Chats;
namespace Chats.BE.Controllers.Chats.UserChats;

[Route("api/user/chats"), Authorize]
public class UserChatsController(ChatsDB db, CurrentUser currentUser, IUrlEncryptionService idEncryption) : ControllerBase
Expand Down Expand Up @@ -69,7 +69,7 @@ public async Task<ActionResult<PagedResult<ChatsResponse>>> GetChats([FromQuery]
ChatModelId = x.ModelId,
ModelName = x.Model!.Name,
EnableSearch = x.EnableSearch,
Temperature = ConversationService.DefaultTemperature,
Temperature = ChatService.DefaultTemperature,
IsShared = x.IsShared,
ModelProvider = (DBModelProvider)x.Model.ModelKey.ModelProviderId,
UserModelConfig = new JsonUserModelConfig
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Chats.BE.Controllers.Chats.Conversations;
using Chats.BE.Controllers.Chats.Chats;
using Chats.BE.DB;
using Chats.BE.Services;
using Chats.BE.Services.Conversations;
using Chats.BE.Services.Conversations.Dtos;
using Chats.BE.Services.ChatServices;
using Chats.BE.Services.ChatServices.Dtos;
using Chats.BE.Services.OpenAIApiKeySession;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -16,7 +16,7 @@
namespace Chats.BE.Controllers.OpenAICompatible;

[Route("v1"), Authorize(AuthenticationSchemes = "OpenAIApiKey")]
public partial class OpenAICompatibleController(ChatsDB db, CurrentApiKey currentApiKey, ConversationFactory cf, UserModelManager userModelManager, ILogger<OpenAICompatibleController> logger, BalanceService balanceService) : ControllerBase
public partial class OpenAICompatibleController(ChatsDB db, CurrentApiKey currentApiKey, ChatFactory cf, UserModelManager userModelManager, ILogger<OpenAICompatibleController> logger, BalanceService balanceService) : ControllerBase
{
[HttpPost("chat/completions")]
public async Task<ActionResult> ChatCompletion([FromBody] JsonObject json, [FromServices] ClientInfoManager clientInfoManager, CancellationToken cancellationToken)
Expand All @@ -36,7 +36,7 @@ public async Task<ActionResult> ChatCompletion([FromBody] JsonObject json, [From
if (userModel == null) return InvalidModel(cco.Model);

Model cm = userModel.Model;
using ConversationService s = cf.CreateConversationService(cm);
using ChatService s = cf.CreateConversationService(cm);

UserBalance userBalance = await db.UserBalances
.Where(x => x.UserId == currentApiKey.User.Id)
Expand Down
11 changes: 6 additions & 5 deletions src/BE/DB/Enums/DBModelProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
public enum DBModelProvider
{
Test = 0,
Azure = 1,
AzureOpenAI = 1,
HunYuan = 2,
LingYi = 3,
Moonshot = 4,
OpenAI = 5,
QianFan = 6,
QianWen = 7,
Spark = 8,
ZhiPuAI = 9
WenXinQianFan = 6,
AliyunDashscope = 7,
Sparkdesk = 8,
ZhiPuAI = 9,
DeepSeek = 10,
}
Loading

0 comments on commit 7414e1b

Please sign in to comment.