Skip to content

Commit

Permalink
drop hunyuan sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
sdcb committed Feb 24, 2025
1 parent a03ee33 commit a798cbf
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 224 deletions.
3 changes: 2 additions & 1 deletion src/BE/Chats.BE.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
<PackageReference Include="Microsoft.ML.Tokenizers.Data.O200kBase" Version="1.0.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.3" />
<PackageReference Include="OpenAI" Version="2.1.0" />
<!-- todo -->
<PackageReference Include="Sdcb.DashScope" Version="2.0.0" />
<!-- todo -->
<PackageReference Include="Sdcb.WenXinQianFan" Version="1.2.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.6.0" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
<PackageReference Include="System.Runtime.Caching" Version="9.0.2" />
<PackageReference Include="TencentCloudSDK.Hunyuan" Version="3.0.1185" />
<PackageReference Include="TencentCloudSDK.Sms" Version="3.0.1185" />
</ItemGroup>

Expand Down
10 changes: 9 additions & 1 deletion src/BE/Controllers/Chats/Chats/ChatController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
using System.Text.Json;
using System.Threading.Channels;
using OpenAIChatMessage = OpenAI.Chat.ChatMessage;
using System;
using System.Reflection.Metadata.Ecma335;

namespace Chats.BE.Controllers.Chats.Chats;

