Skip to content

Commit

Permalink
[对话] 新增 Silicon Flow 服务 (#12)
Browse files Browse the repository at this point in the history
* 支持 Silicon Flow 对话服务

* 更新子模块
  • Loading branch information
Richasy authored Jun 23, 2024
1 parent 2bb8772 commit e95020b
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Core/RodelAgent.Models/Constants/ProviderConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ public static class ProviderConstants
public const string DeepSeekApi = "https://api.deepseek.com";
public const string HunYuanApi = "https://hunyuan.tencentcloudapi.com";
public const string OllamaApi = "http://localhost:11434/v1";
public const string SiliconFlowApi = "https://api.siliconflow.cn/v1";
}
1 change: 1 addition & 0 deletions src/Core/RodelAgent.Statics/ChatStatics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static Dictionary<ProviderType, string> GetOnlineChatServices()
{ ProviderType.QianFan, resourceToolkit.GetString("QianFan") },
{ ProviderType.HunYuan, resourceToolkit.GetString("HunYuan") },
{ ProviderType.SparkDesk, resourceToolkit.GetString("SparkDesk") },
{ ProviderType.SiliconFlow, "Silicon Cloud" },
{ ProviderType.OpenRouter, "Open Router" },
{ ProviderType.TogetherAI, "Together AI" },
{ ProviderType.Groq, "Groq" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,12 @@ private void InjectOllama(OllamaClientConfig? config)
AddCreateMethod(ProviderType.Ollama, () => new OllamaProvider(config));
}
}

private void InjectSiliconFlow(SiliconFlowClientConfig? config)
{
if (!string.IsNullOrEmpty(config?.Key))
{
AddCreateMethod(ProviderType.SiliconFlow, () => new SiliconFlowProvider(config));
}
}
}
1 change: 1 addition & 0 deletions src/Core/RodelChat.Core/Factories/ChatProviderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ private void Initialize(ChatClientConfiguration config)
InjectDeepSeek(config.DeepSeek);
InjectHunYuan(config.HunYuan);
InjectOllama(config.Ollama);
InjectSiliconFlow(config.SiliconFlow);
}

private void RemoveProvider(ProviderType type)
Expand Down
31 changes: 31 additions & 0 deletions src/Core/RodelChat.Core/Providers/Predefined/SiliconFlow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Rodel. All rights reserved.

using RodelChat.Models.Client;

/// <summary>
/// 预定义模型.
/// </summary>
internal static partial class PredefinedModels
{
internal static List<ChatModel> SiliconFlowModels { get; } = new List<ChatModel>
{
new ChatModel
{
DisplayName = "DeepSeek Chat V2",
Id = "deepseek-ai/DeepSeek-V2-Chat",
ContextLength = 32_768,
},
new ChatModel
{
DisplayName = "Qwen2 72B",
Id = "Qwen/Qwen2-72B-Instruct",
ContextLength = 32_768,
},
new ChatModel
{
DisplayName = "ChatGLM4 9B",
Id = "THUDM/glm-4-9b-chat",
ContextLength = 32_768,
},
};
}
36 changes: 36 additions & 0 deletions src/Core/RodelChat.Core/Providers/SiliconFlowProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) Rodel. All rights reserved.

using Microsoft.SemanticKernel;
using RodelChat.Interfaces.Client;
using RodelChat.Models.Client;

namespace RodelChat.Core.Providers;

/// <summary>
/// Silicon Flow 服务商.
/// </summary>
public sealed class SiliconFlowProvider : ProviderBase, IProvider
{
/// <summary>
/// Initializes a new instance of the <see cref="SiliconFlowProvider"/> class.
/// </summary>
public SiliconFlowProvider(SiliconFlowClientConfig config)
: base(config.Key, config.CustomModels)
{
ServerModels = PredefinedModels.SiliconFlowModels;
SetBaseUri(ProviderConstants.SiliconFlowApi);
}

/// <inheritdoc/>
public Kernel? GetOrCreateKernel(string modelId)
{
if (ShouldRecreateKernel(modelId))
{
Kernel = Kernel.CreateBuilder()
.AddOpenAIChatCompletion(modelId, BaseUri, AccessKey)
.Build();
}

return Kernel;
}
}
13 changes: 13 additions & 0 deletions src/Core/RodelChat.Models/Client/ChatClientConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ public sealed class ChatClientConfiguration
[JsonPropertyName("hunyuan")]
public HunYuanClientConfig? HunYuan { get; set; }

/// <summary>
/// 硅动客户端配置.
/// </summary>
[JsonPropertyName("silicon_flow")]
public SiliconFlowClientConfig? SiliconFlow { get; set; }

/// <summary>
/// 本地模型配置.
/// </summary>
Expand Down Expand Up @@ -291,6 +297,13 @@ public sealed class AnthropicClientConfig : ClientEndpointConfigBase
{
}

/// <summary>
/// 硅动客户端配置.
/// </summary>
public sealed class SiliconFlowClientConfig : ClientConfigBase
{
}

/// <summary>
/// Ollama 客户端配置.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions src/Core/RodelChat.Models/Constants/ProviderType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ public enum ProviderType
/// Ollama.
/// </summary>
Ollama,

/// <summary>
/// Silicon Flow.
/// </summary>
SiliconFlow,
}

/// <summary>
Expand Down Expand Up @@ -131,6 +136,7 @@ public override ProviderType Read(ref Utf8JsonReader reader, Type typeToConvert,
"deep_seek" or "deepseek" => ProviderType.DeepSeek,
"hunyuan" => ProviderType.HunYuan,
"ollama" => ProviderType.Ollama,
"silicon_flow" or "siliconflow" => ProviderType.SiliconFlow,
_ => throw new JsonException(),
};
}
Expand Down Expand Up @@ -158,6 +164,7 @@ public override void Write(Utf8JsonWriter writer, ProviderType value, JsonSerial
ProviderType.DeepSeek => "deep_seek",
ProviderType.HunYuan => "hunyuan",
ProviderType.Ollama => "ollama",
ProviderType.SiliconFlow => "silicon_flow",
_ => throw new JsonException(),
};

Expand Down
1 change: 1 addition & 0 deletions src/Desktop/RodelAgent.UI/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<GradientStop Offset="0" Color="#00F3CE" />
<GradientStop Offset="1" Color="#3F30FF" />
</LinearGradientBrush>
<SolidColorBrush x:Key="SiliconFlowColor" Color="#7C3AED" />
</ResourceDictionary>
</Application.Resources>
</Application>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ or ProviderType.Groq
or ProviderType.MistralAI
or ProviderType.TogetherAI
or ProviderType.Perplexity
or ProviderType.SiliconFlow
or ProviderType.DashScope => CreateForm<ChatClientConfigSection>(),
ProviderType.Anthropic
or ProviderType.Gemini
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ or ProviderType.Groq
or ProviderType.MistralAI
or ProviderType.TogetherAI
or ProviderType.Perplexity
or ProviderType.SiliconFlow
or ProviderType.DashScope => CreateForm<ModelClientConfigSettingSection>(item),
ProviderType.Anthropic
or ProviderType.Gemini
Expand Down

0 comments on commit e95020b

Please sign in to comment.