Class GenerativeModel (1.14.0)

public final class GenerativeModel

This class holds a generative model that can complete what you provided. This class is thread-safe.

Note: The instances of ChatSession returned by GenerativeModel#startChat() are NOT thread-safe.

Inheritance

java.lang.Object > GenerativeModel

Constructors

GenerativeModel(String modelName, VertexAI vertexAi)

public GenerativeModel(String modelName, VertexAI vertexAi)

Constructs a GenerativeModel instance.

Parameters
Name Description
modelName String

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-models

vertexAi VertexAI

a com.google.cloud.vertexai.VertexAI that contains the default configs for the generative model

Methods

countTokens(Content content)

public CountTokensResponse countTokens(Content content)

Counts tokens in a single content.

Parameter
Name Description
content Content

a com.google.cloud.vertexai.api.Content to count tokens

Returns
Type Description
CountTokensResponse

a com.google.cloud.vertexai.api.CountTokensResponse instance that contains the total tokens and total billable characters of the given list of contents

Exceptions
Type Description
IOException

if an I/O error occurs while making the API call

countTokens(String text)

public CountTokensResponse countTokens(String text)

Counts tokens in a text message.

Parameter
Name Description
text String

a text message to count tokens

Returns
Type Description
CountTokensResponse

a com.google.cloud.vertexai.api.CountTokensResponse instance that contains the total tokens and total billable characters of the given list of contents

Exceptions
Type Description
IOException

if an I/O error occurs while making the API call

countTokens(List<Content> contents)

public CountTokensResponse countTokens(List<Content> contents)

Counts tokens in a list of contents.

Parameter
Name Description
contents List<Content>

a list of com.google.cloud.vertexai.api.Content to count tokens

Returns
Type Description
CountTokensResponse

a com.google.cloud.vertexai.api.CountTokensResponse instance that contains the total tokens and total billable characters of the given list of contents

Exceptions
Type Description
IOException

if an I/O error occurs while making the API call

generateContent(Content content)

public GenerateContentResponse generateContent(Content content)

Generates content from this model given a single content.

Parameter
Name Description
content Content

a com.google.cloud.vertexai.api.Content to send to the generative model

Returns
Type Description
GenerateContentResponse

a com.google.cloud.vertexai.api.GenerateContentResponse instance that contains response contents and other metadata

Exceptions
Type Description
IOException

if an I/O error occurs while making the API call

generateContent(String text)

public GenerateContentResponse generateContent(String text)

Generates content from generative model given a text.

Parameter
Name Description
text String

a text message to send to the generative model

Returns
Type Description
GenerateContentResponse

a com.google.cloud.vertexai.api.GenerateContentResponse instance that contains response contents and other metadata

Exceptions
Type Description
IOException

if an I/O error occurs while making the API call

generateContent(List<Content> contents)

public GenerateContentResponse generateContent(List<Content> contents)

Generates content from this model given a list of contents.

Parameter
Name Description
contents List<Content>

a list of com.google.cloud.vertexai.api.Content to send to the generative model

Returns
Type Description
GenerateContentResponse

a com.google.cloud.vertexai.api.GenerateContentResponse instance that contains response contents and other metadata

Exceptions
Type Description
IOException

if an I/O error occurs while making the API call

generateContentAsync(Content content)

public ApiFuture<GenerateContentResponse> generateContentAsync(Content content)

Asynchronously generates content from generative model given a single Content.

Parameter
Name Description
content Content

a com.google.cloud.vertexai.api.Content to send to the generative model. The role of the content is "user".

Returns
Type Description
ApiFuture<GenerateContentResponse>

a com.google.api.core.ApiFuture represents the response of an asynchronous generateContent request

Exceptions
Type Description
IOException

if an I/O error occurs while making the API call

generateContentAsync(String text)

public ApiFuture<GenerateContentResponse> generateContentAsync(String text)

Asynchronously generates content from generative model given a text.

Parameter
Name Description
text String

a text message to send to the generative model

Returns
Type Description
ApiFuture<GenerateContentResponse>

a com.google.api.core.ApiFuture represents the response of an asynchronous generateContent request

Exceptions
Type Description
IOException

if an I/O error occurs while making the API call

generateContentAsync(List<Content> contents)

public ApiFuture<GenerateContentResponse> generateContentAsync(List<Content> contents)

Asynchronously generates content from generative model given a list of contents.

Parameter
Name Description
contents List<Content>

a list of com.google.cloud.vertexai.api.Content to send to the generative model

Returns
Type Description
ApiFuture<GenerateContentResponse>

a com.google.api.core.ApiFuture represents the response of an asynchronous generateContent request

Exceptions
Type Description
IOException

if an I/O error occurs while making the API call

generateContentStream(Content content)

