Skip to content

Commit ee8df9c

Browse files
authored
Fix memory extraction json format issue (#1193)
### 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 😄
1 parent 5ce1686 commit ee8df9c

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

webapi/Controllers/ChatMemoryController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ await memoryClient.SearchMemoryAsync(
9797
this._promptOptions.MemoryIndexName,
9898
"*",
9999
relevanceThreshold: 0,
100-
resultCount: 1,
100+
resultCount: -1,
101101
chatId,
102102
memoryContainerName);
103103

webapi/Plugins/Chat/SemanticChatMemoryExtractor.cs

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

33
using System;
4+
using System.Collections.Generic;
45
using System.Globalization;
56
using System.Threading;
67
using System.Threading.Tasks;
@@ -11,6 +12,7 @@
1112
using Microsoft.Extensions.Logging;
1213
using Microsoft.KernelMemory;
1314
using Microsoft.SemanticKernel;
15+
using Microsoft.SemanticKernel.Connectors.OpenAI;
1416

1517
namespace CopilotChat.WebApi.Plugins.Chat;
1618

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

81-
var memoryExtractionArguments = new KernelArguments(kernelArguments);
83+
#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.
84+
var memoryExtractionArguments = new KernelArguments(kernelArguments, executionSettings: new Dictionary<string, PromptExecutionSettings>
85+
{
86+
{ PromptExecutionSettings.DefaultServiceId, new OpenAIPromptExecutionSettings { ResponseFormat = "json_object" } }
87+
});
88+
#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.
89+
8290
memoryExtractionArguments["tokenLimit"] = remainingToken.ToString(new NumberFormatInfo());
8391
memoryExtractionArguments["memoryName"] = memoryName;
8492
memoryExtractionArguments["format"] = options.MemoryFormat;

0 commit comments

Comments
 (0)