Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[.Net][AutoGen.OpenAI] Allow nullable system message, temperature and max token to support o1-preview model #3524

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
}
}
16 changes: 8 additions & 8 deletions dotnet/src/AutoGen.OpenAI/Agent/OpenAIChatAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>.
Expand All @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -124,7 +124,7 @@ private IEnumerable<ChatMessage> CreateChatMessages(IEnumerable<IMessage> 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);
}
Expand Down Expand Up @@ -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<ChatTool>? functions = null)
Expand Down
Loading