Skip to content

Commit

Permalink
Merge pull request #47 from sdcb/dev
Browse files Browse the repository at this point in the history
deepseek-r1, db refactor
  • Loading branch information
sdcb authored Jan 21, 2025
2 parents 9ce6ae5 + 6d381ec commit 31f7dba
Show file tree
Hide file tree
Showing 11 changed files with 491 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task<ActionResult<AdminModelDto[]>> GetAdminModels(bool all, Cancel
ModelProviderId = x.ModelKey.ModelProviderId,
ModelReferenceId = x.ModelReferenceId,
ModelReferenceName = x.ModelReference.Name,
ModelReferenceShortName = x.ModelReference.ShortName,
ModelReferenceShortName = x.ModelReference.DisplayName,
InputTokenPrice1M = x.InputTokenPrice1M,
OutputTokenPrice1M = x.OutputTokenPrice1M,
Rank = x.Order,
Expand Down Expand Up @@ -207,6 +207,7 @@ public async Task<ActionResult<UserModelDto[]>> GetUserModels(int userId, Cancel
UserModelDto[] userModels = await db.Models
.Where(x => !x.IsDeleted)
.OrderBy(x => x.Order)
.ThenByDescending(x => x.Id)
.Select(x => new
{
Model = x,
Expand Down
8 changes: 6 additions & 2 deletions src/BE/Controllers/Admin/ModelKeys/ModelKeysController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ public async Task<ActionResult<PossibleModelDto[]>> ListModelKeyPossibleModels(s
DeploymentName = model,
ReferenceId = modelReferenceId,
ReferenceName = referenceOptions[modelReferenceId].Name,
IsLegacy = referenceOptions[modelReferenceId].IsLegacy,
IsLegacy = referenceOptions[modelReferenceId].PublishDate switch
{
var x when x < new DateOnly(2024, 7, 1) => true,
_ => false
},
IsExists = existsDeploymentNames.Contains(model),
};
}));
Expand Down Expand Up @@ -192,7 +196,7 @@ public async Task<ActionResult<PossibleModelDto[]>> ListModelKeyPossibleModels(s
DeploymentName = x.Models.FirstOrDefault(m => m.ModelKeyId == modelKeyId)!.DeploymentName,
ReferenceId = x.Id,
ReferenceName = x.Name,
IsLegacy = x.IsLegacy,
IsLegacy = x.PublishDate == null || x.PublishDate < new DateOnly(2024, 7, 1),
IsExists = x.Models.Any(m => m.ModelKeyId == modelKeyId),
})
.OrderBy(x => (x.IsLegacy ? 1 : 0) + (x.IsExists ? 2 : 0))
Expand Down
2 changes: 1 addition & 1 deletion src/BE/Controllers/Chats/Models/ModelsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task<ActionResult<AdminModelDto[]>> Get([FromServices] ChatsDB db,
ModelProviderId = x.ModelKey.ModelProviderId,
ModelReferenceId = x.ModelReferenceId,
ModelReferenceName = x.ModelReference.Name,
ModelReferenceShortName = x.ModelReference.ShortName,
ModelReferenceShortName = x.ModelReference.DisplayName,
InputTokenPrice1M = x.InputTokenPrice1M,
OutputTokenPrice1M = x.OutputTokenPrice1M,
Rank = x.Order,
Expand Down
327 changes: 164 additions & 163 deletions src/BE/DB/Init/BasicData.cs

Large diffs are not rendered by default.

21 changes: 20 additions & 1 deletion src/BE/DB/Init/scaffold-basic-data.linq
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ static int GetValueDisplayLength(object? value)
null => 4,
string str => GetStringDisplayLength(str),
decimal d => d.ToString().Length + 1,
DateOnly d => 26,
not null => value.ToString()!.Length,
};

Expand Down Expand Up @@ -160,7 +161,7 @@ static int MaxConsecutiveChar(string s, char c)
return maxCount;
}

