Generate text using Generative AI Model

This code sample demonstrates how to use Generative AI model to generate content for a given text input.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

C#

Before trying this sample, follow the C# setup instructions in the Vertex AI quickstart using client libraries . For more information, see the Vertex AI C# API reference documentation .

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  using 
  
 Google.GenAI 
 ; 
 using 
  
 Google.GenAI.Types 
 ; 
 using 
  
 System 
 ; 
 using 
  
 System.Threading.Tasks 
 ; 
 public 
  
 class 
  
 TextGenWithTxt 
 { 
  
 public 
  
 async 
  
 Task<string> 
  
 GenerateContent 
 ( 
  
 string 
  
 projectId 
  
 = 
  
 "your-project-id" 
 , 
  
 string 
  
 location 
  
 = 
  
 "global" 
 , 
  
 string 
  
 model 
  
 = 
  
 "gemini-2.5-flash" 
 ) 
  
 { 
  
 await 
  
 using 
  
 var 
  
 client 
  
 = 
  
 new 
  
 Client 
 ( 
  
 project 
 : 
  
 projectId 
 , 
  
 location 
 : 
  
 location 
 , 
  
 vertexAI 
 : 
  
 true 
 , 
  
 httpOptions 
 : 
  
 new 
  
 HttpOptions 
  
 { 
  
 ApiVersion 
  
 = 
  
 "v1" 
  
 }); 
  
 GenerateContentResponse 
  
 response 
  
 = 
  
 await 
  
 client 
 . 
 Models 
 . 
 GenerateContentAsync 
 ( 
 model 
 : 
  
 model 
 , 
  
 contents 
 : 
  
 "How does AI work?" 
 ); 
  
 string 
  
 responseText 
  
 = 
  
 response 
 . 
 Candidates 
 [ 
 0 
 ]. 
 Content 
 . 
 Parts 
 [ 
 0 
 ]. 
 Text 
 ; 
  
 Console 
 . 
 WriteLine 
 ( 
 responseText 
 ); 
  
 // Example reponse: 
  
 // AI, or Artificial Intelligence, at its core, is about creating machines that can perform... 
  
 // Here's a breakdown of how it generally works... 
  
 return 
  
 responseText 
 ; 
  
 } 
 } 
 

Go

Before trying this sample, follow the Go setup instructions in the Vertex AI quickstart using client libraries . For more information, see the Vertex AI Go API reference documentation .

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  import 
  
 ( 
  
 "context" 
  
 "fmt" 
  
 "io" 
  
 "google.golang.org/genai" 
 ) 
 // generateWithText shows how to generate text using a text prompt. 
 func 
  
 generateWithText 
 ( 
 w 
  
 io 
 . 
 Writer 
 ) 
  
 error 
  
 { 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 genai 
 . 
 NewClient 
 ( 
 ctx 
 , 
  
& genai 
 . 
 ClientConfig 
 { 
  
 HTTPOptions 
 : 
  
 genai 
 . 
 HTTPOptions 
 { 
 APIVersion 
 : 
  
 "v1" 
 }, 
  
 }) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "failed to create genai client: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 resp 
 , 
  
 err 
  
 := 
  
 client 
 . 
 Models 
 . 
 GenerateContent 
 ( 
 ctx 
 , 
  
 "gemini-2.5-flash" 
 , 
  
 genai 
 . 
 Text 
 ( 
 "How does AI work?" 
 ), 
  
 nil 
 , 
  
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "failed to generate content: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 respText 
  
 := 
  
 resp 
 . 
 Text 
 () 
  
 fmt 
 . 
 Fprintln 
 ( 
 w 
 , 
  
 respText 
 ) 
  
 // Example response: 
  
 // That's a great question! Understanding how AI works can feel like ... 
  
 // ... 
  
 // **1. The Foundation: Data and Algorithms** 
  
 // ... 
  
 return 
  
 nil 
 } 
 

Java

Before trying this sample, follow the Java setup instructions in the Vertex AI quickstart using client libraries . For more information, see the Vertex AI Java API reference documentation .

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  import 
  
 com.google.genai.Client 
 ; 
 import 
  
 com.google.genai.types.GenerateContentResponse 
 ; 
 import 
  
 com.google.genai.types.HttpOptions 
 ; 
 public 
  
 class 
 TextGenerationWithText 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 String 
  
 modelId 
  
 = 
  
 "gemini-2.5-flash" 
 ; 
  
 generateContent 
 ( 
 modelId 
 ); 
  
 } 
  
 // Generates text with text input 
  
 public 
  
 static 
  
 String 
  
 generateContent 
 ( 
 String 
  
 modelId 
 ) 
  
 { 
  
 // Initialize client that will be used to send requests. This client only needs to be created 
  
 // once, and can be reused for multiple requests. 
  
 try 
  
 ( 
 Client 
  
 client 
  
 = 
  
 Client 
 . 
 builder 
 () 
  
 . 
 location 
 ( 
 "global" 
 ) 
  
 . 
 vertexAI 
 ( 
 true 
 ) 
  
 . 
 httpOptions 
 ( 
 HttpOptions 
 . 
 builder 
 (). 
 apiVersion 
 ( 
 "v1" 
 ). 
 build 
 ()) 
  
 . 
 build 
 ()) 
  
 { 
  
 GenerateContentResponse 
  
 response 
  
 = 
  
 client 
 . 
 models 
 . 
 generateContent 
 ( 
 modelId 
 , 
  
 "How does AI work?" 
 , 
  
 null 
 ); 
  
 System 
 . 
 out 
 . 
 print 
 ( 
 response 
 . 
 text 
 ()); 
  
 // Example response: 
  
 // Okay, let's break down how AI works. It's a broad field, so I'll focus on the ... 
  
 // 
  
 // Here's a simplified overview: 
  
 // ... 
  
 return 
  
 response 
 . 
 text 
 (); 
  
 } 
  
 } 
 } 
 

