public final class GenerativeModel extends Object
Note: The instances of ChatSession returned by startChat() are
NOT thread-safe.
| Modifier and Type | Class and Description |
|---|---|
static class |
GenerativeModel.Builder
Builder class for
GenerativeModel. |
| Constructor and Description |
|---|
GenerativeModel(String modelName,
VertexAI vertexAi)
Constructs a GenerativeModel instance.
|
| Modifier and Type | Method and Description |
|---|---|
CountTokensResponse |
countTokens(Content content)
Counts tokens in a single content.
|
CountTokensResponse |
countTokens(List<Content> contents)
Counts tokens in a list of contents.
|
CountTokensResponse |
countTokens(String text)
Counts tokens in a text message.
|
GenerateContentResponse |
generateContent(Content content)
Generates content from this model given a single content.
|
GenerateContentResponse |
generateContent(List<Content> contents)
Generates content from this model given a list of contents.
|
GenerateContentResponse |
generateContent(String text)
Generates content from generative model given a text.
|
com.google.api.core.ApiFuture<GenerateContentResponse> |
generateContentAsync(Content content)
Asynchronously generates content from generative model given a single Content.
|
com.google.api.core.ApiFuture<GenerateContentResponse> |
generateContentAsync(List<Content> contents)
Asynchronously generates content from generative model given a list of contents.
|
com.google.api.core.ApiFuture<GenerateContentResponse> |
generateContentAsync(String text)
Asynchronously generates content from generative model given a text.
|
ResponseStream<GenerateContentResponse> |
generateContentStream(Content content)
Generates content with streaming support from generative model given a single Content.
|
ResponseStream<GenerateContentResponse> |
generateContentStream(List<Content> contents)
Generates content with streaming support from generative model given a list of contents.
|
ResponseStream<GenerateContentResponse> |
generateContentStream(String text)
Generates content with streaming support from generative model given a text.
|
GenerationConfig |
getGenerationConfig()
Returns the
GenerationConfig of this generative model. |
String |
getModelName()
Returns the model name of this generative model.
|
com.google.common.collect.ImmutableList<SafetySetting> |
getSafetySettings()
Returns a list of
SafetySetting of this generative model. |
Optional<Content> |
getSystemInstruction()
Returns the optional system instruction of this generative model.
|
Optional<ToolConfig> |
getToolConfig()
Returns the optional
ToolConfig of this generative model. |
com.google.common.collect.ImmutableList<Tool> |
getTools()
Returns a list of
Tool of this generative model. |
ChatSession |
startChat()
Returns a new
ChatSession instance that can be used to start a chat with this model. |
GenerativeModel |
withGenerationConfig(GenerationConfig generationConfig)
Creates a copy of the current model with updated GenerationConfig.
|
GenerativeModel |
withSafetySettings(List<SafetySetting> safetySettings)
Creates a copy of the current model with updated safetySettings.
|
GenerativeModel |
withSystemInstruction(Content systemInstruction)
Creates a copy of the current model with updated system instructions.
|
GenerativeModel |
withToolConfig(ToolConfig toolConfig)
Creates a copy of the current model with updated tool config.
|
GenerativeModel |
withTools(List<Tool> tools)
Creates a copy of the current model with updated tools.
|
public GenerativeModel(String modelName, VertexAI vertexAi)
modelName - the name of the generative model. Supported format: "gemini-pro",
"models/gemini-pro", "publishers/google/models/gemini-pro", where "gemini-pro" is the model
name. Valid model names can be found at
https://cloud.google.com/vertex-ai/docs/generative-ai/learn/models#gemini-modelsvertexAi - a VertexAI that contains the default configs
for the generative modelpublic GenerativeModel withGenerationConfig(GenerationConfig generationConfig)
generationConfig - a GenerationConfig that will be
used in the new model.GenerativeModel instance with the specified GenerationConfig.public GenerativeModel withSafetySettings(List<SafetySetting> safetySettings)
safetySettings - a list of SafetySetting that will
be used in the new model.GenerativeModel instance with the specified safetySettings.public GenerativeModel withTools(List<Tool> tools)
tools - a list of Tool that will be used in the new
model.GenerativeModel instance with the specified tools.public GenerativeModel withToolConfig(ToolConfig toolConfig)
toolConfig - a ToolConfig that will be used in the
new model.GenerativeModel instance with the specified tool config.public GenerativeModel withSystemInstruction(Content systemInstruction)
systemInstruction - a Content containing system
instructions.GenerativeModel instance with the specified tools.@BetaApi public CountTokensResponse countTokens(String text) throws IOException
text - a text message to count tokensCountTokensResponse instance that contains the
total tokens and total billable characters of the given list of contentsIOException - if an I/O error occurs while making the API call@BetaApi public CountTokensResponse countTokens(Content content) throws IOException
content - a Content to count tokensCountTokensResponse instance that contains the
total tokens and total billable characters of the given list of contentsIOException - if an I/O error occurs while making the API call@BetaApi public CountTokensResponse countTokens(List<Content> contents) throws IOException
contents - a list of Content to count tokensCountTokensResponse instance that contains the
total tokens and total billable characters of the given list of contentsIOException - if an I/O error occurs while making the API callpublic GenerateContentResponse generateContent(String text) throws IOException
text - a text message to send to the generative modelGenerateContentResponse instance that contains
response contents and other metadataIOException - if an I/O error occurs while making the API callpublic GenerateContentResponse generateContent(Content content) throws IOException
content - a Content to send to the generative modelGenerateContentResponse instance that contains
response contents and other metadataIOException - if an I/O error occurs while making the API callpublic GenerateContentResponse generateContent(List<Content> contents) throws IOException
contents - a list of Content to send to the
generative modelGenerateContentResponse instance that contains
response contents and other metadataIOException - if an I/O error occurs while making the API callpublic ResponseStream<GenerateContentResponse> generateContentStream(String text) throws IOException
text - a text message to send to the generative modelResponseStream that contains a streaming of GenerateContentResponseIOException - if an I/O error occurs while making the API callpublic ResponseStream<GenerateContentResponse> generateContentStream(Content content) throws IOException
content - a Content to send to the generative model.
The role of the content is "user".ResponseStream that contains a streaming of GenerateContentResponseIOException - if an I/O error occurs while making the API callpublic ResponseStream<GenerateContentResponse> generateContentStream(List<Content> contents) throws IOException
contents - a list of Content to send to the
generative modelResponseStream that contains a streaming of GenerateContentResponseIOException - if an I/O error occurs while making the API callpublic com.google.api.core.ApiFuture<GenerateContentResponse> generateContentAsync(String text) throws IOException
text - a text message to send to the generative modelApiFuture represents the response of an asynchronous
generateContent requestIOException - if an I/O error occurs while making the API callpublic com.google.api.core.ApiFuture<GenerateContentResponse> generateContentAsync(Content content) throws IOException
content - a Content to send to the generative model.
The role of the content is "user".ApiFuture represents the response of an asynchronous
generateContent requestIOException - if an I/O error occurs while making the API callpublic com.google.api.core.ApiFuture<GenerateContentResponse> generateContentAsync(List<Content> contents) throws IOException
contents - a list of Content to send to the
generative modelApiFuture represents the response of an asynchronous
generateContent requestIOException - if an I/O error occurs while making the API callpublic String getModelName()
public GenerationConfig getGenerationConfig()
GenerationConfig of this generative model.public com.google.common.collect.ImmutableList<SafetySetting> getSafetySettings()
SafetySetting of this generative model.public com.google.common.collect.ImmutableList<Tool> getTools()
Tool of this generative model.public Optional<ToolConfig> getToolConfig()
ToolConfig of this generative model.public Optional<Content> getSystemInstruction()
public ChatSession startChat()
ChatSession instance that can be used to start a chat with this model.
Note: the returned ChatSession instance is NOT thread-safe.
Copyright © 2025 Google LLC. All rights reserved.