Skip to content

Commit

Permalink
[C#] feat: O1 model support (#2111)
Browse files Browse the repository at this point in the history
## Linked issues

closes: #2103 

## Details
Added support for `o1-preview` and `o1-mini` models.

* Bumped `OpenAI` and `Azure.AI.OpenAI` to `2.1.0-beta.1`.
* Tested o1 support with light bot sample with monologue augmentation.
* Updated `teamsChefBot-streaming` to use deployed `Microsoft.Teams.AI`
nuget package.
* Fixed `LayoutSection` bug with incorrect ordering of sections.

## Attestation Checklist

- [x] My code follows the style guidelines of this project

- I have checked for/fixed spelling, linting, and other errors
- I have commented my code for clarity
- I have made corresponding changes to the documentation (updating the
doc strings in the code is sufficient)
- My changes generate no new warnings
- I have added tests that validates my changes, and provides sufficient
test coverage. I have tested with:
  - Local testing
  - E2E testing in Teams
- New and existing unit tests pass locally with my changes
  • Loading branch information
singhk97 authored Oct 17, 2024
1 parent d4bf9cc commit d4ac6c4
Show file tree
Hide file tree
Showing 29 changed files with 172 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void Test_Constructor()
{
// Arrange
MessageContent content = OpenAIModelFactory.CreateMessageContent("message", "fileId");
Mock<FileClient> fileClientMock = new Mock<FileClient>();
Mock<OpenAIFileClient> fileClientMock = new Mock<OpenAIFileClient>();
fileClientMock.Setup(fileClient => fileClient.DownloadFileAsync("fileId", It.IsAny<CancellationToken>())).Returns(() =>
{
return Task.FromResult(ClientResult.FromValue(BinaryData.FromString("test"), new Mock<PipelineResponse>().Object));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void Test_Initialization_From_OpenAISdk_ChatMessage()
""citations"": [
{{
""title"": ""test-title"",
""url"": ""test-url"",
""url"": ""https://www.test-uri.com/"",
""content"": ""test-content""
}}
]
Expand All @@ -69,7 +69,7 @@ public void Test_Initialization_From_OpenAISdk_ChatMessage()
Assert.NotNull(context);
Assert.Single(context.Citations);
Assert.Equal("test-title", context.Citations[0].Title);
Assert.Equal("test-url", context.Citations[0].Url);
Assert.Equal("https://www.test-uri.com/", context.Citations[0].Url);
Assert.Equal("test-content", context.Citations[0].Content);
}

Expand Down Expand Up @@ -179,10 +179,10 @@ public void Test_AssistantRole_ToOpenAISdkChatMessage_FunctionCall()
// Assert
var assistantMessage = result as AssistantChatMessage;
Assert.NotNull(assistantMessage);
Assert.Equal("test-content", assistantMessage.Content[0].Text);
Assert.Empty(assistantMessage.Content);
// TODO: Uncomment when participant name issue is resolved.
//Assert.Equal("test-name", assistantMessage.ParticipantName);
Assert.Equal("test-arg1", assistantMessage.FunctionCall.FunctionArguments);
Assert.Equal("test-arg1", assistantMessage.FunctionCall.FunctionArguments.ToString());
Assert.Equal("test-name", assistantMessage.FunctionCall.FunctionName);
}

Expand All @@ -206,7 +206,7 @@ public void Test_AssistantRole_ToOpenAISdkChatMessage_ActionCall()
// Assert
var assistantMessage = result as AssistantChatMessage;
Assert.NotNull(assistantMessage);
Assert.Equal("test-content", assistantMessage.Content[0].Text);
Assert.Empty(assistantMessage.Content);
// TODO: Uncomment when participant name issue is resolved.
//Assert.Equal("test-name", assistantMessage.ParticipantName);

Expand All @@ -215,7 +215,7 @@ public void Test_AssistantRole_ToOpenAISdkChatMessage_ActionCall()
Assert.NotNull(toolCall);
Assert.Equal("test-id", toolCall.Id);
Assert.Equal("test-tool-name", toolCall.FunctionName);
Assert.Equal("test-tool-arg1", toolCall.FunctionArguments);
Assert.Equal("test-tool-arg1", toolCall.FunctionArguments.ToString());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public sealed class ChatCompletionToolCallTests
public void Test_ChatCompletionsToolCall_ToFunctionToolCall()
{
// Arrange
var functionToolCall = ChatToolCall.CreateFunctionToolCall("test-id", "test-name", "test-arg1");
var functionToolCall = ChatToolCall.CreateFunctionToolCall("test-id", "test-name", BinaryData.FromString("test-arg1"));

// Act
var azureSdkFunctionToolCall = ChatCompletionsToolCall.FromChatToolCall(functionToolCall);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ public void Test_AssistantRole_ToOpenAISdkChatMessage_FunctionCall()
// Assert
var assistantMessage = result as AssistantChatMessage;
Assert.NotNull(assistantMessage);
Assert.Equal("test-content", assistantMessage.Content[0].Text);
Assert.Empty(assistantMessage.Content);
// TODO: Uncomment when participant name issue is resolved.
//Assert.Equal("test-name", assistantMessage.ParticipantName);
Assert.Equal("test-arg1", assistantMessage.FunctionCall.FunctionArguments);
Assert.Equal("test-arg1", assistantMessage.FunctionCall.FunctionArguments.ToString());
Assert.Equal("test-name", assistantMessage.FunctionCall.FunctionName);
}

Expand All @@ -113,14 +113,14 @@ public void Test_AssistantRole_ToOpenAISdkChatMessage_ToolCall()
// Assert
var assistantMessage = result as AssistantChatMessage;
Assert.NotNull(assistantMessage);
Assert.Equal("test-content", assistantMessage.Content[0].Text);
Assert.Empty(assistantMessage.Content);

Assert.Single(assistantMessage.ToolCalls);
ChatToolCall toolCall = assistantMessage.ToolCalls[0];
Assert.NotNull(toolCall);
Assert.Equal("test-id", toolCall.Id);
Assert.Equal("test-tool-name", toolCall.FunctionName);
Assert.Equal("test-tool-arg1", toolCall.FunctionArguments);
Assert.Equal("test-tool-arg1", toolCall.FunctionArguments.ToString());
}

[Fact]
Expand Down Expand Up @@ -198,7 +198,7 @@ public void Test_ChatCompletionsToolCall_ToFunctionToolCall()
Assert.NotNull(chatToolCall);
Assert.Equal("test-id", chatToolCall.Id);
Assert.Equal("test-name", chatToolCall.FunctionName);
Assert.Equal("test-arg1", chatToolCall.FunctionArguments);
Assert.Equal("test-arg1", chatToolCall.FunctionArguments.ToString());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void Test_Constructor_AzureOpenAI_InvalidAzureApiVersion()
var options = new AzureOpenAIModelOptions("test-key", "test-deployment", "https://test.openai.azure.com/");
var versions = new List<string>
{
"2024-04-01-preview", "2024-05-01-preview", "2024-06-01"
"2024-06-01", "2024-08-01-preview", "2024-10-01-preview"
};

// Act
Expand Down Expand Up @@ -279,8 +279,8 @@ public async Task Test_CompletePromptAsync_AzureOpenAI_Chat_WithTools()

Assert.NotNull(result.Message.ActionCalls);
Assert.Single(result.Message.ActionCalls);
Assert.Equal("testAction", result.Message.ActionCalls[0].Function.Name);

Assert.Equal("testAction", result.Message.ActionCalls[0].Function!.Name);
Assert.Null(result.Error);
Assert.Equal(ChatRole.Assistant, result.Message.Role);
Assert.Null(result.Message.Content);
Expand Down Expand Up @@ -326,7 +326,7 @@ public async Task Test_CompletePromptAsync_AzureOpenAI_Streaming()
]
}}"));

