Skip to content

Commit bddf020

Browse files
committed
Added code samples
1 parent 3ecdc26 commit bddf020

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

learn-pr/wwl-data-ai/develop-ai-agent-with-semantic-kernel/includes/3-create-azure-ai-agent.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,63 @@ An AzureAIAgent object encapsulates all the core capabilities you typically use
77
To use an AzureAIAgent:
88
1. Create an Azure AI Foundry project
99
1. Add the project connection string to your Semantic Kernel application code
10+
1. Create an AzureAIAgentSettings object
1011
1. Create an AzureAIAgent client
1112
1. Create an agent definition on the agent service provided by the client
1213
1. Create an agent based on the definition
1314

14-
Once your agent is defined, you can interact with your agent and invoke responses for inputs.
15+
Here is the code that illustrates how to create an AzureAIAgent:
16+
17+
```python
18+
from azure.identity.aio import DefaultAzureCredential
19+
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings
20+
21+
# Create an AzureAIAgentSettings object
22+
ai_agent_settings = AzureAIAgentSettings.create()
23+
24+
# Create an AzureAIAgent client
25+
async with (@
26+
DefaultAzureCredential() as creds,
27+
AzureAIAgent.create_client(credential=creds) as client,
28+
):
29+
# Create an agent definition on the agent service provided by the client
30+
agent_definition = await client.agents.create_agent(
31+
model=ai_agent_settings.model_deployment_name,
32+
name="<name>",
33+
instructions="<instructions>",
34+
)
35+
36+
# Create the AI agent based on the agent definition
37+
agent = AzureAIAgent(
38+
client=client,
39+
definition=agent_definition,
40+
)
41+
```
42+
43+
Once your agent is defined, you can interact with your agent and invoke responses for inputs. To invoke responses, you create an agent thread and use the agent to add prompt and retrieve a response. For example:
44+
45+
```python
46+
thread: AzureAIAgentThread = AzureAIAgentThread()
47+
48+
try:
49+
# Create a prompt
50+
prompt = "What are the largest semiconductor manufacturing companies?"
51+
52+
# Instruct the agent to add a message to the thread
53+
await agent.add_chat_message(thread_id=thread.id, message=prompt)
54+
55+
# Invoke the agent for response on the thread
56+
response = await agent.get_response(thread_id=thread.id)
57+
finally:
58+
await thread.delete() if thread else None
59+
```
1560

1661
### AzureAIAgent key components
1762

1863
The Semantic Kernel AzureAIAgent object relies on the following components to function:
1964

65+
- **AzureAISAgentSettings** - an object that automatically includes the Azure AI Agent settings from the environment configuration. These settings will be used by the AzureAIAgents you create.
66+
2067
- **AzureAIAgent client** - an object that manages the connection to your Azure AI Foundry project. This object allows you to access the services and models associated with your project.
2168

2269
- **Agent service** - the AzureAIAgent client also contains an agent operations service. This service helps streamline the process of creating, managing, and running the agents for your project.

0 commit comments

Comments
 (0)