diff --git a/docs/guides/images-vision/responses/analyze_images_passing_base64_string.cs b/docs/guides/images-vision/responses/analyze_images_passing_base64_string.cs new file mode 100644 index 000000000..1fd56f963 --- /dev/null +++ b/docs/guides/images-vision/responses/analyze_images_passing_base64_string.cs @@ -0,0 +1,39 @@ +// SAMPLE: Analyzes image by passing a base64-encoded image through Responses API +// PAGE: https://platform.openai.com/docs/guides/images-vision?api-mode=responses&format=base64-encoded#analyze-images +// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start +#pragma warning disable OPENAI001 + +#:package OpenAI@2.* +#:property PublishAot=false + +using OpenAI.Responses; + +string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!; +OpenAIResponseClient client = new(model: "gpt-5", apiKey: key); + +Uri imageUrl = new("https://openai-documentation.vercel.app/images/cat_and_otter.png"); +using HttpClient http = new(); + +// Download an image as stream +using var stream = await http.GetStreamAsync(imageUrl); + +OpenAIResponse response1 = (OpenAIResponse)client.CreateResponse([ + ResponseItem.CreateUserMessageItem([ + ResponseContentPart.CreateInputTextPart("What is in this image?"), + ResponseContentPart.CreateInputImagePart(BinaryData.FromStream(stream), "image/png") + ]) +]); + +Console.WriteLine($"From image stream: {response1.GetOutputText()}"); + +// Download an image as byte array +byte[] bytes = await http.GetByteArrayAsync(imageUrl); + +OpenAIResponse response2 = (OpenAIResponse)client.CreateResponse([ + ResponseItem.CreateUserMessageItem([ + ResponseContentPart.CreateInputTextPart("What is in this image?"), + ResponseContentPart.CreateInputImagePart(BinaryData.FromBytes(bytes), "image/png") + ]) +]); + +Console.WriteLine($"From byte array: {response2.GetOutputText()}"); diff --git a/docs/guides/images-vision/responses/analyze_images_passing_file.cs b/docs/guides/images-vision/responses/analyze_images_passing_file.cs new file mode 100644 index 000000000..0cb3fd622 --- /dev/null +++ b/docs/guides/images-vision/responses/analyze_images_passing_file.cs @@ -0,0 +1,32 @@ +// SAMPLE: Analyzes file from a file upload through Responses API +// PAGE: https://platform.openai.com/docs/guides/images-vision?api-mode=responses&format=file#analyze-images +// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start +#pragma warning disable OPENAI001 + +#:package OpenAI@2.* +#:property PublishAot=false + +using OpenAI.Files; +using OpenAI.Responses; + +string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!; +OpenAIResponseClient client = new(model: "gpt-5", apiKey: key); + +string filename = "cat_and_otter.png"; +Uri imageUrl = new($"https://openai-documentation.vercel.app/images/{filename}"); +using var http = new HttpClient(); + +// Download an image as stream +using var stream = await http.GetStreamAsync(imageUrl); + +OpenAIFileClient files = new(key); +OpenAIFile file = await files.UploadFileAsync(BinaryData.FromStream(stream), filename, FileUploadPurpose.Vision); + +OpenAIResponse response = (OpenAIResponse)client.CreateResponse([ + ResponseItem.CreateUserMessageItem([ + ResponseContentPart.CreateInputTextPart("what's in this image?"), + ResponseContentPart.CreateInputImagePart(file.Id) + ]) +]); + +Console.WriteLine(response.GetOutputText()); \ No newline at end of file diff --git a/docs/guides/images-vision/responses/analyze_images_passing_url.cs b/docs/guides/images-vision/responses/analyze_images_passing_url.cs new file mode 100644 index 000000000..003da272d --- /dev/null +++ b/docs/guides/images-vision/responses/analyze_images_passing_url.cs @@ -0,0 +1,23 @@ +// SAMPLE: Analyzes image by passing an image URL through Responses API +// PAGE: https://platform.openai.com/docs/guides/images-vision?api-mode=responses&format=url#analyze-images +// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start +#pragma warning disable OPENAI001 + +#:package OpenAI@2.* +#:property PublishAot=false + +using OpenAI.Responses; + +string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!; +OpenAIResponseClient client = new(model: "gpt-5", apiKey: key); + +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"); + +OpenAIResponse response = (OpenAIResponse)client.CreateResponse([ + ResponseItem.CreateUserMessageItem([ + ResponseContentPart.CreateInputTextPart("What is in this image?"), + ResponseContentPart.CreateInputImagePart(imageUrl) + ]) +]); + +Console.WriteLine(response.GetOutputText()); \ No newline at end of file