Skip to content

Commit e4edc8e

Browse files
committed
Update ImageGenerationToolInputImageMask
1 parent 8ded201 commit e4edc8e

File tree

5 files changed

+165
-13
lines changed

5 files changed

+165
-13
lines changed

api/OpenAI.net8.0.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5048,8 +5048,11 @@ public class ImageGenerationTool : ResponseTool, IJsonModel<ImageGenerationTool>
50485048
}
50495049
[Experimental("OPENAI001")]
50505050
public class ImageGenerationToolInputImageMask : IJsonModel<ImageGenerationToolInputImageMask>, IPersistableModel<ImageGenerationToolInputImageMask> {
5051-
public string FileId { get; set; }
5052-
public string ImageUrl { get; set; }
5051+
public ImageGenerationToolInputImageMask(BinaryData imageBytes, string imageBytesMediaType);
5052+
public ImageGenerationToolInputImageMask(string fileId);
5053+
public ImageGenerationToolInputImageMask(Uri imageUri);
5054+
public string FileId { get; }
5055+
public string ImageUrl { get; }
50535056
protected virtual ImageGenerationToolInputImageMask JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options);
50545057
protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
50555058
protected virtual ImageGenerationToolInputImageMask PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options);

api/OpenAI.netstandard2.0.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4441,8 +4441,11 @@ public class ImageGenerationTool : ResponseTool, IJsonModel<ImageGenerationTool>
44414441
public override readonly string ToString();
44424442
}
44434443
public class ImageGenerationToolInputImageMask : IJsonModel<ImageGenerationToolInputImageMask>, IPersistableModel<ImageGenerationToolInputImageMask> {
4444-
public string FileId { get; set; }
4445-
public string ImageUrl { get; set; }
4444+
public ImageGenerationToolInputImageMask(BinaryData imageBytes, string imageBytesMediaType);
4445+
public ImageGenerationToolInputImageMask(string fileId);
4446+
public ImageGenerationToolInputImageMask(Uri imageUri);
4447+
public string FileId { get; }
4448+
public string ImageUrl { get; }
44464449
protected virtual ImageGenerationToolInputImageMask JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options);
44474450
protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
44484451
protected virtual ImageGenerationToolInputImageMask PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options);
Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,41 @@
1-
namespace OpenAI.Responses;
1+
using System;
2+
3+
namespace OpenAI.Responses;
24

35
// CUSTOM: Renamed.
46
[CodeGenType("ImageGenToolInputImageMask")]
57
public partial class ImageGenerationToolInputImageMask
68
{
9+
internal ImageGenerationToolInputImageMask() { }
10+
11+
public ImageGenerationToolInputImageMask(string fileId)
12+
{
13+
Argument.AssertNotNullOrEmpty(fileId, nameof(fileId));
14+
15+
FileId = fileId;
16+
}
17+
18+
public ImageGenerationToolInputImageMask(BinaryData imageBytes, string imageBytesMediaType)
19+
{
20+
Argument.AssertNotNull(imageBytes, nameof(imageBytes));
21+
Argument.AssertNotNullOrEmpty(imageBytesMediaType, nameof(imageBytesMediaType));
22+
23+
string base64EncodedData = Convert.ToBase64String(imageBytes.ToArray());
24+
ImageUrl = $"data:{imageBytesMediaType};base64,{base64EncodedData}";
25+
}
26+
27+
public ImageGenerationToolInputImageMask(Uri imageUri)
28+
{
29+
Argument.AssertNotNull(imageUri, nameof(imageUri));
30+
31+
ImageUrl = imageUri?.AbsoluteUri;
32+
}
33+
34+
// CUSTOM: Removed setter.
35+
[CodeGenMember("ImageUrl")]
36+
public string ImageUrl { get; }
37+
38+
// CUSTOM: Removed setter.
39+
[CodeGenMember("FileId")]
40+
public string FileId { get; }
741
}

src/Generated/Models/Responses/ImageGenerationToolInputImageMask.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,13 @@ public partial class ImageGenerationToolInputImageMask
1313
{
1414
private protected IDictionary<string, BinaryData> _additionalBinaryDataProperties;
1515

16-
public ImageGenerationToolInputImageMask()
17-
{
18-
}
19-
2016
internal ImageGenerationToolInputImageMask(string imageUrl, string fileId, IDictionary<string, BinaryData> additionalBinaryDataProperties)
2117
{
2218
ImageUrl = imageUrl;
2319
FileId = fileId;
2420
_additionalBinaryDataProperties = additionalBinaryDataProperties;
2521
}
2622

27-
public string ImageUrl { get; set; }
28-
29-
public string FileId { get; set; }
30-
3123
internal IDictionary<string, BinaryData> SerializedAdditionalRawData
3224
{
3325
get => _additionalBinaryDataProperties;

tests/Responses/ResponsesTests.cs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,126 @@ in client.CreateResponseStreamingAsync(message, responseOptions))
340340
Assert.That(imageGenItemId, Is.Not.Null.And.Not.Empty);
341341
}
342342

