Commit Graph
397 Commits
Author SHA1 Message Date
Tao Chen e1184aea74 Python: Add embedding and text completion samples (#10797)
### Motivation and Context

<!-- 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.
-->
Closing https://github.com/microsoft/semantic-kernel/issues/9963

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
1. Add text embedding and text completion samples.
2. Move the embedding generator base to a
`semantic_kernel.connectors.ai`.

### 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-03-05 19:39:07 +00:00
Evan Mattson e7beb6a143 Python: Update Cross Language Test to get raw request and now raw response (#10799)
### Motivation and Context

In our cross language tests we were getting the raw request using a
custom logging transport class. This didn't show how to get the raw HTTP
response back after making the call to the OpenAI SDK.

<!-- 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 PR updates the cross language test to get the raw request and now
the raw response.

<!-- 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-03-05 19:18:59 +00:00
Evan Mattson fb70825df7 Python: Improve handling the Dapr process context (#10725)
### Motivation and Context

There is a bug in the current Dapr runtime code that prohibits trying to
get the Dapr process context out from a process once it's finished. This
is related to incorrect handling of serializing/deserializing the Dapr
process context. Additionally, in a state that handles
`KernelProcessStepState`, due to Dapr serialization constraints, the
step step injected to a step is not the actual object. We use Pydantic's
`model_validate` to get the actual step object, but and we need to make
sure we re-assign that object back to the underlying step state so the
objects don't diverge (`model_validate` creates a new object).

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

Improve the handling for the Dapr process context so we're properly
serializing data through the Dapr process.
- Fixes the issue brought up in the discussion: #10234

<!-- 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-03-05 16:08:15 +00:00
Eduard van ValkenburgandEvan Mattson de54193a0f Python: Introducing Realtime Clients for OpenAI and Azure OpenAI (#10127)
### Motivation and Context

<!-- 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.
-->
Implements the OpenAI Realtime API with Semantic Kernel

### Description

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

Adds a RealtimeClientBase
Adds RealtimeEvents
Adds OpenAI and Azure Realtime clients

Closes: #10108

### 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 😄

---------

Co-authored-by: Evan Mattson <[email protected]>
2025-03-04 07:44:10 +00:00
Tao Chen 8fd6da2bd8 Python: Improve Bedrock service setup docs (#10772)
### Motivation and Context

<!-- 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.
-->
Closing: https://github.com/microsoft/semantic-kernel/issues/9910

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
1. Add detailed instructions on how to setup one's environment to use
Bedrock services, including models and agents.
2. Re-enable bedrock integration tests.
3. Some other cleanups.

### 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-03-04 00:22:13 +00:00
Tao Chen 75453a1219 Python: Move to a thinner wrap for Bedrock Agent in Python (#10740)
### Motivation and Context

<!-- 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.
-->
Though the current Bedrock agent implementation uses a "thin wrap"
approach, it still has some abstractions that we don't want to maintain.
Just like what we have done for the other agents, we remove those
abstractions.

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
1. Update the Bedrock agent abstraction to be thinner.
2. Update samples and tests.

Note: The .Net implementation doesn't need to be updated as it was
already using a thinner wrap.
Note: This breaks the current usage pattern for Bedrock agents in
Python.

### 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
- [ ] I didn't break anyone 😄
2025-03-03 22:07:41 +00:00
Evan Mattson 0153073aa3 Python: Bump Python version to 1.22.1 for a release (#10751)
### Motivation and Context

Bump Python version to 1.22.1 for a release

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

Bump Python version to 1.22.1 for a release

<!-- 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-03-03 12:03:18 +00:00
Evan Mattson fd27470c86 Python: Allow plugins via agent constructors. Update samples. (#10707)
### Motivation and Context

In Semantic Kernel, our goal is to simplify working with agents. When an
agent is created without a specified kernel, one is automatically
created on the user’s behalf. This enables developers to pass in a list
of plugins that are then added to the underlying kernel. If both the
agent constructor and the provided kernel include plugins, those
specified via the agent constructor take precedence—since plugins are
stored in a dictionary, any duplicate names result in the agent’s plugin
overwriting the existing one.

For the `ChatCompletionAgent`, usage is further simplified by allowing a
`ChatCompletionClientBase` service to be passed via the constructor. If
a kernel is supplied and already contains a chat completion service, the
new service is added to the kernel. Moreover, if no execution settings
are provided via KernelArguments, the first service registered on the
kernel is used.

New usage to create a ChatCompletionAgent

```python
agent = ChatCompletionAgent(
    service=AzureChatCompletion(),
    name="<name>",
    instructions="<instructions>",
)
```

New usage to create a ChatCompletionAgent with plugins:

```python
agent = ChatCompletionAgent(
    service=AzureChatCompletion(),
    name="<name>",
    instructions="<instructions>",
    plugins=[SamplePlugin()],
)
```

Previously, the `service_id` constructor argument was retained after
introducing KernelArguments. However, with our transition to using the
kernel’s AI service selector, the `service_id` parameter has become
redundant. As we move from the experimental phase toward a release
candidate, removing `service_id` is a necessary, albeit breaking,
change.

The `getting_started_with_agents/chat_completion` samples now begin by
demonstrating the simplest way to configure the agent. The subsequent
step illustrates the original method for setting up the chat completion
service on the kernel. Similarly, for plugins, the initial approach is
the easiest to follow, while the following step shows the traditional
method of managing the kernel.

Updated documentation will follow, once these changes are released in a
new package.

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

Simplifying interactions with agents.
- Updated the `ChatCompletionAgent`, `AzureAssistantAgent`, and
`OpenAIAssistantAgent` classes with the `release_candidate` decorator in
place of the `experimental` decorator.
- Add a chat completion agent structured outputs sample
- Closes #10604

<!-- 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
- [ ] I didn't break anyone 😄
2025-02-28 01:39:36 +00:00
Eduard van Valkenburg 16d48bbe0b Python: lazy create stores in integration tests (#10705)
### Motivation and Context

<!-- 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.
-->
Moved the import and creation of the store into methods that get called
when accessed only.

### Description

<!-- 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-02-27 16:44:25 +00:00
Tao ChenandEvan Mattson 15ca7a7192 Python: Agent get_response API (#10701)
### Motivation and Context

<!-- 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.
-->
With the recent push for improving developer's quality of life, we have
decided to make it easier for developers to get started with agents by
introducing a new `get_response` API, that hides the complexity of
interacting with agents when developers are ramping up with the Agent
concept.

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
1. Add the `get_response` API on all agent types. This API skips hides
the intermediate steps of the agent execution and only returns the final
results as a single message.
2. Add tests and samples.
3. Updates the `Agent` class to be an `ABC`, and adds `get_response`,
`invoke`, and `invoke_stream` methods as overridable.

### 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 😄

---------

Co-authored-by: Evan Mattson <[email protected]>
2025-02-27 20:22:05 +09:00
Eduard van ValkenburgandTao Chen 536edaa585 Python: Introducing the Chroma Connector with the new vector store design (#10678)
### Motivation and Context

<!-- 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.
-->
Adds ChromaStore and ChromaConnector

Closes: #9926

### Description

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

---------

Co-authored-by: Tao Chen <[email protected]>
2025-02-27 08:13:17 +00:00
Evan Mattson 174bdabf38 Python: Introduce feature decorator to allow for experimental and release candidate decorator usage (#10691)
### Motivation and Context

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

This change is required to improve the flexibility and maintainability
of our feature annotation system. Previously, separate decorators (e.g.,
`experimental_function` and `experimental_class`) were used to mark
experimental features, resulting in code duplication and limiting our
ability to handle additional feature stages. As our SDK evolves, we need
a unified approach that can support multiple stages—such as
experimental, release candidate, and future states—while also allowing
version information for release candidate features to be centrally
managed.

### Description

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

This PR refactors our feature decorators by introducing a unified
`stage` decorator that updates the docstring and attaches metadata for
both functions and classes. Two convenience decorators, `experimental`
and `release_candidate`, are built on top of `stage`:
- The `experimental` decorator marks features as experimental and sets
an `is_experimental` attribute.
- The `release_candidate` decorator supports multiple usage patterns
(with or without parentheses and with an optional version parameter) to
mark features as release candidate and sets an `is_release_candidate`
attribute.

This unified approach reduces duplication, simplifies the codebase, and
lays the groundwork for easily extending feature stages in the future.

This decorator supports the following usage patterns:
- `@experimental` (for both classes and functions)
- `@release_candidate`  (no parentheses)
- `@release_candidate()`  (empty parentheses)
- `@release_candidate("1.21.3-rc1")`  (positional version)
- `@release_candidate(version="1.21.3-rc1")`  (keyword version)

### 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-02-27 00:17:54 +00:00
Tao Chen 60df17d37d Python: Improve agent getting started samples (#10667)
### Motivation and Context

<!-- 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.
-->
We are improving developer experience by updating the agent getting
started samples.

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Simplify the agent getting started samples by removing unnecessary code
and add excessive comments to make sure we are only doing what we need
to get started.

### 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-02-26 22:32:47 +00:00
Evan Mattson 9e979f8463 Python: Improve/Simplify the Assistant Agents (#10666)
### Motivation and Context

The current abstractions we have around the Semantic Kernel Assistant
Agents are considered to be "fully-wrapped." This is beneficial in that
SK provides a tight/complete abstraction over the agent; however, it
doesn't scale well when new features are added to the underlying APIs
(OpenAI) - it requires updates from SK to make each and every update. It
also adds a lot more complexity in terms of needing to handle everything
in SK, requires more code coverage, and chances for bugs to surface due
to these complexities.

> **NOTE:** this update is _breaking_. We're moving to simplify the
abstractions/APIs before we move Assistant Agents from **experimental**
to GA.

Similar to how the `AzureAIAgent` was created, we're moving to a
"less-wrapped" abstraction around the OpenAI Assistant v2 APIs. We do
have some "convenience methods" on the Agents, via class methods, to
configure things like the code interpreter tool and resource, file
search tool and resource, the clients required to interact with the
Assistants, as well as being able to easily configure structured
outputs, similar to how it is done with a ChatCompletion (either provide
the Pydantic model or the Python class).

To work with Assistant Agents now, it's as simple as:

```python
from semantic_kernel.agents.open_ai import AzureAssistantAgent
from semantic_kernel.functions import kernel_function

class MenuPlugin:
   # Plugin code here using the @kernel_function decorator

# Create the client using Azure OpenAI resources and configuration
client, model = AzureAssistantAgent.setup_resources()

# Create the assistant definition
definition = await client.beta.assistants.create(
    model=model,
    instructions="Answer questions about the menu.",
    name="Host",
)

agent = AzureAssistantAgent(
    client=client,
    definition=definition,
    plugins=[MenuPlugin()],  # The plugins can be passed in as a list to the constructor
)

# Note: plugins can also be configured on the Kernel and passed in as a parameter to the OpenAIAssistantAgent

# Define a thread and invoke the agent with the user input
thread = await agent.client.beta.threads.create()
```

**I will create a migration guide (issue) showing what needs to be
updated from the previous usage to this new pattern.**

<!-- 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 PR provides the new abstractions for the
`AzureAssistantAgent`/`OpenAIAssistantAgent` classes.
- Updates underlying code
- Updates samples showing new patterns
- Updates unit tests
- Updates READMEs.
- Closes #10605 

Samples will be updated accordingly to show new patterns.

<!-- 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
- [ ] I didn't break anyone 😄
2025-02-26 10:59:14 +00:00
K. Andrew Parker d91c96d4e1 Python: resolving #10642 - public events fail to propagate (#10643)
### Motivation and Context

Resolves #10642 -- failure of `KernelProcessEventVisibility.Public`
events to propagate.

### Description

First, `KernelProcessEvent` was configured to use enum values -- though
throughout the code, `KernelProcessEvent.visibility` is compared as an
enum.

Second, when handling events emitted by a KernelProcessStep, any
`Public` visibility events are/were passed through
`LocalStep.scoped_event` (which modifies the event's namespace) *before*
queueing up any matching edges on the source step. This change to the
event namespace effectively prevents the event from propagating (since I
don't know how we'd 'listen' for events on the local process handle from
within the function that builds that very process...)

Allowing `visibility` on `KernelProcessEvent` to be an enum, combined
with an exchange in the order of processing event messages (matching
event to edge _before_ re-emitting any events marked as 'Public') causes
public events to propagate as expected.

### Contribution Checklist

- [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-02-25 21:17:58 +00:00
Eduard van Valkenburg 534d8609e3 Python: Introducing AzureCosmosDBforMongoDB store and collection (#10609)
### Motivation and Context

<!-- 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.
-->
Adds Azure Cosmos DB for MongoDB support using the new vector store
model.
Builds on the MongoDB Atlas integration, replaced the index creation and
settings to make it work.

Closes #6836

### Description

<!-- 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-02-24 11:16:10 +00:00
Evan MattsonandChris 2984423aa0 Python: Add support for AutoGen's 0.2 ConversableAgent (#10607)
### Motivation and Context

For those who use AutoGen's 0.2 `ConversableAgent`, we're providing
support in Semantic Kernel to run this agent type. This assumes one will
port their existing AG 0.2.X `ConversableAgent` code and run in Semantic
Kernel.

Note: as we move towards GA for our agent chat patterns, we are
analyzing how to support AutoGen agents with a shared time. This PR does
not provide support for the AG `ConversibleAgent` group chat patterns
that exist in the 0.2.X package.

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

Add support and samples for the AG 0.2.X ConversableAgent. 
- Add unit test coverage.
- Add samples and READMEs.
- Closes #10407

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

---------

Co-authored-by: Chris <[email protected]>
2025-02-20 11:11:05 +00:00
Evan Mattson 68448b1427 Python: Fix kernel parameter metadata input type arg to call type_ (#10603)
### Motivation and Context

To address a typing issue, we updated the kernel parameter metadata so
that the input type argument now calls `type` (configured as an alias)
rather than the model’s parameter `type_`. Previously, setting the alias
did not update the underlying `type_` attribute, leaving it as `None`.
As a result, when generating the JSON schema, the `parameter_type` was
omitted, causing some customers who sent `KernelFunctionMetadata` to the
OpenAI model to receive a 400 error due to a malformed schema.

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

Revert the change and add a unit test to make sure we check for the
expected schema.

<!-- 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-02-19 10:40:40 +00:00
Eduard van ValkenburgandEvan Mattson 374d8f91ce Python: filter improvements (#10588)
### Motivation and Context

<!-- 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.
-->
Fixes some inconsistency in the filter setup between dotnet and python
Adds a sample for how to use a filter to retry a prompt with a different
model.
Adds a sample for semantic caching

Closes: #10572 #5924 #10595

### Description

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

---------

Co-authored-by: Evan Mattson <[email protected]>
2025-02-19 00:40:51 +00:00
Evan Mattson 5cad2864df Python: Fix Infinite Recursion in Hashing Logic by Tracking Visited Objects (#10598)
### Motivation and Context

We encountered an issue where our hashing utility (`make_hashable(...)`)
could enter an infinite recursion loop whenever it encountered cyclical
references. This was particularly evident with Pydantic models that
reference each other (or themselves), or with dictionaries/lists that
recursively include a reference to an already-seen object. In such
scenarios, the function continued to expand and re-expand the same
objects indefinitely, triggering a RecursionError.

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

We introduced a visited dictionary in `make_hashable`. Before diving
deeper into any object -- whether it’s a dict, a list, a set, a tuple,
or a Pydantic model -- we record its id() in visited. The very next time
we see that same object ID, we immediately return its already-computed
placeholder or final representation, short-circuiting the cycle.
- Fixes #10592

<!-- 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-02-18 22:55:04 +00:00
Evan Mattson 76d101eafd Python: add AzureAIAgent client creation method. Handle user_agent. (#10538)
### Motivation and Context

We weren't sending the user_agent as part of client creation for the
AzureAIAgent. Create a convenience class method to create the
AIProjectClient, which handles creating the user_agent.

<!-- 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 PR:
- adds a class method to create the AIProjectClient as part of
AzureAIAgent.
- handles configuring the user_agent.
- fixes a check where annotation content indices might not be present
- adds `/.default` to default entra scope
- adds unit tests
- closes #10527
- closes #10520

<!-- 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-02-14 05:20:24 +00:00
a03c5d6fdc Python: removes AWS "us." region prefix from model id (#10329)
### Motivation and Context

relates to this
[issue](https://github.com/microsoft/semantic-kernel/issues/10238).

<!-- 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
The current implementation of `bedrock_model_provider.py` is hard coded
to return the zeroth element when splitting `model_id` on (`.`) periods.
However this will return an incorrect name for the provider when working
with an [inference
profile](https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles.html)
since the `model_id` will contain the AWS region in the ID. For example,
a `model_id` can be `us.meta.llama3-XXX:0` and the call
`to_model_provider` will incorrectly try to look for a `us` model. This
error is thrown when we are selecting an LLM model using an AWS
inference profile.

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
I have made a change to the `to_model_provider` class method in the
`bedrock_model_provider.py` script that checks for a region prefix and
then strips it. I have not tested this against all AWS regions.

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] 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
- [ ] All unit tests pass, and I have added new tests where possible
- [X] I didn't break anyone 😄

---------

Co-authored-by: Tao Chen <[email protected]>
Co-authored-by: Chris <[email protected]>
2025-02-13 22:22:50 +00:00
Evan Mattson 784f13e400 Python: Fix Chat History Agent format instructions regression. (#10512)
### Motivation and Context

The chat history agent templating was inadvertently broken. This PR
restores the functionality to be able to use templating with a Chat
Completion Agent.

<!-- 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 the templating for a Chat Completion Agent. Add unit tests for more
coverage.
- Fix a unit test fixture that was annotated to return an AsyncMock, but
it's actually returning an async generator function.

<!-- 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-02-13 11:53:21 +00:00
892c2066da Python: Adding Crew.AI as a plugin. (#10474)
### Motivation and Context

Adding Crew.AI as a plugin to the Semantic Kernel will allow users to
leverage the capabilities of Crew.AI in app. This plugin works with
Crews that have been deployed to the Crew.AI Enterprise service.

### Description

Adding Crew.AI as a plugin to the Semantic Kernel will allow users to
leverage the capabilities of Crew.AI in app. This plugin works with
Crews that have been deployed to the Crew.AI Enterprise service.

The plugin functionality has been manually verified with the concept
samples. Other tests will come in the next update.

### 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 😄

---------

Co-authored-by: Ben Thomas <[email protected]>
Co-authored-by: Eduard van Valkenburg <[email protected]>
2025-02-13 04:19:14 +00:00
Eduard van Valkenburg cbfd7e9330 Python: improved content inits, added ndarray support for binary content and small fixes to defaults (#10469)
### Motivation and Context

<!-- 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.
-->
This PR adds support for ndarray's as the content carrier for all binary
content types (binary, image, audio) as that is more optimized for
larger content.

It also does some fixes to the initialization of those content types and
the underlying data uri type.

Also some fixes for unspecified default param in pydantic Field,
language servers do not recognize `Field("default value")` as having a
default, so changed those occurances to `Field(default="default value")`

### Description

<!-- 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-02-12 16:59:08 +00:00
Eduard van Valkenburg 7bdf55028c Python: experimental py3.13 support (#10276)
### Motivation and Context

<!-- 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.
-->
Some small fixes in uv setup within actions
Added experimental unit tests runs for py3.13
First typing improvement from pyright and pyright config.

### Description

<!-- 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-02-12 13:05:39 +00:00
Eduard van Valkenburg 9d5ad78fb2 Python: set openai package to newer version and removed default value of parallel tool calls (#10502)
### Motivation and Context

<!-- 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.
-->
We noticed that the openai package dependency spec was off and did not
update that when present to the newest version, fixed now.
Also we mistakenly had a default value for parallel_tool_calls in our
prompt executions settings, as with other settings we do not maintain a
default value if the API has a default and in this case having that
parameter when calling a reasoning model threw an error, fixed here.

### Description

<!-- 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-02-12 10:16:33 +00:00
Evan Mattson 8f4efa5d10 Python: Allow for factory callbacks in the process framework (#10451)
### Motivation and Context

In the current Python process framework, in runtimes like Dapr, there is
no easy way to pass complex (unserializable) dependencies to a step --
this includes things like a ChatCompletion service or an agent of type
ChatCompletionAgent.

Similar to how the kernel dependency was propagated to the step_actor or
process_actor, we're introducing the ability to specify a factory
callback that will be called as the step is instantiated. The factory is
created, if specified via the optional kwarg when adding a step to the
process builder like:

```python
myBStep = process.add_step(step_type=BStep, factory_function=bstep_factory)
```

The `bstep_factory` looks like (along with its corresponding step)

```python
async def bstep_factory():
    """Creates a BStep instance with ephemeral references like ChatCompletionAgent."""
    kernel = Kernel()
    kernel.add_service(AzureChatCompletion())

    agent = ChatCompletionAgent(kernel=kernel, name="echo", instructions="repeat the input back")
    step_instance = BStep()
    step_instance.agent = agent

    return step_instance

class BStep(KernelProcessStep):
    """A sample BStep that optionally holds a ChatCompletionAgent.

    By design, the agent is ephemeral (not stored in state).
    """

    # Ephemeral references won't be persisted to Dapr
    # because we do not place them in a step state model.
    # We'll set this in the factory function:
    agent: ChatCompletionAgent | None = None

    @kernel_function(name="do_it")
    async def do_it(self, context: KernelProcessStepContext):
        print("##### BStep ran (do_it).")
        await asyncio.sleep(2)

        if self.agent:
            history = ChatHistory()
            history.add_user_message("Hello from BStep!")
            async for msg in self.agent.invoke(history):
                print(f"BStep got agent response: {msg.content}")

        await context.emit_event(process_event="BStepDone", data="I did B")
```

Although this isn't explicitly necessary with the local runtime, the
factory callback will also work, if desired.

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

Adds the ability to specify a factory callback for a step in the process
framework.
- Adjusts the Dapr FastAPI demo sample to show how one can include a
dependency like a `ChatCompletionAgent` and use the factory callback for
`BStep`. Although the output from the agent isn't needed, it
demonstrates the capability to handle these types of dependencies while
running Dapr.
- Adds unit tests
- Closes #10409

<!-- 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-02-11 01:39:08 +00:00
Evan Mattson c3ab713322 Python: Add support for Azure AI Agent Service (#10414)
### Motivation and Context

Azure AI Agents are a managed offering, and are built on top of the
OpenAI assistant v2 APIs. Until now we have not had support for them in
SK Python. This PR adds support for Azure AI Agents in SK Python. The
current abstractions allow one to run managed Azure AI Agents using SK
constructs as is demonstrated via the getting started samples and/or the
`azure_ai_agent` concept code samples. The developer must first follow
the required Azure AI Agent deployment steps, and bring their connection
string as well as their model deployment name.

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

Add support for Azure AI Agent Service in SK
- Add sample code
- Add unit tests
- Restructures the `getting_started_with_agents` samples to move the
respective agent code into folders like `chat_completion`,
`azure_ai_agent`, etc. Updates the README as well.
- Closes #10187 

**TODO**: add more test coverage either in this PR, or a subsequent one.

<!-- 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-02-07 02:11:10 +00:00
Eduard van ValkenburgandEvan Mattson c445f5ce51 Python: Implement MongoDB Atlas store (#10177)
Implement a new version of the MongoDB Atlas Memory Connector with
MongoDBAtlasStore and MongoDBAtlasCollection classes.

* **MongoDBAtlasStore**:
- Add
`python/semantic_kernel/connectors/memory/mongodb_atlas/mongodb_atlas_store.py`
to implement the MongoDBAtlasStore class.
- Add unit tests in
`python/tests/unit/connectors/memory/mongodb_atlas/test_mongodb_atlas_store.py`
to test the MongoDBAtlasStore class.

* **MongoDBAtlasCollection**:
- Add
`python/semantic_kernel/connectors/memory/mongodb_atlas/mongodb_atlas_collection.py`
to implement the MongoDBAtlasCollection class.
- Add unit tests in
`python/tests/unit/connectors/memory/mongodb_atlas/test_mongodb_atlas_collection.py`
to test the MongoDBAtlasCollection class.

---

For more details, open the [Copilot Workspace
session](https://copilot-workspace.githubnext.com/microsoft/semantic-kernel/pull/10177?shareId=e9e73180-0fff-4e60-bbb5-5c3493b27e61).

closes #6764

---------

Co-authored-by: Evan Mattson <[email protected]>
2025-02-06 03:49:29 +00:00
Tao ChenandEvan Mattson 35641b7213 Python: Allow AI Inference connector to use Azure AI Services resource (#10427)
### Motivation and Context

<!-- 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.
-->
Addresses: https://github.com/microsoft/semantic-kernel/issues/10339

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Users can now use the Azure AI Services resource with the AI Inference
connector.

### 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 😄

Co-authored-by: Evan Mattson <[email protected]>
2025-02-06 00:15:53 +00:00
Tao Chen ce6002d9d2 Python: Add Bedrock agent integration tests (#10400)
### Motivation and Context

<!-- 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.
-->
Integration tests for Bedrock agents in Python

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
1. Add integration tests for Bedrock agent integration in
`python/tests/integration/agents/bedrock_agent`.
2. Restructure the agent concept samples folder.

### 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
- [ ] I didn't break anyone 😄

---

For more details, open the [Copilot Workspace
session](https://copilot-workspace.githubnext.com/microsoft/semantic-kernel/pull/10400?shareId=42d1f5cd-0a90-48c3-810f-bb2a6744026e).
2025-02-05 20:12:54 +00:00
Tao Chen 0a8c4d8afa Python: Update agent tracing span name and attributes (#10398)
### Motivation and Context

<!-- 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.
-->
Python parity with this PR:
https://github.com/microsoft/semantic-kernel/pull/10387

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Update agent invoke span name and add attributes.

This is a breaking change: users who are currently relying on the span
name in their queries will need to update their queries to use the new
name or match new patterns. This is an experimental feature so breaking
changes are expected.

### 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
- [ ] I didn't break anyone 😄
2025-02-05 00:37:03 +00:00
Tao ChenandEvan Mattson 2a6cdd81a7 Python: Add unit tests for Bedrock agent integration (#10362)
### Motivation and Context
Add unit tests for Bedrock agent integration in Semantic Kernel Python.

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

  3. What problem does it solve?
  5. What scenario does it contribute to?
  7. If it fixes an open issue, please link to the issue here.
-->

### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Code coverage for Bedrock agent related implementations reached 100%
except for the `bedrock_agent.py` file and `bedrock_agent_base.py` file,
which are at 82% and 95% respectively.


### 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 😄


For more details, open the [Copilot Workspace
session](https://copilot-workspace.githubnext.com/microsoft/semantic-kernel/pull/10362?shareId=f83b0466-6d2b-4639-88c8-23229aa2694c).

---------

Co-authored-by: Evan Mattson <[email protected]>
2025-02-05 00:29:04 +00:00
7c10d5f7f5 Python: Fixed Python bug when enable_json_response=True (#10376)
### Description
Fixed Python bug when enable_json_response=True

- Closes #10375

### Contribution Checklist

- [Y] The code builds clean without any errors or warnings
- [Y] 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
- [Y] All unit tests pass, and I have added new tests where possible
- [Y] I didn't break anyone 😄

Fixes #10375

---------

Co-authored-by: Adit Sheth <[email protected]>
Co-authored-by: Evan Mattson <[email protected]>
2025-02-05 00:16:56 +00:00
Tao Chen 1b85aa902c Python: Fix OpenAI assistant metadata type (#10383)
### Motivation and Context

<!-- 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.
-->
OpenAI Python SDK released 1.61.0 that contained a change which broke
code.

Changes in the release:
https://github.com/openai/openai-python/commit/12b87a4a1e6cb071a6b063d089585dec56a5d534

### Description

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

This pull request includes several changes to the `metadata` type and
the handling of `metadata` in various files, as well as updates to test
files to reflect these changes. The most important changes include
modifying the `metadata` type from `dict[str, Any]` to `dict[str, str]`
and updating the tests to use `json.dumps` for `metadata` values.

Note: this is a breaking change because if users are not passing in the
expected type before, they will need to update their code to pass in the
correct type now.

### 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
- [ ] I didn't break anyone 😄
2025-02-04 01:48:29 +00:00
Tao Chen 7789c5fe55 Python: Bedrock agent (#10307)
### Motivation and Context

<!-- 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.
-->
Address https://github.com/microsoft/semantic-kernel/issues/10284

Integration of AWS Bedrock agent to SK Python

### Description
1. Implementation of Bedrock agent in SK (essentially an agent connector
to the Bedrock agent service).
2. A dedicated channel that works with Bedrock agents.
3. Samples on how to use Bedrock agents.

Unit and integration tests will be included in another PR to prevent
this PR from becoming too big.

<!-- 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-01-31 19:44:39 +00:00
K. Andrew ParkerandEvan Mattson 10c1dfecb7 Python: [python] Bug fix 10340 (#10341)
### Motivation and Context

See #10340 

### Description

Implement a similar approach as is done in
`process_step_edge_builder.py`, which supports function_name when
building the target function.

### Contribution Checklist

- [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 😄

(well, we're still passing/xpass/xfail the same as main is)

---------

Co-authored-by: Evan Mattson <[email protected]>
2025-01-31 01:47:37 +00:00
Evan Mattson 605db09bce Python: intro allowed content types in chat history channel receive. Add mixed chat image sample. (#10347)
### Motivation and Context

During a group chat, any file reference content created by an assistant
agent, doesn't need to be communicated to a chat completion agent.
Filter these types out and only include other types, like text, if
available.

<!-- 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 PR:
- Adds a mixed chat image sample to have an assistant agent generate an
image, along with text, and call the chat completion agent successfully
with only the allowed types (like text).
- Adds a unit test to exercise the same behavior.
- Closes #10317

<!-- 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-01-30 23:36:47 +00:00
Rob Emanuele 3724e245be Python: Add vector search to Postgres connector (#10213)
### Motivation and Context

Following up on #8951, this PR adds an implementation of
`VectorSearchBase` to `PostgresCollection`. This implementation provides
vectorized search and does not implement text search or vectorizable
text search.

Unit and integration tests are added, and the
`python/samples/getting_started/third_party/postgres-memory.ipynb`
notebook was expanded to include vector search in the example.

### Contribution Checklist

- [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-01-30 15:44:49 +00:00
Evan Mattson 44034ebd23 Python: Improve hashing of cmc and scmc items. Add tests. (#10332)
### Motivation and Context

We have some content types that can't be hashed as-is. This PR makes
sure that if there are unhashable types, that we turn them into hashable
types. Add unit tests to exercise the scenarios.

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

<!-- 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-01-29 07:54:42 +00:00
ec9b980e30 Python: improve agent samples and chat history handling (#10301)
### Motivation and Context

<!-- 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.
-->
When passing the same object to `agent.reduce_history` as is present in
the `history_reducer` attribute of the agent that the function doesn't
accurately behave. This fixes that.

Also updates the sample to be a bit more concise.

Also fixes the way the single_dispatch is setup in ChatHistory.

Also ensures system/developer messages are not reduced away as that
might impact performance.

The `reduce_history` method was removed from the agent base class, in
favor of having the caller manage the change history and reduction as
needed. The `reduce_history` was added to the agent group chat, as the
chat history is managed internally as agents are invoked.

### Description

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

---------

Co-authored-by: Evan Mattson <[email protected]>
Co-authored-by: Evan Mattson <[email protected]>
2025-01-29 00:48:26 +00:00
Evan Mattson 7e206318dd Python: Allow enums to be passed in to on_function_result. Improve handling FRC result so it can be hashed. (#10316)
### Motivation and Context

Two topics in this PR:
- A request to be able to pass in Enums to the Python process
`on_function_result`, instead of just strings.
- There were some custom types (Pydantic models) that were unable to
hash during an agent group chat. One could mark the model `frozen` so
that it is hashable; however, we should add better handling in our code,
in case a dev doesn't want to do that.

<!-- 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 PR:
- checks if the function name in `on_function_result` in our process
framework is an enum, if so, convert to a string.
- improves the parsing of the `result` in `FunctionResultContent` so
that it can be included in the hash function during an agent group chat.
Adding tests to handle multiple different scenarios.
- Closes #10299 
- Closes #10305

<!-- 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-01-28 21:35:37 +00:00
Evan Mattson 1a84cb64b2 Python: prompt template config and kernel args support for agents (#10298)
### Motivation and Context

To bring the SK Python framework closer to .Net parity, we need to
support the ability to specify a prompt template config and kernel
arguments for ChatCompletion Agents as well as for Assistant Agents.

<!-- 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 PR adds support for:
- being able to specify a prompt template config for SK agents.
- allows kernel arguments to be passed to the agent as well as during
invoke - invoke args, similar to other kwargs, take precedence over
agent level config.
- adds test coverage
- creates agent templating samples for both ChatCompletionAgents and
OpenAI/AzureAssistantAgents
- closes #8022 
- closes #8699

<!-- 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-01-28 04:41:16 +00:00
Evan Mattson 5098ba39a8 Python: Improve handling for kernel plugin from file. (#10286)
### Motivation and Context

This PR addresses an issue in the `KernelPlugin.from_directory()` method
where classes without any `@kernel_function` decorated methods are still
being initialized during plugin loading.

The fix makes sure that only classes with at least one
`@kernel_function` decorated method are instantiated and included as
plugins. Classes without `@kernel_function` methods are now skipped
entirely.

<!-- 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 PR:
- Updates the `from_python_file` to inspect each class for
`@kernel_function` decorated methods before instantiation.
- Updates the `add_plugin` method `plugin_name` to make sure it is
indeed a string, per the type hint.
   - Adds a unit test to exercise the new behavior
- Closes #10280

<!-- 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-01-24 18:00:28 +00:00
Evan Mattson 4d29656780 Python: Introduce the chat history reducer (#10190)
### Motivation and Context

The SK Python framework has been missing the ability to configure a chat
history reducer of type `ChatHistoryTruncationReducer` and
`ChatHistorySummarizationReducer` which have existed in the .Net SK
Agent framework for some time.

The goal of this PR is to introduce the chat history reducers and allow
them for use for not only the agent framework, but also anything else
that uses a chat history (chat completion, for example). The
ChatHistoryReducer extends the ChatHistory class, and so it's simple to
include a reducer and logic to reduce messages as one manages the chat
history either in an agent framework setting or in a chat completion
setting.

<!-- 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 PR:
- Introduces the chat history reducer functionality in Python -- both
the `ChatHistoryTruncationReducer` and
`ChatHistorySummarizationReducer`.
- Add unit tests for code coverage.
- Adds a sample `Chat Completion History Reducer` to show how to
configure both reducers and what each parameter does.
- Add chat completion samples showing how to manage a chat history
reducer, including how to specify that function call content and
function result content should be part of the summarization payload.
- Updates the Agent SelectionStrategy, KernelFunctionSelectionStrategy
and KernelFunctionTermination strategy to use the reducer.
- Additionally updates the classes above to use a new `select_agent`
abstract method so that one can define an initial agent to run in a
particular scenario.
- Removes the deprecated `FunctionCallBehavior` class, and removes some
nasty circular dependencies that we had lurking in the code base for
some time. This `FunctionCallBehavior` has been marked with a
deprecation warning for 6+ months now. All samples and docs have moved
over to use `FunctionChoiceBehavior` - developers using
`FunctionCallBehavior` should have had enough time to switch.
- Closes #7969
- Closes #10102

<!-- 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-01-23 22:30:54 +00:00
Eduard van Valkenburg e44f70d9d6 Python: bug fixes from graphrag insights (#10270)
### Motivation and Context

<!-- 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.
-->
Based on insights from the graphrag team, several bug fixes related to
data and model validators.

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
For all cases of model_validator in before mode, there is no guarantee
that the data passed is a dict,
especially when a model is used as a field in another model, then a
string is passed to the before validator.
So in those cases we check before doing logic, otherwise just pass it on
and let Pydantic handle the invalid data.

Some small logic fixes in the vector store model decorator:
- updated docstring with the right exceptions
- additional checks on attributes
- additional checks on VectorFields with tests

### 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-01-23 20:22:29 +00:00
Tao ChenandEvan Mattson 6ecfdcfda9 Python: Add agent invocation spans (#10255)
### Motivation and Context

<!-- 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.
-->
Address feature request:
https://github.com/microsoft/semantic-kernel/issues/10174

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
This pull request introduces a new decorator for tracing agent
invocations and applies it to various methods in the
`ChatCompletionAgent` and `OpenAIAssistantAgent` classes. Additionally,
it includes new unit tests to ensure the decorator is correctly applied
and functions as expected.

### Key Changes:

**Decorator Implementation:**
* Added a new `trace_agent_invocation` decorator in
`python/semantic_kernel/utils/telemetry/agent_diagnostics/decorators.py`
to trace agent invocations using OpenTelemetry.

**Decorator Application:**
* Applied the `trace_agent_invocation` decorator to the `invoke` and
`invoke_stream` methods in `ChatCompletionAgent`
(`python/semantic_kernel/agents/chat_completion/chat_completion_agent.py`).
[[1]](diffhunk://#diff-0f0a27c107368504c4347c88528b7b4234dcead1919005bcae13f5d16d6cf26dR19)
[[2]](diffhunk://#diff-0f0a27c107368504c4347c88528b7b4234dcead1919005bcae13f5d16d6cf26dR80)
[[3]](diffhunk://#diff-0f0a27c107368504c4347c88528b7b4234dcead1919005bcae13f5d16d6cf26dR134)
* Applied the `trace_agent_invocation` decorator to the `invoke` and
`invoke_stream` methods in `OpenAIAssistantAgent`
(`python/semantic_kernel/agents/open_ai/open_ai_assistant_base.py`).
[[1]](diffhunk://#diff-70e75c57136d3a90d045af4b8d59fb8e8101251d7c5126458bb5d9e6c36556a4R46)
[[2]](diffhunk://#diff-70e75c57136d3a90d045af4b8d59fb8e8101251d7c5126458bb5d9e6c36556a4R608)
[[3]](diffhunk://#diff-70e75c57136d3a90d045af4b8d59fb8e8101251d7c5126458bb5d9e6c36556a4R861)

### 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 😄

---------

Co-authored-by: Evan Mattson <[email protected]>
2025-01-22 16:26:18 +09:00
Evan Mattson 3a7f2a3b73 Python: Grab function_name from kwargs if present in ProcessStepEdgeBuilder (#10245)
### Motivation and Context

In the ProcessStepEdgeBuilder, we allow one to pass in a target of type
`"ProcessFunctionTargetBuilder | ProcessStepBuilder"`. If the target
`isinstance(target, ProcessStepBuilder)`, we construct the
`ProcessFunctionTargetBuilder`. We weren't grabbing the `function_name`
from the kwargs if it was presented, resulting in an exception that the
function_name needs to be provided because multiple kernel functions
exist.

<!-- 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 PR:
- correctly passes in the `function_name` kwargs, if present.
- Adds a unit test to exercise functionality

<!-- 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-01-21 22:50:46 +00:00
Evan MattsonandEduard van Valkenburg 71e5040a78 Python: Update sort step method for assistant invoke. (#10191)
### Motivation and Context

At times, when the (Azure) OpenAI Assistant Agent makes a tool call,
that tool call's creation timestamp is coming after the message creation
timestamp (the message creation being the text that the assistant
responds with). Currently in our code, if we have a tool call
(`FunctionCallContent`), we first yield that, and then we make a call to
get completed steps, to then yield more content like
`FunctionResultContent` and `TextContent`. There will be two steps (or
more, depending upon the number of tool calls).

Right now we sort the completed steps in this way:

```python
completed_steps_to_process: list[RunStep] = sorted(
    [s for s in steps if s.completed_at is not None and s.id not in processed_step_ids],
    key=lambda s: s.created_at,
)
```
When there are no failures, it's because the tool call was created
before the final message content (as has been since this assistant was
first coded). However, it appears that processing on the server-side can
cause fluctuations in when the steps are created/processed. When we have
a failure, we now have the message_creation (`TextContent`) yielded
before the `FunctionResultContent`, which if sent to an (Azure) OpenAI
Chat Completion endpoint will break with a 400 because of the gap in the
ordering between the `FunctionCallContent` and the
`FunctionResultContent`:

```
FunctionCallContent
TextContent # this should follow `FunctionResultContent` (and it does during times when we don't see a 400)
FunctionResultContent
```

The 400 error isn't 100% repeatable because of server-side processing,
so we will get the correct ordering:

```
FunctionCallContent
FunctionResultContent
TextContent
```

This PR updates the ordering of how we sort of the completed steps -- if
we have a step_type == "tool_calls" and "message_creation", we sort to
allow for "tool_calls" to come first, and any ties are broken by the
step.completed_at timestamp. With this update, we no longer receive 400s
because the ordering of the content types is correct when sending to an
OpenAI model.

<!-- 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 PR:
- Updates the way we sort completed messages during an assistant invoke.
- Adds logging for the assistant invoke method at the `info` level.
- Closes #10141

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

---------

Co-authored-by: Eduard van Valkenburg <[email protected]>
2025-01-20 19:29:13 +00:00