Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: ChatCompletionAgent constructor and invocation improvements #10604

Closed
moonbox3 opened this issue Feb 19, 2025 · 3 comments · Fixed by #10707
Closed

Python: ChatCompletionAgent constructor and invocation improvements #10604

moonbox3 opened this issue Feb 19, 2025 · 3 comments · Fixed by #10707
Assignees
Labels
agents python Pull requests for the Python Semantic Kernel

Comments

@moonbox3
Copy link
Contributor

Supporting the simplified agent scenario:

# necessary imports

async def main():
    agent = ChatCompletionAgent(
        service=OpenAIChatCompletion(),
        plugins=[DatePlugin(), WeatherPlugin()], #optional
        instructions="you are a helpful assistant that can answer questions about the world", #optional
    )

    print(await agent.invoke("What is the capital of France?"))

if __name__ == "__main__":
    asyncio.run(main())

as well as make updates for invoke: from AsyncIterable -> couroutine.

@moonbox3 moonbox3 added agents python Pull requests for the Python Semantic Kernel labels Feb 19, 2025
@moonbox3 moonbox3 self-assigned this Feb 19, 2025
@moonbox3 moonbox3 changed the title Python: agent "quality of life" updates Python: ChatCompletionAgent constructor and invocation improvements Feb 19, 2025
@eavanvalkenburg
Copy link
Member

Wouldn't you need Kernel as well in order to do the function calling and have things like Filters available as well?

# necessary imports

async def main():
    kernel.add_plugins([DatePlugin(), WeatherPlugin()])
    agent = ChatCompletionAgent(
        service=OpenAIChatCompletion(),
        kernel=kernel
        instructions="you are a helpful assistant that can answer questions about the world", #optional
    )

    print(await agent.invoke("What is the capital of France?"))

if __name__ == "__main__":
    asyncio.run(main())

@moonbox3
Copy link
Contributor Author

Today, if a kernel is not provided, we create one. So we'll use a model_validator after the model is formed, and add the plugins to the kernel for the caller. This is considered the "easiest" of easy paths where someone wants to write the most minimal amount of code to get started. We will still support that path where one can configure the kernel and pass it in, which you could say is the "medium/intermediate" path. Passing in plugins via the constructor will take precedence over any plugins added previously.

@eavanvalkenburg
Copy link
Member

gotcha, thanks for clarifying, that sounds good, less is more!

@crickman crickman moved this to Backlog in Semantic Kernel Feb 21, 2025
@crickman crickman moved this from Backlog to Backlog: Planned in Semantic Kernel Feb 21, 2025
github-merge-queue bot pushed a commit that referenced this issue Feb 28, 2025
### 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 😄
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agents python Pull requests for the Python Semantic Kernel
Projects
Status: Backlog: Planned
Development

Successfully merging a pull request may close this issue.

2 participants