-
Notifications
You must be signed in to change notification settings - Fork 342
Add code samples for the "images and vision" page #701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9d7bef3
Add image generation code sample
justinyoo 71fff7a
Add code samples for the "images and vision" page
justinyoo a4511d8
Merge branch 'main' into docs/images-vision
justinyoo 5aaabae
Update sample codes for images/vision
justinyoo b7d0979
Change the order of prompts to make all the samples consistent
justinyoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
docs/guides/images-vision/responses/analyze_images_passing_base64_string.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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()}"); |
32 changes: 32 additions & 0 deletions
32
docs/guides/images-vision/responses/analyze_images_passing_file.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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.CreateInputImagePart(file.Id), | ||
| ResponseContentPart.CreateInputTextPart("what's in this image?") | ||
| ]) | ||
| ]); | ||
|
|
||
| Console.WriteLine(response.GetOutputText()); | ||
23 changes: 23 additions & 0 deletions
23
docs/guides/images-vision/responses/analyze_images_passing_url.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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()); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.