-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[C#] feat: Migrate
AssistantsPlanner
to official OpenAI .NET SDK (#…
…1895) ## Linked issues closes: #1831 (issue number) ## Details * Updated `AssistantsPlanner`, unit tests, and math & order bot. * The underlying SDK uses `beta-v2` by default. ## 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
Showing
19 changed files
with
802 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI.Tests/TestUtils/OpenAIModelFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
using OpenAI.Assistants; | ||
using System.ClientModel; | ||
using System.ClientModel.Primitives; | ||
|
||
namespace Microsoft.Teams.AI.Tests.TestUtils | ||
{ | ||
internal sealed class OpenAIModelFactory | ||
{ | ||
public static RunCreationOptions CreateRunOptions() | ||
{ | ||
return new RunCreationOptions(); | ||
} | ||
|
||
public static RequiredAction CreateRequiredAction(string toolCallId, string functionName, string functionArguments) | ||
{ | ||
return new TestRequiredAction(toolCallId, functionName, functionArguments); | ||
} | ||
|
||
public static Assistant CreateAssistant() | ||
{ | ||
return ModelReaderWriter.Read<Assistant>(BinaryData.FromString(@$"{{ | ||
""id"": ""{Guid.NewGuid()}"", | ||
""object"": ""assistant"", | ||
""created_at"": {DateTime.Now.Second} | ||
}}"))!; | ||
} | ||
|
||
public static AssistantThread CreateAssistantThread(string guid, DateTimeOffset offset) | ||
{ | ||
return ModelReaderWriter.Read<AssistantThread>(BinaryData.FromString(@$"{{ | ||
""id"": ""{guid}"", | ||
""created_at"": {offset.Second} | ||
}}"))!; | ||
} | ||
|
||
public static ThreadMessage CreateThreadMessage(string threadId, string message) | ||
{ | ||
var json = @$"{{ | ||
""id"": ""{Guid.NewGuid()}"", | ||
""thread_id"": ""{threadId}"", | ||
""created_at"": {DateTime.Now.Second}, | ||
""content"": [ | ||
{{ | ||
""type"": ""text"", | ||
""text"": {{ | ||
""value"": ""{message}"", | ||
""annotations"": [] | ||
}} | ||
}} | ||
] | ||
}}"; | ||
return ModelReaderWriter.Read<ThreadMessage>(BinaryData.FromString(json))!; | ||
} | ||
|
||
public static ThreadRun CreateThreadRun(string threadId, string runStatus, string? runId = null, IList<RequiredAction> requiredActions = null!) | ||
{ | ||
var raJson = "{}"; | ||
if (requiredActions != null && requiredActions.Count > 0) | ||
{ | ||
var toolCalls = requiredActions.Select((requiredAction) => | ||
{ | ||
var ra = (TestRequiredAction)requiredAction; | ||
return $@"{{ | ||
""id"": ""{ra.ToolCallId}"", | ||
""type"": ""function"", | ||
""function"": {{ | ||
""name"": ""{ra.FunctionName}"", | ||
""arguments"": ""{ra.FunctionArguments}"" | ||
}} | ||
}}"; | ||
}); | ||
|
||
raJson = $@"{{ | ||
""type"": ""submit_tool_outputs"", | ||
""submit_tool_outputs"": {{ | ||
""tool_calls"": [ | ||
{string.Join(",", toolCalls)} | ||
] | ||
}} | ||
}} | ||
"; | ||
} | ||
|
||
return ModelReaderWriter.Read<ThreadRun>(BinaryData.FromString(@$"{{ | ||
""id"": ""{runId ?? Guid.NewGuid().ToString()}"", | ||
""thread_id"": ""{threadId}"", | ||
""created_at"": {DateTime.Now.Second}, | ||
""status"": ""{runStatus}"", | ||
""required_action"": {raJson} | ||
}}"))!; | ||
} | ||
} | ||
|
||
internal sealed class TestRequiredAction : RequiredAction | ||
{ | ||
public new string FunctionName; | ||
|
||
public new string FunctionArguments; | ||
|
||
public new string ToolCallId; | ||
|
||
public TestRequiredAction(string toolCallId, string functionName, string functionArguments) | ||
{ | ||
this.FunctionName = functionName; | ||
this.FunctionArguments = functionArguments; | ||
this.ToolCallId = toolCallId; | ||
} | ||
} | ||
|
||
internal sealed class TestAsyncPageableCollection<T> : AsyncPageableCollection<T> where T : class | ||
{ | ||
public List<T> Items; | ||
|
||
internal PipelineResponse _pipelineResponse; | ||
|
||
public TestAsyncPageableCollection(List<T> items, PipelineResponse response) | ||
{ | ||
Items = items; | ||
_pipelineResponse = response; | ||
} | ||
|
||
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously | ||
public override async IAsyncEnumerable<ResultPage<T>> AsPages(string? continuationToken = null, int? pageSizeHint = null) | ||
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously | ||
{ | ||
yield return ResultPage<T>.Create(Items, null, _pipelineResponse); | ||
} | ||
} | ||
} |
Oops, something went wrong.