Skip to content

Commit

Permalink
Merge branch 'dev' of private:sdcb/chats into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
greywen committed Dec 11, 2024
2 parents 127e235 + 1633075 commit e52187a
Show file tree
Hide file tree
Showing 20 changed files with 59 additions and 133 deletions.
8 changes: 4 additions & 4 deletions src/BE/Chats.BE.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemGroup>
<PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.14.1" />
<PackageReference Include="AWSSDK.S3" Version="3.7.410.1" />
<PackageReference Include="AWSSDK.S3" Version="3.7.410.3" />
<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.23.0" /> <!-- fix security issue from Azure.AI.OpenAI -->
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
Expand All @@ -26,12 +26,12 @@
<PackageReference Include="OpenAI" Version="2.1.0" />
<PackageReference Include="Sdcb.DashScope" Version="2.0.0" />
<PackageReference Include="Sdcb.WenXinQianFan" Version="1.2.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.3.0" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
<PackageReference Include="System.Runtime.Caching" Version="9.0.0" />
<PackageReference Include="TencentCloudSDK.Hunyuan" Version="3.0.1137" />
<PackageReference Include="TencentCloudSDK.Sms" Version="3.0.1137" />
<PackageReference Include="TencentCloudSDK.Hunyuan" Version="3.0.1139" />
<PackageReference Include="TencentCloudSDK.Sms" Version="3.0.1139" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions src/BE/DB/Enums/DBModelProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public enum DBModelProvider
Sparkdesk = 8,
ZhiPuAI = 9,
DeepSeek = 10,
xAI = 11,
GithubModels = 12,
}
28 changes: 14 additions & 14 deletions src/BE/DB/Init/BasicData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,22 +247,22 @@ private static void InsertModelReferences(ChatsDB db)

