-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add getting start sample project * update * update * revert change
- Loading branch information
1 parent
668146e
commit 0027699
Showing
11 changed files
with
575 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
dotnet/sample/AutoGen.BasicSamples/GettingStart/Agent_Middleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Agent_Middleware.cs | ||
|
||
#region Using | ||
using AutoGen.Core; | ||
using AutoGen.OpenAI; | ||
using AutoGen.OpenAI.Extension; | ||
using Azure.AI.OpenAI; | ||
#endregion Using | ||
using FluentAssertions; | ||
|
||
namespace AutoGen.BasicSample; | ||
|
||
public class Agent_Middleware | ||
{ | ||
public static async Task RunTokenCountAsync() | ||
{ | ||
#region Create_Agent | ||
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new InvalidOperationException("Please set the environment variable OPENAI_API_KEY"); | ||
var model = "gpt-3.5-turbo"; | ||
var openaiClient = new OpenAIClient(apiKey); | ||
var openaiMessageConnector = new OpenAIChatRequestMessageConnector(); | ||
var totalTokenCount = 0; | ||
var agent = new OpenAIChatAgent( | ||
openAIClient: openaiClient, | ||
name: "agent", | ||
modelName: model, | ||
systemMessage: "You are a helpful AI assistant") | ||
.RegisterMiddleware(async (messages, option, innerAgent, ct) => | ||
{ | ||
var reply = await innerAgent.GenerateReplyAsync(messages, option, ct); | ||
if (reply is MessageEnvelope<ChatCompletions> chatCompletions) | ||
{ | ||
var tokenCount = chatCompletions.Content.Usage.TotalTokens; | ||
totalTokenCount += tokenCount; | ||
} | ||
return reply; | ||
}) | ||
.RegisterMiddleware(openaiMessageConnector); | ||
#endregion Create_Agent | ||
|
||
#region Chat_With_Agent | ||
var reply = await agent.SendAsync("Tell me a joke"); | ||
Console.WriteLine($"Total token count: {totalTokenCount}"); | ||
#endregion Chat_With_Agent | ||
|
||
#region verify_reply | ||
reply.Should().BeOfType<TextMessage>(); | ||
totalTokenCount.Should().BeGreaterThan(0); | ||
#endregion verify_reply | ||
} | ||
|
||
public static async Task RunRagTaskAsync() | ||
{ | ||
#region Create_Agent | ||
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new InvalidOperationException("Please set the environment variable OPENAI_API_KEY"); | ||
var model = "gpt-3.5-turbo"; | ||
var openaiClient = new OpenAIClient(apiKey); | ||
var openaiMessageConnector = new OpenAIChatRequestMessageConnector(); | ||
var agent = new OpenAIChatAgent( | ||
openAIClient: openaiClient, | ||
name: "agent", | ||
modelName: model, | ||
systemMessage: "You are a helpful AI assistant") | ||
.RegisterMessageConnector() | ||
.RegisterMiddleware(async (messages, option, innerAgent, ct) => | ||
{ | ||
var today = DateTime.UtcNow; | ||
var todayMessage = new TextMessage(Role.System, $"Today is {today:yyyy-MM-dd}"); | ||
messages = messages.Concat(new[] { todayMessage }); | ||
return await innerAgent.GenerateReplyAsync(messages, option, ct); | ||
}) | ||
.RegisterPrintMessage(); | ||
#endregion Create_Agent | ||
|
||
#region Chat_With_Agent | ||
var reply = await agent.SendAsync("what's the date today"); | ||
#endregion Chat_With_Agent | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
dotnet/sample/AutoGen.BasicSamples/GettingStart/Chat_With_Agent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Chat_With_Agent.cs | ||
|
||
#region Using | ||
using AutoGen.Core; | ||
using AutoGen.OpenAI; | ||
using AutoGen.OpenAI.Extension; | ||
using Azure.AI.OpenAI; | ||
#endregion Using | ||
|
||
using FluentAssertions; | ||
|
||
namespace AutoGen.BasicSample; | ||
|
||
public class Chat_With_Agent | ||
{ | ||
public static async Task RunAsync() | ||
{ | ||
#region Create_Agent | ||
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable."); | ||
var model = "gpt-3.5-turbo"; | ||
var openaiClient = new OpenAIClient(apiKey); | ||
var agent = new OpenAIChatAgent( | ||
openAIClient: openaiClient, | ||
name: "agent", | ||
modelName: model, | ||
systemMessage: "You are a helpful AI assistant") | ||
.RegisterMessageConnector(); // convert OpenAI message to AutoGen message | ||
#endregion Create_Agent | ||
|
||
#region Chat_With_Agent | ||
var reply = await agent.SendAsync("Tell me a joke"); | ||
reply.Should().BeOfType<TextMessage>(); | ||
if (reply is TextMessage textMessage) | ||
{ | ||
Console.WriteLine(textMessage.Content); | ||
} | ||
#endregion Chat_With_Agent | ||
|
||
#region Chat_With_History | ||
reply = await agent.SendAsync("summarize the conversation", chatHistory: [reply]); | ||
#endregion Chat_With_History | ||
|
||
#region Streaming_Chat | ||
var question = new TextMessage(Role.User, "Tell me a long joke"); | ||
await foreach (var streamingReply in agent.GenerateStreamingReplyAsync([question])) | ||
{ | ||
if (streamingReply is TextMessageUpdate textMessageUpdate) | ||
{ | ||
Console.WriteLine(textMessageUpdate.Content); | ||
} | ||
} | ||
#endregion Streaming_Chat | ||
|
||
#region verify_reply | ||
reply.Should().BeOfType<TextMessage>(); | ||
#endregion verify_reply | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
dotnet/sample/AutoGen.BasicSamples/GettingStart/Dynamic_Group_Chat.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Dynamic_GroupChat.cs | ||
|
||
using AutoGen.Core; | ||
using AutoGen.OpenAI; | ||
using AutoGen.OpenAI.Extension; | ||
using AutoGen.SemanticKernel; | ||
using AutoGen.SemanticKernel.Extension; | ||
using Azure.AI.OpenAI; | ||
using Microsoft.SemanticKernel; | ||
|
||
namespace AutoGen.BasicSample; | ||
|
||
public class Dynamic_Group_Chat | ||
{ | ||
public static async Task RunAsync() | ||
{ | ||
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable."); | ||
var model = "gpt-3.5-turbo"; | ||
|
||
#region Create_Coder | ||
var openaiClient = new OpenAIClient(apiKey); | ||
var coder = new OpenAIChatAgent( | ||
openAIClient: openaiClient, | ||
name: "coder", | ||
modelName: model, | ||
systemMessage: "You are a C# coder, when writing csharp code, please put the code between ```csharp and ```") | ||
.RegisterMessageConnector() // convert OpenAI message to AutoGen message | ||
.RegisterPrintMessage(); // print the message content | ||
#endregion Create_Coder | ||
|
||
#region Create_Commenter | ||
var kernel = Kernel | ||
.CreateBuilder() | ||
.AddOpenAIChatCompletion(modelId: model, apiKey: apiKey) | ||
.Build(); | ||
var commenter = new SemanticKernelAgent( | ||
kernel: kernel, | ||
name: "commenter", | ||
systemMessage: "You write inline comments for the code snippet and add unit tests if necessary") | ||
.RegisterMessageConnector() // register message connector so it support AutoGen built-in message types like TextMessage. | ||
.RegisterPrintMessage(); // pretty print the message to the console | ||
#endregion Create_Commenter | ||
|
||
#region Create_UserProxy | ||
var userProxy = new DefaultReplyAgent("user", defaultReply: "END") | ||
.RegisterPrintMessage(); // print the message content | ||
#endregion Create_UserProxy | ||
|
||
#region Create_Group | ||
var admin = new OpenAIChatAgent( | ||
openAIClient: openaiClient, | ||
name: "admin", | ||
modelName: model) | ||
.RegisterMessageConnector(); // convert OpenAI message to AutoGen message | ||
|
||
var group = new GroupChat( | ||
members: [coder, commenter, userProxy], | ||
admin: admin); | ||
#endregion Create_Group | ||
|
||
#region Chat_With_Group | ||
var workflowInstruction = new TextMessage( | ||
Role.User, | ||
""" | ||
Here is the workflow of this group chat: | ||
User{Ask a question} -> Coder{Write code} | ||
Coder{Write code} -> Commenter{Add comments to the code} | ||
Commenter{Add comments to the code} -> User{END} | ||
"""); | ||
|
||
var question = new TextMessage(Role.User, "How to calculate the 100th Fibonacci number?"); | ||
var chatHistory = new List<IMessage> { workflowInstruction, question }; | ||
while (true) | ||
{ | ||
var replies = await group.CallAsync(chatHistory, maxRound: 1); | ||
var lastReply = replies.Last(); | ||
chatHistory.Add(lastReply); | ||
|
||
if (lastReply.From == userProxy.Name) | ||
{ | ||
break; | ||
} | ||
} | ||
#endregion Chat_With_Group | ||
|
||
#region Summarize_Chat_History | ||
var summary = await coder.SendAsync("summarize the conversation", chatHistory: chatHistory); | ||
#endregion Summarize_Chat_History | ||
} | ||
} |
Oops, something went wrong.