Commit 45fe605
Python: Fix issue with orphaned tool/tool_calls (#12923)
### Motivation and Context
Tool/tool_calls are getting orphaned.
Issue : #12708
How to reproduce :
```python
from semantic_kernel.contents import (
AuthorRole,
ChatMessageContent,
FunctionCallContent,
FunctionResultContent,
ChatHistoryTruncationReducer,
)
messages = [
ChatMessageContent(role=AuthorRole.SYSTEM, content="sys"),
ChatMessageContent(role=AuthorRole.USER, content="user A"),
ChatMessageContent(
role=AuthorRole.ASSISTANT,
items=[
FunctionCallContent(
id="123", function_name="search", plugin_name="plugin", arguments={"q": "x"}
)
],
),
ChatMessageContent(
role=AuthorRole.TOOL,
items=[
FunctionResultContent(
id="123", function_name="search", plugin_name="plugin", result="RESULT"
)
],
),
ChatMessageContent(role=AuthorRole.USER, content="user B"),
ChatMessageContent(role=AuthorRole.ASSISTANT, content="done"),
]
reducer = ChatHistoryTruncationReducer(target_count=3, threshold_count=0)
reducer.messages = messages
print("Before reduce:", [m.role for m in reducer.messages])
await reducer.reduce()
print("After reduce:", [m.role for m in reducer.messages])
# After reduce: [<AuthorRole.TOOL: 'tool'>, <AuthorRole.USER: 'user'>, <AuthorRole.ASSISTANT: 'assistant'>]
```
### Description
Move backward as long as we are in a tool/tool_call island when reducing
the messages.
---------
Co-authored-by: Allaoua Benchikh <[email protected]>
Co-authored-by: Evan Mattson <[email protected]>
Co-authored-by: Evan Mattson <[email protected]>1 parent 09fb007 commit 45fe605
File tree
2 files changed
+47
-4
lines changed- python
- semantic_kernel/contents/history_reducer
- tests/unit/contents
2 files changed
+47
-4
lines changedLines changed: 10 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | | - | |
| 99 | + | |
100 | 100 | | |
101 | | - | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
102 | 106 | | |
103 | | - | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
104 | 110 | | |
| 111 | + | |
105 | 112 | | |
106 | 113 | | |
107 | 114 | | |
| |||
Lines changed: 37 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
6 | 8 | | |
7 | 9 | | |
8 | 10 | | |
| |||
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
21 | 41 | | |
22 | 42 | | |
23 | 43 | | |
| |||
66 | 86 | | |
67 | 87 | | |
68 | 88 | | |
69 | | - | |
| 89 | + | |
70 | 90 | | |
71 | 91 | | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
0 commit comments