Skip to content

Commit 77eb55f

Browse files
authored
Add code samples for the "images and vision" page (#701)
* Add image generation code sample * Add code samples for the "images and vision" page * Update sample codes for images/vision * Change the order of prompts to make all the samples consistent
1 parent d456cbe commit 77eb55f

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SAMPLE: Analyzes image by passing a base64-encoded image through Responses API
2+
// PAGE: https://platform.openai.com/docs/guides/images-vision?api-mode=responses&format=base64-encoded#analyze-images
3+
// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start
4+
#pragma warning disable OPENAI001
5+
6+
#:package OpenAI@2.*
7+
#:property PublishAot=false
8+
9+
using OpenAI.Responses;
10+
11+
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
12+
OpenAIResponseClient client = new(model: "gpt-5", apiKey: key);
13+
14+
Uri imageUrl = new("https://openai-documentation.vercel.app/images/cat_and_otter.png");
15+
using HttpClient http = new();
16+
17+
// Download an image as stream
18+
using var stream = await http.GetStreamAsync(imageUrl);
19+
20+
OpenAIResponse response1 = (OpenAIResponse)client.CreateResponse([
21+
ResponseItem.CreateUserMessageItem([
22+
ResponseContentPart.CreateInputTextPart("What is in this image?"),
23+
ResponseContentPart.CreateInputImagePart(BinaryData.FromStream(stream), "image/png")
24+
])
25+
]);
26+
27+
Console.WriteLine($"From image stream: {response1.GetOutputText()}");
28+
29+
// Download an image as byte array
30+
byte[] bytes = await http.GetByteArrayAsync(imageUrl);
31+
32+
OpenAIResponse response2 = (OpenAIResponse)client.CreateResponse([
33+
ResponseItem.CreateUserMessageItem([
34+
ResponseContentPart.CreateInputTextPart("What is in this image?"),
35+
ResponseContentPart.CreateInputImagePart(BinaryData.FromBytes(bytes), "image/png")
36+
])
37+
]);
38+
39+
Console.WriteLine($"From byte array: {response2.GetOutputText()}");
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SAMPLE: Analyzes file from a file upload through Responses API
2+
// PAGE: https://platform.openai.com/docs/guides/images-vision?api-mode=responses&format=file#analyze-images
3+
// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start
4+
#pragma warning disable OPENAI001
5+
6+
#:package OpenAI@2.*
7+
#:property PublishAot=false
8+
9+
using OpenAI.Files;
10+
using OpenAI.Responses;
11+
12+
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
13+
OpenAIResponseClient client = new(model: "gpt-5", apiKey: key);
14+
15+
string filename = "cat_and_otter.png";
16+
Uri imageUrl = new($"https://openai-documentation.vercel.app/images/{filename}");
17+
using var http = new HttpClient();
18+
19+
// Download an image as stream
20+
using var stream = await http.GetStreamAsync(imageUrl);
21+
22+
OpenAIFileClient files = new(key);
23+
OpenAIFile file = await files.UploadFileAsync(BinaryData.FromStream(stream), filename, FileUploadPurpose.Vision);
24+
25+
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
26+
ResponseItem.CreateUserMessageItem([
27+
ResponseContentPart.CreateInputTextPart("what's in this image?"),
28+
ResponseContentPart.CreateInputImagePart(file.Id)
29+
])
30+
]);
31+
32+
Console.WriteLine(response.GetOutputText());
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// SAMPLE: Analyzes image by passing an image URL through Responses API
2+
// PAGE: https://platform.openai.com/docs/guides/images-vision?api-mode=responses&format=url#analyze-images
3+
// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start
4+
#pragma warning disable OPENAI001
5+
6+
#:package OpenAI@2.*
7+
#:property PublishAot=false
8+
9+
using OpenAI.Responses;
10+
11+
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
12+
OpenAIResponseClient client = new(model: "gpt-5", apiKey: key);
13+
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+
16+
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
17+
ResponseItem.CreateUserMessageItem([
18+
ResponseContentPart.CreateInputTextPart("What is in this image?"),
19+
ResponseContentPart.CreateInputImagePart(imageUrl)
20+
])
21+
]);
22+
23+
Console.WriteLine(response.GetOutputText());

0 commit comments

Comments
 (0)