-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Python: Azure AI Inference Function Calling #7035
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
Merged
TaoChenOSU
merged 17 commits into
microsoft:main
from
TaoChenOSU:taochen/python-maas-function-calling
Jul 9, 2024
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
70a1f66
Add function calling t non-streaming, next streaming and sample
TaoChenOSU 3df6683
streaming
TaoChenOSU 20a7a7a
Configure function call behavior
TaoChenOSU 11af91e
Comments
TaoChenOSU e8dfbb1
Fix unit test
TaoChenOSU a97fca8
Address comments
TaoChenOSU 27557b2
Merge branch 'main' into local-branch-azure-ai-inference-function-cal…
TaoChenOSU 44ec18e
Override _prepare_chat_history_for_request
TaoChenOSU 6cb10fb
update_settings_from_function_call_configuration
TaoChenOSU 25c6b87
Merge branch 'main' into taochen/python-maas-function-calling
TaoChenOSU 5fe4a11
Merge branch 'main' into taochen/python-maas-function-calling
TaoChenOSU b4a14a2
Merge branch 'main' into taochen/python-maas-function-calling
TaoChenOSU 6438c84
Address comments
TaoChenOSU 5e692ed
Merge branch 'main' into taochen/python-maas-function-calling
TaoChenOSU b51c21d
_process_function_calls -> _invoke_function_calls
TaoChenOSU 91cee27
Merge branch 'main' into taochen/python-maas-function-calling
TaoChenOSU 003e539
Merge branch 'main' into taochen/python-maas-function-calling
TaoChenOSU 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
346 changes: 246 additions & 100 deletions
346
...ic_kernel/connectors/ai/azure_ai_inference/services/azure_ai_inference_chat_completion.py
Large diffs are not rendered by default.
Oops, something went wrong.
135 changes: 135 additions & 0 deletions
135
python/semantic_kernel/connectors/ai/azure_ai_inference/services/utils.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,135 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| import logging | ||
| from collections.abc import Callable | ||
|
|
||
| from azure.ai.inference.models import ( | ||
| AssistantMessage, | ||
| ChatCompletionsFunctionToolCall, | ||
| ChatRequestMessage, | ||
| FunctionCall, | ||
| ImageContentItem, | ||
| ImageDetailLevel, | ||
| ImageUrl, | ||
| SystemMessage, | ||
| TextContentItem, | ||
| ToolMessage, | ||
| UserMessage, | ||
| ) | ||
|
|
||
| from semantic_kernel.contents.chat_message_content import ChatMessageContent | ||
| from semantic_kernel.contents.function_call_content import FunctionCallContent | ||
| from semantic_kernel.contents.function_result_content import FunctionResultContent | ||
| from semantic_kernel.contents.image_content import ImageContent | ||
| from semantic_kernel.contents.text_content import TextContent | ||
| from semantic_kernel.contents.utils.author_role import AuthorRole | ||
|
|
||
| logger: logging.Logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def _format_system_message(message: ChatMessageContent) -> SystemMessage: | ||
| """Format a system message to the expected object for the client. | ||
|
|
||
| Args: | ||
| message: The system message. | ||
|
|
||
| Returns: | ||
| The formatted system message. | ||
| """ | ||
| return SystemMessage(content=message.content) | ||
|
|
||
|
|
||
| def _format_user_message(message: ChatMessageContent) -> UserMessage: | ||
| """Format a user message to the expected object for the client. | ||
|
|
||
| If there are any image items in the message, we need to create a list of content items, | ||
| otherwise we need to just pass in the content as a string or it will error. | ||
|
|
||
| Args: | ||
| message: The user message. | ||
|
|
||
| Returns: | ||
| The formatted user message. | ||
| """ | ||
| if not any(isinstance(item, (ImageContent)) for item in message.items): | ||
| return UserMessage(content=message.content) | ||
|
|
||
| contentItems = [] | ||
| for item in message.items: | ||
| if isinstance(item, TextContent): | ||
| contentItems.append(TextContentItem(text=item.text)) | ||
| elif isinstance(item, ImageContent) and (item.data_uri or item.uri): | ||
| contentItems.append( | ||
| ImageContentItem(image_url=ImageUrl(url=item.data_uri or str(item.uri), detail=ImageDetailLevel.Auto)) | ||
| ) | ||
| else: | ||
| logger.warning( | ||
| "Unsupported item type in User message while formatting chat history for Azure AI" | ||
| f" Inference: {type(item)}" | ||
| ) | ||
|
|
||
| return UserMessage(content=contentItems) | ||
|
|
||
|
|
||
| def _format_assistant_message(message: ChatMessageContent) -> AssistantMessage: | ||
| """Format an assistant message to the expected object for the client. | ||
|
|
||
| Args: | ||
| message: The assistant message. | ||
|
|
||
| Returns: | ||
| The formatted assistant message. | ||
| """ | ||
| contentItems = [] | ||
| toolCalls = [] | ||
|
|
||
| for item in message.items: | ||
| if isinstance(item, TextContent): | ||
| contentItems.append(TextContentItem(text=item.text)) | ||
| elif isinstance(item, FunctionCallContent): | ||
| toolCalls.append( | ||
| ChatCompletionsFunctionToolCall( | ||
| id=item.id, function=FunctionCall(name=item.name, arguments=item.arguments) | ||
| ) | ||
| ) | ||
| else: | ||
| logger.warning( | ||
| "Unsupported item type in Assistant message while formatting chat history for Azure AI" | ||
| f" Inference: {type(item)}" | ||
| ) | ||
|
|
||
| # tollCalls cannot be an empty list, so we need to set it to None if it is empty | ||
| return AssistantMessage(content=contentItems, tool_calls=toolCalls if toolCalls else None) | ||
|
|
||
|
|
||
| def _format_tool_message(message: ChatMessageContent) -> ToolMessage: | ||
| """Format a tool message to the expected object for the client. | ||
|
|
||
| Args: | ||
| message: The tool message. | ||
|
|
||
| Returns: | ||
| The formatted tool message. | ||
| """ | ||
| if len(message.items) != 1: | ||
| logger.warning( | ||
| "Unsupported number of items in Tool message while formatting chat history for Azure AI" | ||
| f" Inference: {len(message.items)}" | ||
| ) | ||
|
|
||
| if not isinstance(message.items[0], FunctionResultContent): | ||
| logger.warning( | ||
| "Unsupported item type in Tool message while formatting chat history for Azure AI" | ||
| f" Inference: {type(message.items[0])}" | ||
| ) | ||
|
|
||
| # The API expects the result to be a string, so we need to convert it to a string | ||
| return ToolMessage(content=str(message.items[0].result), tool_call_id=message.items[0].id) | ||
|
|
||
|
|
||
| MESSAGE_CONVERTERS: dict[AuthorRole, Callable[[ChatMessageContent], ChatRequestMessage]] = { | ||
| AuthorRole.SYSTEM: _format_system_message, | ||
| AuthorRole.USER: _format_user_message, | ||
| AuthorRole.ASSISTANT: _format_assistant_message, | ||
| AuthorRole.TOOL: _format_tool_message, | ||
| } |
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
28 changes: 10 additions & 18 deletions
28
python/semantic_kernel/connectors/ai/function_calling_utils.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
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
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
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
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.