diff --git a/dotnet/agent-framework-dotnet.slnx b/dotnet/agent-framework-dotnet.slnx index 816ffdb678..46286a22d6 100644 --- a/dotnet/agent-framework-dotnet.slnx +++ b/dotnet/agent-framework-dotnet.slnx @@ -104,6 +104,8 @@ + + diff --git a/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step03_CreateFromChatClient/Agent_OpenAI_Step03_CreateFromChatClient.csproj b/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step03_CreateFromChatClient/Agent_OpenAI_Step03_CreateFromChatClient.csproj new file mode 100644 index 0000000000..eeda3eef6f --- /dev/null +++ b/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step03_CreateFromChatClient/Agent_OpenAI_Step03_CreateFromChatClient.csproj @@ -0,0 +1,15 @@ + + + + Exe + net10.0 + + enable + enable + + + + + + + diff --git a/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step03_CreateFromChatClient/Program.cs b/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step03_CreateFromChatClient/Program.cs new file mode 100644 index 0000000000..dd72019125 --- /dev/null +++ b/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step03_CreateFromChatClient/Program.cs @@ -0,0 +1,31 @@ +// Copyright (c) Microsoft. All rights reserved. + +// This sample demonstrates how to create an AI agent directly from an OpenAI.Chat.ChatClient instance using OpenAIChatClientAgent. + +using OpenAI; +using OpenAI.Chat; + +string apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new InvalidOperationException("OPENAI_API_KEY is not set."); +string model = Environment.GetEnvironmentVariable("OPENAI_MODEL") ?? "gpt-4o-mini"; + +// Create a ChatClient directly from OpenAIClient +ChatClient chatClient = new OpenAIClient(apiKey).GetChatClient(model); + +// Create an agent directly from the ChatClient using OpenAIChatClientAgent +OpenAIChatClientAgent agent = new(chatClient, instructions: "You are good at telling jokes.", name: "Joker"); + +UserChatMessage chatMessage = new("Tell me a joke about a pirate."); + +// Invoke the agent and output the text result. +ChatCompletion chatCompletion = await agent.RunAsync([chatMessage]); +Console.WriteLine(chatCompletion.Content.Last().Text); + +// Invoke the agent with streaming support. +IAsyncEnumerable completionUpdates = agent.RunStreamingAsync([chatMessage]); +await foreach (StreamingChatCompletionUpdate completionUpdate in completionUpdates) +{ + if (completionUpdate.ContentUpdate.Count > 0) + { + Console.WriteLine(completionUpdate.ContentUpdate[0].Text); + } +} diff --git a/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step03_CreateFromChatClient/README.md b/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step03_CreateFromChatClient/README.md new file mode 100644 index 0000000000..a4d9dd76a5 --- /dev/null +++ b/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step03_CreateFromChatClient/README.md @@ -0,0 +1,22 @@ +# Creating an Agent from a ChatClient + +This sample demonstrates how to create an AI agent directly from an `OpenAI.Chat.ChatClient` instance using the `OpenAIChatClientAgent` class. + +## What This Sample Shows + +- **Direct ChatClient Creation**: Shows how to create an `OpenAI.Chat.ChatClient` from `OpenAI.OpenAIClient` and then use it to instantiate an agent +- **OpenAIChatClientAgent**: Demonstrates using the OpenAI SDK primitives instead of the ones from Microsoft.Extensions.AI and Microsoft.Agents.AI abstractions +- **Full Agent Capabilities**: Shows both regular and streaming invocation of the agent + +## Running the Sample + +1. Set the required environment variables: + ```bash + set OPENAI_API_KEY=your_api_key_here + set OPENAI_MODEL=gpt-4o-mini + ``` + +2. Run the sample: + ```bash + dotnet run + ``` diff --git a/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient.csproj b/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient.csproj new file mode 100644 index 0000000000..eeda3eef6f --- /dev/null +++ b/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient.csproj @@ -0,0 +1,15 @@ + + + + Exe + net10.0 + + enable + enable + + + + + + + diff --git a/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient/Program.cs b/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient/Program.cs new file mode 100644 index 0000000000..555ec60ac5 --- /dev/null +++ b/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient/Program.cs @@ -0,0 +1,31 @@ +// Copyright (c) Microsoft. All rights reserved. + +// This sample demonstrates how to create OpenAIResponseClientAgent directly from an OpenAIResponseClient instance. + +using OpenAI; +using OpenAI.Responses; + +var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new InvalidOperationException("OPENAI_API_KEY is not set."); +var model = Environment.GetEnvironmentVariable("OPENAI_MODEL") ?? "gpt-4o-mini"; + +// Create an OpenAIResponseClient directly from OpenAIClient +OpenAIResponseClient responseClient = new OpenAIClient(apiKey).GetOpenAIResponseClient(model); + +// Create an agent directly from the OpenAIResponseClient using OpenAIResponseClientAgent +OpenAIResponseClientAgent agent = new(responseClient, instructions: "You are good at telling jokes.", name: "Joker"); + +ResponseItem userMessage = ResponseItem.CreateUserMessageItem("Tell me a joke about a pirate."); + +// Invoke the agent and output the text result. +OpenAIResponse response = await agent.RunAsync([userMessage]); +Console.WriteLine(response.GetOutputText()); + +// Invoke the agent with streaming support. +IAsyncEnumerable responseUpdates = agent.RunStreamingAsync([userMessage]); +await foreach (StreamingResponseUpdate responseUpdate in responseUpdates) +{ + if (responseUpdate is StreamingResponseOutputTextDeltaUpdate textUpdate) + { + Console.WriteLine(textUpdate.Delta); + } +} diff --git a/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient/README.md b/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient/README.md new file mode 100644 index 0000000000..32e19caa62 --- /dev/null +++ b/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient/README.md @@ -0,0 +1,22 @@ +# Creating an Agent from an OpenAIResponseClient + +This sample demonstrates how to create an AI agent directly from an `OpenAI.Responses.OpenAIResponseClient` instance using the `OpenAIResponseClientAgent` class. + +## What This Sample Shows + +- **Direct OpenAIResponseClient Creation**: Shows how to create an `OpenAI.Responses.OpenAIResponseClient` from `OpenAI.OpenAIClient` and then use it to instantiate an agent +- **OpenAIResponseClientAgent**: Demonstrates using the OpenAI SDK primitives instead of the ones from Microsoft.Extensions.AI and Microsoft.Agents.AI abstractions +- **Full Agent Capabilities**: Shows both regular and streaming invocation of the agent + +## Running the Sample + +1. Set the required environment variables: + ```bash + set OPENAI_API_KEY=your_api_key_here + set OPENAI_MODEL=gpt-4o-mini + ``` + +2. Run the sample: + ```bash + dotnet run + ``` diff --git a/dotnet/samples/GettingStarted/AgentWithOpenAI/README.md b/dotnet/samples/GettingStarted/AgentWithOpenAI/README.md index 4ed609ae81..6f2c77f39b 100644 --- a/dotnet/samples/GettingStarted/AgentWithOpenAI/README.md +++ b/dotnet/samples/GettingStarted/AgentWithOpenAI/README.md @@ -10,5 +10,7 @@ Agent Framework provides additional support to allow OpenAI developers to use th |Sample|Description| |---|---| -|[Creating an AIAgent](./Agent_OpenAI_Step01_Running/)|This sample demonstrates how to create and run a basic agent instructions with native OpenAI SDK types.| - +|[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.| +|[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.| +|[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.| +|[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.| \ No newline at end of file