static LiteralExpressionSyntax ValueToLiteral(object? value)
static ExpressionSyntax ValueToLiteral(object? value)
{
return value switch
{
Expand All @@ -180,6 +181,24 @@ static LiteralExpressionSyntax ValueToLiteral(object? value)
? SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression)
: SyntaxFactory.LiteralExpression(SyntaxKind.FalseLiteralExpression),

DateOnly date => ObjectCreationExpression(
Token(SyntaxKind.NewKeyword).WithTrailingTrivia(Whitespace(" ")),
IdentifierName("DateOnly"),
ArgumentList(
Token(SyntaxKind.OpenParenToken),
SeparatedList<ArgumentSyntax>(
[
Argument(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(date.Year))),
Argument(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(date.Month).WithLeadingTrivia(Whitespace(date.Month < 10 ? " " : "")))),
Argument(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(date.Day).WithLeadingTrivia(Whitespace(date.Day < 10 ? " " : "")))),
],
[
Token(SyntaxKind.CommaToken).WithTrailingTrivia(Whitespace(" ")),
Token(SyntaxKind.CommaToken).WithTrailingTrivia(Whitespace(" ")),
]),
Token(SyntaxKind.CloseParenToken)),
null),

string stringValue => stringValue switch
{
var x when x.Contains('"') => CreateRawStringLiteralExpression(stringValue),
Expand Down
2 changes: 2 additions & 0 deletions src/BE/DB/ModelProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public partial class ModelProvider
[Unicode(false)]
public string? InitialSecret { get; set; }

public bool RequireDeploymentName { get; set; }

[InverseProperty("ModelProvider")]
public virtual ICollection<ModelKey> ModelKeys { get; set; } = new List<ModelKey>();

Expand Down
4 changes: 2 additions & 2 deletions src/BE/DB/ModelReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public partial class ModelReference
public string Name { get; set; } = null!;

[StringLength(50)]
public string? ShortName { get; set; }
public string? DisplayName { get; set; }

public bool IsLegacy { get; set; }
public DateOnly? PublishDate { get; set; }

[Column(TypeName = "decimal(3, 2)")]
public decimal MinTemperature { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/BE/Services/Models/ChatServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected virtual async Task<ChatMessage[]> FEPreprocess(IReadOnlyList<ChatMessa
{
existingSystemPrompt.Content[0] = existingSystemPrompt.Content[0].Text
.Replace("{{CURRENT_DATE}}", now.ToString("yyyy/MM/dd"))
.Replace("{{MODEL_NAME}}", Model.ModelReference.ShortName ?? Model.ModelReference.Name)
.Replace("{{MODEL_NAME}}", Model.ModelReference.DisplayName ?? Model.ModelReference.Name)
.Replace("{{CURRENT_TIME}}", now.ToString("HH:mm:ss"));
;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
using Chats.BE.DB;
using OpenAI.Chat;

namespace Chats.BE.Services.Models.ChatServices.OpenAI;

public class DeepSeekChatService(Model model) : OpenAIChatService(model, new Uri("https://api.deepseek.com/v1"));
public class DeepSeekChatService(Model model) : OpenAIChatService(model, new Uri("https://api.deepseek.com/v1"))
{
protected override Task<ChatMessage[]> FEPreprocess(IReadOnlyList<ChatMessage> messages, ChatCompletionOptions options, ChatExtraDetails feOptions, CancellationToken cancellationToken)
{
if (Model.ModelReference.Name == "deepseek-reasoner")
{
// deepseek-reasoner model does not support temperature
options.Temperature = null;
}
return base.FEPreprocess(messages, options, feOptions, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Chats.BE.Services.Models.ChatServices.OpenAI;
{
protected override Task<ChatMessage[]> FEPreprocess(IReadOnlyList<ChatMessage> messages, ChatCompletionOptions options, ChatExtraDetails feOptions, CancellationToken cancellationToken)
{
if (Model.ModelReference.ShortName == "Mistral")
if (Model.ModelReference.DisplayName == "Mistral")
{
// Mistral model does not support end-user ID
options.EndUserId = null;
Expand Down
Loading

0 comments on commit 31f7dba

Please sign in to comment.