Skip to content

Commit

Permalink
[.Net][AutoGen.OpenAI] Allow nullable system message, temperature and…
Browse files Browse the repository at this point in the history
… max token to support o1-preview model (#3524)

* add connect to o1 example

* Update Connect_To_OpenAI_o1_preview.cs
  • Loading branch information
LittleLittleCloud authored Sep 13, 2024
1 parent 63d3297 commit e76c39e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
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

0 comments on commit e76c39e

Please sign in to comment.