From 03de0a3711ff3c90463fc63c9d51ef4bba58bf5f Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Tue, 30 May 2023 14:47:13 -0400 Subject: [PATCH] Make optional strings default to null instead of empty (#1259) ### Motivation and Context Null is the typical / expected default value for optional strings in cases like this. --- .../Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs | 2 +- .../ChatCompletion/AzureChatCompletion.cs | 2 +- .../ChatCompletion/OpenAIChatCompletion.cs | 2 +- .../AI/ChatCompletion/IChatCompletion.cs | 2 +- dotnet/src/SemanticKernel.Abstractions/IKernel.cs | 2 +- dotnet/src/SemanticKernel/Kernel.cs | 4 ++-- .../SkillDefinition/InlineFunctionsDefinitionExtension.cs | 6 +++--- dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs | 4 ++-- .../kernel-syntax-examples/Example34_CustomChatModel.cs | 2 +- 9 files changed, 13 insertions(+), 13 deletions(-) diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs index a82abf46b361..8e974ece65f9 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs @@ -203,7 +203,7 @@ private protected async IAsyncEnumerable InternalGenerateChatMessageStre /// /// Optional chat instructions for the AI service /// Chat object - private protected static ChatHistory InternalCreateNewChat(string instructions = "") + private protected static ChatHistory InternalCreateNewChat(string? instructions = null) { return new OpenAIChatHistory(instructions); } diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/AzureChatCompletion.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/AzureChatCompletion.cs index 50065b6ab6e9..a4cca388cd85 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/AzureChatCompletion.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/AzureChatCompletion.cs @@ -72,7 +72,7 @@ public IAsyncEnumerable GenerateMessageStreamAsync( } /// - public ChatHistory CreateNewChat(string instructions = "") + public ChatHistory CreateNewChat(string? instructions = null) { return InternalCreateNewChat(instructions); } diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/OpenAIChatCompletion.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/OpenAIChatCompletion.cs index 0b063b5beb15..362d86e92b9b 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/OpenAIChatCompletion.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/OpenAIChatCompletion.cs @@ -55,7 +55,7 @@ public IAsyncEnumerable GenerateMessageStreamAsync( } /// - public ChatHistory CreateNewChat(string instructions = "") + public ChatHistory CreateNewChat(string? instructions = null) { return InternalCreateNewChat(instructions); } diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/IChatCompletion.cs b/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/IChatCompletion.cs index 9138488f3d86..a8b422d45d0b 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/IChatCompletion.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/IChatCompletion.cs @@ -14,7 +14,7 @@ public interface IChatCompletion : IAIService /// /// Optional chat instructions for the AI service /// Chat object - public ChatHistory CreateNewChat(string instructions = ""); + public ChatHistory CreateNewChat(string? instructions = null); /// /// Generate a new chat message diff --git a/dotnet/src/SemanticKernel.Abstractions/IKernel.cs b/dotnet/src/SemanticKernel.Abstractions/IKernel.cs index 8fb73c051b58..f706b49961c3 100644 --- a/dotnet/src/SemanticKernel.Abstractions/IKernel.cs +++ b/dotnet/src/SemanticKernel.Abstractions/IKernel.cs @@ -93,7 +93,7 @@ ISKFunction RegisterSemanticFunction( /// Name of the skill for skill collection and prompt templates. If the value is empty functions are registered in the global namespace. /// Service used for trust checks (if null will use the default registered in the kernel). /// A list of all the semantic functions found in the directory, indexed by function name. - IDictionary ImportSkill(object skillInstance, string skillName = "", ITrustService? trustService = null); + IDictionary ImportSkill(object skillInstance, string? skillName = null, ITrustService? trustService = null); /// /// Set the semantic memory to use diff --git a/dotnet/src/SemanticKernel/Kernel.cs b/dotnet/src/SemanticKernel/Kernel.cs index 677e0f118421..44ed9644ab71 100644 --- a/dotnet/src/SemanticKernel/Kernel.cs +++ b/dotnet/src/SemanticKernel/Kernel.cs @@ -108,7 +108,7 @@ public ISKFunction RegisterSemanticFunction(string skillName, string functionNam } /// - public IDictionary ImportSkill(object skillInstance, string skillName = "", ITrustService? trustService = null) + public IDictionary ImportSkill(object skillInstance, string? skillName = null, ITrustService? trustService = null) { if (string.IsNullOrWhiteSpace(skillName)) { @@ -122,7 +122,7 @@ public IDictionary ImportSkill(object skillInstance, string Dictionary skill = ImportSkill( skillInstance, - skillName, + skillName!, // Use the default trust service registered if none is provided trustService ?? this.TrustServiceInstance, this.Log diff --git a/dotnet/src/SemanticKernel/SkillDefinition/InlineFunctionsDefinitionExtension.cs b/dotnet/src/SemanticKernel/SkillDefinition/InlineFunctionsDefinitionExtension.cs index 457a71c17b94..6f136c12fb77 100644 --- a/dotnet/src/SemanticKernel/SkillDefinition/InlineFunctionsDefinitionExtension.cs +++ b/dotnet/src/SemanticKernel/SkillDefinition/InlineFunctionsDefinitionExtension.cs @@ -41,7 +41,7 @@ public static ISKFunction CreateSemanticFunction( this IKernel kernel, string promptTemplate, string? functionName = null, - string skillName = "", + string? skillName = null, string? description = null, int maxTokens = 256, double temperature = 0, @@ -94,7 +94,7 @@ public static ISKFunction CreateSemanticFunction( string promptTemplate, PromptTemplateConfig config, string? functionName = null, - string skillName = "", + string? skillName = null, ITrustService? trustService = null) { functionName ??= RandomFunctionName(); @@ -109,7 +109,7 @@ public static ISKFunction CreateSemanticFunction( // TODO: manage overwrites, potentially error out return string.IsNullOrEmpty(skillName) ? kernel.RegisterSemanticFunction(functionName, functionConfig, trustService) - : kernel.RegisterSemanticFunction(skillName, functionName, functionConfig, trustService); + : kernel.RegisterSemanticFunction(skillName!, functionName, functionConfig, trustService); } private static string RandomFunctionName() => "func" + Guid.NewGuid().ToString("N"); diff --git a/dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs b/dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs index 5da842aaab34..2bce25ede8ac 100644 --- a/dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs +++ b/dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs @@ -66,7 +66,7 @@ public sealed class SKFunction : ISKFunction, IDisposable public static ISKFunction? FromNativeMethod( MethodInfo methodSignature, object? methodContainerInstance = null, - string skillName = "", + string? skillName = null, ITrustService? trustService = null, ILogger? log = null) { @@ -91,7 +91,7 @@ public sealed class SKFunction : ISKFunction, IDisposable return new SKFunction( delegateFunction: methodDetails.Function, parameters: methodDetails.Parameters, - skillName: skillName, + skillName: skillName!, functionName: methodDetails.Name, isSemantic: false, description: methodDetails.Description, diff --git a/samples/dotnet/kernel-syntax-examples/Example34_CustomChatModel.cs b/samples/dotnet/kernel-syntax-examples/Example34_CustomChatModel.cs index ab41bd8adb4b..6c000b757078 100644 --- a/samples/dotnet/kernel-syntax-examples/Example34_CustomChatModel.cs +++ b/samples/dotnet/kernel-syntax-examples/Example34_CustomChatModel.cs @@ -23,7 +23,7 @@ public sealed class MyChatCompletionService : IChatCompletion { private const string OutputAssistantResult = "Hi I'm your SK Custom Assistant and I'm here to help you to create custom chats like this. :)"; - public ChatHistory CreateNewChat(string instructions = "") + public ChatHistory CreateNewChat(string? instructions = null) { var chatHistory = new ChatHistory();