TestAsyncResultCollection<StreamingChatCompletionUpdate> updates = new(update!, Mock.Of<PipelineResponse>());
TestAsyncCollectionResult<StreamingChatCompletionUpdate> updates = new(update!, Mock.Of<PipelineResponse>());

var response = new TestResponse(200, string.Empty);
clientMock.Setup((client) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task Test_OpenAI_CreateEmbeddings_ReturnEmbeddings()
IList<string> inputs = new List<string> { "test" };
var clientMock = new Mock<OpenAIClient>(new ApiKeyCredential(apiKey), It.IsAny<OpenAIClientOptions>());
var response = new TestResponse(200, string.Empty);
var embeddingCollection = ModelReaderWriter.Read<EmbeddingCollection>(BinaryData.FromString(@"{
var embeddingCollection = ModelReaderWriter.Read<OpenAIEmbeddingCollection>(BinaryData.FromString(@"{
""data"": [
{
""object"": ""embedding"",
Expand Down Expand Up @@ -76,7 +76,7 @@ public async Task Test_AzureOpenAI_CreateEmbeddings_ReturnEmbeddings()
IList<string> inputs = new List<string> { "test" };
var clientMock = new Mock<OpenAIClient>(new ApiKeyCredential(apiKey), It.IsAny<OpenAIClientOptions>());
var response = new TestResponse(200, string.Empty);
var embeddingCollection = ModelReaderWriter.Read<EmbeddingCollection>(BinaryData.FromString(@"{
var embeddingCollection = ModelReaderWriter.Read<OpenAIEmbeddingCollection>(BinaryData.FromString(@"{
""data"": [
{
""object"": ""embedding"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" Version="2.0.0-beta.5" />
<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0-beta.1" />
<PackageReference Include="Azure.Identity" Version="1.12.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageReference Include="Microsoft.Bot.Builder" Version="4.22.9" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="OpenAI" Version="2.0.0-beta.11" />
<PackageReference Include="OpenAI" Version="2.1.0-beta.1" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using OpenAI.Assistants;
using OpenAI.Files;
using System.ClientModel;
using System.ClientModel.Primitives;
using OAI = OpenAI;

namespace Microsoft.Teams.AI.Tests.TestUtils
{
Expand Down Expand Up @@ -89,7 +89,7 @@ public static MessageContent CreateMessageContent(string message, string fileId)
return threadMessage.Content[0];
}

public static OpenAIFileInfo CreateOpenAIFileInfo(string fileId)
public static OAI.Files.OpenAIFile CreateOpenAIFileInfo(string fileId)
{
var json = @$"{{
""id"": ""{fileId}"",
Expand All @@ -100,7 +100,7 @@ public static OpenAIFileInfo CreateOpenAIFileInfo(string fileId)
""purpose"": ""assistants""
}}";

var fileInfo = ModelReaderWriter.Read<OpenAIFileInfo>(BinaryData.FromString(json))!;
var fileInfo = ModelReaderWriter.Read<OAI.Files.OpenAIFile>(BinaryData.FromString(json))!;

return fileInfo;
}
Expand Down Expand Up @@ -160,82 +160,40 @@ public TestRequiredAction(string toolCallId, string functionName, string functio
}
}

internal sealed class TestAsyncPageCollection<T> : AsyncPageCollection<T> where T : class
internal sealed class TestAsyncCollectionResult<T> : AsyncCollectionResult<T> where T : class
{
public List<T> Items;
internal PipelineResponse _pipelineResponse;
private IAsyncEnumerator<PageResult<T>> _enumerator;

public TestAsyncPageCollection(List<T> items, PipelineResponse response)
public TestAsyncCollectionResult(List<T> items, PipelineResponse response)
{
Items = items;
_pipelineResponse = response;
_enumerator = new TestAsyncEnumerator<T>(items, response);
}

protected override IAsyncEnumerator<PageResult<T>> GetAsyncEnumeratorCore(CancellationToken cancellationToken = default)
public TestAsyncCollectionResult(T item, PipelineResponse response)
{
return _enumerator;
}

protected override Task<PageResult<T>> GetCurrentPageAsyncCore()
{
return Task.FromResult(_enumerator.Current);
}
}

internal sealed class TestAsyncEnumerator<T> : IAsyncEnumerator<PageResult<T>> where T : class
{
private readonly List<T> _items;
private readonly PipelineResponse _pipelineResponse;
private bool _movedOnToNext;

public TestAsyncEnumerator(List<T> items, PipelineResponse response)
{
_items = items;
Items = new() { item };
_pipelineResponse = response;
_movedOnToNext = false;
}

public PageResult<T> Current => PageResult<T>.Create(_items, ContinuationToken.FromBytes(BinaryData.FromString("")), null, _pipelineResponse);

public ValueTask DisposeAsync()
public override ContinuationToken? GetContinuationToken(ClientResult page)
{
return new ValueTask();
return ContinuationToken.FromBytes(BinaryData.FromString(""));
}

public ValueTask<bool> MoveNextAsync()
public async override IAsyncEnumerable<ClientResult> GetRawPagesAsync()
{
if (!_movedOnToNext)
{
return new ValueTask<bool>(true);
}
else
{
_movedOnToNext = true;
return new ValueTask<bool>(false);
}

yield return await Task.FromResult(ClientResult.FromValue(Items, _pipelineResponse));
}
}

internal sealed class TestAsyncResultCollection<T> : AsyncCollectionResult<T> where T : class
{
public List<T> Items = new();

internal PipelineResponse _pipelineResponse;

public TestAsyncResultCollection(T item, PipelineResponse response)
protected async override IAsyncEnumerable<T> GetValuesFromPageAsync(ClientResult page)
{
Items.Add(item);
_pipelineResponse = response;
}
foreach (T item in Items)
{
yield return await Task.FromResult(item);
}

#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
public override async IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{
yield return FromValue(Items[0], _pipelineResponse);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private ThreadMessage _CreateMessage(string threadId, string message)
return newMessage;
}

public override AsyncPageCollection<ThreadMessage> GetMessagesAsync(string threadId, MessageCollectionOptions options, CancellationToken cancellationToken = default)
public override AsyncCollectionResult<ThreadMessage> GetMessagesAsync(string threadId, MessageCollectionOptions options, CancellationToken cancellationToken = default)
{
while (RemainingMessages.Count > 0)
{
Expand All @@ -86,12 +86,12 @@ public override AsyncPageCollection<ThreadMessage> GetMessagesAsync(string threa

// Sorted by oldest first
List<ThreadMessage> messages = Messages[threadId].ToList();
if (options != null && options.Order != null && options.Order.Value == ListOrder.NewestFirst)
if (options != null && options.Order != null && options.Order.Value == MessageCollectionOrder.Descending)
{
messages.Reverse();
}

return new TestAsyncPageCollection<ThreadMessage>(messages, Mock.Of<PipelineResponse>());
return new TestAsyncCollectionResult<ThreadMessage>(messages, Mock.Of<PipelineResponse>());
}

public override Task<ClientResult<ThreadRun>> CreateRunAsync(string threadId, string assistantId, RunCreationOptions createRunOptions, CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -152,22 +152,22 @@ public override Task<ClientResult<ThreadRun>> GetRunAsync(string threadId, strin
return runWithUpdatedStatus;
}

public override AsyncPageCollection<ThreadRun> GetRunsAsync(string threadId, RunCollectionOptions? options = null, CancellationToken cancellationToken = default)
public override AsyncCollectionResult<ThreadRun> GetRunsAsync(string threadId, RunCollectionOptions? options = null, CancellationToken cancellationToken = default)
{
AsyncPageCollection<ThreadRun> response;
AsyncCollectionResult<ThreadRun> response;

// AssistantsPlanner only needs the get the latest.
if (Runs[threadId].Count() == 0)
{
response = new TestAsyncPageCollection<ThreadRun>(new List<ThreadRun>(), Mock.Of<PipelineResponse>());
response = new TestAsyncCollectionResult<ThreadRun>(new List<ThreadRun>(), Mock.Of<PipelineResponse>());
return response;
}

int lastIndex = Runs[threadId].Count() - 1;
ThreadRun run = Runs[threadId][lastIndex];
ThreadRun runWithUpdatedStatus = _GetRun(threadId, run.Id)!;

response = new TestAsyncPageCollection<ThreadRun>(new List<ThreadRun>() { runWithUpdatedStatus }, Mock.Of<PipelineResponse>());
response = new TestAsyncCollectionResult<ThreadRun>(new List<ThreadRun>() { runWithUpdatedStatus }, Mock.Of<PipelineResponse>());
return response;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ CancellationToken cancellationToken
repairTemplate.Prompt = new(new()
{
this.Options.Template.Prompt,
new ConversationHistorySection($"{this.Options.HistoryVariable}-repair")
new ConversationHistorySection($"{this.Options.HistoryVariable}-repair", -1)
});

if (this.Options.LogRepairs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ public async Task<EmbeddingsResponse> CreateEmbeddingsAsync(IList<string> inputs
try
{
DateTime startTime = DateTime.Now;
ClientResult<EmbeddingCollection> response = await embeddingsClient.GenerateEmbeddingsAsync(inputs);
List<ReadOnlyMemory<float>> embeddingItems = response.Value.OrderBy(item => item.Index).Select(item => item.Vector).ToList();
ClientResult<OpenAIEmbeddingCollection> response = await embeddingsClient.GenerateEmbeddingsAsync(inputs);
List<ReadOnlyMemory<float>> embeddingItems = response.Value.OrderBy(item => item.Index).Select(item => item.ToFloats()).ToList();

if (_options.LogRequests!.Value)
{
Expand Down Expand Up @@ -170,9 +170,9 @@ public async Task<EmbeddingsResponse> CreateEmbeddingsAsync(IList<string> inputs
{
return apiVersion switch
{
"2024-04-01-preview" => ServiceVersion.V2024_04_01_Preview,
"2024-05-01-preview" => ServiceVersion.V2024_05_01_Preview,
"2024-06-01" => ServiceVersion.V2024_06_01,
"2024-08-01-preview" => ServiceVersion.V2024_08_01_Preview,
"2024-10-01-preview" => ServiceVersion.V2024_10_01_Preview,
_ => null,
};
}
Expand Down
Loading

0 comments on commit d4ac6c4

Please sign in to comment.