Python: Introduce the non-chat, non-streaming OpenAIAssistantAgent, samples, and tests (#7477)
### Motivation and Context
To align with SK dotnet and to allow SK Python devs to leverage the
OpenAI/Azure OpenAI assistants, this code is a required component for SK
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?
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're introducing the code to build non-chat, non-streaming
OpenAIAssistantAgents for both the OpenAI and Azure OpenAI styles. This
PR introduces:
- The required code to create OpenAI/AzureOpenAI assistants using the v2
library.
- Samples showing how to create the agent, and how to use the tool
resources like the code interpreter and file search.
- Unit test coverage at 100%
- Full mypy coverage
- Closes #7080

One to-do item:
- introduce Agent integration tests
Note: streaming assistant agent support will come in the next PR.
<!-- 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 :smile:
2024-08-01 09:45:06 -04:00
|
|
|
# Copyright (c) Microsoft. All rights reserved.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from semantic_kernel.exceptions.kernel_exceptions import KernelException
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AgentException(KernelException):
|
2024-08-05 18:33:49 +02:00
|
|
|
"""Base class for all agent exceptions."""
|
|
|
|
|
|
Python: Introduce the non-chat, non-streaming OpenAIAssistantAgent, samples, and tests (#7477)
### Motivation and Context
To align with SK dotnet and to allow SK Python devs to leverage the
OpenAI/Azure OpenAI assistants, this code is a required component for SK
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?
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're introducing the code to build non-chat, non-streaming
OpenAIAssistantAgents for both the OpenAI and Azure OpenAI styles. This
PR introduces:
- The required code to create OpenAI/AzureOpenAI assistants using the v2
library.
- Samples showing how to create the agent, and how to use the tool
resources like the code interpreter and file search.
- Unit test coverage at 100%
- Full mypy coverage
- Closes #7080

One to-do item:
- introduce Agent integration tests
Note: streaming assistant agent support will come in the next PR.
<!-- 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 :smile:
2024-08-01 09:45:06 -04:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AgentFileNotFoundException(AgentException):
|
2024-08-05 18:33:49 +02:00
|
|
|
"""The requested file was not found."""
|
|
|
|
|
|
Python: Introduce the non-chat, non-streaming OpenAIAssistantAgent, samples, and tests (#7477)
### Motivation and Context
To align with SK dotnet and to allow SK Python devs to leverage the
OpenAI/Azure OpenAI assistants, this code is a required component for SK
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?
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're introducing the code to build non-chat, non-streaming
OpenAIAssistantAgents for both the OpenAI and Azure OpenAI styles. This
PR introduces:
- The required code to create OpenAI/AzureOpenAI assistants using the v2
library.
- Samples showing how to create the agent, and how to use the tool
resources like the code interpreter and file search.
- Unit test coverage at 100%
- Full mypy coverage
- Closes #7080

One to-do item:
- introduce Agent integration tests
Note: streaming assistant agent support will come in the next PR.
<!-- 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 :smile:
2024-08-01 09:45:06 -04:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2024-08-19 14:04:44 -04:00
|
|
|
class AgentInitializationException(AgentException):
|
2024-08-05 18:33:49 +02:00
|
|
|
"""An error occurred while initializing the agent."""
|
|
|
|
|
|
Python: Introduce the non-chat, non-streaming OpenAIAssistantAgent, samples, and tests (#7477)
### Motivation and Context
To align with SK dotnet and to allow SK Python devs to leverage the
OpenAI/Azure OpenAI assistants, this code is a required component for SK
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?
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're introducing the code to build non-chat, non-streaming
OpenAIAssistantAgents for both the OpenAI and Azure OpenAI styles. This
PR introduces:
- The required code to create OpenAI/AzureOpenAI assistants using the v2
library.
- Samples showing how to create the agent, and how to use the tool
resources like the code interpreter and file search.
- Unit test coverage at 100%
- Full mypy coverage
- Closes #7080

One to-do item:
- introduce Agent integration tests
Note: streaming assistant agent support will come in the next PR.
<!-- 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 :smile:
2024-08-01 09:45:06 -04:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2024-08-19 14:04:44 -04:00
|
|
|
class AgentExecutionException(AgentException):
|
2024-08-05 18:33:49 +02:00
|
|
|
"""An error occurred while executing the agent."""
|
|
|
|
|
|
Python: Introduce the non-chat, non-streaming OpenAIAssistantAgent, samples, and tests (#7477)
### Motivation and Context
To align with SK dotnet and to allow SK Python devs to leverage the
OpenAI/Azure OpenAI assistants, this code is a required component for SK
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?
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're introducing the code to build non-chat, non-streaming
OpenAIAssistantAgents for both the OpenAI and Azure OpenAI styles. This
PR introduces:
- The required code to create OpenAI/AzureOpenAI assistants using the v2
library.
- Samples showing how to create the agent, and how to use the tool
resources like the code interpreter and file search.
- Unit test coverage at 100%
- Full mypy coverage
- Closes #7080

One to-do item:
- introduce Agent integration tests
Note: streaming assistant agent support will come in the next PR.
<!-- 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 :smile:
2024-08-01 09:45:06 -04:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2024-08-19 14:04:44 -04:00
|
|
|
class AgentInvokeException(AgentException):
|
2024-08-05 18:33:49 +02:00
|
|
|
"""An error occurred while invoking the agent."""
|
|
|
|
|
|
Python: Introduce the non-chat, non-streaming OpenAIAssistantAgent, samples, and tests (#7477)
### Motivation and Context
To align with SK dotnet and to allow SK Python devs to leverage the
OpenAI/Azure OpenAI assistants, this code is a required component for SK
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?
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're introducing the code to build non-chat, non-streaming
OpenAIAssistantAgents for both the OpenAI and Azure OpenAI styles. This
PR introduces:
- The required code to create OpenAI/AzureOpenAI assistants using the v2
library.
- Samples showing how to create the agent, and how to use the tool
resources like the code interpreter and file search.
- Unit test coverage at 100%
- Full mypy coverage
- Closes #7080

One to-do item:
- introduce Agent integration tests
Note: streaming assistant agent support will come in the next PR.
<!-- 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 :smile:
2024-08-01 09:45:06 -04:00
|
|
|
pass
|
2024-08-19 14:04:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class AgentChatException(AgentException):
|
|
|
|
|
"""An error occurred while invoking the agent chat."""
|
|
|
|
|
|
|
|
|
|
pass
|
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 :smile:
2025-01-24 07:30:54 +09:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class AgentChatHistoryReducerException(AgentException):
|
|
|
|
|
"""An error occurred while reducing the chat history."""
|
|
|
|
|
|
|
|
|
|
pass
|
2025-03-24 23:29:06 +00:00
|
|
|
|
|
|
|
|
|
2025-05-06 19:29:29 +09:00
|
|
|
class AgentThreadInitializationException(AgentException):
|
|
|
|
|
"""An error occurred while initializing the agent thread."""
|
|
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2025-03-24 23:29:06 +00:00
|
|
|
class AgentThreadOperationException(AgentException):
|
|
|
|
|
"""An error occurred while performing an operation on the agent thread."""
|
|
|
|
|
|
|
|
|
|
pass
|