Skip to content

Commit

Permalink
处理上游 semantic-kernel 更新所引入的冲突
Browse files Browse the repository at this point in the history
  • Loading branch information
Richasy committed Nov 8, 2024
1 parent 9bf603b commit 32664fe
Show file tree
Hide file tree
Showing 31 changed files with 104 additions and 415 deletions.
139 changes: 0 additions & 139 deletions src/Core/RodelAgent.Models/Constants/AzureOpenAIVersion.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Core/RodelAudio.Core/Providers/OpenAIProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public PromptExecutionSettings ConvertExecutionSettings(AudioSession sessionData
if (ShouldRecreateKernel(modelId))
{
Kernel = Kernel.CreateBuilder()
.AddOpenAITextToAudio(AccessKey, modelId, BaseUri, OrganizationId)
.AddOpenAITextToAudio(AccessKey, modelId, BaseUri?.ToString(), OrganizationId)
.Build();
}

Expand Down
1 change: 1 addition & 0 deletions src/Core/RodelAudio.Core/RodelAudio.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Libs\semantic-kernel\dotnet\src\Connectors\Connectors.AzureOpenAI\Connectors.AzureOpenAI.csproj" />
<ProjectReference Include="..\..\Libs\semantic-kernel\dotnet\src\Connectors\Connectors.OpenAI\Connectors.OpenAI.csproj" />
<ProjectReference Include="..\..\Libs\semantic-kernel\dotnet\src\SemanticKernel.Abstractions\SemanticKernel.Abstractions.csproj" />
<ProjectReference Include="..\..\Libs\semantic-kernel\dotnet\src\SemanticKernel.Core\SemanticKernel.Core.csproj" />
Expand Down
24 changes: 1 addition & 23 deletions src/Core/RodelChat.Core/Providers/AzureOpenAIProvider.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Rodel. All rights reserved.

using Azure.AI.OpenAI;
using Microsoft.SemanticKernel;
using RodelAgent.Models.Constants;
using RodelChat.Interfaces.Client;
using RodelChat.Models.Client;

Expand All @@ -20,38 +18,18 @@ public AzureOpenAIProvider(AzureOpenAIClientConfig config)
: base(config.Key, config.CustomModels)
{
SetBaseUri(config.Endpoint);
Version = config.Version;
}

/// <summary>
/// 获取 API 版本.
/// </summary>
private AzureOpenAIVersion Version { get; }

/// <inheritdoc/>
public Kernel? GetOrCreateKernel(string modelId)
{
if (ShouldRecreateKernel(modelId))
{
Kernel = Kernel.CreateBuilder()
.AddAzureOpenAIChatCompletion(modelId, BaseUri.AbsoluteUri, AccessKey, apiVersion: ConvertAzureOpenAIVersion(Version), modelId: modelId)
.AddAzureOpenAIChatCompletion(modelId, BaseUri.AbsoluteUri, AccessKey, modelId: modelId)
.Build();
}

return Kernel;
}

private static OpenAIClientOptions.ServiceVersion ConvertAzureOpenAIVersion(AzureOpenAIVersion version)
{
return version switch
{
AzureOpenAIVersion.V2022_12_01 => OpenAIClientOptions.ServiceVersion.V2022_12_01,
AzureOpenAIVersion.V2023_05_15 or AzureOpenAIVersion.V2023_10_01_Preview => OpenAIClientOptions.ServiceVersion.V2023_05_15,
AzureOpenAIVersion.V2023_06_01_Preview => OpenAIClientOptions.ServiceVersion.V2023_06_01_Preview,
AzureOpenAIVersion.V2024_02_15_Preview => OpenAIClientOptions.ServiceVersion.V2024_02_15_Preview,
AzureOpenAIVersion.V2024_03_01_Preview => OpenAIClientOptions.ServiceVersion.V2024_03_01_Preview,
AzureOpenAIVersion.V2024_02_01 => OpenAIClientOptions.ServiceVersion.V2024_02_15_Preview,
_ => throw new NotSupportedException("Version not supported."),
};
}
}
11 changes: 10 additions & 1 deletion src/Core/RodelChat.Core/Providers/ProviderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ public void Release()
/// <param name="sessionData">会话.</param>
/// <returns>执行设置.</returns>
public virtual PromptExecutionSettings ConvertExecutionSettings(ChatSessionPreset sessionData)
=> new OpenAIPromptExecutionSettings
{
var settings = new OpenAIPromptExecutionSettings
{
PresencePenalty = sessionData.Parameters.GetValueOrDefault<double>(nameof(OpenAIChatParameters.FrequencyPenalty)),
FrequencyPenalty = sessionData.Parameters.GetValueOrDefault<double>(nameof(OpenAIChatParameters.PresencePenalty)),
Expand All @@ -150,6 +151,14 @@ public virtual PromptExecutionSettings ConvertExecutionSettings(ChatSessionPrese
ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions,
};

if (settings.MaxTokens == 0)
{
settings.MaxTokens = null;
}

return settings;
}

internal static string GetKernelModelId(Kernel? kernel)
{
if (kernel == null)
Expand Down
1 change: 1 addition & 0 deletions src/Core/RodelChat.Core/RodelChat.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ItemGroup>
<ProjectReference Include="..\..\Libs\semantic-kernel\dotnet\src\Agents\Core\Agents.Core.csproj" />
<ProjectReference Include="..\..\Libs\semantic-kernel\dotnet\src\Connectors\Connectors.Anthropic\Connectors.Anthropic.csproj" />
<ProjectReference Include="..\..\Libs\semantic-kernel\dotnet\src\Connectors\Connectors.AzureOpenAI\Connectors.AzureOpenAI.csproj" />
<ProjectReference Include="..\..\Libs\semantic-kernel\dotnet\src\Connectors\Connectors.DouBao\Connectors.DouBao.csproj" />
<ProjectReference Include="..\..\Libs\semantic-kernel\dotnet\src\Connectors\Connectors.Google\Connectors.Google.csproj" />
<ProjectReference Include="..\..\Libs\semantic-kernel\dotnet\src\Connectors\Connectors.HunYuan\Connectors.HunYuan.csproj" />
Expand Down
7 changes: 0 additions & 7 deletions src/Core/RodelChat.Models/Client/ChatClientConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

using System.Collections.Generic;
using System.Text.Json.Serialization;
using RodelAgent.Models.Constants;

namespace RodelChat.Models.Client;

Expand Down Expand Up @@ -149,12 +148,6 @@ public class OpenAIClientConfig : ClientEndpointConfigBase
/// </summary>
public class AzureOpenAIClientConfig : ClientEndpointConfigBase
{
/// <summary>
/// 版本.
/// </summary>
[JsonPropertyName("version")]
public AzureOpenAIVersion Version { get; set; } = AzureOpenAIVersion.V2024_02_01;

/// <inheritdoc/>
public override bool IsValid()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Core/RodelDraw.Core/DrawClient.Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace RodelDraw.Core;
/// </summary>
public sealed partial class DrawClient
{
private DrawExecutionSettings GetExecutionSettings(DrawSession session)
=> GetProvider(session.Provider).ConvertExecutionSettings(session);
private DrawParameters GetExecutionSettings(DrawSession session)
=> GetProvider(session.Provider).ConvertDrawParameters(session);

private Kernel? FindKernelProvider(ProviderType type, string modelId)
=> GetProvider(type).GetOrCreateKernel(modelId);
Expand Down
2 changes: 1 addition & 1 deletion src/Core/RodelDraw.Core/DrawClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task<string> DrawAsync(DrawSession session, CancellationToken cance
session.Parameters ??= GetDrawParameters(session.Provider);
var settings = GetExecutionSettings(session);
var drawService = kernel.GetRequiredService<ITextToImageService>();
var result = await drawService.GenerateImageAsync(session.Request.Prompt, settings, cancellationToken: cancellationToken).ConfigureAwait(false);
var result = await drawService.GenerateImageAsync(session.Request.Prompt, settings.Width, settings.Height, cancellationToken: cancellationToken).ConfigureAwait(false);
session.Time = DateTimeOffset.Now;
if (result.StartsWith("http"))
{
Expand Down
21 changes: 3 additions & 18 deletions src/Core/RodelDraw.Core/Providers/AzureOpenAIProvider.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// Copyright (c) Rodel. All rights reserved.

using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using RodelAgent.Models.Abstractions;
using RodelAgent.Models.Constants;
using RodelDraw.Interfaces.Client;
using RodelDraw.Models.Client;

Expand All @@ -23,28 +20,16 @@ public AzureOpenAIProvider(AzureOpenAIClientConfig config)
: base(config.Key, config.CustomModels)
{
SetBaseUri(config.Endpoint);
Version = config.Version;
}

/// <summary>
/// 获取 API 版本.
/// </summary>
private AzureOpenAIVersion Version { get; }

/// <inheritdoc/>
public DrawExecutionSettings ConvertExecutionSettings(DrawSession sessionData)
public DrawParameters ConvertDrawParameters(DrawSession sessionData)
{
var size = sessionData.Request?.Size ?? "1024x1024";
var split = size.Split('x');
var width = int.Parse(split[0]);
var height = int.Parse(split[1]);
return new OpenAIDrawExecutionSettings
{
ModelId = sessionData.Model,
Width = width,
Height = height,
Number = sessionData.Parameters.GetValueOrDefault<int>(nameof(AzureOpenAIDrawParameters.Number)),
};
return new DrawParameters(sessionData.Model, width, height);
}

/// <inheritdoc/>
Expand All @@ -53,7 +38,7 @@ public DrawExecutionSettings ConvertExecutionSettings(DrawSession sessionData)
if (ShouldRecreateKernel(modelId))
{
Kernel = Kernel.CreateBuilder()
.AddAzureOpenAITextToImage(modelId, endpoint: BaseUri.AbsoluteUri, apiKey: AccessKey, apiVersion: JsonSerializer.Serialize(Version), modelId: modelId)
.AddAzureOpenAITextToImage(modelId, endpoint: BaseUri.AbsoluteUri, apiKey: AccessKey, modelId: modelId)
.Build();
}

Expand Down
10 changes: 2 additions & 8 deletions src/Core/RodelDraw.Core/Providers/HunYuanProvider.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Rodel. All rights reserved.

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.HunYuan;
using RodelDraw.Interfaces.Client;
using RodelDraw.Models.Client;

Expand All @@ -25,18 +24,13 @@ public HunYuanProvider(HunYuanClientConfig config)
private string Secret { get; set; }

/// <inheritdoc/>
public DrawExecutionSettings ConvertExecutionSettings(DrawSession sessionData)
public DrawParameters ConvertDrawParameters(DrawSession sessionData)
{
var size = sessionData.Request?.Size ?? "1024x1024";
var split = size.Split('x');
var width = int.Parse(split[0]);
var height = int.Parse(split[1]);
return new HunYuanDrawExecutionSettings
{
ModelId = sessionData.Model,
Width = width,
Height = height,
};
return new DrawParameters(sessionData.Model, width, height);
}

/// <inheritdoc/>
Expand Down
13 changes: 3 additions & 10 deletions src/Core/RodelDraw.Core/Providers/OpenAIProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

using System.Text.Json.Serialization;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using RodelAgent.Models.Abstractions;
using RodelDraw.Interfaces.Client;
using RodelDraw.Models.Client;
Expand Down Expand Up @@ -31,19 +30,13 @@ public OpenAIProvider(OpenAIClientConfig config)
private string OrganizationId { get; }

/// <inheritdoc/>
public DrawExecutionSettings ConvertExecutionSettings(DrawSession sessionData)
public DrawParameters ConvertDrawParameters(DrawSession sessionData)
{
var size = sessionData.Request?.Size ?? "1024x1024";
var split = size.Split('x');
var width = int.Parse(split[0]);
var height = int.Parse(split[1]);
return new OpenAIDrawExecutionSettings
{
ModelId = sessionData.Model,
Width = width,
Height = height,
Number = sessionData.Parameters.GetValueOrDefault<int>(nameof(OpenAIDrawParameters.Number)),
};
return new DrawParameters(sessionData.Model, width, height);
}

/// <inheritdoc/>
Expand All @@ -52,7 +45,7 @@ public DrawExecutionSettings ConvertExecutionSettings(DrawSession sessionData)
if (ShouldRecreateKernel(modelId))
{
Kernel = Kernel.CreateBuilder()
.AddOpenAITextToImage(AccessKey, modelId, BaseUri, OrganizationId)
.AddOpenAITextToImage(AccessKey, OrganizationId, modelId, BaseUri?.ToString())
.Build();
}

Expand Down
Loading

0 comments on commit 32664fe

Please sign in to comment.