Skip to content

Commit

Permalink
Make optional strings default to null instead of empty (#1259)
Browse files Browse the repository at this point in the history
### Motivation and Context
Null is the typical / expected default value for optional strings in
cases like this.
  • Loading branch information
stephentoub authored May 30, 2023
1 parent 2b4e597 commit 03de0a3
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private protected async IAsyncEnumerable<string> InternalGenerateChatMessageStre
/// </summary>
/// <param name="instructions">Optional chat instructions for the AI service</param>
/// <returns>Chat object</returns>
private protected static ChatHistory InternalCreateNewChat(string instructions = "")
private protected static ChatHistory InternalCreateNewChat(string? instructions = null)
{
return new OpenAIChatHistory(instructions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public IAsyncEnumerable<string> GenerateMessageStreamAsync(
}

/// <inheritdoc/>
public ChatHistory CreateNewChat(string instructions = "")
public ChatHistory CreateNewChat(string? instructions = null)
{
return InternalCreateNewChat(instructions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public IAsyncEnumerable<string> GenerateMessageStreamAsync(
}

/// <inheritdoc/>
public ChatHistory CreateNewChat(string instructions = "")
public ChatHistory CreateNewChat(string? instructions = null)
{
return InternalCreateNewChat(instructions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface IChatCompletion : IAIService
/// </summary>
/// <param name="instructions">Optional chat instructions for the AI service</param>
/// <returns>Chat object</returns>
public ChatHistory CreateNewChat(string instructions = "");
public ChatHistory CreateNewChat(string? instructions = null);

/// <summary>
/// Generate a new chat message
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/SemanticKernel.Abstractions/IKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ ISKFunction RegisterSemanticFunction(
/// <param name="skillName">Name of the skill for skill collection and prompt templates. If the value is empty functions are registered in the global namespace.</param>
/// <param name="trustService">Service used for trust checks (if null will use the default registered in the kernel).</param>
/// <returns>A list of all the semantic functions found in the directory, indexed by function name.</returns>
IDictionary<string, ISKFunction> ImportSkill(object skillInstance, string skillName = "", ITrustService? trustService = null);
IDictionary<string, ISKFunction> ImportSkill(object skillInstance, string? skillName = null, ITrustService? trustService = null);

/// <summary>
/// Set the semantic memory to use
Expand Down
4 changes: 2 additions & 2 deletions dotnet/src/SemanticKernel/Kernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public ISKFunction RegisterSemanticFunction(string skillName, string functionNam
}

/// <inheritdoc/>
public IDictionary<string, ISKFunction> ImportSkill(object skillInstance, string skillName = "", ITrustService? trustService = null)
public IDictionary<string, ISKFunction> ImportSkill(object skillInstance, string? skillName = null, ITrustService? trustService = null)
{
if (string.IsNullOrWhiteSpace(skillName))
{
Expand All @@ -122,7 +122,7 @@ public IDictionary<string, ISKFunction> ImportSkill(object skillInstance, string

Dictionary<string, ISKFunction> skill = ImportSkill(
skillInstance,
skillName,
skillName!,
// Use the default trust service registered if none is provided
trustService ?? this.TrustServiceInstance,
this.Log
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand All @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 03de0a3

Please sign in to comment.