-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Python: Preserve auto function call text #14019
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pragnyanramtha
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
pragnyanramtha:codex/python-preserve-auto-invoke-text
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+238
−3
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
python/tests/unit/connectors/ai/test_chat_completion_client_base.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| from semantic_kernel.connectors.ai.chat_completion_client_base import ChatCompletionClientBase | ||
| from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceBehavior | ||
| from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings | ||
| from semantic_kernel.contents import ( | ||
| ChatHistory, | ||
| ChatMessageContent, | ||
| FunctionCallContent, | ||
| FunctionResultContent, | ||
| TextContent, | ||
| ) | ||
| from semantic_kernel.contents.utils.author_role import AuthorRole | ||
| from semantic_kernel.filters.filter_types import FilterTypes | ||
| from semantic_kernel.functions.kernel_arguments import KernelArguments | ||
| from semantic_kernel.functions.kernel_function_decorator import kernel_function | ||
| from semantic_kernel.kernel import Kernel | ||
|
|
||
|
|
||
| class StubChatCompletion(ChatCompletionClientBase): | ||
| SUPPORTS_FUNCTION_CALLING = True | ||
| responses: list[list[ChatMessageContent]] | ||
|
|
||
| async def _inner_get_chat_message_contents( | ||
| self, | ||
| chat_history: ChatHistory, | ||
| settings: PromptExecutionSettings, | ||
| ) -> list[ChatMessageContent]: | ||
| return self.responses.pop(0) | ||
|
|
||
|
|
||
| def _tool_call_message(text: str | None, call_id: str = "call_1") -> ChatMessageContent: | ||
| items = [] | ||
| if text is not None: | ||
| items.append(TextContent(text=text)) | ||
| items.append(FunctionCallContent(id=call_id, name="test-tool", arguments={})) | ||
| return ChatMessageContent(role=AuthorRole.ASSISTANT, items=items) | ||
|
|
||
|
|
||
| async def test_auto_function_call_preserves_intermediate_text_in_final_response() -> None: | ||
| kernel = Kernel() | ||
| kernel.add_function("test", kernel_function(lambda: "tool result", name="tool")) | ||
|
|
||
| chat_completion = StubChatCompletion( | ||
| ai_model_id="test-model", | ||
| responses=[ | ||
| [ | ||
| ChatMessageContent( | ||
| role=AuthorRole.ASSISTANT, | ||
| items=[ | ||
| TextContent(text="I'll check that."), | ||
| FunctionCallContent(id="call_1", name="test-tool", arguments={}), | ||
| ], | ||
| ) | ||
| ], | ||
| [ChatMessageContent(role=AuthorRole.ASSISTANT, content="The answer is ready.")], | ||
| ], | ||
| ) | ||
|
|
||
| result = await chat_completion.get_chat_message_contents( | ||
| chat_history=ChatHistory(system_message="Test"), | ||
| settings=PromptExecutionSettings(function_choice_behavior=FunctionChoiceBehavior.Auto()), | ||
| kernel=kernel, | ||
| arguments=KernelArguments(), | ||
| ) | ||
|
|
||
| assert result[0].content == "I'll check that.\n\nThe answer is ready." | ||
|
|
||
|
|
||
| async def test_auto_function_call_preserves_intermediate_text_after_max_attempts() -> None: | ||
| kernel = Kernel() | ||
| kernel.add_function("test", kernel_function(lambda: "tool result", name="tool")) | ||
|
|
||
| chat_completion = StubChatCompletion( | ||
| ai_model_id="test-model", | ||
| responses=[ | ||
| [_tool_call_message("I'll check that.")], | ||
| [ChatMessageContent(role=AuthorRole.ASSISTANT, content="The answer is ready.")], | ||
| ], | ||
| ) | ||
|
|
||
| result = await chat_completion.get_chat_message_contents( | ||
| chat_history=ChatHistory(system_message="Test"), | ||
| settings=PromptExecutionSettings( | ||
| function_choice_behavior=FunctionChoiceBehavior.Auto(maximum_auto_invoke_attempts=1), | ||
| ), | ||
| kernel=kernel, | ||
| arguments=KernelArguments(), | ||
| ) | ||
|
|
||
| assert result[0].content == "I'll check that.\n\nThe answer is ready." | ||
|
|
||
|
|
||
| async def test_auto_function_call_preserves_text_from_multiple_intermediate_responses() -> None: | ||
| kernel = Kernel() | ||
| kernel.add_function("test", kernel_function(lambda: "tool result", name="tool")) | ||
|
|
||
| chat_completion = StubChatCompletion( | ||
| ai_model_id="test-model", | ||
| responses=[ | ||
| [_tool_call_message("First tool call.", call_id="call_1")], | ||
| [_tool_call_message("Second tool call.", call_id="call_2")], | ||
| [ChatMessageContent(role=AuthorRole.ASSISTANT, content="The answer is ready.")], | ||
| ], | ||
| ) | ||
|
|
||
| result = await chat_completion.get_chat_message_contents( | ||
| chat_history=ChatHistory(system_message="Test"), | ||
| settings=PromptExecutionSettings(function_choice_behavior=FunctionChoiceBehavior.Auto()), | ||
| kernel=kernel, | ||
| arguments=KernelArguments(), | ||
| ) | ||
|
|
||
| assert result[0].content == "First tool call.\n\nSecond tool call.\n\nThe answer is ready." | ||
|
|
||
|
|
||
| async def test_auto_function_call_inserts_intermediate_text_when_final_response_has_no_text() -> None: | ||
| kernel = Kernel() | ||
| kernel.add_function("test", kernel_function(lambda: "tool result", name="tool")) | ||
|
|
||
| chat_completion = StubChatCompletion( | ||
| ai_model_id="test-model", | ||
| responses=[ | ||
| [_tool_call_message("I'll check that.")], | ||
| [ChatMessageContent(role=AuthorRole.ASSISTANT, items=[])], | ||
| ], | ||
| ) | ||
|
|
||
| result = await chat_completion.get_chat_message_contents( | ||
| chat_history=ChatHistory(system_message="Test"), | ||
| settings=PromptExecutionSettings(function_choice_behavior=FunctionChoiceBehavior.Auto()), | ||
| kernel=kernel, | ||
| arguments=KernelArguments(), | ||
| ) | ||
|
|
||
| assert result[0].content == "I'll check that." | ||
|
|
||
|
|
||
| async def test_auto_function_call_preserves_intermediate_text_when_filter_terminates() -> None: | ||
| kernel = Kernel() | ||
| kernel.add_function("test", kernel_function(lambda: "tool result", name="tool")) | ||
|
|
||
| @kernel.filter(FilterTypes.AUTO_FUNCTION_INVOCATION) | ||
| async def auto_invoke_terminate(context, next): | ||
| await next(context) | ||
| context.terminate = True | ||
|
|
||
| chat_completion = StubChatCompletion( | ||
| ai_model_id="test-model", | ||
| responses=[ | ||
| [_tool_call_message("I'll check that.")], | ||
| ], | ||
| ) | ||
|
|
||
| result = await chat_completion.get_chat_message_contents( | ||
| chat_history=ChatHistory(system_message="Test"), | ||
| settings=PromptExecutionSettings(function_choice_behavior=FunctionChoiceBehavior.Auto()), | ||
| kernel=kernel, | ||
| arguments=KernelArguments(), | ||
| ) | ||
|
|
||
| assert result[0].content == "I'll check that." | ||
| assert any(isinstance(item, FunctionResultContent) for item in result[0].items) | ||
|
|
||
|
|
||
| async def test_auto_function_call_preserves_final_response_when_intermediate_has_no_text() -> None: | ||
| kernel = Kernel() | ||
| kernel.add_function("test", kernel_function(lambda: "tool result", name="tool")) | ||
| final_response = ChatMessageContent(role=AuthorRole.ASSISTANT, content="The answer is ready.") | ||
|
|
||
| chat_completion = StubChatCompletion( | ||
| ai_model_id="test-model", | ||
| responses=[ | ||
| [ | ||
| ChatMessageContent( | ||
| role=AuthorRole.ASSISTANT, | ||
| items=[FunctionCallContent(id="call_1", name="test-tool", arguments={})], | ||
| ) | ||
| ], | ||
| [final_response], | ||
| ], | ||
| ) | ||
|
|
||
| result = await chat_completion.get_chat_message_contents( | ||
| chat_history=ChatHistory(system_message="Test"), | ||
| settings=PromptExecutionSettings(function_choice_behavior=FunctionChoiceBehavior.Auto()), | ||
| kernel=kernel, | ||
| arguments=KernelArguments(), | ||
| ) | ||
|
|
||
| assert result == [final_response] | ||
|
pragnyanramtha marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.