Skip to content

Commit

Permalink
Python: Combine FunctionResultContent to one ChatMessageContent for A…
Browse files Browse the repository at this point in the history
…uto Func Invoke Filter (microsoft#8098)

### Motivation and Context

We made an update to better handle the function result content when
using the auto func invoke filter; however, if parallel func calls
exist, they won't be handled properly in the current code.

<!-- 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.
-->

### Description

This change combines the number of FunctionResultContent types returned
during the handling of the auto func invoke filter and combines them as
the items to one ChatMessageContent.
- The sample is also updated

The result of 3 parallel tool calls now looks correct:

```text
User:> What is the curent time, 9494+49494, and 333-33?  

Auto function invocation filter
Function: now
Request sequence: 0
Function sequence: 0
Number of function calls: 3

Auto function invocation filter
Function: Add
Request sequence: 0
Function sequence: 0
Number of function calls: 1
Altering the Math plugin

Auto function invocation filter
Function: Subtract
Request sequence: 0
Function sequence: 0
Number of function calls: 1
Altering the Math plugin
Mosscap:> Tuesday, August 13, 2024 10:56 AM for function: time-now
Mosscap:> Stop trying to ask me to do math, I don't like it! for function: math-Add
Mosscap:> Stop trying to ask me to do math, I don't like it! for function: math-Subtract
```

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### 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 😄
  • Loading branch information
moonbox3 authored Aug 13, 2024
1 parent 2787e18 commit 7e2bca7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
23 changes: 14 additions & 9 deletions python/samples/concepts/filtering/auto_function_invoke_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,19 @@ async def auto_function_invocation_filter(context: AutoFunctionInvocationContext
print(f"Number of function calls: {len(function_calls)}")
# if we don't call next, it will skip this function, and go to the next one
await next(context)
#############################
# Note: to simply return the unaltered function results, uncomment the `context.terminate = True` line and
# comment out the lines starting with `result = context.function_result` through `context.terminate = True`.
# context.terminate = True
#############################
result = context.function_result
for fc in function_calls:
if fc.plugin_name == "math":
context.function_result = FunctionResult(
function=result.function, value="Stop trying to ask me to do math, I don't like it!"
)
context.terminate = True
if context.function.plugin_name == "math":
print("Altering the Math plugin")
context.function_result = FunctionResult(
function=result.function,
value="Stop trying to ask me to do math, I don't like it!",
)
context.terminate = True


def print_tool_calls(message: ChatMessageContent) -> None:
Expand Down Expand Up @@ -142,12 +148,11 @@ async def chat() -> bool:

history.add_user_message(user_input)

# Check if any result.value is a FunctionResult
# Check if any result.value is a FunctionResultContent
if any(isinstance(item, FunctionResultContent) for item in result.value[0].items):
# Iterate through each result to process FunctionResult instances
for fr in result.value[0].items:
if isinstance(fr, FunctionResultContent):
print(f"Mosscap:> {fr.result}")
print(f"Mosscap:> {fr.result} for function: {fr.name}")
history.add_assistant_message(str(fr.result))
elif any(isinstance(item, FunctionCallContent) for item in result.value[0].items):
# If tools are used, and auto invoke tool calls is False, the response will be of type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from functools import reduce
from typing import TYPE_CHECKING, Any, ClassVar

from semantic_kernel.contents.function_result_content import FunctionResultContent

if sys.version_info >= (3, 12):
from typing import override # pragma: no cover
else:
Expand Down Expand Up @@ -135,7 +137,7 @@ async def get_chat_message_contents(
)

if any(result.terminate for result in results if result is not None):
return [chat_history.messages[-1]]
return self._create_filter_early_terminate_chat_message_content(chat_history.messages[-len(results) :])

self._update_settings(settings, chat_history, kernel=kernel)
else:
Expand Down Expand Up @@ -235,7 +237,7 @@ async def get_streaming_chat_message_contents(
],
)
if any(result.terminate for result in results if result is not None):
yield [chat_history.messages[-1]] # type: ignore
yield self._create_filter_early_terminate_chat_message_content(chat_history.messages[-len(results) :]) # type: ignore
break

self._update_settings(settings, chat_history, kernel=kernel)
Expand Down Expand Up @@ -314,6 +316,25 @@ def _create_streaming_chat_message_content(
items=items,
)

def _create_filter_early_terminate_chat_message_content(
self,
messages: list[ChatMessageContent],
) -> list[ChatMessageContent]:
"""Add an early termination message to the chat messages.
This method combines the FunctionResultContent items from separate ChatMessageContent messages,
and is used in the event that the `context.terminate = True` condition is met.
"""
items: list[Any] = []
for message in messages:
items.extend([item for item in message.items if isinstance(item, FunctionResultContent)])
return [
ChatMessageContent(
role=AuthorRole.TOOL,
items=items,
)
]

def _get_metadata_from_chat_response(self, response: ChatCompletion) -> dict[str, Any]:
"""Get metadata from a chat response."""
return {
Expand Down

0 comments on commit 7e2bca7

Please sign in to comment.