public ResponseStream<GenerateContentResponse> generateContentStream(Content content)

Generates content with streaming support from generative model given a single Content.

Parameter
Name Description
content Content

a com.google.cloud.vertexai.api.Content to send to the generative model. The role of the content is "user".

Returns
Type Description
ResponseStream<GenerateContentResponse>

a ResponseStream that contains a streaming of com.google.cloud.vertexai.api.GenerateContentResponse

Exceptions
Type Description
IOException

if an I/O error occurs while making the API call

generateContentStream(String text)

public ResponseStream<GenerateContentResponse> generateContentStream(String text)

Generates content with streaming support from generative model given a text.

Parameter
Name Description
text String

a text message to send to the generative model

Returns
Type Description
ResponseStream<GenerateContentResponse>

a ResponseStream that contains a streaming of com.google.cloud.vertexai.api.GenerateContentResponse

Exceptions
Type Description
IOException

if an I/O error occurs while making the API call

generateContentStream(List<Content> contents)

public ResponseStream<GenerateContentResponse> generateContentStream(List<Content> contents)

Generates content with streaming support from generative model given a list of contents.

Parameter
Name Description
contents List<Content>

a list of com.google.cloud.vertexai.api.Content to send to the generative model

Returns
Type Description
ResponseStream<GenerateContentResponse>

a ResponseStream that contains a streaming of com.google.cloud.vertexai.api.GenerateContentResponse

Exceptions
Type Description
IOException

if an I/O error occurs while making the API call

getGenerationConfig()

public GenerationConfig getGenerationConfig()

Returns the com.google.cloud.vertexai.api.GenerationConfig of this generative model.

Returns
Type Description
GenerationConfig

getModelName()

public String getModelName()

Returns the model name of this generative model.

Returns
Type Description
String

getSafetySettings()

public ImmutableList<SafetySetting> getSafetySettings()

Returns a list of com.google.cloud.vertexai.api.SafetySetting of this generative model.

Returns
Type Description
com.google.common.collect.ImmutableList<SafetySetting>

getSystemInstruction()

public Optional<Content> getSystemInstruction()

Returns the optional system instruction of this generative model.

Returns
Type Description
Optional<Content>

getToolConfig()

public Optional<ToolConfig> getToolConfig()

Returns the optional com.google.cloud.vertexai.api.ToolConfig of this generative model.

Returns
Type Description
Optional<ToolConfig>

getTools()

public ImmutableList<Tool> getTools()

Returns a list of com.google.cloud.vertexai.api.Tool of this generative model.

Returns
Type Description
com.google.common.collect.ImmutableList<Tool>

startChat()

public ChatSession startChat()

Returns a new ChatSession instance that can be used to start a chat with this model.

Note: the returned ChatSession instance is NOT thread-safe.

Returns
Type Description
ChatSession

withGenerationConfig(GenerationConfig generationConfig)

public GenerativeModel withGenerationConfig(GenerationConfig generationConfig)

Creates a copy of the current model with updated GenerationConfig.

Parameter
Name Description
generationConfig GenerationConfig

a com.google.cloud.vertexai.api.GenerationConfig that will be used in the new model.

Returns
Type Description
GenerativeModel

a new GenerativeModel instance with the specified GenerationConfig.

withSafetySettings(List<SafetySetting> safetySettings)

public GenerativeModel withSafetySettings(List<SafetySetting> safetySettings)

Creates a copy of the current model with updated safetySettings.

Parameter
Name Description
safetySettings List<SafetySetting>

a list of com.google.cloud.vertexai.api.SafetySetting that will be used in the new model.

Returns
Type Description
GenerativeModel

a new GenerativeModel instance with the specified safetySettings.

withSystemInstruction(Content systemInstruction)

public GenerativeModel withSystemInstruction(Content systemInstruction)

Creates a copy of the current model with updated system instructions.

Parameter
Name Description
systemInstruction Content

a com.google.cloud.vertexai.api.Content containing system instructions.

Returns
Type Description
GenerativeModel

a new GenerativeModel instance with the specified tools.

withToolConfig(ToolConfig toolConfig)

public GenerativeModel withToolConfig(ToolConfig toolConfig)

Creates a copy of the current model with updated tool config.

Parameter
Name Description
toolConfig ToolConfig

a com.google.cloud.vertexai.api.ToolConfig that will be used in the new model.

Returns
Type Description
GenerativeModel

a new GenerativeModel instance with the specified tool config.

withTools(List<Tool> tools)

public GenerativeModel withTools(List<Tool> tools)

Creates a copy of the current model with updated tools.

Parameter
Name Description
tools List<Tool>

a list of com.google.cloud.vertexai.api.Tool that will be used in the new model.

Returns
Type Description
GenerativeModel

a new GenerativeModel instance with the specified tools.