Skip to content

Commit

Permalink
try implement sparkdesk
Browse files Browse the repository at this point in the history
  • Loading branch information
sdcb committed Dec 11, 2024
1 parent 0d1cab2 commit 1633075
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 21 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>
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", }
]);
}
};
2 changes: 1 addition & 1 deletion src/BE/Services/ChatServices/ChatFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ 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),
Expand Down
4 changes: 2 additions & 2 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 Down
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
@@ -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 1633075

Please sign in to comment.