diff --git a/dotnet/sample/AutoGen.OpenAI.Sample/Connect_To_OpenAI_o1_preview.cs b/dotnet/sample/AutoGen.OpenAI.Sample/Connect_To_OpenAI_o1_preview.cs new file mode 100644 index 000000000000..52bc6381b9d5 --- /dev/null +++ b/dotnet/sample/AutoGen.OpenAI.Sample/Connect_To_OpenAI_o1_preview.cs @@ -0,0 +1,37 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Connect_To_OpenAI_o1_preview.cs + +using AutoGen.Core; +using OpenAI; + +namespace AutoGen.OpenAI.Sample; + +public class Connect_To_OpenAI_o1_preview +{ + public static async Task RunAsync() + { + #region create_agent + var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new InvalidOperationException("Please set environment variable OPENAI_API_KEY"); + var openAIClient = new OpenAIClient(apiKey); + + // until 2024/09/12 + // openai o1-preview doesn't support systemMessage, temperature, maxTokens, streaming output + // so in order to use OpenAIChatAgent with o1-preview, you need to set those parameters to null + var agent = new OpenAIChatAgent( + chatClient: openAIClient.GetChatClient("o1-preview"), + name: "assistant", + systemMessage: null, + temperature: null, + maxTokens: null, + seed: 0) + // by using RegisterMiddleware instead of RegisterStreamingMiddleware + // it turns an IStreamingAgent into an IAgent and disables streaming + .RegisterMiddleware(new OpenAIChatRequestMessageConnector()) + .RegisterPrintMessage(); + #endregion create_agent + + #region send_message + await agent.SendAsync("Can you write a piece of C# code to calculate 100th of fibonacci?"); + #endregion send_message + } +} diff --git a/dotnet/src/AutoGen.OpenAI/Agent/OpenAIChatAgent.cs b/dotnet/src/AutoGen.OpenAI/Agent/OpenAIChatAgent.cs index 44711c5e4289..b0085d0f33c6 100644 --- a/dotnet/src/AutoGen.OpenAI/Agent/OpenAIChatAgent.cs +++ b/dotnet/src/AutoGen.OpenAI/Agent/OpenAIChatAgent.cs @@ -34,7 +34,7 @@ public class OpenAIChatAgent : IStreamingAgent { private readonly ChatClient chatClient; private readonly ChatCompletionOptions options; - private readonly string systemMessage; + private readonly string? systemMessage; /// /// Create a new instance of . @@ -50,9 +50,9 @@ public class OpenAIChatAgent : IStreamingAgent public OpenAIChatAgent( ChatClient chatClient, string name, - string systemMessage = "You are a helpful AI assistant", - float temperature = 0.7f, - int maxTokens = 1024, + string? systemMessage = "You are a helpful AI assistant", + float? temperature = null, + int? maxTokens = null, int? seed = null, ChatResponseFormat? responseFormat = null, IEnumerable? functions = null) @@ -75,7 +75,7 @@ public OpenAIChatAgent( ChatClient chatClient, string name, ChatCompletionOptions options, - string systemMessage = "You are a helpful AI assistant") + string? systemMessage = "You are a helpful AI assistant") { this.chatClient = chatClient; this.Name = name; @@ -124,7 +124,7 @@ private IEnumerable CreateChatMessages(IEnumerable messag }); // add system message if there's no system message in messages - if (!oaiMessages.Any(m => m is SystemChatMessage)) + if (!oaiMessages.Any(m => m is SystemChatMessage) && systemMessage is not null) { oaiMessages = new[] { new SystemChatMessage(systemMessage) }.Concat(oaiMessages); } @@ -192,8 +192,8 @@ private ChatCompletionOptions CreateChatCompletionsOptions(GenerateReplyOptions? } private static ChatCompletionOptions CreateChatCompletionOptions( - float temperature = 0.7f, - int maxTokens = 1024, + float? temperature = 0.7f, + int? maxTokens = 1024, int? seed = null, ChatResponseFormat? responseFormat = null, IEnumerable? functions = null)