Files
semantic-kernel/python/samples/learn_resources
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
..

SK Python Documentation Examples

This project contains a collection of examples used in documentation on learn.microsoft.com.

Prerequisites

  • Python 3.10 and above
  • Install Semantic Kernel through PyPi:
    pip install semantic-kernel
    

Configuring the sample

The samples can be configured with a .env file in the project which holds api keys and other secrets and configurations.

Make sure you have an Open AI API Key or Azure Open AI service key

Copy the .env.example file to a new file named .env. Then, copy those keys into the .env file:

GLOBAL_LLM_SERVICE="OpenAI" # Toggle between "OpenAI" or "AzureOpenAI"

OPENAI_CHAT_MODEL_ID="gpt-3.5-turbo-0125"
OPENAI_TEXT_MODEL_ID="gpt-3.5-turbo-instruct"
OPENAI_API_KEY=""
OPENAI_ORG_ID=""

AZURE_OPENAI_CHAT_DEPLOYMENT_NAME="gpt-35-turbo"
AZURE_OPENAI_TEXT_DEPLOYMENT_NAME="gpt-35-turbo-instruct"
AZURE_OPENAI_ENDPOINT=""
AZURE_OPENAI_API_KEY=""
AZURE_OPENAI_API_VERSION=""

Note: if running the examples with VSCode, it will look for your .env file at the main root of the repository.

Running the sample

To run the console application within Visual Studio Code, just hit F5. Otherwise the sample can be run via the command line:

python.exe <absolute_path_to_sk_code>/python/samples/documentation_examples/planner.py