343+
[Test]
344+
public async Task ImageGenToolInputMaskWithImageBytes()
345+
{
346+
OpenAIResponseClient client = GetTestClient();
347+
348+
string imageFilename = "images_dog_and_cat.png";
349+
string imagePath = Path.Combine("Assets", imageFilename);
350+
ResponseCreationOptions options = new()
351+
{
352+
Tools =
353+
{
354+
ResponseTool.CreateImageGenerationTool(
355+
model: "gpt-image-1",
356+
outputFileFormat: ImageGenerationToolOutputFileFormat.Png,
357+
inputImageMask: new(BinaryData.FromBytes(File.ReadAllBytes(imagePath)), "image/png"))
358+
}
359+
};
360+
361+
OpenAIResponse response = await client.CreateResponseAsync(
362+
"Generate an image of gray tabby cat hugging an otter with an orange scarf",
363+
options);
364+
365+
Assert.That(response.OutputItems, Has.Count.EqualTo(2));
366+
Assert.That(response.OutputItems[0], Is.InstanceOf<ImageGenerationCallResponseItem>());
367+
Assert.That(response.OutputItems[1], Is.InstanceOf<MessageResponseItem>());
368+
369+
MessageResponseItem message = (MessageResponseItem)response.OutputItems[1];
370+
Assert.That(message.Content, Has.Count.GreaterThan(0));
371+
Assert.That(message.Content[0].Kind, Is.EqualTo(ResponseContentPartKind.OutputText));
372+
373+
Assert.That(response.Tools.FirstOrDefault(), Is.TypeOf<ImageGenerationTool>());
374+
375+
ImageGenerationCallResponseItem imageGenResponse = (ImageGenerationCallResponseItem)response.OutputItems[0];
376+
Assert.AreEqual(imageGenResponse.Status, ImageGenerationCallStatus.Completed);
377+
Assert.That(imageGenResponse.GeneratedImageBytes.ToArray(), Is.Not.Null.And.Not.Empty);
378+
}
379+
380+
[Test]
381+
public async Task ImageGenToolInputMaskWithImageUri()
382+
{
383+
OpenAIResponseClient client = GetTestClient();
384+
385+
ResponseCreationOptions options = new()
386+
{
387+
Tools =
388+
{
389+
ResponseTool.CreateImageGenerationTool(
390+
model: "gpt-image-1",
391+
outputFileFormat: ImageGenerationToolOutputFileFormat.Png,
392+
inputImageMask: new(imageUri: new Uri("https://upload.wikimedia.org/wikipedia/commons/c/c3/Openai.png")))
393+
}
394+
};
395+
396+
OpenAIResponse response = await client.CreateResponseAsync(
397+
"Generate an image of gray tabby cat hugging an otter with an orange scarf",
398+
options);
399+
400+
Assert.That(response.OutputItems, Has.Count.EqualTo(2));
401+
Assert.That(response.OutputItems[0], Is.InstanceOf<ImageGenerationCallResponseItem>());
402+
Assert.That(response.OutputItems[1], Is.InstanceOf<MessageResponseItem>());
403+
404+
MessageResponseItem message = (MessageResponseItem)response.OutputItems[1];
405+
Assert.That(message.Content, Has.Count.GreaterThan(0));
406+
Assert.That(message.Content[0].Kind, Is.EqualTo(ResponseContentPartKind.OutputText));
407+
408+
Assert.That(response.Tools.FirstOrDefault(), Is.TypeOf<ImageGenerationTool>());
409+
410+
ImageGenerationCallResponseItem imageGenResponse = (ImageGenerationCallResponseItem)response.OutputItems[0];
411+
Assert.AreEqual(imageGenResponse.Status, ImageGenerationCallStatus.Completed);
412+
Assert.That(imageGenResponse.GeneratedImageBytes.ToArray(), Is.Not.Null.And.Not.Empty);
413+
}
414+
415+
[Test]
416+
public async Task ImageGenToolInputMaskWithFileId()
417+
{
418+
OpenAIResponseClient client = GetTestClient();
419+
420+
OpenAIFileClient fileClient = GetTestClient<OpenAIFileClient>(TestScenario.Files);
421+
422+
string imageFilename = "images_dog_and_cat.png";
423+
string imagePath = Path.Combine("Assets", imageFilename);
424+
using Stream image = File.OpenRead(imagePath);
425+
BinaryData imageData = BinaryData.FromStream(image);
426+
427+
OpenAIFile file = await fileClient.UploadFileAsync(
428+
imageData,
429+
imageFilename,
430+
FileUploadPurpose.UserData);
431+
Validate(file);
432+
433+
ResponseCreationOptions options = new()
434+
{
435+
Tools =
436+
{
437+
ResponseTool.CreateImageGenerationTool(
438+
model: "gpt-image-1",
439+
outputFileFormat: ImageGenerationToolOutputFileFormat.Png,
440+
inputImageMask: new(fileId: file.Id))
441+
}
442+
};
443+
444+
OpenAIResponse response = await client.CreateResponseAsync(
445+
"Generate an image of gray tabby cat hugging an otter with an orange scarf",
446+
options);
447+
448+
Assert.That(response.OutputItems, Has.Count.EqualTo(2));
449+
Assert.That(response.OutputItems[0], Is.InstanceOf<ImageGenerationCallResponseItem>());
450+
Assert.That(response.OutputItems[1], Is.InstanceOf<MessageResponseItem>());
451+
452+
MessageResponseItem message = (MessageResponseItem)response.OutputItems[1];
453+
Assert.That(message.Content, Has.Count.GreaterThan(0));
454+
Assert.That(message.Content[0].Kind, Is.EqualTo(ResponseContentPartKind.OutputText));
455+
456+
Assert.That(response.Tools.FirstOrDefault(), Is.TypeOf<ImageGenerationTool>());
457+
458+
ImageGenerationCallResponseItem imageGenResponse = (ImageGenerationCallResponseItem)response.OutputItems[0];
459+
Assert.AreEqual(imageGenResponse.Status, ImageGenerationCallStatus.Completed);
460+
Assert.That(imageGenResponse.GeneratedImageBytes.ToArray(), Is.Not.Null.And.Not.Empty);
461+
}
462+
343463
[Test]
344464
public async Task StreamingResponses()
345465
{

0 commit comments

Comments
 (0)