Skip to content

Commit 85a0eeb

Browse files
authored
Merge pull request #634 from westey-m/add-run-agent-tutorial
Add run agent tutorial
2 parents 63305c4 + dc1a428 commit 85a0eeb

5 files changed

Lines changed: 140 additions & 1 deletion

File tree

agent-framework/TOC.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
- name: Index
2-
href: index.md
2+
href: index.md
3+
- name: Tutorials
4+
href: tutorials/TOC.yml

agent-framework/tutorials/TOC.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- name: Index
2+
href: index.md
3+
- name: Create and run a simple agent
4+
href: run-agent.md

agent-framework/tutorials/index.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Agent Framework Tutorials
3+
description: Agent Framework Tutorials
4+
author: westey-m
5+
ms.topic: tutorial
6+
ms.author: westey-m
7+
ms.date: 09/15/2025
8+
ms.service: agent-framework
9+
---
10+
11+
# Agent Framework Tutorials
12+
13+
## Single Agent Tutorials
14+
15+
- [Create and run a simple agent](./run-agent.md)
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
title: Create and run a simple agent
3+
description: Create and run a simple agent
4+
zone_pivot_groups: programming-languages
5+
author: westey-m
6+
ms.topic: tutorial
7+
ms.author: westey
8+
ms.date: 09/15/2025
9+
ms.service: agent-framework
10+
---
11+
12+
# Create and run a simple agent
13+
14+
::: zone pivot="programming-language-csharp"
15+
16+
This tutorial shows you how to create and run a simple agent, based on the Azure OpenAI Chat Completion service.
17+
18+
> [!IMPORTANT]
19+
> The agent framework supports many different types of agents. This tutorial uses an agent based on a Chat Completion service, but all other agent types are run in the same way. See the [Agent Framework user guide](../user-guide/index.md) for more information on other agent types and how to construct them.
20+
21+
## Prerequisites
22+
23+
Before you begin, ensure you have the following prerequisites:
24+
25+
- [.NET 8.0 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/8.0)
26+
- [Azure OpenAI service endpoint and deployment configured](https://learn.microsoft.com/azure/ai-foundry/openai/how-to/create-resource)
27+
- [Azure CLI installed](https://learn.microsoft.com/cli/azure/install-azure-cli) and [authenticated (for Azure credential authentication)](https://learn.microsoft.com/cli/azure/authenticate-azure-cli)
28+
- [User has the `Cognitive Services OpenAI User` or `Cognitive Services OpenAI Contributor` roles, depending on need, for the Azure OpenAI resource.](https://learn.microsoft.com/azure/ai-foundry/openai/how-to/role-based-access-control)
29+
30+
> [!IMPORTANT]
31+
> For this tutorial we are using Azure OpenAI for the Chat Completion service, but you can use any inference service that is compatible with [Microsoft.Extensions.AI.IChatClient](https://learn.microsoft.com/dotnet/api/microsoft.extensions.ai.ichatclient).
32+
33+
## Installing Nuget packages
34+
35+
To use the AgentFramework with Azure OpenAI, you need to install the following NuGet packages:
36+
37+
```powershell
38+
dotnet add package Azure.Identity
39+
dotnet add package Azure.AI.OpenAI
40+
dotnet add package Microsoft.Extensions.AI.OpenAI
41+
dotnet add package Microsoft.Agents.OpenAI
42+
```
43+
44+
## Creating the agent
45+
46+
- First we create create a client for Azure OpenAI, by providing the Azure OpenAI endpoint and using the same login as was used when authenticating with the Azure CLI in the [Prerequisites](#prerequisites) step.
47+
- Then we get a chat client for communicating with the chat completion service, where we also specify the specific model deployment to use. Use one of the deployments that you created in the [Prerequisites](#prerequisites) step.
48+
- Finally we create the agent, providing instructions and a name for the agent.
49+
50+
```csharp
51+
using System;
52+
using Azure.AI.OpenAI;
53+
using Azure.Identity;
54+
using Microsoft.Extensions.AI;
55+
using OpenAI;
56+
57+
AIAgent agent = new AzureOpenAIClient(
58+
new Uri("https://<myresource>.openai.azure.com"),
59+
new AzureCliCredential())
60+
.GetChatClient("gpt-4o-mini")
61+
.CreateAIAgent(instructions: "You are good at telling jokes.", name: "Joker");
62+
```
63+
64+
## Running the agent
65+
66+
To run the agent, call the `RunAsync` method on the agent instance, providing the user input.
67+
The agent will return a response object, and calling `.ToString()` or `.Text` on this response object, provides the text result from the agent.
68+
69+
```csharp
70+
Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate."));
71+
```
72+
73+
## Running the agent with streaming
74+
75+
To run the agent with streaming, call the `RunStreamingAsync` method on the agent instance, providing the user input.
76+
The agent will stream a list of update objects, and calling `.ToString()` or `.Text` on each update object provides the part of the text result contained in that update.
77+
78+
```csharp
79+
await foreach (var update in agent.RunStreamingAsync("Tell me a joke about a pirate."))
80+
{
81+
Console.WriteLine(update);
82+
}
83+
```
84+
85+
## Running the agent with a ChatMessage
86+
87+
Instead of a simple string, you can also provide one or more `ChatMessage` objects to the `RunAsync` and `RunStreamingAsync` methods.
88+
89+
```csharp
90+
ChatMessage message = new(ChatRole.User, [
91+
new TextContent("Tell me a joke about this image?"),
92+
new UriContent("https://samplesite.org/clown.jpg", "image/jpeg")
93+
]);
94+
95+
Console.WriteLine(await agent.RunAsync(message));
96+
```
97+
98+
::: zone-end
99+
::: zone pivot="programming-language-python"
100+
101+
Tutorial coming soon.
102+
103+
::: zone-end
104+
105+
## Next steps
106+
107+
> [!div class="nextstepaction"]
108+
> [Having a multi-turn conversation with an agent](./multi-turn-conversation.md)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# YamlMime:ZonePivotGroups
2+
groups:
3+
- id: programming-languages
4+
title: Programming languages
5+
prompt: Choose a programming language
6+
pivots:
7+
- id: programming-language-csharp
8+
title: C#
9+
- id: programming-language-python
10+
title: Python

0 commit comments

Comments
 (0)