Files
semantic-kernel/python/samples/concepts
Evan Mattson fb0e418a7c Python: Fix for AzureAIAgent streaming func call invocations. Other fixes/improvements for tool call content types, and logging. (#12335)
### Motivation and Context

The AzureAIAgent streaming function call invocation is broken when
handling more than one required function call from the service. There is
nested logic to handle passing the tool calls back to the service
because it requires one to pass in a handler.

In this PR, we're now controlling everything from the main loop and are
passing back the handler as the event stream for further processing.
This does require some logic to determine if we have an initial async
context manager (based on the first call to create the run stream) or if
we're now using the handler from submitting tool calls. We can now see
that when we have multiple functions involved, we are able to get the
results as we expect:

```
Sample Output:

# User: 'What is the price of the special drink and then special food item added together?'
Function Call:> MenuPlugin-get_specials with arguments: {}
Function Result:> 
        Special Soup: Clam Chowder
        Special Salad: Cobb Salad
        Special Drink: Chai Tea
        for function: MenuPlugin-get_specials
Function Call:> MenuPlugin-get_item_price with arguments: {"menu_item": "Chai Tea"}
Function Call:> MenuPlugin-get_item_price with arguments: {"menu_item": "Clam Chowder"}
Function Result:> $9.99 for function: MenuPlugin-get_item_price
Function Result:> $9.99 for function: MenuPlugin-get_item_price
Function Call:> MathPlugin-Add with arguments: {"input":9.99,"amount":9.99}
Function Result:> 19.98 for function: MathPlugin-Add
# AuthorRole.ASSISTANT: The price of the special drink, Chai Tea, is $9.99 and the price of the special food 
    item, Clam Chowder, is $9.99. Added together, the total price is $19.98.
```

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

Fix handling multiple function calls for the streaming AzureAIAgent.
- Fixes a missing `quote` from the url citation for streaming annotation
content
- Shows the correct way to retrieve Bing Grounding tool calls in the two
concept samples.
- Adds handling for tool content content for OpenAPI plugins.
- Adds logging for streaming agent invocation tool calls.
- Relaxes the handling of the `FunctionResultContent` id attribute to be
`str | None` similar to that of `FunctionCallContent`. The type hints
for `id` have always been `str | None`, but the actual type hint on the
attribute has been `str`. We don't always get a `FunctionResultContent`
id from the Azure Agent Service during streaming invocations - message
addition via the overide `__add__` method still work properly.
- Move the AzureAIAgent pkg dependencies to be installed by default,
instead of requiring the `azure` extra.
- Closes #12312
- Closes #12328
- Closes #12331
- Closes #12324

<!-- 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 😄
2025-06-03 01:11:56 +00:00
..

Semantic Kernel Concepts by Feature

Table of Contents

Agents - Creating and using agents in Semantic Kernel

Azure AI Agent

Bedrock Agent

Chat Completion Agent

Mixed Agent Group Chat

OpenAI Assistant Agent

OpenAI Responses Agent

Audio - Using services that support audio-to-text and text-to-audio conversion

AutoFunctionCalling - Using Auto Function Calling to allow function call capable models to invoke Kernel Functions automatically

ChatCompletion - Using ChatCompletion messaging capable service with models

ChatHistory - Using and serializing the ChatHistory

Filtering - Creating and using Filters

Functions - Invoking Method or Prompt functions with Kernel

Grounding - An example of how to perform LLM grounding

Local Models - Using the OpenAI connector and OnnxGenAI connector to talk to models hosted locally in Ollama, OnnxGenAI, and LM Studio

Logging - Showing how to set up logging

Memory - Using Memory AI concepts

Model-as-a-Service - Using models deployed as serverless APIs on Azure AI Studio to benchmark model performance against open-source datasets

On Your Data - Examples of using AzureOpenAI On Your Data

Plugins - Different ways of creating and using Plugins

Processes - Examples of using the Process Framework

PromptTemplates - Using Templates with parametrization for Prompt rendering

RAG - Different ways of RAG (Retrieval-Augmented Generation)

Reasoning - Using ChatCompletion to reason with OpenAI Reasoning

Search - Using Search services information

Service Selector - Shows how to create and use a custom service selector class

Setup - How to set up environment variables for Semantic Kernel

Structured Outputs - How to leverage OpenAI's json_schema Structured Outputs functionality

TextGeneration - Using TextGeneration capable service with models

Configuring the Kernel

In Semantic Kernel for Python, we leverage Pydantic Settings to manage configurations for AI and Memory Connectors, among other components. Here’s a clear guide on how to configure your settings effectively:

Steps for Configuration

  1. Reading Environment Variables:

    • Primary Source: Pydantic first attempts to read the required settings from environment variables.
  2. Using a .env File:

    • Fallback Source: If the required environment variables are not set, Pydantic will look for a .env file in the current working directory.
    • Custom Path (Optional): You can specify an alternative path for the .env file via env_file_path. This can be either a relative or an absolute path.
  3. Direct Constructor Input:

    • As an alternative to environment variables and .env files, you can pass the required settings directly through the constructor of the AI Connector or Memory Connector.

Microsoft Entra Token Authentication

To authenticate to your Azure resources using a Microsoft Entra Authentication Token, the AzureChatCompletion AI Service connector now supports this as a built-in feature. If you do not provide an API key -- either through an environment variable, a .env file, or the constructor -- and you also do not provide a custom AsyncAzureOpenAI client, an ad_token, or an ad_token_provider, the AzureChatCompletion connector will attempt to retrieve a token using the DefaultAzureCredential.

To successfully retrieve and use the Entra Auth Token, you need the Cognitive Services OpenAI Contributor role assigned to your Azure OpenAI resource. By default, the https://cognitiveservices.azure.com token endpoint is used. You can override this endpoint by setting an environment variable .env variable as AZURE_OPENAI_TOKEN_ENDPOINT or by passing a new value to the AzureChatCompletion constructor as part of the AzureOpenAISettings.

Best Practices

  • .env File Placement: We highly recommend placing the .env file in the semantic-kernel/python root directory. This is a common practice when developing in the Semantic Kernel repository.

By following these guidelines, you can ensure that your settings for various components are configured correctly, enabling seamless functionality and integration of Semantic Kernel in your Python projects.