-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into lilyydu/tools
- Loading branch information
Showing
41 changed files
with
1,157 additions
and
480 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
29 changes: 29 additions & 0 deletions
29
dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI.Tests/AITests/AssistantMessageTest.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,29 @@ | ||
using Microsoft.Teams.AI.AI.Models; | ||
using Microsoft.Teams.AI.Tests.TestUtils; | ||
using OpenAI.Assistants; | ||
|
||
namespace Microsoft.Teams.AI.Tests.AITests | ||
{ | ||
public class AssistantsMessageTest | ||
{ | ||
[Fact] | ||
public void Test_Constructor() | ||
{ | ||
// Arrange | ||
MessageContent content = OpenAIModelFactory.CreateMessageContent("message", "fileId"); | ||
|
||
// Act | ||
AssistantsMessage assistantMessage = new AssistantsMessage(content); | ||
|
||
// Assert | ||
Assert.Equal(assistantMessage.MessageContent, content); | ||
|
||
ChatMessage chatMessage = assistantMessage; | ||
Assert.NotNull(chatMessage); | ||
Assert.Equal(chatMessage.Content, "message"); | ||
Assert.Equal(chatMessage.Context!.Citations[0].Url, "fileId"); | ||
Assert.Equal(chatMessage.Context.Citations[0].Title, ""); | ||
Assert.Equal(chatMessage.Context.Citations[0].Content, ""); | ||
} | ||
} | ||
} |
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
159 changes: 159 additions & 0 deletions
159
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,159 @@ | ||
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 MessageContent CreateMessageContent(string message, string fileId) | ||
{ | ||
var json = @$"{{ | ||
""id"": ""test"", | ||
""thread_id"": ""test"", | ||
""created_at"": 0, | ||
""content"": [ | ||
{{ | ||
""type"": ""text"", | ||
""text"": {{ | ||
""value"": ""{message}"", | ||
""annotations"": [ | ||
{{ | ||
""type"": ""file_citation"", | ||
""file_citation"": {{ | ||
""file_id"": ""{fileId}"" | ||
}} | ||
}} | ||
] | ||
}} | ||
}} | ||
] | ||
}}"; | ||
|
||
// Unable to directly read `MessageContent`. | ||
var threadMessage = ModelReaderWriter.Read<ThreadMessage>(BinaryData.FromString(json))!; | ||
|
||
return threadMessage.Content[0]; | ||
} | ||
|
||
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.