Files
semantic-kernel/samples/apps/copilot-chat-app/webapi/FunctionLoadingExtensions.cs
T
Tao Chen bc2d7d76fa Copilot Chat Persistent Chat History (#439)
### Motivation and Context
The Copilot chat app currently doesn't support multiple isolated chat
sessions and persist chat history once the Webapp is terminated.

We would like to support multiple chat sessions so that users can create
chats for different topics. We would also like to have persistent chat
history so that users can return to their chats any time after they
close the App.

### Description
1. Create isolated chat sessions.
2. Create models to save chat sessions and chat history.
3. Use repository pattern as a data access pattern for saving and
retrieving chat sessions and chat history. The underlying storage for
the data can be easily swapped (The implementation is currently using
InMemory storage).
4. Have the front end automatically load chat history once the user is
authenticated.
2023-04-14 10:38:07 -07:00

60 lines
1.7 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.CoreSkills;
using Microsoft.SemanticKernel.KernelExtensions;
using Microsoft.SemanticKernel.TemplateEngine;
using SemanticKernel.Service.Skills;
using SKWebApi.Skills;
using SKWebApi.Storage;
namespace CopilotChatApi.Service;
internal static class FunctionLoadingExtensions
{
internal static void RegisterSemanticSkills(
this IKernel kernel,
string skillsDirectory,
ILogger logger)
{
string[] subDirectories = Directory.GetDirectories(skillsDirectory);
foreach (string subDir in subDirectories)
{
try
{
kernel.ImportSemanticSkillFromDirectory(skillsDirectory, Path.GetFileName(subDir)!);
}
catch (TemplateException e)
{
logger.LogError("Could not load skill from {Directory}: {Message}", subDir, e.Message);
}
}
}
internal static void RegisterNativeSkills(
this IKernel kernel,
ChatSessionRepository chatSessionRepository,
ChatMessageRepository chatMessageRepository,
ILogger logger)
{
// Hardcode your native function registrations here
var timeSkill = new TimeSkill();
kernel.ImportSkill(timeSkill, nameof(TimeSkill));
var chatSkill = new ChatSkill(
kernel,
chatMessageRepository,
chatSessionRepository
);
kernel.ImportSkill(chatSkill, nameof(ChatSkill));
var chatHistorySkill = new ChatHistorySkill(
chatMessageRepository,
chatSessionRepository
);
kernel.ImportSkill(chatHistorySkill, nameof(ChatHistorySkill));
}
}