Skip to content

Commit 5aaabae

Browse files
committed
Update sample codes for images/vision
1 parent a4511d8 commit 5aaabae

File tree

4 files changed

+12
-53
lines changed

4 files changed

+12
-53
lines changed

docs/guides/images-vision/responses/analyze_images_passing_base64_string.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
1212
OpenAIResponseClient client = new(model: "gpt-5", apiKey: key);
1313

14-
using var http = new HttpClient();
14+
Uri imageUrl = new("https://openai-documentation.vercel.app/images/cat_and_otter.png");
15+
using HttpClient http = new();
1516

1617
// Download an image as stream
17-
using var stream = await http.GetStreamAsync("https://openai-documentation.vercel.app/images/cat_and_otter.png");
18+
using var stream = await http.GetStreamAsync(imageUrl);
1819

1920
OpenAIResponse response1 = (OpenAIResponse)client.CreateResponse([
2021
ResponseItem.CreateUserMessageItem([
@@ -26,7 +27,7 @@
2627
Console.WriteLine($"From image stream: {response1.GetOutputText()}");
2728

2829
// Download an image as byte array
29-
byte[] bytes = await http.GetByteArrayAsync("https://openai-documentation.vercel.app/images/cat_and_otter.png");
30+
byte[] bytes = await http.GetByteArrayAsync(imageUrl);
3031

3132
OpenAIResponse response2 = (OpenAIResponse)client.CreateResponse([
3233
ResponseItem.CreateUserMessageItem([
@@ -36,15 +37,3 @@
3637
]);
3738

3839
Console.WriteLine($"From byte array: {response2.GetOutputText()}");
39-
40-
// Convert the byte array to a base64 string
41-
string base64 = Convert.ToBase64String(bytes);
42-
43-
OpenAIResponse response3 = (OpenAIResponse)client.CreateResponse([
44-
ResponseItem.CreateUserMessageItem([
45-
ResponseContentPart.CreateInputTextPart("What is in this image?"),
46-
ResponseContentPart.CreateInputImagePart(BinaryData.FromString(base64), "image/png")
47-
])
48-
]);
49-
50-
Console.WriteLine($"From base64 string: {response3.GetOutputText()}");

docs/guides/images-vision/responses/analyze_images_passing_file.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@
1212
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
1313
OpenAIResponseClient client = new(model: "gpt-5", apiKey: key);
1414

15+
string filename = "cat_and_otter.png";
16+
Uri imageUrl = new($"https://openai-documentation.vercel.app/images/{filename}");
1517
using var http = new HttpClient();
1618

1719
// Download an image as stream
18-
using var stream = await http.GetStreamAsync("https://openai-documentation.vercel.app/images/cat_and_otter.png");
20+
using var stream = await http.GetStreamAsync(imageUrl);
1921

2022
OpenAIFileClient files = new(key);
21-
OpenAIFile file = await files.UploadFileAsync(BinaryData.FromStream(stream), "cat_and_otter.png", FileUploadPurpose.Vision);
23+
OpenAIFile file = await files.UploadFileAsync(BinaryData.FromStream(stream), filename, FileUploadPurpose.Vision);
2224

2325
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
2426
ResponseItem.CreateUserMessageItem([
25-
ResponseContentPart.CreateInputFilePart(file.Id),
27+
ResponseContentPart.CreateInputImagePart(file.Id),
2628
ResponseContentPart.CreateInputTextPart("what's in this image?")
2729
])
2830
]);

docs/guides/images-vision/responses/analyze_images_passing_url.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
1212
OpenAIResponseClient client = new(model: "gpt-5", apiKey: key);
1313

14+
Uri imageUrl = new("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg");
15+
1416
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
1517
ResponseItem.CreateUserMessageItem([
1618
ResponseContentPart.CreateInputTextPart("What is in this image?"),
17-
ResponseContentPart.CreateInputImagePart(new Uri("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"))
19+
ResponseContentPart.CreateInputImagePart(imageUrl)
1820
])
1921
]);
2022

docs/guides/images-vision/responses/generate-edit-images.cs

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)