Skip to main content
For the complete documentation index for agents and LLMs, see llms.txt.

VertexAICodeGenerator

Generate code using Google Vertex AI generative models.

Key Features

  • Generates code using Vertex AI models (code-bison, code-bison-32k, code-gecko).
  • Accepts a code prefix and an optional suffix to enable in-fill code generation.
  • Authenticates using Google Cloud Application Default Credentials.
  • Returns a list of generated code snippets.

Configuration

Authentication

This component authenticates using Google Cloud Application Default Credentials (ADCs). For more information, see the official Google documentation.

Create secrets with the following keys: GCP_PROJECT_ID and optionally GCP_DEFAULT_REGION. For detailed instructions on creating secrets, see Create Secrets.

  1. Drag the VertexAICodeGenerator component onto the canvas from the Component Library.
  2. Click the component to open the configuration panel.
  3. On the General tab:
    1. Enter the model name (for example, code-bison).
  4. Go to the Advanced tab to configure the project ID, location, and additional model kwargs.

Connections

VertexAICodeGenerator accepts prefix (code before the current point) and an optional suffix (code after the current point) as inputs. It outputs a list of generated code snippets (replies).

Typically, you connect the pipeline query input to prefix and connect replies to AnswerBuilder.

Usage Example

This pipeline uses VertexAICodeGenerator to generate code:

components:
VertexAICodeGenerator:
type: haystack_integrations.components.generators.google_vertex.code_generator.VertexAICodeGenerator
init_parameters:
model: code-bison
project_id:
location:
AnswerBuilder:
type: haystack.components.builders.answer_builder.AnswerBuilder
init_parameters:
pattern:
reference_pattern:
last_message_only: false
return_only_referenced_documents: true

connections:
- sender: VertexAICodeGenerator.replies
receiver: AnswerBuilder.replies

max_runs_per_component: 100

metadata: {}

inputs:
query:
- VertexAICodeGenerator.prefix
- AnswerBuilder.query

outputs:
answers: AnswerBuilder.answers

Parameters

Inputs

ParameterTypeDefaultDescription
prefixstrCode before the current point.
suffixOptional[str]NoneCode after the current point.

Outputs

ParameterTypeDefaultDescription
repliesList[str]A list of generated code snippets.

Init Parameters

These are the parameters you can configure in Pipeline Builder:

ParameterTypeDefaultDescription
project_idOptional[str]NoneID of the GCP project to use. By default, it is set during Google Cloud authentication.
modelstrcode-bisonName of the model to use.
locationOptional[str]NoneThe default location to use when making API calls. If not set, uses us-central-1.
kwargsAnyAdditional keyword arguments to pass to the model. See the TextGenerationModel.predict() documentation.

Run Method Parameters

These are the parameters you can configure for the component's run() method. This means you can pass these parameters at query time through the API, in Playground, or when running a job. For details, see Modify Pipeline Parameters at Query Time.

ParameterTypeDefaultDescription
prefixstrCode before the current point.
suffixOptional[str]NoneCode after the current point.