Skip to content

Commit ea69b5f

Browse files
eavanvalkenburgshawncalawharrison-28
authored andcommitted
Python: added num_records parameter to text memory skill (microsoft#2236)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel 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. --> The `text_memory_skill` in the core skills was setup to only return 1 record, while the underlying memories all have support for multiple, so I added a parameter to the recall function to use that. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added parameter, passed that on to the memory call. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄 --------- Co-authored-by: Shawn Callegari <[email protected]> Co-authored-by: Abby Harrison <[email protected]>
1 parent 90bd5ed commit ea69b5f

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

python/semantic_kernel/core_skills/text_memory_skill.py

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Copyright (c) Microsoft. All rights reserved.
2+
import json
23

34
from semantic_kernel.orchestration.sk_context import SKContext
45
from semantic_kernel.sk_pydantic import PydanticField
@@ -9,8 +10,10 @@ class TextMemorySkill(PydanticField):
910
COLLECTION_PARAM = "collection"
1011
RELEVANCE_PARAM = "relevance"
1112
KEY_PARAM = "key"
13+
LIMIT_PARAM = "limit"
1214
DEFAULT_COLLECTION = "generic"
1315
DEFAULT_RELEVANCE = 0.75
16+
DEFAULT_LIMIT = 1
1417

1518
# @staticmethod
1619
@sk_function(
@@ -28,6 +31,11 @@ class TextMemorySkill(PydanticField):
2831
description="The relevance score, from 0.0 to 1.0; 1.0 means perfect match",
2932
default_value=DEFAULT_RELEVANCE,
3033
)
34+
@sk_function_context_parameter(
35+
name=LIMIT_PARAM,
36+
description="The maximum number of relevant memories to recall.",
37+
default_value=DEFAULT_LIMIT,
38+
)
3139
async def recall_async(self, ask: str, context: SKContext) -> str:
3240
"""
3341
Recall a fact from the long term memory.
@@ -39,10 +47,11 @@ async def recall_async(self, ask: str, context: SKContext) -> str:
3947
Args:
4048
ask -- The question to ask the memory
4149
context -- Contains the 'collection' to search for information
42-
and the 'relevance' score to use when searching
50+
, the 'relevance' score to use when searching
51+
and the 'limit' of relevant memories to retrieve.
4352
4453
Returns:
45-
The nearest item from the memory store
54+
The nearest item from the memory store as a string or empty string if not found.
4655
"""
4756
if context.variables is None:
4857
raise ValueError("Context has no variables")
@@ -65,15 +74,25 @@ async def recall_async(self, ask: str, context: SKContext) -> str:
6574
if relevance is None or str(relevance).strip() == "":
6675
relevance = TextMemorySkill.DEFAULT_RELEVANCE
6776

77+
limit = (
78+
context.variables[TextMemorySkill.LIMIT_PARAM]
79+
if context.variables.contains_key(TextMemorySkill.LIMIT_PARAM)
80+
else TextMemorySkill.DEFAULT_LIMIT
81+
)
82+
if limit is None or str(limit).strip() == "":
83+
limit = TextMemorySkill.DEFAULT_LIMIT
84+
6885
results = await context.memory.search_async(
69-
collection, ask, min_relevance_score=float(relevance)
86+
collection=collection,
87+
query=ask,
88+
limit=int(limit),
89+
min_relevance_score=float(relevance),
7090
)
7191
if results is None or len(results) == 0:
7292
if context.log is not None:
7393
context.log.warning(f"Memory not found in collection: {collection}")
7494
return ""
75-
76-
return results[0].text if results[0].text is not None else ""
95+
return results[0].text if limit == 1 else json.dumps([r.text for r in results])
7796

7897
@sk_function(
7998
description="Save information to semantic memory",

0 commit comments

Comments
 (0)