A programming framework for agentic AI
[.Net][AutoGen.OpenAI] Allow nullable system message, temperature and max token to support o1-preview model (#3524)
Xiaoyun Zhange76c39e52d1d
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.csnew file mode 100644index 000000000..52bc6381b--- /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.csindex 44711c5e4..b0085d0f3 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;/// <summary>/// Create a new instance of <see cref="OpenAIChatAgent"/>.@@ -50,9 +50,9 @@ public class OpenAIChatAgent : IStreamingAgentpublic 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<ChatTool>? functions = null)@@ -75,7 +75,7 @@ public class OpenAIChatAgent : IStreamingAgentChatClient 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 @@ public class OpenAIChatAgent : IStreamingAgent});// 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 @@ public class OpenAIChatAgent : IStreamingAgent}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<ChatTool>? functions = null)