Skip to content

[C#] fix: Map tokens config to max_tokens when non-o1 model is used. #2150

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

Merged
merged 1 commit into from
Oct 31, 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
Expand Up @@ -33,6 +33,25 @@ public void Test_Constructor_OpenAI()
new OpenAIModel(options);
}

[Fact]
public void Test_SetMaxTokens()
{
// Arrange
var options = new OpenAIModelOptions("test-key", "test-model");
var chatCompletionOptions = new ChatCompletionOptions();
var model = new OpenAIModel(options);
var testTokens = 100;

// Act
model.SetMaxTokens(testTokens, chatCompletionOptions);

// Assert
MethodInfo info = chatCompletionOptions.GetType().GetMethod("get__deprecatedMaxTokens", BindingFlags.NonPublic | BindingFlags.Instance)!;
int maxTokens = (int)info.Invoke(chatCompletionOptions, null)!;
Assert.Equal(testTokens, maxTokens);
}


[Fact]
public void Test_Constructor_AzureOpenAI_InvalidAzureApiVersion()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
using Azure.AI.OpenAI.Chat;
using OpenAI.Chat;
using Microsoft.Teams.AI.Application;
using System.Reflection;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Microsoft.Teams.AI.Tests")]
#pragma warning disable AOAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
namespace Microsoft.Teams.AI.AI.Models
{
Expand Down Expand Up @@ -223,6 +226,10 @@ public async Task<PromptResponse> CompletePromptAsync(ITurnContext turnContext,
chatCompletionOptions.Temperature = 1;
chatCompletionOptions.TopP = 1;
chatCompletionOptions.PresencePenalty = 0;
} else
{
// `MaxOutputTokenCount` is not supported for non-o1 Azure OpenAI models, hence it needs to be set for it to work.
SetMaxTokens(completion.MaxTokens, chatCompletionOptions);
}

// Set tools configurations
Expand All @@ -248,6 +255,12 @@ public async Task<PromptResponse> CompletePromptAsync(ITurnContext turnContext,
AddAzureChatExtensionConfigurations(chatCompletionOptions, additionalData);
}

if (_options.LogRequests!.Value)
{
_logger.LogTrace("CHAT COMPLETION CONFIG:");
_logger.LogTrace(JsonSerializer.Serialize(chatCompletionOptions, _serializerOptions));
}


PipelineResponse? rawResponse = null;
ClientResult<ChatCompletion>? chatCompletionsResponse = null;
Expand Down Expand Up @@ -458,6 +471,12 @@ private void AddAzureChatExtensionConfigurations(ChatCompletionOptions options,
}
}
}

internal void SetMaxTokens(int maxTokens, ChatCompletionOptions options)
{
MethodInfo setMaxTokens = options.GetType().GetMethod("set__deprecatedMaxTokens", BindingFlags.NonPublic | BindingFlags.Instance);
setMaxTokens.Invoke(options, new object[] { maxTokens });
}
}
}
#pragma warning restore AOAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
1 change: 1 addition & 0 deletions dotnet/samples/04.e.twentyQuestions/teamsapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,4 @@ deploy:
with:
artifactFolder: bin/Release/net6.0/win-x86/publish
resourceId: ${{BOT_AZURE_APP_SERVICE_RESOURCE_ID}}
projectId: 6d2b99e4-1480-4218-8d1c-46630228f713
1 change: 1 addition & 0 deletions dotnet/samples/08.datasource.azureopenai/teamsapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,4 @@ deploy:
# You can replace it with an existing Azure Resource ID or other
# custom environment variable.
resourceId: ${{BOT_AZURE_APP_SERVICE_RESOURCE_ID}}
projectId: 8c3187b5-ec34-4e3b-afd8-4526ab52d06e
Loading