Skip to content

Commit

Permalink
Update SK packages (microsoft#120)
Browse files Browse the repository at this point in the history
### Motivation and Context

Weekly OSS merge current semantic-kernel packages: 0.19.230804.2

### Description

SK Deprectations: 
- `SKContext.Fail()` => `throw`
- `SKContext.Log` => `SKContext.Logger`
- `SKContext.LastErrorDescription` => `SKContext.LastException.Message`
- `SKContext.this[]` => `SKContext.Variables[]`
- `SKContext.ModelResults` breaking type change (`ChatCompletions`=>
`ChatModelResult`)

Also:
- Replaced "" with string.Empty

### Contribution Checklist

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [Contribution
Guidelines](https://github.com/microsoft/copilot-chat/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/copilot-chat/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄
  • Loading branch information
crickman authored Aug 8, 2023
1 parent a9b6515 commit 3b22b1b
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 108 deletions.
4 changes: 2 additions & 2 deletions webapi/Controllers/BotController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public async Task<ActionResult<ChatSession>> UploadAsync(

return this.CreatedAtAction(
nameof(ChatHistoryController.GetChatSessionByIdAsync),
nameof(ChatHistoryController).Replace("Controller", "", StringComparison.OrdinalIgnoreCase),
nameof(ChatHistoryController).Replace("Controller", string.Empty, StringComparison.OrdinalIgnoreCase),
new { chatId },
newChat);
}
Expand Down Expand Up @@ -208,7 +208,7 @@ private async Task GetMemoryRecordsAndAppendToEmbeddingsAsync(
IKernel kernel,
string collectionName,
List<KeyValuePair<string, List<MemoryQueryResult>>> embeddings,
string newCollectionName = "")
string? newCollectionName = null)
{
List<MemoryQueryResult> collectionMemoryRecords;
try
Expand Down
10 changes: 5 additions & 5 deletions webapi/Controllers/ChatController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public async Task<IActionResult> ChatAsync(
return this.BadRequest(string.Concat(aiException.Message, " - Detail: " + aiException.Detail));
}

return this.BadRequest(result.LastErrorDescription);
return this.BadRequest(result.LastException!.Message);
}

AskResult chatSkillAskResult = new()
Expand Down Expand Up @@ -174,15 +174,15 @@ private async Task RegisterPlannerSkillsAsync(CopilotChatPlanner planner, Dictio
this._logger.LogInformation("Registering Klarna plugin");

// Register the Klarna shopping ChatGPT plugin with the planner's kernel. There is no authentication required for this plugin.
await planner.Kernel.ImportChatGptPluginSkillFromUrlAsync("KlarnaShoppingPlugin", new Uri("https://www.klarna.com/.well-known/ai-plugin.json"), new OpenApiSkillExecutionParameters());
await planner.Kernel.ImportAIPluginAsync("KlarnaShoppingPlugin", new Uri("https://www.klarna.com/.well-known/ai-plugin.json"), new OpenApiSkillExecutionParameters());
}

// GitHub
if (openApiSkillsAuthHeaders.TryGetValue("GITHUB", out string? GithubAuthHeader))
{
this._logger.LogInformation("Enabling GitHub plugin.");
BearerAuthenticationProvider authenticationProvider = new(() => Task.FromResult(GithubAuthHeader));
await planner.Kernel.ImportOpenApiSkillFromFileAsync(
await planner.Kernel.ImportAIPluginAsync(
skillName: "GitHubPlugin",
filePath: Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "CopilotChat", "Skills", "OpenApiPlugins/GitHubPlugin/openapi.json"),
new OpenApiSkillExecutionParameters
Expand All @@ -198,7 +198,7 @@ await planner.Kernel.ImportOpenApiSkillFromFileAsync(
var authenticationProvider = new BasicAuthenticationProvider(() => { return Task.FromResult(JiraAuthHeader); });
var hasServerUrlOverride = variables.TryGetValue("jira-server-url", out string? serverUrlOverride);

await planner.Kernel.ImportOpenApiSkillFromFileAsync(
await planner.Kernel.ImportAIPluginAsync(
skillName: "JiraPlugin",
filePath: Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "CopilotChat", "Skills", "OpenApiPlugins/JiraPlugin/openapi.json"),
new OpenApiSkillExecutionParameters
Expand Down Expand Up @@ -241,7 +241,7 @@ await planner.Kernel.ImportOpenApiSkillFromFileAsync(
var requiresAuth = !plugin.AuthType.Equals("none", StringComparison.OrdinalIgnoreCase);
BearerAuthenticationProvider authenticationProvider = new(() => Task.FromResult(PluginAuthValue));

await planner.Kernel.ImportChatGptPluginSkillFromUrlAsync(
await planner.Kernel.ImportAIPluginAsync(
$"{plugin.NameForModel}Plugin",
uriBuilder.Uri,
new OpenApiSkillExecutionParameters
Expand Down
4 changes: 2 additions & 2 deletions webapi/Controllers/ChatMemoryController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private bool ValidateMemoryName(string memoryName)
}

/// <summary>
/// Sanitizes the log input by removing new line characters.
/// Sanitizes the log input by removing new line characters.
/// This helps prevent log forgery attacks from malicious text.
/// </summary>
/// <remarks>
Expand All @@ -124,7 +124,7 @@ private bool ValidateMemoryName(string memoryName)
/// <returns>The sanitized input.</returns>
private string SanitizeLogInput(string input)
{
return input.Replace(Environment.NewLine, "", StringComparison.Ordinal);
return input.Replace(Environment.NewLine, string.Empty, StringComparison.Ordinal);
}

# endregion
Expand Down
2 changes: 1 addition & 1 deletion webapi/Controllers/SpeechTokenController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ private async Task<TokenResult> FetchTokenAsync(string fetchUri, string subscrip
return new TokenResult { Token = token, ResponseCode = response.StatusCode };
}

return new TokenResult { Token = "", ResponseCode = HttpStatusCode.NotFound };
return new TokenResult { ResponseCode = HttpStatusCode.NotFound };
}
}
16 changes: 8 additions & 8 deletions webapi/CopilotChatWebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
<ItemGroup>
<PackageReference Include="Azure.AI.FormRecognizer" Version="4.0.0" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.35.2" />
<PackageReference Include="Microsoft.SemanticKernel" Version="0.18.230725.3-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.AI.OpenAI" Version="0.18.230725.3-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.AzureCognitiveSearch" Version="0.18.230725.3-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.Chroma" Version="0.18.230725.3-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.Qdrant" Version="0.18.230725.3-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Skills.MsGraph" Version="0.18.230725.3-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Skills.OpenAPI" Version="0.18.230725.3-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Skills.Web" Version="0.18.230725.3-preview" />
<PackageReference Include="Microsoft.SemanticKernel" Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.AI.OpenAI" Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.AzureCognitiveSearch" Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.Chroma" Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.Qdrant" Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Skills.MsGraph" Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Skills.OpenAPI" Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Skills.Web" Version="0.19.230804.2-preview" />
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.2.2" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageReference Include="Microsoft.Identity.Web" Version="2.13.2" />
Expand Down
15 changes: 14 additions & 1 deletion webapi/Extensions/SemanticKernelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using CopilotChat.WebApi.Skills.ChatSkills;
using CopilotChat.WebApi.Storage;
using static CopilotChat.WebApi.Options.MemoriesStoreOptions;
using Microsoft.SemanticKernel.Orchestration;

namespace CopilotChat.WebApi.Extensions;

Expand Down Expand Up @@ -106,6 +107,18 @@ public static IKernel RegisterChatSkill(this IKernel kernel, IServiceProvider sp
return kernel;
}

/// <summary>
/// Propagate exception from within semantic function
/// </summary>
public static void ThrowIfFailed(this SKContext context)
{
if (context.ErrorOccurred)
{
context.Logger.LogError(context.LastException, "{0}", context.LastException?.Message);
throw context.LastException!;
}
}

/// <summary>
/// Register the skills with the kernel.
/// </summary>
Expand All @@ -129,7 +142,7 @@ private static Task RegisterSkillsAsync(IServiceProvider sp, IKernel kernel)
}
catch (TemplateException e)
{
kernel.Log.LogError("Could not load skill from {Directory}: {Message}", subDir, e.Message);
kernel.Logger.LogError("Could not load skill from {Directory}: {Message}", subDir, e.Message);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions webapi/Models/Storage/ChatMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public ChatMessage(
string userName,
string chatId,
string content,
string prompt = "",
string? prompt = null,
AuthorRoles authorRole = AuthorRoles.User,
ChatMessageType type = ChatMessageType.Message,
Dictionary<string, int>? tokenUsage = null)
Expand All @@ -145,7 +145,7 @@ public ChatMessage(
this.ChatId = chatId;
this.Content = content;
this.Id = Guid.NewGuid().ToString();
this.Prompt = prompt;
this.Prompt = prompt ?? string.Empty;
this.AuthorRole = authorRole;
this.Type = type;
this.TokenUsage = tokenUsage;
Expand Down
Loading

0 comments on commit 3b22b1b

Please sign in to comment.