openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
docs/quickstart/responses/analyze_images_files_file_url.cs
26lines · modecode
| 1 | // SAMPLE: Analyzes file from a file URL through Responses API |
| 2 | // PAGE: https://platform.openai.com/docs/quickstart?input-type=file-url#analyze-images-and-files |
| 3 | // GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start |
| 4 | #pragma warning disable OPENAI001 |
| 5 | |
| 6 | #:package OpenAI@2.* |
| 7 | |
| 8 | using OpenAI.Files; |
| 9 | using OpenAI.Responses; |
| 10 | |
| 11 | string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!; |
| 12 | ResponsesClient client = new(apiKey: key); |
| 13 | |
| 14 | using HttpClient http = new(); |
| 15 | using Stream stream = await http.GetStreamAsync("https://www.berkshirehathaway.com/letters/2024ltr.pdf"); |
| 16 | OpenAIFileClient files = new(key); |
| 17 | OpenAIFile file = files.UploadFile(stream, "2024ltr.pdf", FileUploadPurpose.UserData); |
| 18 | |
| 19 | ResponseResult response = client.CreateResponse("gpt-5.2", [ |
| 20 | ResponseItem.CreateUserMessageItem([ |
| 21 | ResponseContentPart.CreateInputTextPart("Analyze the letter and provide a summary of the key points."), |
| 22 | ResponseContentPart.CreateInputFilePart(file.Id) |
| 23 | ]) |
| 24 | ]); |
| 25 | |
| 26 | Console.WriteLine(response.GetOutputText()); |