Skip to content

Commit b8dbea5

Browse files
authored
Agents review (#29)
* temp checkin of agent review * Updating the agents section
1 parent 8df1ede commit b8dbea5

File tree

2 files changed

+135
-72
lines changed

2 files changed

+135
-72
lines changed
+135-72
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,162 @@
1-
<div align="center">
2-
<h1>Generative AI Fundamentals for .NET</h1>
3-
<h2>Lesson 3.3: Agents and Conclusions </h2>
4-
<p><em>Learn how to create Agentic AI in .NET!</em></p>
5-
</div>
1+
# AI Agents
2+
3+
In this lesson learn to create an AI entity that... makes decisions and executes actions without continuous human interaction? That's right, AI agents are able to perform specific tasks independently.
64

75
---
8-
## Agents / Assistants
9-
![Agents / Assistants](../03-CoreGenerativeAITechniques/images/Agents-Assistants.png)
106

11-
Agents are AI applications which have some autonomy, and can perform tasks following workflows using certain LLM archtectures. Usually, agents need plugins to perform tasks, native functions, and optimized logic to invoke functions in the right order.
7+
**INSERT: LESSON 3 AGENT VIDEO HERE**
8+
9+
AI agents allow LLMs to evolve from assistants into entities capable of taking actions on behalf of users. Agents are even able to interact with other agents to perform tasks. Some of the key attributes of an agent include a level of **autonomy** allowing the agent to initiate actions based on their programming which leads to the ability for **decision-making** based on pre-defined objectives. They are also **adaptable** in that they learn and adjust to improve performance over time.
10+
11+
One key thing to keep in mind when building agents is that they are focused on doing only thing. You want to narrow down their purpose as much as possible.
12+
13+
> 🧑‍🏫**Learn more**: Learn more about the fundamentals of AI Agents [Generative AI for Beginners: AI Agents](https://github.com/microsoft/generative-ai-for-beginners/tree/main/17-ai-agents).
14+
15+
## Creating an AI Agent
16+
17+
We'll be working with a couple of new concepts in order to build an AI agent in .NET. We'll be using a new SDK and will have to do some additional setup in Azure AI Foundry to get things started.
18+
19+
> 🧑‍💻**Code sample:** We'll be working from the [AgentLabs-01-Simple sample](./src/AgentLabs-01-Simple/) for this lesson.
20+
>
21+
> We did include some more advanced samples in the `/src/` folder as well. You can view the README's of [AgentLabs-02-Functions](./src/AgentLabs-02-Functions/) or [AgentLabs-03-OpenAPIs](./src/AgentLabs-03-OpenAPIs/) or [AgentLabs-03-PythonParksInformationServer](./src/AgentLabs-03-PythonParksInformationServer/) for more info on them.
22+
23+
### Azure AI Agent Service
24+
25+
We're going to introduce a new Azure Service that will help us build agents, the appropriately named [Azure AI Agent Service](https://learn.microsoft.com/azure/ai-services/agents/overview).
26+
27+
To run the code samples included in this lesson, you'll need to perform some additional setup in Azure AI Foundry. You can follow [these instructions to setup a **Basic Agent**](https://learn.microsoft.com/azure/ai-services/agents/quickstart?pivots=programming-language-csharp).
28+
29+
### Azure AI Projects library
30+
31+
Agents are composed of 3 parts. The **LLM** or the model. **State** or the context (much like a conversation) that helps guide decisions based off of past results. And **Tools** which are like [functions we learned about before](./01-lm-completions-functions.md#function-calling) that allow a bridge between the model and external systems.
32+
33+
So, in theory, you could build AI Agents with what you've learned already. But the **Azure AI Projects for .NET** library makes developing agents easier by providing an API that streamlines a lot of the typical tasks for you.
34+
35+
There are a couple of concepts (which map to classes) to understand when working with the Azure AI Projects library.
36+
37+
- `AgentClient`: The overall client that creates and hosts the agents, manages threads in which they run, and handles the connection to the cloud.
38+
- `Agent`: The agent that holds instructions on what it's to do as well as definitions for tools it has access to.
39+
- `ThreadMessage`: These are messages - almost like prompts we learned about before - that get passed to the agent. Agents also create `ThreadMessage` objects to communicate.
40+
- `ThreadRun`: A thread on which messages are passed to the agent on. The thread is started and can be provided additional instructions and then is polled as to its status.
1241

13-
In our demos, we have the following structure:
42+
Let's see a simple example of this in action!
1443

15-
- `Agent Client`: The client that hosts the agent, configuring the connection to the Cloud, AI models and be the base for the agent.
16-
- `Agent`: The agent itself, with the logic to perform the tasks, using the plugins and functions to perform the tasks. Agents can be shaped as needed to solve and perform tasks.
17-
- `Tools`: Tools are the plugins and functions that the agent uses to perform the tasks. They can be used to perform tasks, like getting the weather, sending emails, or even controlling IoT devices.
44+
### Build a math agent
1845

19-
In the `src/Agents-01-Simple` we can see how to create a simple agent that gets helps in simple math, look into the Agent Client code:
46+
We'll be building a single purpose agent that acts as a tutor to math students. It's sole purpose in life is to solve and then explain math problems the user asks.
2047

21-
```csharp
22-
// Configure the connection to the Cloud, AI models and be the base for the agent
23-
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
24-
var options = new DefaultAzureCredentialOptions
25-
{
26-
ExcludeEnvironmentCredential = true,
27-
ExcludeWorkloadIdentityCredential = true,
28-
TenantId = config["tenantid"]
29-
};
30-
var connectionString = config["connectionString"];
48+
1. To start with, we need to create an `AgentsClient` object that is responsible for managing the connection to Azure, the agent itself, the threads, the messages, and so on.
3149

32-
// Create the agent client with the connection string and the Azure credentials
33-
AgentsClient client = new AgentsClient(connectionString, new AgentsClient client = new AgentsClient(connectionString, new DefaultAzureCredential(options));
34-
```
50+
```csharp
51+
string projectConnectionString = "< YOU GET THIS FROM THE PROJECT IN AI FOUNDRY >";
52+
AgentsClient client = new(projectConnectionString, new DefaultAzureCredential());
53+
```
3554

36-
For the agent, we can see how to create a simple agent that gets helps in simple math, look into the Agent code:
55+
You can find the project connection string in AI Foundry by opening up the Hub you created, then the project. It will be on the right-hand side.
3756

38-
```csharp
39-
// Create Agent with the model, name, instructions and tools, to do that we use the Agent Client
40-
Response<Agent> agentResponse = await client.CreateAgentAsync(
41-
model: "gpt-4o-mini",
57+
![Screenshot of the project homepage in AI Foundry with the project connection string highlighted in red](./images/project-connection-string.png)
58+
59+
1. Next we want to create the tutor agent. Remeber, it should be focused only on one thing.
60+
61+
```csharp
62+
Agent tutorAgent = (await client.CreateAgentAsync(
63+
model: "gpt-4o",
4264
name: "Math Tutor",
4365
instructions: "You are a personal math tutor. Write and run code to answer math questions.",
44-
tools: [new CodeInterpreterToolDefinition()]);
45-
Agent agentMathTutor = agentResponse.Value;
46-
// Use the Agent to create a thread and start the conversation with the Client
47-
Response<AgentThread> threadResponse = await client.CreateThreadAsync();
48-
AgentThread thread = threadResponse.Value;
66+
tools: [new CodeInterpreterToolDefinition()])).Value;
67+
```
68+
69+
A couple of things to note here. The first is the `tools` parameter. We're creating a `CodeInterpreterToolDefinition` object (that is apart of the **Azure.AI.Projects** SDK) that will allow the agent to create and execute code.
70+
71+
> 🗒️**Note**: You can create your own tools too. See the [Functions](./src/AgentLabs-02-Functions/) to learn more.
72+
73+
Second note the `instructions` that are being sent along. It's a prompt and we're limiting it to answer math questions. Then last creating the agent is an async operation. That's because it's creating an object within Azure AI Foundry Agents service. So we both `await` the `CreateAgentAsync` function and then grab the `Value` of its return to get at the actual `Agent` object. You'll see this pattern occur over and over again when creating objects with the **Azure.AI.Projects** SDK.
74+
75+
1. An `AgentThread` is an object that handles the communication between individual agents and the user and so on. We'll need to create that so we can add a `ThreadMessage` on to it. And in this case it's the user's first question.
76+
77+
```csharp
78+
AgentThread thread = (await client.CreateThreadAsync()).Value;
79+
80+
// Creating the first user message to AN agent - notice how we're putting it on a thread
81+
ThreadMessage userMessage = (await client.CreateMessageAsync(
82+
thread.Id,
83+
MessageRole.User,
84+
"Hello, I need to solve the equation `3x + 11 = 14`. Can you help me?")
85+
).Value;
86+
```
87+
88+
Note the `ThreadMessage` has a type of `MessageRole.User`. And notice we're not sending the message to a specific agent, rather we're just putting it onto a thread.
89+
90+
1. Next up, we're going to get the agent to provide an initial response and put that on the thread and then kick the thread off. When we start the thread we're going to provide the initial agent's id to run and any additional instructions.
91+
92+
```csharp
93+
ThreadMessage agentMessage = await client.CreateMessageAsync(
94+
thread.Id,
95+
MessageRole.Agent,
96+
"Please address the user as their name. The user has a basic account, so just share the answer to the question.")
97+
).Value;
98+
99+
ThreadRun run = (await client.CreateRunAsync(
100+
thread.Id,
101+
assistantId: agentMathTutor.Id,
102+
additionalInstructions: "You are working in FREE TIER EXPERIENCE mode`, every user has premium account for a short period of time. Explain detailed the steps to answer the user questions")
103+
).Value;
104+
```
105+
106+
1. All that's left then is to check the status of the run
107+
108+
```csharp
109+
do
110+
{
111+
await Task.Delay(Timespan.FromMilliseconds(100));
112+
run = (await client.GetRunAsync(thread.Id, run.Id)).Value;
113+
114+
Console.WriteLine($"Run Status: {run.Status}");
115+
}
116+
while (run.Status == RunStatus.Queued || run.Status == RunStatus.InProgress);
117+
```
49118

50-
```
119+
1. And then display the messages from the results
51120

52-
Look how the Agent Client and the Agent are interconnected, the Agent Client is the base for the Agent, and the Agent is the one that performs the tasks.
121+
```csharp
122+
Response<PageableList<ThreadMessage>> afterRunMessagesResponse = await client.GetMessagesAsync(thread.Id);
123+
IReadOnlyList<ThreadMessage> messages = afterRunMessagesResponse.Value.Data;
53124

54-
For the tools, we can see how to create a tool for a travel agency, look into the `src/Agents-02-TravelAgency` Agent:
125+
// sort by creation date
126+
messages = messages.OrderBy(m => m.CreatedAt).ToList();
55127

56-
```csharp
57-
// create Agent
58-
Response<Agent> agentResponse = await client.CreateAgentAsync(
59-
model: "gpt-4o-mini",
60-
name: "SDK Test Agent - Vacation",
61-
instructions: @"You are a travel assistant. Use the provided functions to help answer questions.
62-
Customize your responses to the user's preferences as much as possible. Write and run code to answer user questions.",
63-
// Add the tools to the agent, tools are the plugins and functions that the agent uses to perform the tasks.
64-
tools: new List<ToolDefinition> {
65-
CityInfo.getUserFavoriteCityTool,
66-
CityInfo.getWeatherAtLocationTool,
67-
CityInfo.getParksAtLocationTool}
68-
);
69-
Agent agentTravelAssistant = agentResponse.Value;
70-
Response<AgentThread> threadResponse = await client.CreateThreadAsync();
71-
AgentThread thread = threadResponse.Value;
72-
```
128+
foreach (ThreadMessage msg in messages)
129+
{
130+
Console.Write($"{msg.CreatedAt:yyyy-MM-dd HH:mm:ss} - {msg.Role,10}: ");
73131

74-
The tools are the plugins and functions that the agent uses to perform the tasks. In this case, the agent uses the CityInfo tools to get information about cities. Know more about `CityInfo` tools in the `src/Agents-02-TravelAgency` sample.
132+
foreach (MessageContent contentItem in msg.ContentItems)
133+
{
134+
if (contentItem is MessageTextContent textItem)
135+
Console.Write(textItem.Text);
136+
}
137+
Console.WriteLine();
138+
}
139+
```
75140

141+
> 🙋 **Need help?**: If you encounter any issues, open an issue in the repository.
76142

77-
## Conclusions and resources
143+
The logical next step is to start to use multiple agents to create an automous system. A next step might be to have an agent that checks to see if the user has a premium account or not.
78144

79-
In this chapter, we explored the core generative AI techniques, including Language Model Completions, RAG, Vision and Audio applications.
145+
## Summary
80146

81-
In the next chapter, we will explore how to implement these techniques in practice, using real-world examples and complex samples.
147+
AI Agents are autonomous AI entities that go beyond simple chat interactions - they can:
82148

83-
### Additional Resources
149+
- Make Independent Decisions: Execute tasks without constant human input
150+
- Maintain Context: Hold state and remember previous interactions
151+
- Use Tools: Access external systems and APIs to accomplish tasks
152+
- Collaborate: Work with other agents to solve complex problems
84153

85-
> ⚠️ **Note**: If you encounter any issues, open an issue in the repository.
154+
And you learned how to use the **Azure AI Agents** service with the **Azure AI Project** SDK to create a rudimentary agent.
86155

87-
- [GitHub Codespaces Documentation](https://docs.github.com/en/codespaces)
88-
- [GitHub Models Documentation](https://docs.github.com/en/github-models/prototyping-with-ai-models)
89-
- [Generative AI for Beginners](https://github.com/microsoft/generative-ai-for-beginners)
90-
- [Semantic Kernel Documentation](https://learn.microsoft.com/en-us/semantic-kernel/get-started/quick-start-guide?pivots=programming-language-csharp)
91-
- [MEAI Documentation](https://devblogs.microsoft.com/dotnet/introducing-microsoft-extensions-ai-preview/)
156+
Think of agents as AI assistants with agency - they don't just respond, they act based on their programming and objectives.
92157

93-
### Next Steps
158+
## Next Steps
94159

95-
Next, we'll explore some samples in how to implement these algoritms pratically.
160+
You've come a long way! From learning about simple one and done text completions to building agents!
96161

97-
<p align="center">
98-
<a href="../04-Practical.NETGenAISamples/readme.md">Go to Chapter 4</a>
99-
</p>
162+
In the [next lesson see some real-life practical examples](../04-Practical.NETGenAISamples/readme.md) of using everything together.
Loading

0 commit comments

Comments
 (0)