Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ public string Uri
[JsonIgnore]
public string MediaType { get; }

/// <summary>Gets or sets an optional name associated with the data.</summary>
/// <remarks>
/// A service might use this name as part of citations or to help infer the type of data
/// being represented based on a file extension.
/// </remarks>
public string? Name { get; set; }

/// <summary>Gets the data represented by this instance.</summary>
/// <remarks>
/// If the instance was constructed from a <see cref="ReadOnlyMemory{Byte}"/>, this property returns that data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,10 @@
"Member": "System.ReadOnlyMemory<byte> Microsoft.Extensions.AI.DataContent.Data { get; }",
"Stage": "Stable"
},
{
"Member": "string? Microsoft.Extensions.AI.DataContent.Name { get; set; }",
"Stage": "Stable"
},
{
"Member": "string Microsoft.Extensions.AI.DataContent.MediaType { get; }",
"Stage": "Stable"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ private static List<ChatMessageContentPart> ToOpenAIChatContent(IList<AIContent>
break;

case DataContent dataContent when dataContent.MediaType.StartsWith("application/pdf", StringComparison.OrdinalIgnoreCase):
return ChatMessageContentPart.CreateFilePart(BinaryData.FromBytes(dataContent.Data), dataContent.MediaType, $"{Guid.NewGuid():N}.pdf");
return ChatMessageContentPart.CreateFilePart(BinaryData.FromBytes(dataContent.Data), dataContent.MediaType, dataContent.Name ?? $"{Guid.NewGuid():N}.pdf");

case AIContent when content.RawRepresentation is ChatMessageContentPart rawContentPart:
return rawContentPart;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ private static List<ResponseContentPart> ToOpenAIResponsesContent(IList<AIConten
break;

case DataContent dataContent when dataContent.MediaType.StartsWith("application/pdf", StringComparison.OrdinalIgnoreCase):
parts.Add(ResponseContentPart.CreateInputFilePart(BinaryData.FromBytes(dataContent.Data), dataContent.MediaType, $"{Guid.NewGuid():N}.pdf"));
parts.Add(ResponseContentPart.CreateInputFilePart(BinaryData.FromBytes(dataContent.Data), dataContent.MediaType, dataContent.Name ?? $"{Guid.NewGuid():N}.pdf"));
break;

case ErrorContent errorContent when errorContent.ErrorCode == nameof(ResponseContentPartKind.Refusal):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,20 @@ public void Serialize_MatchesExpectedJson()
{
Assert.Equal(
"""{"uri":"data:application/octet-stream;base64,AQIDBA=="}""",
JsonSerializer.Serialize(new DataContent(
uri: "data:application/octet-stream;base64,AQIDBA=="), TestJsonSerializerContext.Default.Options));
JsonSerializer.Serialize(
new DataContent(uri: "data:application/octet-stream;base64,AQIDBA=="),
TestJsonSerializerContext.Default.Options));

Assert.Equal(
"""{"uri":"data:application/octet-stream;base64,AQIDBA=="}""",
JsonSerializer.Serialize(new DataContent(
new ReadOnlyMemory<byte>([0x01, 0x02, 0x03, 0x04]), "application/octet-stream"),
JsonSerializer.Serialize(
new DataContent(new ReadOnlyMemory<byte>([0x01, 0x02, 0x03, 0x04]), "application/octet-stream"),
TestJsonSerializerContext.Default.Options));

Assert.Equal(
"""{"uri":"data:application/octet-stream;base64,AQIDBA==","name":"test.bin"}""",
JsonSerializer.Serialize(
new DataContent(new ReadOnlyMemory<byte>([0x01, 0x02, 0x03, 0x04]), "application/octet-stream") { Name = "test.bin" },
TestJsonSerializerContext.Default.Options));
}

Expand Down Expand Up @@ -260,4 +267,13 @@ public void NonBase64Data_Normalized()
Assert.Equal("aGVsbG8gd29ybGQ=", content.Base64Data.ToString());
Assert.Equal("hello world", Encoding.ASCII.GetString(content.Data.ToArray()));
}

[Fact]
public void FileName_Roundtrips()
{
DataContent content = new(new byte[] { 1, 2, 3 }, "application/octet-stream");
Assert.Null(content.Name);
content.Name = "test.bin";
Assert.Equal("test.bin", content.Name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public virtual async Task MultiModal_DescribePdf()
new(ChatRole.User,
[
new TextContent("What text does this document contain?"),
new DataContent(ImageDataUri.GetPdfDataUri(), "application/pdf"),
new DataContent(ImageDataUri.GetPdfDataUri(), "application/pdf") { Name = "sample.pdf" },
])
],
new() { ModelId = GetModel_MultiModal_DescribeImage() });
Expand Down
Loading