mirror of https://github.com/microsoft/autogen.git
[.Net][AutoGen.OpenAI] Allow nullable system message, temperature and max token to support o1-preview model (#3524)
* add connect to o1 example * Update Connect_To_OpenAI_o1_preview.cs
This commit is contained in:
parent
63d3297ffc
commit
e76c39e52d
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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 : 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<ChatTool>? functions = null)
|
||||
|
@ -75,7 +75,7 @@ public class OpenAIChatAgent : IStreamingAgent
|
|||
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 @@ 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)
|
||||
|
|
Loading…
Reference in New Issue