Skip to content

Commit

Permalink
Merge branch 'main' into py-openapi-param-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
moonbox3 authored Jan 7, 2025
2 parents 3fef191 + 5b97ad1 commit 6118747
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 12 deletions.
1 change: 1 addition & 0 deletions dotnet/samples/Demos/HomeAutomation/HomeAutomation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Connectors\Connectors.AzureOpenAI\Connectors.AzureOpenAI.csproj" />
<ProjectReference Include="..\..\..\src\Connectors\Connectors.OpenAI\Connectors.OpenAI.csproj" />
<ProjectReference Include="..\..\..\src\SemanticKernel.Abstractions\SemanticKernel.Abstractions.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace HomeAutomation.Options;
/// </summary>
public sealed class AzureOpenAIOptions
{
public const string SectionName = "AzureOpenAI";

[Required]
public string ChatDeploymentName { get; set; } = string.Empty;

Expand Down
2 changes: 2 additions & 0 deletions dotnet/samples/Demos/HomeAutomation/Options/OpenAIOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace HomeAutomation.Options;
/// </summary>
public sealed class OpenAIOptions
{
public const string SectionName = "OpenAI";

[Required]
public string ChatModelId { get; set; } = string.Empty;

Expand Down
26 changes: 20 additions & 6 deletions dotnet/samples/Demos/HomeAutomation/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ Example that demonstrates how to use Semantic Kernel in conjunction with depende

using HomeAutomation.Options;
using HomeAutomation.Plugins;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
// For Azure OpenAI configuration
#pragma warning disable IDE0005 // Using directive is unnecessary.
using Microsoft.SemanticKernel.Connectors.AzureOpenAI;
using Microsoft.SemanticKernel.Connectors.OpenAI;

namespace HomeAutomation;
Expand All @@ -27,30 +31,40 @@ internal static class Program
internal static async Task Main(string[] args)
{
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Configuration.AddUserSecrets<Worker>();

// Actual code to execute is found in Worker class
builder.Services.AddHostedService<Worker>();

// Get configuration
builder.Services.AddOptions<OpenAIOptions>()
.Bind(builder.Configuration.GetSection(OpenAIOptions.SectionName))
.ValidateDataAnnotations()
.ValidateOnStart();

/* Alternatively, you can use plain, Azure OpenAI after loading AzureOpenAIOptions instead of OpenAI
builder.Services.AddOptions<AzureOpenAIOptions>()
.Bind(builder.Configuration.GetSection(nameof(AzureOpenAIOptions)))
.Bind(builder.Configuration.GetSection(AzureOpenAIOptions.SectionName))
.ValidateDataAnnotations()
.ValidateOnStart();
*/

// Chat completion service that kernels will use
builder.Services.AddSingleton<IChatCompletionService>(sp =>
{
OpenAIOptions options = sp.GetRequiredService<IOptions<OpenAIOptions>>().Value;
OpenAIOptions openAIOptions = sp.GetRequiredService<IOptions<OpenAIOptions>>().Value;

// A custom HttpClient can be provided to this constructor
return new OpenAIChatCompletionService(options.ChatModelId, options.ApiKey);
return new OpenAIChatCompletionService(openAIOptions.ChatModelId, openAIOptions.ApiKey);

/* Alternatively, you can use plain, Azure OpenAI after loading AzureOpenAIOptions instead
of OpenAI options with builder.Services.AddOptions:
AzureOpenAIOptions azureOpenAIOptions = sp.GetRequiredService<IOptions<AzureOpenAIOptions>>().Value;
return new AzureOpenAIChatCompletionService(azureOpenAIOptions.ChatDeploymentName, azureOpenAIOptions.Endpoint, azureOpenAIOptions.ApiKey);
AzureOpenAIOptions options = sp.GetRequiredService<IOptions<AzureOpenAI>>().Value;
return new AzureOpenAIChatCompletionService(options.ChatDeploymentName, options.Endpoint, options.ApiKey); */
*/
});

// Add plugins that can be used by kernels
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Microsoft.SemanticKernel.Connectors.HuggingFace;
/// <summary>
/// HuggingFace Execution Settings.
/// </summary>
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
public sealed class HuggingFacePromptExecutionSettings : PromptExecutionSettings
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Microsoft.SemanticKernel.Connectors.Ollama;
/// <summary>
/// Ollama Prompt Execution Settings.
/// </summary>
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
public sealed class OllamaPromptExecutionSettings : PromptExecutionSettings
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Microsoft.SemanticKernel.Connectors.OpenAI;
/// Execution settings for OpenAI audio-to-text request.
/// </summary>
[Experimental("SKEXP0010")]
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
public sealed class OpenAIAudioToTextExecutionSettings : PromptExecutionSettings
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Microsoft.SemanticKernel.Connectors.OpenAI;
/// Execution settings for OpenAI text-to-audio request.
/// </summary>
[Experimental("SKEXP0001")]
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
public sealed class OpenAITextToAudioExecutionSettings : PromptExecutionSettings
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,10 +518,10 @@ def _serialize_data_model_to_dict(self, record: TModel, **kwargs: Any) -> OneOrM
"""
if self.data_model_definition.to_dict:
return self.data_model_definition.to_dict(record, **kwargs)
if isinstance(record, BaseModel):
return self._serialize_vectors(record.model_dump())
if isinstance(record, ToDictMethodProtocol):
return self._serialize_vectors(record.to_dict())
if isinstance(record, BaseModel):
return self._serialize_vectors(record.model_dump())

store_model = {}
for field_name in self.data_model_definition.field_names:
Expand Down Expand Up @@ -611,14 +611,14 @@ def _deserialize_dict_to_data_model(self, record: OneOrMany[dict[str, Any]], **k
"Cannot deserialize multiple records to a single record unless you are using a container."
)
record = record[0]
if issubclass(self.data_model_type, BaseModel):
if include_vectors:
record = self._deserialize_vector(record)
return self.data_model_type.model_validate(record) # type: ignore
if func := getattr(self.data_model_type, "from_dict", None):
if include_vectors:
record = self._deserialize_vector(record)
return func(record)
if issubclass(self.data_model_type, BaseModel):
if include_vectors:
record = self._deserialize_vector(record)
return self.data_model_type.model_validate(record) # type: ignore
data_model_dict: dict[str, Any] = {}
for field_name in self.data_model_definition.fields:
if not include_vectors and field_name in self.data_model_definition.vector_field_names:
Expand Down

0 comments on commit 6118747

Please sign in to comment.