Node.js

Before trying this sample, follow the Node.js setup instructions in the Vertex AI quickstart using client libraries . For more information, see the Vertex AI Node.js API reference documentation .

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  const 
  
 { 
 GoogleGenAI 
 } 
  
 = 
  
 require 
 ( 
 '@google/genai' 
 ); 
 const 
  
 GOOGLE_CLOUD_PROJECT 
  
 = 
  
 process 
 . 
 env 
 . 
 GOOGLE_CLOUD_PROJECT 
 ; 
 const 
  
 GOOGLE_CLOUD_LOCATION 
  
 = 
  
 process 
 . 
 env 
 . 
 GOOGLE_CLOUD_LOCATION 
  
 || 
  
 'global' 
 ; 
 async 
  
 function 
  
 generateContent 
 ( 
  
 projectId 
  
 = 
  
 GOOGLE_CLOUD_PROJECT 
 , 
  
 location 
  
 = 
  
 GOOGLE_CLOUD_LOCATION 
 ) 
  
 { 
  
 const 
  
 client 
  
 = 
  
 new 
  
 GoogleGenAI 
 ({ 
  
 vertexai 
 : 
  
 true 
 , 
  
 project 
 : 
  
 projectId 
 , 
  
 location 
 : 
  
 location 
 , 
  
 }); 
  
 const 
  
 response 
  
 = 
  
 await 
  
 client 
 . 
 models 
 . 
 generateContent 
 ({ 
  
 model 
 : 
  
 'gemini-2.5-flash' 
 , 
  
 contents 
 : 
  
 'How does AI work?' 
 , 
  
 }); 
  
 console 
 . 
 log 
 ( 
 response 
 . 
 text 
 ); 
  
 return 
  
 response 
 . 
 text 
 ; 
 } 
 

Python

Before trying this sample, follow the Python setup instructions in the Vertex AI quickstart using client libraries . For more information, see the Vertex AI Python API reference documentation .

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  from 
  
 google 
  
 import 
 genai 
 from 
  
 google.genai.types 
  
 import 
 HttpOptions 
 client 
 = 
 genai 
 . 
 Client 
 ( 
 http_options 
 = 
 HttpOptions 
 ( 
 api_version 
 = 
 "v1" 
 )) 
 response 
 = 
 client 
 . 
 models 
 . 
 generate_content 
 ( 
 model 
 = 
 "gemini-2.5-flash" 
 , 
 contents 
 = 
 "How does AI work?" 
 , 
 ) 
 print 
 ( 
 response 
 . 
 text 
 ) 
 # Example response: 
 # Okay, let's break down how AI works. It's a broad field, so I'll focus on the ... 
 # 
 # Here's a simplified overview: 
 # ... 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

Design a Mobile Site
View Site in Mobile | Classic
Share by: