From 16330759d625e73020d0fbae0adc13e7a4f5eecc Mon Sep 17 00:00:00 2001 From: sdcb Date: Wed, 11 Dec 2024 19:01:08 +0800 Subject: [PATCH] try implement sparkdesk --- src/BE/Chats.BE.csproj | 8 +++--- src/BE/DB/Init/BasicData.cs | 28 +++++++++---------- src/BE/Services/ChatServices/ChatFactory.cs | 2 +- .../ChatServices/ChatServiceExtensions.cs | 4 +-- .../OpenAI/SparkDeskChatService.cs | 18 ++++++++++++ .../2024/20241211-xai-githubmodels.sql | 1 + 6 files changed, 40 insertions(+), 21 deletions(-) create mode 100644 src/BE/Services/ChatServices/Implementations/OpenAI/SparkDeskChatService.cs diff --git a/src/BE/Chats.BE.csproj b/src/BE/Chats.BE.csproj index 22774063..97cb4b4f 100644 --- a/src/BE/Chats.BE.csproj +++ b/src/BE/Chats.BE.csproj @@ -9,7 +9,7 @@ - + @@ -26,12 +26,12 @@ - + - - + + diff --git a/src/BE/DB/Init/BasicData.cs b/src/BE/DB/Init/BasicData.cs index 34f1388a..57fdb52d 100644 --- a/src/BE/DB/Init/BasicData.cs +++ b/src/BE/DB/Init/BasicData.cs @@ -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://.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://.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", } ]); } }; \ No newline at end of file diff --git a/src/BE/Services/ChatServices/ChatFactory.cs b/src/BE/Services/ChatServices/ChatFactory.cs index d4f2d4bd..a08ab5e7 100644 --- a/src/BE/Services/ChatServices/ChatFactory.cs +++ b/src/BE/Services/ChatServices/ChatFactory.cs @@ -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), diff --git a/src/BE/Services/ChatServices/ChatServiceExtensions.cs b/src/BE/Services/ChatServices/ChatServiceExtensions.cs index 165d2a04..5dcc9c11 100644 --- a/src/BE/Services/ChatServices/ChatServiceExtensions.cs +++ b/src/BE/Services/ChatServices/ChatServiceExtensions.cs @@ -9,7 +9,7 @@ public abstract partial class ChatService { public async IAsyncEnumerable ChatStreamedFEProcessed(IReadOnlyList 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)) { @@ -48,7 +48,7 @@ public async IAsyncEnumerable ChatStreamedSimulated(bool su } } - private ChatMessage[] FEProcessMessages(IReadOnlyList messages, ChatCompletionOptions options) + protected virtual ChatMessage[] FEPreprocess(IReadOnlyList messages, ChatCompletionOptions options) { if (!Model.ModelReference.AllowSystemPrompt) { diff --git a/src/BE/Services/ChatServices/Implementations/OpenAI/SparkDeskChatService.cs b/src/BE/Services/ChatServices/Implementations/OpenAI/SparkDeskChatService.cs new file mode 100644 index 00000000..99167ed5 --- /dev/null +++ b/src/BE/Services/ChatServices/Implementations/OpenAI/SparkDeskChatService.cs @@ -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 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; + } +} \ No newline at end of file diff --git a/src/scripts/db-migration/2024/20241211-xai-githubmodels.sql b/src/scripts/db-migration/2024/20241211-xai-githubmodels.sql index b40fc861..f9e00fb9 100644 --- a/src/scripts/db-migration/2024/20241211-xai-githubmodels.sql +++ b/src/scripts/db-migration/2024/20241211-xai-githubmodels.sql @@ -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'),