Class VertexAiApi

java.lang.Object
org.springframework.ai.vertex.api.VertexAiApi

public class VertexAiApi extends Object
Vertex AI API client for the Generative Language model. https://developers.generativeai.google/api/rest/generativelanguage https://cloud.google.com/vertex-ai/docs/generative-ai/learn/streaming Provides methods to generate a response from the model given an input https://developers.generativeai.google/api/rest/generativelanguage/models/generateMessage as well as to generate embeddings for the input text: https://developers.generativeai.google/api/rest/generativelanguage/models/embedText Supported models:
                name=models/chat-bison-001,
                version=001,
                displayName=Chat Bison,
                description=Chat-optimized generative language model.,
                inputTokenLimit=4096,
                outputTokenLimit=1024,
                supportedGenerationMethods=[generateMessage, countMessageTokens],
                temperature=0.25,
                topP=0.95,
                topK=40

                name=models/text-bison-001,
                version=001,
                displayName=Text Bison,
                description=Model targeted for text generation.,
                inputTokenLimit=8196,
                outputTokenLimit=1024,
                supportedGenerationMethods=[generateText, countTextTokens, createTunedTextModel],
                temperature=0.7,
                topP=0.95,
                topK=40

                name=models/embedding-gecko-001,
                version=001,
                displayName=Embedding Gecko, description=Obtain a distributed representation of a text.,
                inputTokenLimit=1024,
                outputTokenLimit=1,
                supportedGenerationMethods=[embedText, countTextTokens],
                temperature=null,
                topP=null,
                topK=null
 
Author:
Christian Tzolov
  • Field Details

    • DEFAULT_GENERATE_MODEL

      public static final String DEFAULT_GENERATE_MODEL
      The default generation model. This model is used to generate responses for the input text.
      See Also:
    • DEFAULT_EMBEDDING_MODEL

      public static final String DEFAULT_EMBEDDING_MODEL
      The default embedding model. This model is used to generate embeddings for the input text.
      See Also:
    • DEFAULT_BASE_URL

      public static final String DEFAULT_BASE_URL
      The default base URL for accessing the Vertex AI API.
      See Also:
  • Constructor Details

    • VertexAiApi

      public VertexAiApi(String apiKey)
      Create a new chat completion api.
      Parameters:
      apiKey - vertex apiKey.
    • VertexAiApi

      public VertexAiApi(String baseUrl, String apiKey, String model, String embeddingModel, org.springframework.web.client.RestClient.Builder restClientBuilder)
      Create a new chat completion api.
      Parameters:
      baseUrl - api base URL.
      apiKey - vertex apiKey.
      model - vertex model.
      embeddingModel - vertex embedding model.
      restClientBuilder - RestClient builder.
  • Method Details

    • generateMessage

      Generates a response from the model given an input.
      Parameters:
      request - Request body.
      Returns:
      Response body.
    • embedText

      public VertexAiApi.Embedding embedText(String text)
      Generates a response from the model given an input.
      Parameters:
      text - Text to embed.
      Returns:
      Embedding response.
    • batchEmbedText

      public List<VertexAiApi.Embedding> batchEmbedText(List<String> texts)
      Generates a response from the model given an input.
      Parameters:
      texts - List of texts to embed.
      Returns:
      Embedding response containing a list of embeddings.
    • countMessageTokens

      public Integer countMessageTokens(VertexAiApi.MessagePrompt prompt)
      Returns the number of tokens in the message prompt.
      Parameters:
      prompt - Message prompt to count tokens for.
      Returns:
      Number of tokens in the message prompt.
    • listModels

      public List<String> listModels()
      Returns the list of models available for use.
      Returns:
      List of models available for use.
    • getModel

      public VertexAiApi.Model getModel(String modelName)
      Returns the model details.
      Parameters:
      modelName - Name of the model to get details for.
      Returns:
      Model details.