Expand Down Expand Up @@ -451,6 +453,12 @@ private static async Task<ChatSpanResponse> ProcessChatSpan(
icc.FinishReason = DBFinishReason.Cancelled;
errorText = "Conversation cancelled";
}
catch (UriFormatException e)
{
icc.FinishReason = DBFinishReason.InvalidApiHostUrl;
errorText = e.Message;
logger.LogError(e, "Invalid URL in conversation for message: {userMessageId}", req.MessageId);
}
catch (Exception e)
{
icc.FinishReason = DBFinishReason.UnknownError;
Expand Down Expand Up @@ -489,7 +497,7 @@ private static async Task<ChatSpanResponse> ProcessChatSpan(
{
await writer.WriteAsync(SseResponseLine.Error(span.Id, errorText), cancellationToken);
}
dbAssistantMessage.Usage = icc.ToUserModelUsage(currentUser.Id, await clientInfoTask, isApi: false);
dbAssistantMessage.Usage = icc.ToUserModelUsage(currentUser.Id, userModel, await clientInfoTask, isApi: false);
await writer.WriteAsync(new SseResponseLine { Kind = SseResponseKind.End, Result = dbAssistantMessage, SpanId = span.Id }, cancellationToken);
writer.Complete();
return new ChatSpanResponse()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public async Task<ActionResult> ChatCompletion([FromBody] JsonObject json, [From
UserApiUsage usage = new()
{
ApiKeyId = currentApiKey.ApiKeyId,
Usage = icc.ToUserModelUsage(currentApiKey.User.Id, await clientInfoManager.GetClientInfo(cancellationToken), isApi: true),
Usage = icc.ToUserModelUsage(currentApiKey.User.Id, userModel, await clientInfoManager.GetClientInfo(cancellationToken), isApi: true),
};
db.UserApiUsages.Add(usage);
await db.SaveChangesAsync(cancellationToken);
Expand Down
1 change: 0 additions & 1 deletion src/BE/Services/Models/ChatFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Chats.BE.DB;
using Chats.BE.DB.Enums;
using Chats.BE.Services.Models.ChatServices.DashScope;
using Chats.BE.Services.Models.ChatServices.Hunyuan;
using Chats.BE.Services.Models.ChatServices.OpenAI;
using Chats.BE.Services.Models.ChatServices.QianFan;
using Chats.BE.Services.Models.ChatServices.Test;
Expand Down
127 changes: 0 additions & 127 deletions src/BE/Services/Models/ChatServices/Hunyuan/HunyuanChatService.cs

This file was deleted.

51 changes: 0 additions & 51 deletions src/BE/Services/Models/ChatServices/Hunyuan/HuyuanChatSegment.cs

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Chats.BE.DB;

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

public class HunyuanChatService(Model model) : OpenAIChatService(model, new Uri("https://api.hunyuan.cloud.tencent.com/v1"));
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public partial class OpenAIChatService : ChatService
{
private readonly ChatClient _chatClient;

public OpenAIChatService(Model model, Uri? enforcedApiHost = null) : base(model)
public OpenAIChatService(Model model, Uri? suggestedApiUrl = null) : base(model)
{
ArgumentException.ThrowIfNullOrWhiteSpace(model.ModelKey.Secret, nameof(model.ModelKey.Secret));

OpenAIClient api = new(new ApiKeyCredential(model.ModelKey.Secret!), new OpenAIClientOptions()
{
Endpoint = enforcedApiHost ?? (!string.IsNullOrWhiteSpace(model.ModelKey.Host) ? new Uri(model.ModelKey.Host) : null),
Endpoint = !string.IsNullOrWhiteSpace(model.ModelKey.Host) ? new Uri(model.ModelKey.Host) : suggestedApiUrl,
});
_chatClient = api.GetChatClient(model.ApiModelId);
}
Expand Down
3 changes: 2 additions & 1 deletion src/BE/Services/Models/DBFinishReason.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public enum DBFinishReason : byte
InvalidModel = 103,
SubscriptionExpired = 104,
BadParameter = 105,
Cancelled = 106
Cancelled = 106,
InvalidApiHostUrl = 107,
}
10 changes: 4 additions & 6 deletions src/BE/Services/Models/InChatContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class InChatContext(long firstTick)
private long _preprocessTick, _firstReasoningTick, _firstResponseTick, _endResponseTick, _finishTick;
private short _segmentCount;
public UserModelBalanceCost Cost { get; private set; } = UserModelBalanceCost.Empty;
private UserModel _userModel = null!;
private InternalChatSegment _lastSegment = InternalChatSegment.Empty;
private readonly StringBuilder _fullContent = new();
private readonly StringBuilder _fullReasoningContent = new();
Expand All @@ -24,7 +23,6 @@ public class InChatContext(long firstTick)
public async IAsyncEnumerable<InternalChatSegment> Run(decimal userBalance, UserModel userModel, IAsyncEnumerable<InternalChatSegment> segments)
{
_preprocessTick = _firstReasoningTick = _firstResponseTick = _endResponseTick = _finishTick = Stopwatch.GetTimestamp();
_userModel = userModel;
if (userModel.ExpiresAt.IsExpired())
{
throw new SubscriptionExpiredException(userModel.ExpiresAt);
Expand Down Expand Up @@ -94,14 +92,14 @@ public async IAsyncEnumerable<InternalChatSegment> Run(decimal userBalance, User

public int ReasoningDurationMs => (int)Stopwatch.GetElapsedTime(_firstReasoningTick, _firstResponseTick).TotalMilliseconds;

public UserModelUsage ToUserModelUsage(int userId, ClientInfo clientInfo, bool isApi)
public UserModelUsage ToUserModelUsage(int userId, UserModel userModel, ClientInfo clientInfo, bool isApi)
{
if (_finishTick == _preprocessTick) _finishTick = Stopwatch.GetTimestamp();

UserModelUsage usage = new()
{
UserModelId = _userModel.Id,
UserModel = _userModel,
UserModelId = userModel.Id,
UserModel = userModel,
CreatedAt = DateTime.UtcNow,
FinishReasonId = (byte)FinishReason,
SegmentCount = _segmentCount,
Expand Down Expand Up @@ -135,7 +133,7 @@ public UserModelUsage ToUserModelUsage(int userId, ClientInfo clientInfo, bool i
{
usage.UsageTransaction = new()
{
UserModelId = _userModel.Id,
UserModelId = userModel.Id,
CreditUserId = userId,
CreatedAt = usage.CreatedAt,
CountAmount = -Cost.CostCount,
Expand Down
17 changes: 17 additions & 0 deletions src/scripts/db-migration/2025/20250224-more-ds.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- Region Parameters
DECLARE @p0 SmallInt = 2
DECLARE @p1 VarChar(1000) = 'https://api.hunyuan.cloud.tencent.com/v1'
DECLARE @p2 VarChar(1000) = 'sk-'
-- EndRegion
UPDATE [ModelProvider]
SET [InitialHost] = @p1, [InitialSecret] = @p2
WHERE [Id] = @p0
GO

-- Region Parameters
DECLARE @p0 TinyInt = 107
DECLARE @p1 VarChar(1000) = 'InvalidApiHostUrl'
-- EndRegion
INSERT INTO [FinishReason]([Id], [Name])
VALUES (@p0, @p1)
GO

0 comments on commit a798cbf

Please sign in to comment.