66using Microsoft . SemanticKernel ;
77using Microsoft . SemanticKernel . AI . Embeddings ;
88using Microsoft . SemanticKernel . Connectors . AI . OpenAI . TextEmbedding ;
9+ using Microsoft . SemanticKernel . Connectors . Memory . AzureCognitiveSearch ;
910using Microsoft . SemanticKernel . Connectors . Memory . Qdrant ;
1011using Microsoft . SemanticKernel . Memory ;
1112using Microsoft . SemanticKernel . SkillDefinition ;
@@ -22,7 +23,7 @@ internal static class SemanticKernelExtensions
2223 /// </summary>
2324 internal static IServiceCollection AddSemanticKernelServices ( this IServiceCollection services )
2425 {
25- // The chat skill's prompts are stored in a separate file.
26+ // Load the chat skill's prompts from the prompt configuration file.
2627 services . AddSingleton < PromptsConfig > ( sp =>
2728 {
2829 string promptsConfigPath = Path . Combine ( Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) ! , "prompts.json" ) ;
@@ -34,38 +35,7 @@ internal static IServiceCollection AddSemanticKernelServices(this IServiceCollec
3435 services . AddSingleton < PromptSettings > ( ) ;
3536
3637 // Add the semantic memory with backing memory store.
37- services . AddSingleton < IMemoryStore > ( serviceProvider =>
38- {
39- MemoriesStoreOptions config = serviceProvider . GetRequiredService < IOptions < MemoriesStoreOptions > > ( ) . Value ;
40-
41- switch ( config . Type )
42- {
43- case MemoriesStoreOptions . MemoriesStoreType . Volatile :
44- return new VolatileMemoryStore ( ) ;
45-
46- case MemoriesStoreOptions . MemoriesStoreType . Qdrant :
47- if ( config . Qdrant is null )
48- {
49- throw new InvalidOperationException (
50- $ "MemoriesStore:Qdrant is required when MemoriesStore:Type is '{ MemoriesStoreOptions . MemoriesStoreType . Qdrant } '") ;
51- }
52-
53- return new QdrantMemoryStore (
54- host : config . Qdrant . Host ,
55- port : config . Qdrant . Port ,
56- vectorSize : config . Qdrant . VectorSize ,
57- logger : serviceProvider . GetRequiredService < ILogger < QdrantMemoryStore > > ( ) ) ;
58-
59- default :
60- throw new InvalidOperationException ( $ "Invalid 'MemoriesStore' type '{ config . Type } '.") ;
61- }
62- } ) ;
63-
64- services . AddScoped < ISemanticTextMemory > ( serviceProvider
65- => new SemanticTextMemory (
66- serviceProvider . GetRequiredService < IMemoryStore > ( ) ,
67- serviceProvider . GetRequiredService < IOptionsSnapshot < AIServiceOptions > > ( ) . Get ( AIServiceOptions . EmbeddingPropertyName )
68- . ToTextEmbeddingsService ( logger : serviceProvider . GetRequiredService < ILogger < AIServiceOptions > > ( ) ) ) ) ;
38+ services . AddScoped < ISemanticTextMemory > ( CreateSemanticTextMemory ) ;
6939
7040 // Add the planner.
7141 services . AddScoped < CopilotChatPlanner > ( sp =>
@@ -96,10 +66,38 @@ internal static IServiceCollection AddSemanticKernelServices(this IServiceCollec
9666 return services ;
9767 }
9868
69+ /// <summary>
70+ /// Create the semantic memory with backing memory store.
71+ /// </summary>
72+ private static ISemanticTextMemory CreateSemanticTextMemory ( this IServiceProvider serviceProvider )
73+ {
74+ MemoriesStoreOptions config = serviceProvider . GetRequiredService < IOptions < MemoriesStoreOptions > > ( ) . Value ;
75+ switch ( config . Type )
76+ {
77+ case MemoriesStoreOptions . MemoriesStoreType . Volatile :
78+ return new SemanticTextMemory (
79+ new VolatileMemoryStore ( ) ,
80+ serviceProvider . GetRequiredService < IOptionsSnapshot < AIServiceOptions > > ( ) . Get ( AIServiceOptions . EmbeddingPropertyName )
81+ . ToTextEmbeddingsService ( logger : serviceProvider . GetRequiredService < ILogger < AIServiceOptions > > ( ) ) ) ;
82+
83+ case MemoriesStoreOptions . MemoriesStoreType . Qdrant :
84+ return new SemanticTextMemory (
85+ new QdrantMemoryStore ( config . Qdrant ! . Host , config . Qdrant . Port , config . Qdrant . VectorSize , serviceProvider . GetRequiredService < ILogger < QdrantMemoryStore > > ( ) ) ,
86+ serviceProvider . GetRequiredService < IOptionsSnapshot < AIServiceOptions > > ( ) . Get ( AIServiceOptions . EmbeddingPropertyName )
87+ . ToTextEmbeddingsService ( logger : serviceProvider . GetRequiredService < ILogger < AIServiceOptions > > ( ) ) ) ;
88+
89+ case MemoriesStoreOptions . MemoriesStoreType . AzureCognitiveSearch :
90+ return new AzureCognitiveSearchMemory ( config . AzureCognitiveSearch ! . Endpoint , config . AzureCognitiveSearch . Key ) ;
91+
92+ default :
93+ throw new InvalidOperationException ( $ "Invalid 'MemoriesStore' type '{ config . Type } '.") ;
94+ }
95+ }
96+
9997 /// <summary>
10098 /// Add the completion backend to the kernel config
10199 /// </summary>
102- internal static KernelConfig AddCompletionBackend ( this KernelConfig kernelConfig , AIServiceOptions aiServiceOptions )
100+ private static KernelConfig AddCompletionBackend ( this KernelConfig kernelConfig , AIServiceOptions aiServiceOptions )
103101 {
104102 switch ( aiServiceOptions . AIService )
105103 {
@@ -126,7 +124,7 @@ internal static KernelConfig AddCompletionBackend(this KernelConfig kernelConfig
126124 /// <summary>
127125 /// Add the embedding backend to the kernel config
128126 /// </summary>
129- internal static KernelConfig AddEmbeddingBackend ( this KernelConfig kernelConfig , AIServiceOptions aiServiceOptions )
127+ private static KernelConfig AddEmbeddingBackend ( this KernelConfig kernelConfig , AIServiceOptions aiServiceOptions )
130128 {
131129 switch ( aiServiceOptions . AIService )
132130 {
@@ -158,7 +156,7 @@ internal static KernelConfig AddEmbeddingBackend(this KernelConfig kernelConfig,
158156 /// <param name="serviceConfig">The service configuration</param>
159157 /// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param>
160158 /// <param name="logger">Application logger</param>
161- internal static IEmbeddingGeneration < string , float > ToTextEmbeddingsService ( this AIServiceOptions serviceConfig ,
159+ private static IEmbeddingGeneration < string , float > ToTextEmbeddingsService ( this AIServiceOptions serviceConfig ,
162160 HttpClient ? httpClient = null ,
163161 ILogger ? logger = null )
164162 {
0 commit comments