-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[C#] feat:
AssistantsPlanner
support for file_search
tool annotat…
…ions (#1913) ## Linked issues closes: #minor #1590, #1585 ## Details * ~Bump `OpenAI` to 2.0.0-beta.8~ * Add `AssistantMessage` class that represents a message returned by the OpenAI Assistant. ## 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
4 changed files
with
120 additions
and
5 deletions.
There are no files selected for viewing
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
58 changes: 58 additions & 0 deletions
58
dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Models/AssistantsMessage.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,58 @@ | ||
using OpenAI.Assistants; | ||
|
||
namespace Microsoft.Teams.AI.AI.Models | ||
{ | ||
/// <summary> | ||
/// Represents a message returned by the OpenAI Assistants API. | ||
/// </summary> | ||
public class AssistantsMessage : ChatMessage | ||
{ | ||
/// <summary> | ||
/// The message contents from an assistants api response. | ||
/// </summary> | ||
public MessageContent MessageContent; | ||
|
||
/// <summary> | ||
/// Creates an AssistantMessage. | ||
/// </summary> | ||
/// <param name="content">The Assistants API thread message.</param> | ||
public AssistantsMessage(MessageContent content) : base(ChatRole.Assistant) | ||
{ | ||
this.MessageContent = content; | ||
|
||
if (content != null) | ||
{ | ||
string? textContent = content.Text; | ||
if (content.Text != null && content.Text != string.Empty) | ||
{ | ||
this.Content = content.Text; | ||
} | ||
|
||
MessageContext context = new(); | ||
for (int i = 0; i < content.TextAnnotations.Count; i++) | ||
{ | ||
TextAnnotation annotation = content.TextAnnotations[i]; | ||
if (annotation?.TextToReplace != null) | ||
{ | ||
textContent.Replace(annotation.TextToReplace, $"[{i}]"); | ||
} | ||
|
||
if (annotation?.InputFileId != null) | ||
{ | ||
// Retrieve file info object | ||
// Neither `content` or `title` is provided in the annotations | ||
context.Citations.Add(new("", "", annotation.InputFileId)); | ||
} | ||
|
||
if (annotation?.OutputFileId != null) | ||
{ | ||
// TODO: Download files or provide link to end user. | ||
// Files were generated by code interpretor tool. | ||
} | ||
} | ||
|
||
Context = context; | ||
} | ||
} | ||
} | ||
} |
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