private static void InsertModelProviders(ChatsDB db)
{
// Generated from data, hash: 918110f3f52c51c8fdebe00ca4e2e9dac5d2f2e7f892ed5c7197be6a7fbcfbdf
// Generated from data, hash: 615290985867eb3467e7889a31c4edf5dcdbd113930d67df8e19b6cd27c955ca
db.ModelProviders.AddRange(
[
new(){ Id=0, Name="Test", InitialHost=null, InitialSecret=null, },
new(){ Id=1, Name="Azure OpenAI", InitialHost="https://<resource-name>.openai.azure.com/", InitialSecret="", },
new(){ Id=2, Name="Tencent Hunyuan", InitialHost="hunyuan.tencentcloudapi.com", InitialSecret="""{"secretId":"", "secretKey":""}""", },
new(){ Id=3, Name="01.ai", InitialHost=null, InitialSecret="", },
new(){ Id=4, Name="Moonshot", InitialHost=null, InitialSecret="", },
new(){ Id=5, Name="OpenAI", InitialHost="https://api.openai.com/v1", InitialSecret="", },
new(){ Id=6, Name="Wenxin Qianfan", InitialHost=null, InitialSecret="""{"apiKey":"", "secret":""}""", },
new(){ Id=7, Name="DashScope", InitialHost=null, InitialSecret="", },
new(){ Id=8, Name="Xunfei SparkDesk", InitialHost=null, InitialSecret="""{"appId": "", "apiKey":"", "secret":""}""", },
new(){ Id=9, Name="Zhipu AI", InitialHost=null, InitialSecret="", },
new(){ Id=10, Name="DeepSeek", InitialHost=null, InitialSecret="", },
new(){ Id=11, Name="x.ai", InitialHost=null, InitialSecret="xai-yourkey", },
new(){ Id=12, Name="Github Models", InitialHost=null, InitialSecret="ghp_yourkey", }
new(){ Id=0, Name="Test", InitialHost=null, InitialSecret=null, },
new(){ Id=1, Name="Azure OpenAI", InitialHost="https://<resource-name>.openai.azure.com/", InitialSecret="", },
new(){ Id=2, Name="Tencent Hunyuan", InitialHost="hunyuan.tencentcloudapi.com", InitialSecret="""{"secretId":"", "secretKey":""}""", },
new(){ Id=3, Name="01.ai", InitialHost=null, InitialSecret="", },
new(){ Id=4, Name="Moonshot", InitialHost=null, InitialSecret="", },
new(){ Id=5, Name="OpenAI", InitialHost="https://api.openai.com/v1", InitialSecret="", },
new(){ Id=6, Name="Wenxin Qianfan", InitialHost=null, InitialSecret="""{"apiKey":"", "secret":""}""", },
new(){ Id=7, Name="DashScope", InitialHost=null, InitialSecret="", },
new(){ Id=8, Name="Xunfei SparkDesk", InitialHost=null, InitialSecret="", },
new(){ Id=9, Name="Zhipu AI", InitialHost=null, InitialSecret="", },
new(){ Id=10, Name="DeepSeek", InitialHost=null, InitialSecret="", },
new(){ Id=11, Name="x.ai", InitialHost=null, InitialSecret="xai-yourkey", },
new(){ Id=12, Name="Github Models", InitialHost=null, InitialSecret="ghp_yourkey", }
]);
}
};
9 changes: 3 additions & 6 deletions src/BE/Services/ChatServices/ChatFactory.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
using Chats.BE.DB;
using Chats.BE.DB.Enums;
using Chats.BE.Services.ChatServices.Implementations.Azure;
using Chats.BE.Services.ChatServices.Implementations.DashScope;
using Chats.BE.Services.ChatServices.Implementations.DeepSeek;
using Chats.BE.Services.ChatServices.Implementations.GLM;
using Chats.BE.Services.ChatServices.Implementations.Hunyuan;
using Chats.BE.Services.ChatServices.Implementations.Kimi;
using Chats.BE.Services.ChatServices.Implementations.LingYi;
using Chats.BE.Services.ChatServices.Implementations.OpenAI;
using Chats.BE.Services.ChatServices.Implementations.QianFan;
using Chats.BE.Services.ChatServices.Implementations.Test;
Expand All @@ -29,9 +24,11 @@ public ChatService CreateConversationService(Model model)
DBModelProvider.ZhiPuAI => new GLMChatService(model),
DBModelProvider.Moonshot => new KimiChatService(model),
DBModelProvider.HunYuan => new HunyuanChatService(model),
DBModelProvider.Sparkdesk => throw new NotImplementedException("Spark model is not implemented"),
DBModelProvider.Sparkdesk => new SparkDeskChatService(model),
DBModelProvider.LingYi => new LingYiChatService(model),
DBModelProvider.DeepSeek => new DeepSeekChatService(model),
DBModelProvider.xAI => new XAIChatService(model),
DBModelProvider.GithubModels => new GithubModelsChatService(model),
_ => throw new NotSupportedException($"Unknown model provider: {modelProvider}")
};
return cs;
Expand Down
8 changes: 2 additions & 6 deletions src/BE/Services/ChatServices/ChatServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public abstract partial class ChatService
{
public async IAsyncEnumerable<InternalChatSegment> ChatStreamedFEProcessed(IReadOnlyList<ChatMessage> messages, ChatCompletionOptions options, [EnumeratorCancellation] CancellationToken cancellationToken)
{
ChatMessage[] filteredMessage = FEProcessMessages(messages, options);
ChatMessage[] filteredMessage = FEPreprocess(messages, options);

await foreach (InternalChatSegment seg in ChatStreamedSimulated(suggestedStreaming: true, filteredMessage, options, cancellationToken))
{
Expand Down Expand Up @@ -48,7 +48,7 @@ public async IAsyncEnumerable<InternalChatSegment> ChatStreamedSimulated(bool su
}
}

private ChatMessage[] FEProcessMessages(IReadOnlyList<ChatMessage> messages, ChatCompletionOptions options)
protected virtual ChatMessage[] FEPreprocess(IReadOnlyList<ChatMessage> messages, ChatCompletionOptions options)
{
if (!Model.ModelReference.AllowSystemPrompt)
{
Expand All @@ -66,10 +66,6 @@ private ChatMessage[] FEProcessMessages(IReadOnlyList<ChatMessage> messages, Cha
}

ChatMessage[] filteredMessage = messages.Select(m => FilterVision(Model.ModelReference.AllowVision, m)).ToArray();
if (Model.ModelReference.AllowVision)
{
options.MaxOutputTokenCount ??= Model.ModelReference.MaxResponseTokens;
}
if (!Model.ModelReference.AllowSearch)
{
options.RemoveAllowSearch();
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using Azure.AI.OpenAI;
using Chats.BE.DB;
using Chats.BE.Services.ChatServices.Implementations.OpenAI;
using OpenAI;
using OpenAI.Chat;
using System.ClientModel;

namespace Chats.BE.Services.ChatServices.Implementations.Azure;
namespace Chats.BE.Services.ChatServices.Implementations.OpenAI;

public class AzureChatService(Model model) : OpenAIChatService(model, CreateChatClient(model))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Chats.BE.DB;
using Chats.BE.Services.ChatServices.Implementations.OpenAI;

namespace Chats.BE.Services.ChatServices.Implementations.DeepSeek;
namespace Chats.BE.Services.ChatServices.Implementations.OpenAI;

public class DeepSeekChatService(Model model) : OpenAIChatService(model, new Uri("https://api.deepseek.com/v1"));
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Chats.BE.DB;
using Chats.BE.Services.ChatServices.Implementations.OpenAI;

namespace Chats.BE.Services.ChatServices.Implementations.GLM;
namespace Chats.BE.Services.ChatServices.Implementations.OpenAI;

public class GLMChatService(Model model) : OpenAIChatService(model, new Uri("https://open.bigmodel.cn/api/paas/v4/"))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Chats.BE.DB;

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

public class GithubModelsChatService(Model model) : OpenAIChatService(model, new Uri("https://models.inference.ai.azure.com"));

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Chats.BE.DB;
using Chats.BE.Services.ChatServices.Implementations.OpenAI;

namespace Chats.BE.Services.ChatServices.Implementations.Kimi;
namespace Chats.BE.Services.ChatServices.Implementations.OpenAI;

public class KimiChatService(Model model) : OpenAIChatService(model, new Uri("https://api.moonshot.cn/v1"));
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Chats.BE.DB;
using Chats.BE.Services.ChatServices.Implementations.OpenAI;

namespace Chats.BE.Services.ChatServices.Implementations.LingYi;
namespace Chats.BE.Services.ChatServices.Implementations.OpenAI;

public class LingYiChatService(Model model) : OpenAIChatService(model, new Uri("https://api.lingyiwanwu.com/v1"));
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Chats.BE.DB;
using OpenAI.Chat;

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

public class SparkDeskChatService(Model model) : OpenAIChatService(model, new Uri("https://spark-api-open.xf-yun.com/v1"))
{
protected override ChatMessage[] FEPreprocess(IReadOnlyList<ChatMessage> messages, ChatCompletionOptions options)
{
ChatMessage[] toReturn = base.FEPreprocess(messages, options);
// spark desk enable search is set via tool https://www.xfyun.cn/doc/spark/HTTP%E8%B0%83%E7%94%A8%E6%96%87%E6%A1%A3.html#_3-%E8%AF%B7%E6%B1%82%E8%AF%B4%E6%98%8E
// however unfortunately the tool is not available in OpenAI SDK
// otherwise, the code should be:
// bool enableSearch = options.IsSearchEnabled();
// options.Tools.Add(MakeSparkDeskSearchTool(enableSearch));
return toReturn;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Chats.BE.DB;

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

public class XAIChatService(Model model) : OpenAIChatService(model, new Uri("https://api.x.ai/v1"));
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
insert into ModelProvider values(11, 'x.ai', NULL, 'xai-yourkey');
insert into ModelProvider values(12, 'Github Models', NULL, 'ghp_yourkey');
update ModelProvider set InitialSecret = '' where id = 8;

insert into ModelReference values
(1100, 11, 'grok-beta', 'grok', 0, 0, 2, 0, 0, 1, 1, 131072, 4096, NULL, 5, 15, 'USD'),
Expand Down

0 comments on commit e52187a

Please sign in to comment.