Skip to content
Open
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
42 changes: 41 additions & 1 deletion src/Custom/Assistants/MessageCreationAttachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,47 @@ public partial class MessageCreationAttachment
public IReadOnlyList<ToolDefinition> Tools { get; }

private void SerializeTools(Utf8JsonWriter writer, ModelReaderWriterOptions options)
=> writer.WriteObjectValue(Tools, options);
{
if (Tools is null)
{
writer.WriteNullValue();
return;
}

writer.WriteStartArray();
foreach (ToolDefinition tool in Tools)
{
using var ms = new System.IO.MemoryStream();
using (var tempWriter = new Utf8JsonWriter(ms))
{
tempWriter.WriteObjectValue(tool, options);
tempWriter.Flush();
}

using (JsonDocument doc = JsonDocument.Parse(ms.ToArray()))
{
JsonElement root = doc.RootElement;
if (root.ValueKind == JsonValueKind.Object)
{
writer.WriteStartObject();
foreach (var prop in root.EnumerateObject())
{
if (prop.NameEquals("file_search"u8))
{
continue;
}
prop.WriteTo(writer);
}
writer.WriteEndObject();
}
else
{
root.WriteTo(writer);
}
}
}
writer.WriteEndArray();
}

private static void DeserializeTools(JsonProperty property, ref IReadOnlyList<ToolDefinition> tools)
{
Expand Down
60 changes: 52 additions & 8 deletions tests/Assistants/AssistantsTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
using Microsoft.ClientModel.TestFramework;
using NUnit.Framework;
using NUnit.Framework.Internal;
using OpenAI.Assistants;
using OpenAI.Files;
using OpenAI.Tests.Utility;
using OpenAI.VectorStores;
using System;
using System.ClientModel;
using System.ClientModel.Primitives;
Expand All @@ -14,6 +7,19 @@
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.ClientModel.TestFramework;
using NUnit.Framework;
using NUnit.Framework;

Check warning on line 12 in tests/Assistants/AssistantsTests.cs

View workflow job for this annotation

GitHub Actions / Build

The using directive for 'NUnit.Framework' appeared previously in this namespace
using NUnit.Framework.Internal;
using NUnit.Framework.Internal;

Check warning on line 14 in tests/Assistants/AssistantsTests.cs

View workflow job for this annotation

GitHub Actions / Build

The using directive for 'NUnit.Framework.Internal' appeared previously in this namespace
using OpenAI.Assistants;
using OpenAI.Assistants;

Check warning on line 16 in tests/Assistants/AssistantsTests.cs

View workflow job for this annotation

GitHub Actions / Build

The using directive for 'OpenAI.Assistants' appeared previously in this namespace
using OpenAI.Files;
using OpenAI.Files;

Check warning on line 18 in tests/Assistants/AssistantsTests.cs

View workflow job for this annotation

GitHub Actions / Build

The using directive for 'OpenAI.Files' appeared previously in this namespace
using OpenAI.Tests.Utility;
using OpenAI.Tests.Utility;

Check warning on line 20 in tests/Assistants/AssistantsTests.cs

View workflow job for this annotation

GitHub Actions / Build

The using directive for 'OpenAI.Tests.Utility' appeared previously in this namespace
using OpenAI.VectorStores;
using OpenAI.VectorStores;

Check warning on line 22 in tests/Assistants/AssistantsTests.cs

View workflow job for this annotation

GitHub Actions / Build

The using directive for 'OpenAI.VectorStores' appeared previously in this namespace
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you fix the duplicated namespaces?

using static OpenAI.Tests.TestHelpers;

namespace OpenAI.Tests.Assistants;
Expand All @@ -30,7 +36,6 @@
private readonly List<string> _vectorStoreIdsToDelete = [];

private static readonly DateTimeOffset s_2024 = new(2024, 1, 1, 0, 0, 0, TimeSpan.Zero);
private static readonly string s_testAssistantName = $".NET SDK Test Assistant - Please Delete Me";
private static readonly string s_cleanupMetadataKey = $"test_metadata_cleanup_eligible";

private AssistantClient GetTestClient() => GetProxiedOpenAIClient<AssistantClient>(TestScenario.Assistants);
Expand Down Expand Up @@ -847,6 +852,45 @@
});
}

[Test]
public async Task FileOnMessageWorks()
{
// First, we need to upload a simple test file.
OpenAIFileClient fileClient = GetTestClient<OpenAIFileClient>(TestScenario.Files);
OpenAIFile testFile = fileClient.UploadFile(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switch to the async version of this method: UploadFileAsync

BinaryData.FromString("""
This file describes the favorite foods of several people.
Summanus Ferdinand: tacos
Tekakwitha Effie: pizza
Filip Carola: cake
""").ToStream(),
"favorite_foods.txt",
FileUploadPurpose.Assistants);
Validate(testFile);

AssistantClient client = GetTestClient();

var thread = client.CreateThread();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Switch to the async version of this method: CreateThreadAsync.
  2. Then call Validate(thread);

var assistant = client.CreateAssistant("gpt-4o-mini");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Switch to the async version of this method: CreateAssistantAsync
  2. Then call Validate(assistant);


var message = await client.CreateMessageAsync(
thread.Value.Id,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Add indentation.

MessageRole.User,
new[] {
MessageContent.FromText("What is this file?"),
},
new MessageCreationOptions()
{
Attachments = [
new MessageCreationAttachment(testFile.Id, new List<ToolDefinition>() { ToolDefinition.CreateFileSearch() }),
new MessageCreationAttachment(testFile.Id, new List<ToolDefinition>() { ToolDefinition.CreateCodeInterpreter() })
]
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call Validate(message);


var result = client.CreateRunStreamingAsync(thread.Value.Id, assistant.Value.Id);
}

[Test]
public async Task FileSearchStreamingWorks()
{
Expand Down