forked from microsoft/chat-copilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Motivation and Context <!-- Thank you for your contribution to the copilot-chat repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> Add a persona tab to show/enable the following: 1. Meta prompt. 2. Meta prompt editing, 3. Memory (long term & short term) content. 4. Memory bias slider. ### Description 1. Webapi support for editing the meta prompt. 2. Webapi support for retrieving memory content (ChatMemoryController). 3. Webapi support for setting memory bias. 4. Webapp support for showing and enabling the features. 5. Update the initial bot greeting message. ![image](https://github.com/microsoft/chat-copilot/assets/12570346/8ac7f817-bcab-4b71-98c7-03f3afc3b8f9) <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [ ] The code builds clean without any errors or warnings - [ ] 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 - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄
- Loading branch information
1 parent
533e591
commit 09ac9ce
Showing
26 changed files
with
936 additions
and
406 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.SemanticKernel.Memory; | ||
using SemanticKernel.Service.CopilotChat.Options; | ||
using SemanticKernel.Service.CopilotChat.Skills.ChatSkills; | ||
using SemanticKernel.Service.CopilotChat.Storage; | ||
|
||
namespace SemanticKernel.Service.CopilotChat.Controllers; | ||
|
||
/// <summary> | ||
/// Controller for retrieving semantic memory data of chat sessions. | ||
/// </summary> | ||
[ApiController] | ||
[Authorize] | ||
public class ChatMemoryController : ControllerBase | ||
{ | ||
private readonly ILogger<ChatMemoryController> _logger; | ||
|
||
private readonly PromptsOptions _promptOptions; | ||
|
||
private readonly ChatSessionRepository _chatSessionRepository; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ChatMemoryController"/> class. | ||
/// </summary> | ||
/// <param name="logger">The logger.</param> | ||
/// <param name="promptsOptions">The prompts options.</param> | ||
/// <param name="chatSessionRepository">The chat session repository.</param> | ||
public ChatMemoryController( | ||
ILogger<ChatMemoryController> logger, | ||
IOptions<PromptsOptions> promptsOptions, | ||
ChatSessionRepository chatSessionRepository) | ||
{ | ||
this._logger = logger; | ||
this._promptOptions = promptsOptions.Value; | ||
this._chatSessionRepository = chatSessionRepository; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the semantic memory for the chat session. | ||
/// </summary> | ||
/// <param name="semanticTextMemory">The semantic text memory instance.</param> | ||
/// <param name="chatId">The chat id.</param> | ||
/// <param name="memoryName">Name of the memory type.</param> | ||
[HttpGet] | ||
[Route("chatMemory/{chatId:guid}/{memoryName}")] | ||
[ProducesResponseType(StatusCodes.Status200OK)] | ||
[ProducesResponseType(StatusCodes.Status400BadRequest)] | ||
public async Task<IActionResult> GetSemanticMemoriesAsync( | ||
[FromServices] ISemanticTextMemory semanticTextMemory, | ||
[FromRoute] string chatId, | ||
[FromRoute] string memoryName) | ||
{ | ||
// Make sure the chat session exists. | ||
if (!await this._chatSessionRepository.TryFindByIdAsync(chatId, v => _ = v)) | ||
{ | ||
this._logger.LogWarning("Chat session: {0} does not exist.", chatId); | ||
return this.BadRequest($"Chat session: {chatId} does not exist."); | ||
} | ||
|
||
// Make sure the memory name is valid. | ||
if (!this.ValidateMemoryName(memoryName)) | ||
{ | ||
this._logger.LogWarning("Memory name: {0} is invalid.", memoryName); | ||
return this.BadRequest($"Memory name: {memoryName} is invalid."); | ||
} | ||
|
||
// Gather the requested semantic memory. | ||
// ISemanticTextMemory doesn't support retrieving all memories. | ||
// Will use a dummy query since we don't care about relevance. An empty string will cause exception. | ||
// minRelevanceScore is set to 0.0 to return all memories. | ||
List<string> memories = new(); | ||
var results = semanticTextMemory.SearchAsync( | ||
SemanticChatMemoryExtractor.MemoryCollectionName(chatId, memoryName), | ||
"abc", | ||
limit: 100, | ||
minRelevanceScore: 0.0); | ||
await foreach (var memory in results) | ||
{ | ||
memories.Add(memory.Metadata.Text); | ||
} | ||
|
||
return this.Ok(memories); | ||
} | ||
|
||
#region Private | ||
|
||
/// <summary> | ||
/// Validates the memory name. | ||
/// </summary> | ||
/// <param name="memoryName">Name of the memory requested.</param> | ||
/// <returns>True if the memory name is valid.</returns> | ||
private bool ValidateMemoryName(string memoryName) | ||
{ | ||
return this._promptOptions.MemoryMap.ContainsKey(memoryName); | ||
} | ||
|
||
# endregion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.