forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextMemorySkill.cs
187 lines (157 loc) · 8.27 KB
/
TextMemorySkill.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) Microsoft. All rights reserved.
using System.Globalization;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel.Diagnostics;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.SkillDefinition;
namespace Microsoft.SemanticKernel.CoreSkills;
/// <summary>
/// TextMemorySkill provides a skill to save or recall information from the long or short term memory.
/// </summary>
/// <example>
/// Usage: kernel.ImportSkill("memory", new TextMemorySkill());
/// Examples:
/// SKContext["input"] = "what is the capital of France?"
/// {{memory.recall $input }} => "Paris"
/// </example>
public class TextMemorySkill
{
/// <summary>
/// Name of the context variable used to specify which memory collection to use.
/// </summary>
public const string CollectionParam = "collection";
/// <summary>
/// Name of the context variable used to specify memory search relevance score.
/// </summary>
public const string RelevanceParam = "relevance";
/// <summary>
/// Name of the context variable used to specify a unique key associated with stored information.
/// </summary>
public const string KeyParam = "key";
/// <summary>
/// Name of the context variable used to specify the number of memories to recall
/// </summary>
public const string LimitParam = "limit";
private const string DefaultCollection = "generic";
private const string DefaultRelevance = "0.75";
private const string DefaultLimit = "1";
/// <summary>
/// Key-based lookup for a specific memory
/// </summary>
/// <example>
/// SKContext[TextMemorySkill.KeyParam] = "countryInfo1"
/// {{memory.retrieve }}
/// </example>
/// <param name="context">Contains the 'collection' containing the memory to retrieve and the `key` associated with it.</param>
[SKFunction("Key-based lookup for a specific memory")]
[SKFunctionName("Retrieve")]
[SKFunctionContextParameter(Name = CollectionParam, Description = "Memories collection associated with the memory to retrieve",
DefaultValue = DefaultCollection)]
[SKFunctionContextParameter(Name = KeyParam, Description = "The key associated with the memory to retrieve")]
public async Task<string> RetrieveAsync(SKContext context)
{
var collection = context.Variables.ContainsKey(CollectionParam) ? context[CollectionParam] : DefaultCollection;
Verify.NotEmpty(collection, "Memory collection not defined");
var key = context.Variables.ContainsKey(KeyParam) ? context[KeyParam] : string.Empty;
Verify.NotEmpty(key, "Memory key not defined");
context.Log.LogTrace("Recalling memory with key '{0}' from collection '{1}'", key, collection);
var memory = await context.Memory.GetAsync(collection, key);
return memory?.Metadata.Text ?? string.Empty;
}
/// <summary>
/// Semantic search and return up to N memories related to the input text
/// </summary>
/// <example>
/// SKContext["input"] = "what is the capital of France?"
/// {{memory.recall $input }} => "Paris"
/// </example>
/// <param name="text">The input text to find related memories for</param>
/// <param name="context">Contains the 'collection' to search for the topic and 'relevance' score</param>
[SKFunction("Semantic search and return up to N memories related to the input text")]
[SKFunctionName("Recall")]
[SKFunctionInput(Description = "The input text to find related memories for")]
[SKFunctionContextParameter(Name = CollectionParam, Description = "Memories collection to search", DefaultValue = DefaultCollection)]
[SKFunctionContextParameter(Name = RelevanceParam, Description = "The relevance score, from 0.0 to 1.0, where 1.0 means perfect match",
DefaultValue = DefaultRelevance)]
[SKFunctionContextParameter(Name = LimitParam, Description = "The maximum number of relevant memories to recall", DefaultValue = DefaultLimit)]
public string Recall(string text, SKContext context)
{
var collection = context.Variables.ContainsKey(CollectionParam) ? context[CollectionParam] : DefaultCollection;
Verify.NotEmpty(collection, "Memories collection not defined");
var relevance = context.Variables.ContainsKey(RelevanceParam) ? context[RelevanceParam] : DefaultRelevance;
if (string.IsNullOrWhiteSpace(relevance)) { relevance = DefaultRelevance; }
var limit = context.Variables.ContainsKey(LimitParam) ? context[LimitParam] : DefaultLimit;
if (string.IsNullOrWhiteSpace(limit)) { relevance = DefaultLimit; }
context.Log.LogTrace("Searching memories in collection '{0}', relevance '{1}'", collection, relevance);
// TODO: support locales, e.g. "0.7" and "0,7" must both work
int limitInt = int.Parse(limit, CultureInfo.InvariantCulture);
var memories = context.Memory
.SearchAsync(collection, text, limitInt, minRelevanceScore: float.Parse(relevance, CultureInfo.InvariantCulture))
.ToEnumerable();
context.Log.LogTrace("Done looking for memories in collection '{0}')", collection);
string resultString;
if (limitInt == 1)
{
var memory = memories.FirstOrDefault();
resultString = (memory != null) ? memory.Metadata.Text : string.Empty;
}
else
{
resultString = JsonSerializer.Serialize(memories.Select(x => x.Metadata.Text));
}
if (resultString.Length == 0)
{
context.Log.LogWarning("Memories not found in collection: {0}", collection);
}
return resultString;
}
/// <summary>
/// Save information to semantic memory
/// </summary>
/// <example>
/// SKContext["input"] = "the capital of France is Paris"
/// SKContext[TextMemorySkill.KeyParam] = "countryInfo1"
/// {{memory.save $input }}
/// </example>
/// <param name="text">The information to save</param>
/// <param name="context">Contains the 'collection' to save the information and unique 'key' to associate it with.</param>
[SKFunction("Save information to semantic memory")]
[SKFunctionName("Save")]
[SKFunctionInput(Description = "The information to save")]
[SKFunctionContextParameter(Name = CollectionParam, Description = "Memories collection associated with the information to save", DefaultValue = DefaultCollection)]
[SKFunctionContextParameter(Name = KeyParam, Description = "The key associated with the information to save")]
public async Task SaveAsync(string text, SKContext context)
{
var collection = context.Variables.ContainsKey(CollectionParam) ? context[CollectionParam] : DefaultCollection;
Verify.NotEmpty(collection, "Memory collection not defined");
var key = context.Variables.ContainsKey(KeyParam) ? context[KeyParam] : string.Empty;
Verify.NotEmpty(key, "Memory key not defined");
context.Log.LogTrace("Saving memory to collection '{0}'", collection);
await context.Memory.SaveInformationAsync(collection, text: text, id: key);
}
/// <summary>
/// Remove specific memory
/// </summary>
/// <example>
/// SKContext[TextMemorySkill.KeyParam] = "countryInfo1"
/// {{memory.remove }}
/// </example>
/// <param name="context">Contains the 'collection' containing the memory to remove.</param>
[SKFunction("Remove specific memory")]
[SKFunctionName("Remove")]
[SKFunctionContextParameter(Name = CollectionParam, Description = "Memories collection associated with the memory to remove",
DefaultValue = DefaultCollection)]
[SKFunctionContextParameter(Name = KeyParam, Description = "The key associated with the memory to remove")]
public async Task RemoveAsync(SKContext context)
{
var collection = context.Variables.ContainsKey(CollectionParam) ? context[CollectionParam] : DefaultCollection;
Verify.NotEmpty(collection, "Memory collection not defined");
var key = context.Variables.ContainsKey(KeyParam) ? context[KeyParam] : string.Empty;
Verify.NotEmpty(key, "Memory key not defined");
context.Log.LogTrace("Removing memory from collection '{0}'", collection);
await context.Memory.RemoveAsync(collection, key);
}
}