|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// [START googlegenaisdk_textgen_with_pdf] |
| 18 | + |
| 19 | +using Google.GenAI; |
| 20 | +using Google.GenAI.Types; |
| 21 | +using System; |
| 22 | +using System.Collections.Generic; |
| 23 | +using System.Threading.Tasks; |
| 24 | + |
| 25 | +public class TextGenWithPdf |
| 26 | +{ |
| 27 | + public async Task<string> GenerateContent( |
| 28 | + string projectId = "your-project-id", |
| 29 | + string location = "global", |
| 30 | + string model = "gemini-2.5-flash") |
| 31 | + { |
| 32 | + await using var client = new Client( |
| 33 | + project: projectId, |
| 34 | + location: location, |
| 35 | + vertexAI: true, |
| 36 | + httpOptions: new HttpOptions { ApiVersion = "v1" }); |
| 37 | + |
| 38 | + string prompt = @" |
| 39 | + You are a highly skilled document summarization specialist. |
| 40 | + Your task is to provide a concise executive summary of no more than 300 words. |
| 41 | + Please summarize the given document for a general audience."; |
| 42 | + |
| 43 | + var contents = new List<Content> |
| 44 | + { |
| 45 | + new Content |
| 46 | + { |
| 47 | + Role = "user", |
| 48 | + Parts = new List<Part> |
| 49 | + { |
| 50 | + new Part |
| 51 | + { |
| 52 | + FileData = new FileData |
| 53 | + { |
| 54 | + FileUri = "gs://cloud-samples-data/generative-ai/pdf/1706.03762v7.pdf", |
| 55 | + MimeType = "application/pdf", |
| 56 | + } |
| 57 | + }, |
| 58 | + new Part { Text = prompt } |
| 59 | + } |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + GenerateContentResponse response = await client.Models.GenerateContentAsync(model: model, contents: contents); |
| 64 | + |
| 65 | + string responseText = response.Candidates[0].Content.Parts[0].Text; |
| 66 | + Console.WriteLine(responseText); |
| 67 | + // Example reponse: |
| 68 | + // This paper introduces the Transformer, a novel neural network architecture designed |
| 69 | + // for processing sequences, such as those found in language translation. Traditionally, |
| 70 | + // such tasks have relied on complex recurrent or convolutional neural networks... |
| 71 | + return responseText; |
| 72 | + } |
| 73 | +} |
| 74 | +// [END googlegenaisdk_textgen_with_pdf] |
0 commit comments