Skip to content

Commit

Permalink
Fix memory extraction json format issue (#1193)
Browse files Browse the repository at this point in the history
### Motivation and Context

<!-- Thank you for your contribution to the chat-copilot 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.
-->
Model hasn't been generating JSON consistently for memory extraction. 

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Set the response format to JSON to force the model to output valid json
for memory extraction.


### 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/chat-copilot/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/chat-copilot/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
TaoChenOSU authored Oct 24, 2024
1 parent 5ce1686 commit ee8df9c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion webapi/Controllers/ChatMemoryController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ await memoryClient.SearchMemoryAsync(
this._promptOptions.MemoryIndexName,
"*",
relevanceThreshold: 0,
resultCount: 1,
resultCount: -1,
chatId,
memoryContainerName);

Expand Down
10 changes: 9 additions & 1 deletion webapi/Plugins/Chat/SemanticChatMemoryExtractor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -11,6 +12,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.KernelMemory;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;

namespace CopilotChat.WebApi.Plugins.Chat;

Expand Down Expand Up @@ -78,7 +80,13 @@ async Task<SemanticChatMemory> ExtractCognitiveMemoryAsync(string memoryType, st
options.ResponseTokenLimit -
TokenUtils.TokenCount(memoryPrompt);

var memoryExtractionArguments = new KernelArguments(kernelArguments);
#pragma warning disable SKEXP0010 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
var memoryExtractionArguments = new KernelArguments(kernelArguments, executionSettings: new Dictionary<string, PromptExecutionSettings>
{
{ PromptExecutionSettings.DefaultServiceId, new OpenAIPromptExecutionSettings { ResponseFormat = "json_object" } }
});
#pragma warning restore SKEXP0010 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.

memoryExtractionArguments["tokenLimit"] = remainingToken.ToString(new NumberFormatInfo());
memoryExtractionArguments["memoryName"] = memoryName;
memoryExtractionArguments["format"] = options.MemoryFormat;
Expand Down

0 comments on commit ee8df9c

Please sign in to comment.