Skip to content

Commit bf823c5

Browse files
authored
CopilotChat: Options pattern and best practices (#588)
### Motivation and Context Aligning CopilotChat further down the path of a reference application. ### Description - Refactored configurations to use Options pattern as a best practice. - Moved to use Kestral appsettings for the endpoint and we now report the endpoint after the application starts, which will account for any configuration changes that may have occurred. - Moved web and service builder extensions to their own files to slim down `Program.cs` and make it easier to understand each set of extensions. This is mostly to separate out those extensions related to Semantic Kernel so developers can find those code sections more easily. - Renamed bot from "SK Chatbot" to "Copilot" to align with the project name.
1 parent cd01572 commit bf823c5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+17616
-12485
lines changed

samples/apps/copilot-chat-app/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,4 @@ the complete list of current models supporting chat completions.
132132

133133
## Additional resources
134134

135-
1. [Import Document Application](./importdocument/README.md): Ingest a document to memory.
135+
1. [Import Document Application](./importdocument/README.md): Import a document to the memory store.

samples/apps/copilot-chat-app/webapi/Config/AIServiceConfig.cs

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System.ComponentModel.DataAnnotations;
4+
5+
namespace SemanticKernel.Service.Config;
6+
7+
/// <summary>
8+
/// Configuration options for AI services, such as Azure OpenAI and OpenAI.
9+
/// </summary>
10+
public sealed class AIServiceOptions
11+
{
12+
public const string CompletionPropertyName = "Completion";
13+
public const string EmbeddingPropertyName = "Embedding";
14+
15+
public enum AIServiceType
16+
{
17+
AzureOpenAI,
18+
OpenAI
19+
}
20+
21+
/// <summary>
22+
/// Label used for referencing the AI service in Semantic Kernel.
23+
/// </summary>
24+
[Required, NotEmptyOrWhitespace]
25+
public string Label { get; set; } = string.Empty;
26+
27+
/// <summary>
28+
/// Type of AI service.
29+
/// </summary>
30+
[Required]
31+
public AIServiceType AIService { get; set; } = AIServiceType.AzureOpenAI;
32+
33+
/// <summary>
34+
/// Azure OpenAI deployment name or OpenAI model name.
35+
/// </summary>
36+
[Required, NotEmptyOrWhitespace]
37+
public string DeploymentOrModelId { get; set; } = string.Empty;
38+
39+
/// <summary>
40+
/// (Azure OpenAI only) Azure OpenAI endpoint.
41+
/// </summary>
42+
[RequiredOnPropertyValue(nameof(AIService), AIServiceType.AzureOpenAI)]
43+
[NotEmptyOrWhitespace]
44+
public string Endpoint { get; set; } = string.Empty;
45+
46+
/// <summary>
47+
/// Key to access the AI service.
48+
/// </summary>
49+
[Required, NotEmptyOrWhitespace]
50+
public string Key { get; set; } = string.Empty;
51+
52+
// TODO support OpenAI's orgID
53+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System.ComponentModel.DataAnnotations;
4+
using Microsoft.Identity.Web;
5+
6+
namespace SemanticKernel.Service.Config;
7+
8+
/// <summary>
9+
/// Configuration options for authorizing to the service.
10+
/// </summary>
11+
public class AuthorizationOptions
12+
{
13+
public const string PropertyName = "Authorization";
14+
15+
public enum AuthorizationType
16+
{
17+
None,
18+
ApiKey,
19+
AzureAd
20+
}
21+
22+
/// <summary>
23+
/// Type of authorization.
24+
/// </summary>
25+
public AuthorizationType Type { get; set; } = AuthorizationType.None;
26+
27+
/// <summary>
28+
/// When <see cref="Type"/> is <see cref="AuthorizationType.ApiKey"/>, this is the API key to use.
29+
/// </summary>
30+
[Required, NotEmptyOrWhitespace]
31+
public string ApiKey { get; set; } = string.Empty;
32+
33+
/// <summary>
34+
/// When <see cref="Type"/> is <see cref="AuthorizationType.AzureAd"/>, these are the Azure AD options to use.
35+
/// </summary>
36+
[RequiredOnPropertyValue(nameof(Type), AuthorizationType.AzureAd)]
37+
public AzureAdOptions? AzureAd { get; set; }
38+
39+
/// <summary>
40+
/// Configuration options for Azure Active Directory (AAD) authorization.
41+
/// </summary>
42+
public class AzureAdOptions
43+
{
44+
/// <summary>
45+
/// AAD instance url, i.e., https://login.microsoftonline.com/
46+
/// </summary>
47+
[Required, NotEmptyOrWhitespace]
48+
public string Instance { get; set; } = string.Empty;
49+
50+
/// <summary>
51+
/// Tenant (directory) ID
52+
/// </summary>
53+
[Required, NotEmptyOrWhitespace]
54+
public string TenantId { get; set; } = string.Empty;
55+
56+
/// <summary>
57+
/// Application (client) ID
58+
/// </summary>
59+
[Required, NotEmptyOrWhitespace]
60+
public string ClientId { get; set; } = string.Empty;
61+
62+
/// <summary>
63+
/// Required scopes.
64+
/// </summary>
65+
[Required]
66+
public string? Scopes { get; set; } = string.Empty;
67+
}
68+
}

samples/apps/copilot-chat-app/webapi/Config/AzureSpeechConfig.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System.ComponentModel.DataAnnotations;
4+
5+
namespace SemanticKernel.Service.Config;
6+
7+
/// <summary>
8+
/// Configuration options for Azure speech recognition.
9+
/// </summary>
10+
public sealed class AzureSpeechOptions
11+
{
12+
public const string PropertyName = "AzureSpeech";
13+
14+
/// <summary>
15+
/// Location of the Azure speech service to use (e.g. "South Central US")
16+
/// </summary>
17+
[Required, NotEmptyOrWhitespace]
18+
public string? Region { get; set; } = string.Empty;
19+
20+
/// <summary>
21+
/// Key to access the Azure speech service.
22+
/// </summary>
23+
[Required, NotEmptyOrWhitespace]
24+
public string Key { get; set; } = string.Empty;
25+
}

samples/apps/copilot-chat-app/webapi/Config/BotSchemaConfig.cs renamed to samples/apps/copilot-chat-app/webapi/Config/BotSchemaOptions.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

3+
using System.ComponentModel.DataAnnotations;
4+
35
namespace SemanticKernel.Service.Config;
46

57
/// <summary>
6-
/// Configuration settings for the bot file schema that is supported by this application.
8+
/// Configuration options for the bot file schema that is supported by this application.
79
/// </summary>
8-
public class BotSchemaConfig
10+
public class BotSchemaOptions
911
{
12+
public const string PropertyName = "BotSchema";
13+
1014
/// <summary>
1115
/// The name of the schema.
1216
/// </summary>
17+
[Required, NotEmptyOrWhitespace]
1318
public string Name { get; set; } = string.Empty;
1419

1520
/// <summary>
1621
/// The version of the schema.
1722
/// </summary>
18-
public int Version { get; set; } = -1;
23+
[Range(0, int.MaxValue)]
24+
public int Version { get; set; }
1925
}

samples/apps/copilot-chat-app/webapi/Config/ChatStoreConfig.cs renamed to samples/apps/copilot-chat-app/webapi/Config/ChatStoreOptions.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

3+
using static SemanticKernel.Service.Config.AuthorizationOptions;
4+
35
namespace SemanticKernel.Service.Config;
46

57
/// <summary>
68
/// Configuration settings for the chat store.
79
/// </summary>
8-
public class ChatStoreConfig
10+
public class ChatStoreOptions
911
{
12+
public const string PropertyName = "ChatStore";
13+
1014
/// <summary>
1115
/// The type of chat store to use.
1216
/// </summary>
@@ -36,10 +40,12 @@ public enum ChatStoreType
3640
/// <summary>
3741
/// Gets or sets the configuration for the file system chat store.
3842
/// </summary>
39-
public FileSystemConfig? Filesystem { get; set; }
43+
[RequiredOnPropertyValue(nameof(Type), ChatStoreType.Filesystem)]
44+
public FileSystemOptions? Filesystem { get; set; }
4045

4146
/// <summary>
4247
/// Gets or sets the configuration for the Azure CosmosDB chat store.
4348
/// </summary>
44-
public CosmosConfig? Cosmos { get; set; }
49+
[RequiredOnPropertyValue(nameof(Type), ChatStoreType.Cosmos)]
50+
public CosmosOptions? Cosmos { get; set; }
4551
}

0 commit comments

Comments
 (0)