Skip to content

Commit

Permalink
Return FunctionResultContent as tool author
Browse files Browse the repository at this point in the history
  • Loading branch information
moonbox3 committed Dec 17, 2024
1 parent 0d64754 commit af0b481
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from semantic_kernel.contents.chat_message_content import ChatMessageContent
from semantic_kernel.contents.function_call_content import FunctionCallContent
from semantic_kernel.contents.streaming_chat_message_content import StreamingChatMessageContent
from semantic_kernel.contents.utils.author_role import AuthorRole
from semantic_kernel.core_plugins.math_plugin import MathPlugin
from semantic_kernel.core_plugins.time_plugin import TimePlugin
from semantic_kernel.functions import KernelArguments
Expand Down Expand Up @@ -130,13 +131,15 @@ async def handle_streaming(

print("Mosscap:> ", end="")
streamed_chunks: list[StreamingChatMessageContent] = []
result_content = []
result_content: list[StreamingChatMessageContent] = []
async for message in response:
if not execution_settings.function_choice_behavior.auto_invoke_kernel_functions and isinstance(
message[0], StreamingChatMessageContent
if (
not execution_settings.function_choice_behavior.auto_invoke_kernel_functions
and isinstance(message[0], StreamingChatMessageContent)
and message[0].role == AuthorRole.ASSISTANT
):
streamed_chunks.append(message[0])
else:
elif isinstance(message[0], StreamingChatMessageContent) and message[0].role == AuthorRole.ASSISTANT:
result_content.append(message[0])
print(str(message[0]), end="")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,4 @@ def merge_streaming_function_results(

# If we want to be able to support adding the streaming message chunks together, then the author role needs to be
# `Assistant```, as the `Tool` role will cause the add method to break.
return [
StreamingChatMessageContent(role=AuthorRole.ASSISTANT, items=items, choice_index=0, ai_model_id=ai_model_id)
]
return [StreamingChatMessageContent(role=AuthorRole.TOOL, items=items, choice_index=0, ai_model_id=ai_model_id)]
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ async def test_azure_ai_inference_streaming_chat_completion_with_function_choice
)

# Validate the second message
assert all_messages[1].role == "assistant", f"Unexpected role for second message: {all_messages[1].role}"
assert all_messages[1].role == "tool", f"Unexpected role for second message: {all_messages[1].role}"
assert all_messages[1].content == "", f"Unexpected content for second message: {all_messages[1].content}"
assert all_messages[1].finish_reason is None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ async def test_google_ai_streaming_chat_completion_with_function_choice_behavior
)

# Validate the second message
assert all_messages[1].role == "assistant", f"Unexpected role for second message: {all_messages[1].role}"
assert all_messages[1].role == "tool", f"Unexpected role for second message: {all_messages[1].role}"
assert all_messages[1].content == "", f"Unexpected content for second message: {all_messages[1].content}"
assert all_messages[1].finish_reason is None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ async def test_vertex_ai_streaming_chat_completion_with_function_choice_behavior
)

# Validate the second message
assert all_messages[1].role == "assistant", f"Unexpected role for second message: {all_messages[1].role}"
assert all_messages[1].role == "tool", f"Unexpected role for second message: {all_messages[1].role}"
assert all_messages[1].content == "", f"Unexpected content for second message: {all_messages[1].content}"
assert all_messages[1].finish_reason is None

Expand Down

0 comments on commit af0b481

Please sign in to comment.