Skip to content

Commit ff19970

Browse files
authored
Python: Support Declarative Spec for OpenAIAssistantAgent & OpenAIResponsesAgent (#12247)
### Motivation and Context This PR builds on the initial work/abstractions from #11982. The `AzureAssistantAgent` / `OpenAIAssistantAgent` as well as the `AzureResponsesAgent` / `OpenAIResponsesAgent` now support declarative specs. Several examples on how to declare the spec are shown in the respective dirs in `samples/concepts/agents`, as well as a simple getting started example for each agent type. To further simplify working with these two agents, the `setup_resources()` methods for each agent type have been marked with a deprecation tag. The new method to use is `OpenAIAssistantAgent.create_client()` or `OpenAIResponsesAgent.create_client()`. Further, when one needs to access the underlying setting's, it's simple to do so with a settings object and accessing the attribute. In the previous PR for the AzureAIAgent, it lacked support for templating - this is included in this current PR. <!-- 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 Bring in support for the declarative single agent spec for these agent types. - Add samples - Add unit tests - Add support for prompt templates for all current agent types - Closes #11979 <!-- 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 😄
1 parent 4027c5e commit ff19970

File tree

102 files changed

+4342
-1133
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+4342
-1133
lines changed

python/samples/concepts/README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,22 @@
6161
- [Mixed Chat Reset](./agents/mixed_chat/mixed_chat_reset.py)
6262
- [Mixed Chat Streaming](./agents/mixed_chat/mixed_chat_streaming.py)
6363

64-
#### [OpenAI Assistant Agent](../../semantic_kernel/agents/open_ai/open_ai_assistant_agent.py)
64+
#### [OpenAI Assistant Agent](../../semantic_kernel/agents/open_ai/openai_assistant_agent.py)
6565

66+
- [Azure OpenAI Assistant Declarative Code Interpreter](./agents/openai_assistant/azure_openai_assistant_declarative_code_interpreter.py)
67+
- [Azure OpenAI Assistant Declarative File Search](./agents/openai_assistant/azure_openai_assistant_declarative_file_search.py)
68+
- [Azure OpenAI Assistant Declarative Function Calling From File](./agents/openai_assistant/azure_openai_assistant_declarative_function_calling_from_file.py)
69+
- [Azure OpenAI Assistant Declarative Templating](./agents/openai_assistant/azure_openai_assistant_declarative_templating.py)
70+
- [Azure OpenAI Assistant Declarative With Existing Agent ID](./agents/openai_assistant/azure_openai_assistant_declarative_with_existing_agent_id.py)
6671
- [OpenAI Assistant Auto Function Invocation Filter Streaming](./agents/openai_assistant/openai_assistant_auto_func_invocation_filter_streaming.py)
6772
- [OpenAI Assistant Auto Function Invocation Filter](./agents/openai_assistant/openai_assistant_auto_func_invocation_filter.py)
6873
- [OpenAI Assistant Chart Maker Streaming](./agents/openai_assistant/openai_assistant_chart_maker_streaming.py)
6974
- [OpenAI Assistant Chart Maker](./agents/openai_assistant/openai_assistant_chart_maker.py)
75+
- [OpenAI Assistant Declarative Code Interpreter](./agents/openai_assistant/openai_assistant_declarative_code_interpreter.py)
76+
- [OpenAI Assistant Declarative File Search](./agents/openai_assistant/openai_assistant_declarative_file_search.py)
77+
- [OpenAI Assistant Declarative Function Calling From File](./agents/openai_assistant/openai_assistant_declarative_function_calling_from_file.py)
78+
- [OpenAI Assistant Declarative Templating](./agents/openai_assistant/openai_assistant_declarative_templating.py)
79+
- [OpenAI Assistant Declarative With Existing Agent ID](./agents/openai_assistant/openai_assistant_declarative_with_existing_agent_id.py)
7080
- [OpenAI Assistant File Manipulation Streaming](./agents/openai_assistant/openai_assistant_file_manipulation_streaming.py)
7181
- [OpenAI Assistant File Manipulation](./agents/openai_assistant/openai_assistant_file_manipulation.py)
7282
- [OpenAI Assistant Retrieval](./agents/openai_assistant/openai_assistant_retrieval.py)
@@ -79,6 +89,12 @@
7989

8090
#### [OpenAI Responses Agent](../../semantic_kernel/agents/open_ai/openai_responses_agent.py)
8191

92+
- [Azure OpenAI Responses Agent Declarative File Search](./agents/openai_responses/azure_openai_responses_agent_declarative_file_search.py)
93+
- [Azure OpenAI Responses Agent Declarative Function Calling From File](./agents/openai_responses/azure_openai_responses_agent_declarative_function_calling_from_file.py)
94+
- [Azure OpenAI Responses Agent Declarative Templating](./agents/openai_responses/azure_openai_responses_agent_declarative_templating.py)
95+
- [OpenAI Responses Agent Declarative File Search](./agents/openai_responses/openai_responses_agent_declarative_file_search.py)
96+
- [OpenAI Responses Agent Declarative Function Calling From File](./agents/openai_responses/openai_responses_agent_declarative_function_calling_from_file.py)
97+
- [OpenAI Responses Agent Declarative Web Search](./agents/openai_responses/openai_responses_agent_declarative_web_search.py)
8298
- [OpenAI Responses Message Callback Streaming](./agents/openai_responses/responses_agent_message_callback_streaming.py)
8399
- [OpenAI Responses Message Callback](./agents/openai_responses/responses_agent_message_callback.py)
84100
- [OpenAI Responses File Search Streaming](./agents/openai_responses/responses_agent_file_search_streaming.py)

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_declarative_azure_ai_search.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
from azure.identity.aio import DefaultAzureCredential
66

7-
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings
8-
from semantic_kernel.agents.agent import AgentRegistry
7+
from semantic_kernel.agents import AgentRegistry, AzureAIAgent, AzureAIAgentSettings
98
from semantic_kernel.contents.chat_message_content import ChatMessageContent
109
from semantic_kernel.contents.function_call_content import FunctionCallContent
1110
from semantic_kernel.contents.function_result_content import FunctionResultContent

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_declarative_bing_grounding.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
from azure.identity.aio import DefaultAzureCredential
66

7-
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings
8-
from semantic_kernel.agents.agent import AgentRegistry
7+
from semantic_kernel.agents import AgentRegistry, AzureAIAgent, AzureAIAgentSettings
98

109
"""
1110
The following sample demonstrates how to create an Azure AI agent that answers

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_declarative_code_interpreter.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
from azure.ai.agents.models import FilePurpose
77
from azure.identity.aio import DefaultAzureCredential
88

9-
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings
10-
from semantic_kernel.agents.agent import AgentRegistry
9+
from semantic_kernel.agents import AgentRegistry, AzureAIAgent, AzureAIAgentSettings
1110

1211
"""
1312
The following sample demonstrates how to create an Azure AI agent that answers

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_declarative_file_search.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,20 @@
66
from azure.ai.agents.models import VectorStore
77
from azure.identity.aio import DefaultAzureCredential
88

9-
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings
10-
from semantic_kernel.agents.agent import AgentRegistry
9+
from semantic_kernel.agents import AgentRegistry, AzureAIAgent, AzureAIAgentSettings
1110

1211
"""
1312
The following sample demonstrates how to create an Azure AI agent that answers
14-
user questions using the file search tool.
15-
16-
The agent is used to answer user questions that require file search to help ground
17-
answers from the model.
13+
user questions using the file search tool from a declarative spec.
1814
"""
1915

2016
# Define the YAML string for the sample
2117
spec = """
2218
type: foundry_agent
2319
name: FileSearchAgent
24-
description: Agent with code interpreter tool.
20+
description: Agent with file search tool.
2521
instructions: >
26-
Use the code interpreter tool to answer questions that require code to be generated
27-
and executed.
22+
Use the file search tool to answer questions from the user.
2823
model:
2924
id: ${AzureAI:ChatModelId}
3025
connection:

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_declarative_openapi.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
from azure.identity.aio import DefaultAzureCredential
66

7-
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings
8-
from semantic_kernel.agents.agent import AgentRegistry
7+
from semantic_kernel.agents import AgentRegistry, AzureAIAgent, AzureAIAgentSettings
98

109
"""
1110
The following sample demonstrates how to create an Azure AI agent that answers
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
3+
import asyncio
4+
5+
from azure.identity.aio import DefaultAzureCredential
6+
7+
from semantic_kernel.agents import AgentRegistry, AzureAIAgent
8+
9+
"""
10+
The following sample demonstrates how to create an Azure AI Agent that invokes
11+
a story generation task using a prompt template and a declarative spec.
12+
"""
13+
14+
# Define the YAML string for the sample
15+
spec = """
16+
type: foundry_agent
17+
name: StoryAgent
18+
description: An agent that generates a story about a topic.
19+
instructions: Tell a story about {{$topic}} that is {{$length}} sentences long.
20+
model:
21+
id: ${AzureAI:ChatModelId}
22+
connection:
23+
connection_string: ${AzureAI:Endpoint}
24+
inputs:
25+
topic:
26+
description: The topic of the story.
27+
required: true
28+
default: Cats
29+
length:
30+
description: The number of sentences in the story.
31+
required: true
32+
default: 2
33+
outputs:
34+
output1:
35+
description: The generated story.
36+
template:
37+
format: semantic-kernel
38+
"""
39+
40+
41+
async def main():
42+
async with (
43+
DefaultAzureCredential() as creds,
44+
AzureAIAgent.create_client(credential=creds) as client,
45+
):
46+
try:
47+
# Create the AzureAI Agent from the YAML spec
48+
agent: AzureAIAgent = await AgentRegistry.create_from_yaml(
49+
yaml_str=spec,
50+
client=client,
51+
)
52+
53+
# Invoke the agent for the specified task
54+
async for response in agent.invoke(
55+
messages=None,
56+
):
57+
print(f"# {response.name}: {response}")
58+
finally:
59+
# Cleanup: Delete the agent, vector store, and file
60+
await client.agents.delete_agent(agent.id)
61+
62+
"""
63+
Sample output:
64+
65+
# StoryAgent: Under the silvery moon, three mischievous cats tiptoed across the rooftop, chasing
66+
shadows and sharing secret whispers. By dawn, they curled up together, purring softly, dreaming
67+
of adventures yet to come.
68+
"""
69+
70+
71+
if __name__ == "__main__":
72+
asyncio.run(main())

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_declarative_with_existing_agent_id.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
from azure.identity.aio import DefaultAzureCredential
66

7-
from semantic_kernel.agents import AzureAIAgent
8-
from semantic_kernel.agents.agent import AgentRegistry
7+
from semantic_kernel.agents import AgentRegistry, AzureAIAgent
98

109
"""
1110
The following sample demonstrates how to create an Azure AI agent based

python/samples/concepts/agents/mixed_chat/mixed_chat_agents_plugins.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
from typing import Annotated
55

66
from semantic_kernel.agents import AgentGroupChat, AzureAssistantAgent, ChatCompletionAgent
7-
from semantic_kernel.agents.strategies.termination.termination_strategy import TerminationStrategy
7+
from semantic_kernel.agents.strategies import TerminationStrategy
88
from semantic_kernel.connectors.ai import FunctionChoiceBehavior
9-
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
9+
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, AzureOpenAISettings
1010
from semantic_kernel.contents import AuthorRole
1111
from semantic_kernel.functions import KernelArguments, kernel_function
1212
from semantic_kernel.kernel import Kernel
@@ -86,11 +86,11 @@ async def main():
8686
)
8787

8888
# Create the Assistant Agent using Azure OpenAI resources
89-
client, model = AzureAssistantAgent.setup_resources()
89+
client = AzureAssistantAgent.create_client()
9090

9191
# Create the assistant definition
9292
definition = await client.beta.assistants.create(
93-
model=model,
93+
model=AzureOpenAISettings().chat_deployment_name,
9494
name=COPYWRITER_NAME,
9595
instructions=COPYWRITER_INSTRUCTIONS,
9696
)

python/samples/concepts/agents/mixed_chat/mixed_chat_files.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import os
55

66
from semantic_kernel.agents import AgentGroupChat, AzureAssistantAgent, ChatCompletionAgent
7-
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
7+
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, AzureOpenAISettings
88
from semantic_kernel.contents import AnnotationContent, AuthorRole
99
from semantic_kernel.kernel import Kernel
1010

@@ -31,10 +31,10 @@ async def main():
3131
)
3232

3333
# Create the client using Azure OpenAI resources and configuration
34-
client, model = AzureAssistantAgent.setup_resources()
34+
client = AzureAssistantAgent.create_client()
3535

3636
# If desired, create using OpenAI resources
37-
# client, model = OpenAIAssistantAgent.setup_resources()
37+
# client = OpenAIAssistantAgent.create_client()
3838

3939
# Load the text file as a FileObject
4040
with open(file_path, "rb") as file:
@@ -45,7 +45,7 @@ async def main():
4545
)
4646

4747
definition = await client.beta.assistants.create(
48-
model=model,
48+
model=AzureOpenAISettings().chat_deployment_name,
4949
instructions="Create charts as requested without explanation.",
5050
name="ChartMaker",
5151
tools=code_interpreter_tool,

python/samples/concepts/agents/mixed_chat/mixed_chat_images.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import asyncio
44

55
from semantic_kernel.agents import AgentGroupChat, AzureAssistantAgent, ChatCompletionAgent
6-
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
6+
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, AzureOpenAISettings
77
from semantic_kernel.contents import AnnotationContent
88
from semantic_kernel.contents.utils.author_role import AuthorRole
99
from semantic_kernel.kernel import Kernel
@@ -24,14 +24,14 @@ def _create_kernel_with_chat_completion(service_id: str) -> Kernel:
2424

2525
async def main():
2626
# Create the client using Azure OpenAI resources and configuration
27-
client, model = AzureAssistantAgent.setup_resources()
27+
client = AzureAssistantAgent.create_client()
2828

2929
# Get the code interpreter tool and resources
3030
code_interpreter_tool, code_interpreter_resources = AzureAssistantAgent.configure_code_interpreter_tool()
3131

3232
# Create the assistant definition
3333
definition = await client.beta.assistants.create(
34-
model=model,
34+
model=AzureOpenAISettings().chat_deployment_name,
3535
name="Analyst",
3636
instructions="Create charts as requested without explanation",
3737
tools=code_interpreter_tool,

python/samples/concepts/agents/mixed_chat/mixed_chat_reset.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import TYPE_CHECKING
55

66
from semantic_kernel.agents import AgentGroupChat, AzureAssistantAgent, ChatCompletionAgent
7-
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
7+
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, AzureOpenAISettings
88
from semantic_kernel.contents import AuthorRole
99
from semantic_kernel.kernel import Kernel
1010

@@ -41,11 +41,11 @@ async def main():
4141
# Next, we will create the AzureAssistantAgent
4242

4343
# Create the client using Azure OpenAI resources and configuration
44-
client, model = AzureAssistantAgent.setup_resources()
44+
client = AzureAssistantAgent.create_client()
4545

4646
# Create the assistant definition
4747
definition = await client.beta.assistants.create(
48-
model=model,
48+
model=AzureOpenAISettings().chat_deployment_name,
4949
name="copywriter",
5050
instructions="""
5151
The user may either provide information or query on information previously provided.

python/samples/concepts/agents/mixed_chat/mixed_chat_streaming.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from semantic_kernel.agents import AgentGroupChat, AzureAssistantAgent, ChatCompletionAgent
66
from semantic_kernel.agents.strategies import TerminationStrategy
7-
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
7+
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, AzureOpenAISettings
88
from semantic_kernel.contents import AuthorRole
99
from semantic_kernel.kernel import Kernel
1010

@@ -46,11 +46,11 @@ async def main():
4646
# Next, we will create the AzureAssistantAgent
4747

4848
# Create the client using Azure OpenAI resources and configuration
49-
client, model = AzureAssistantAgent.setup_resources()
49+
client = AzureAssistantAgent.create_client()
5050

5151
# Create the assistant definition
5252
definition = await client.beta.assistants.create(
53-
model=model,
53+
model=AzureOpenAISettings().chat_deployment_name,
5454
name="CopyWriter",
5555
instructions="""
5656
You are a copywriter with ten years of experience and are known for brevity and a dry humor.

python/samples/concepts/agents/openai_assistant/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ OpenAI Assistant Agents are created in the following way:
3232
from semantic_kernel.agents import OpenAIAssistantAgent
3333

3434
# Create the client using OpenAI resources and configuration
35-
client, model = OpenAIAssistantAgent.setup_resources()
35+
client = OpenAIAssistantAgent.create_client()
3636

3737
# Create the assistant definition
3838
definition = await client.beta.assistants.create(
39-
model=model,
39+
model=AzureOpenAISettings().chat_deployment_name
4040
instructions="<instructions>",
4141
name="<name>",
4242
)
@@ -73,11 +73,11 @@ Alternatively, you can pass the `api_version` parameter when creating an `AzureA
7373
from semantic_kernel.agents import AzureAssistantAgent
7474

7575
# Create the client using Azure OpenAI resources and configuration
76-
client, model = AzureAssistantAgent.setup_resources()
76+
client = AzureAssistantAgent.create_client()
7777

7878
# Create the assistant definition
7979
definition = await client.beta.assistants.create(
80-
model=model,
80+
model=AzureOpenAISettings().chat_deployment_name
8181
instructions="<instructions>",
8282
name="<name>",
8383
)

0 commit comments

Comments
 (0)