Skip to content

Commit 51721bd

Browse files
.NET: Add samples for OpenAIChatClientAgent and OpenAIResponseClientAgent (#2638)
* add samples for OpenAIChatClientAgent and OpenAIResponseClientAgent * Update dotnet/samples/GettingStarted/AgentWithOpenAI/README.md Co-authored-by: Copilot <[email protected]> * Update dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step03_CreateFromChatClient/README.md Co-authored-by: Copilot <[email protected]> * Update dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step03_CreateFromChatClient/README.md Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent db36809 commit 51721bd

File tree

8 files changed

+142
-2
lines changed

8 files changed

+142
-2
lines changed

dotnet/agent-framework-dotnet.slnx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@
104104
<File Path="samples/GettingStarted/AgentWithOpenAI/README.md" />
105105
<Project Path="samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step01_Running/Agent_OpenAI_Step01_Running.csproj" />
106106
<Project Path="samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step02_Reasoning/Agent_OpenAI_Step02_Reasoning.csproj" />
107+
<Project Path="samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step03_CreateFromChatClient/Agent_OpenAI_Step03_CreateFromChatClient.csproj" />
108+
<Project Path="samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient.csproj" />
107109
</Folder>
108110
<Folder Name="/Samples/Purview/" />
109111
<Folder Name="/Samples/Purview/AgentWithPurview/">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net10.0</TargetFrameworks>
6+
7+
<Nullable>enable</Nullable>
8+
<ImplicitUsings>enable</ImplicitUsings>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.OpenAI\Microsoft.Agents.AI.OpenAI.csproj" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
// This sample demonstrates how to create an AI agent directly from an OpenAI.Chat.ChatClient instance using OpenAIChatClientAgent.
4+
5+
using OpenAI;
6+
using OpenAI.Chat;
7+
8+
string apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new InvalidOperationException("OPENAI_API_KEY is not set.");
9+
string model = Environment.GetEnvironmentVariable("OPENAI_MODEL") ?? "gpt-4o-mini";
10+
11+
// Create a ChatClient directly from OpenAIClient
12+
ChatClient chatClient = new OpenAIClient(apiKey).GetChatClient(model);
13+
14+
// Create an agent directly from the ChatClient using OpenAIChatClientAgent
15+
OpenAIChatClientAgent agent = new(chatClient, instructions: "You are good at telling jokes.", name: "Joker");
16+
17+
UserChatMessage chatMessage = new("Tell me a joke about a pirate.");
18+
19+
// Invoke the agent and output the text result.
20+
ChatCompletion chatCompletion = await agent.RunAsync([chatMessage]);
21+
Console.WriteLine(chatCompletion.Content.Last().Text);
22+
23+
// Invoke the agent with streaming support.
24+
IAsyncEnumerable<StreamingChatCompletionUpdate> completionUpdates = agent.RunStreamingAsync([chatMessage]);
25+
await foreach (StreamingChatCompletionUpdate completionUpdate in completionUpdates)
26+
{
27+
if (completionUpdate.ContentUpdate.Count > 0)
28+
{
29+
Console.WriteLine(completionUpdate.ContentUpdate[0].Text);
30+
}
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Creating an Agent from a ChatClient
2+
3+
This sample demonstrates how to create an AI agent directly from an `OpenAI.Chat.ChatClient` instance using the `OpenAIChatClientAgent` class.
4+
5+
## What This Sample Shows
6+
7+
- **Direct ChatClient Creation**: Shows how to create an `OpenAI.Chat.ChatClient` from `OpenAI.OpenAIClient` and then use it to instantiate an agent
8+
- **OpenAIChatClientAgent**: Demonstrates using the OpenAI SDK primitives instead of the ones from Microsoft.Extensions.AI and Microsoft.Agents.AI abstractions
9+
- **Full Agent Capabilities**: Shows both regular and streaming invocation of the agent
10+
11+
## Running the Sample
12+
13+
1. Set the required environment variables:
14+
```bash
15+
set OPENAI_API_KEY=your_api_key_here
16+
set OPENAI_MODEL=gpt-4o-mini
17+
```
18+
19+
2. Run the sample:
20+
```bash
21+
dotnet run
22+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net10.0</TargetFrameworks>
6+
7+
<Nullable>enable</Nullable>
8+
<ImplicitUsings>enable</ImplicitUsings>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.OpenAI\Microsoft.Agents.AI.OpenAI.csproj" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
// This sample demonstrates how to create OpenAIResponseClientAgent directly from an OpenAIResponseClient instance.
4+
5+
using OpenAI;
6+
using OpenAI.Responses;
7+
8+
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new InvalidOperationException("OPENAI_API_KEY is not set.");
9+
var model = Environment.GetEnvironmentVariable("OPENAI_MODEL") ?? "gpt-4o-mini";
10+
11+
// Create an OpenAIResponseClient directly from OpenAIClient
12+
OpenAIResponseClient responseClient = new OpenAIClient(apiKey).GetOpenAIResponseClient(model);
13+
14+
// Create an agent directly from the OpenAIResponseClient using OpenAIResponseClientAgent
15+
OpenAIResponseClientAgent agent = new(responseClient, instructions: "You are good at telling jokes.", name: "Joker");
16+
17+
ResponseItem userMessage = ResponseItem.CreateUserMessageItem("Tell me a joke about a pirate.");
18+
19+
// Invoke the agent and output the text result.
20+
OpenAIResponse response = await agent.RunAsync([userMessage]);
21+
Console.WriteLine(response.GetOutputText());
22+
23+
// Invoke the agent with streaming support.
24+
IAsyncEnumerable<StreamingResponseUpdate> responseUpdates = agent.RunStreamingAsync([userMessage]);
25+
await foreach (StreamingResponseUpdate responseUpdate in responseUpdates)
26+
{
27+
if (responseUpdate is StreamingResponseOutputTextDeltaUpdate textUpdate)
28+
{
29+
Console.WriteLine(textUpdate.Delta);
30+
}
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Creating an Agent from an OpenAIResponseClient
2+
3+
This sample demonstrates how to create an AI agent directly from an `OpenAI.Responses.OpenAIResponseClient` instance using the `OpenAIResponseClientAgent` class.
4+
5+
## What This Sample Shows
6+
7+
- **Direct OpenAIResponseClient Creation**: Shows how to create an `OpenAI.Responses.OpenAIResponseClient` from `OpenAI.OpenAIClient` and then use it to instantiate an agent
8+
- **OpenAIResponseClientAgent**: Demonstrates using the OpenAI SDK primitives instead of the ones from Microsoft.Extensions.AI and Microsoft.Agents.AI abstractions
9+
- **Full Agent Capabilities**: Shows both regular and streaming invocation of the agent
10+
11+
## Running the Sample
12+
13+
1. Set the required environment variables:
14+
```bash
15+
set OPENAI_API_KEY=your_api_key_here
16+
set OPENAI_MODEL=gpt-4o-mini
17+
```
18+
19+
2. Run the sample:
20+
```bash
21+
dotnet run
22+
```

dotnet/samples/GettingStarted/AgentWithOpenAI/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ Agent Framework provides additional support to allow OpenAI developers to use th
1010

1111
|Sample|Description|
1212
|---|---|
13-
|[Creating an AIAgent](./Agent_OpenAI_Step01_Running/)|This sample demonstrates how to create and run a basic agent instructions with native OpenAI SDK types.|
14-
13+
|[Creating an AIAgent](./Agent_OpenAI_Step01_Running/)|This sample demonstrates how to create and run a basic agent with native OpenAI SDK types. Shows both regular and streaming invocation of the agent.|
14+
|[Using Reasoning Capabilities](./Agent_OpenAI_Step02_Reasoning/)|This sample demonstrates how to create an AI agent with reasoning capabilities using OpenAI's reasoning models and response types.|
15+
|[Creating an Agent from a ChatClient](./Agent_OpenAI_Step03_CreateFromChatClient/)|This sample demonstrates how to create an AI agent directly from an OpenAI.Chat.ChatClient instance using OpenAIChatClientAgent.|
16+
|[Creating an Agent from an OpenAIResponseClient](./Agent_OpenAI_Step04_CreateFromOpenAIResponseClient/)|This sample demonstrates how to create an AI agent directly from an OpenAI.Responses.OpenAIResponseClient instance using OpenAIResponseClientAgent.|

0 commit comments

Comments
 (0)