---
title: "VertexAICodeGenerator"
id: vertexaicodegenerator
slug: "/vertexaicodegenerator"
description: "This component enables code generation using Google Vertex AI generative model."
---

# VertexAICodeGenerator

This component enables code generation using Google Vertex AI generative model.

<div className="key-value-table">

|  |  |
| --- | --- |
| **Mandatory run variables** | `prefix`: A string of code before the current point  <br /> <br />`suffix`: An optional string of code after the current point |
| **Output variables** | `replies`: Code generated by the model |
| **API reference** | [Google Vertex](/reference/integrations-google-vertex) |
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/google_vertex |

</div>

`VertexAICodeGenerator` supports `code-bison`, `code-bison-32k`, and `code-gecko`.

### Parameters Overview

`VertexAICodeGenerator` uses Google Cloud Application Default Credentials (ADCs) for authentication. For more information on how to set up ADCs, see the [official documentation](https://cloud.google.com/docs/authentication/provide-credentials-adc).

Keep in mind that it’s essential to use an account that has access to a project authorized to use Google Vertex AI endpoints.

You can find your project ID in the [GCP resource manager](https://console.cloud.google.com/cloud-resource-manager) or locally by running `gcloud projects list` in your terminal. For more info on the gcloud CLI, see its [official documentation](https://cloud.google.com/cli).

## Usage

You need to install `google-vertex-haystack` package first to use the  `VertexAIImageCaptioner`:

```shell
pip install google-vertex-haystack
```

Basic usage:

````python
from haystack_integrations.components.generators.google_vertex import VertexAICodeGenerator

generator = VertexAICodeGenerator()

result = generator.run(prefix="def to_json(data):")

for answer in result["replies"]:
    print(answer)

>>> ```python
>>> import json
>>>
>>> def to_json(data):
>>>   """Converts a Python object to a JSON string.
>>>
>>>   Args:
>>>     data: The Python object to convert.
>>>
>>>   Returns:
>>>     A JSON string representing the Python object.
>>>   """
>>>
>>>   return json.dumps(data)
>>> ```
````

You can also set other parameters like the number of output tokens, temperature, stop sequences, and the number of candidates.

Let’s try a different model:

```python
from haystack_integrations.components.generators.google_vertex import VertexAICodeGenerator

generator = VertexAICodeGenerator(
	model="code-gecko",
	temperature=0.8,
	candidate_count=3
)

result = generator.run(prefix="def convert_temperature(degrees):")

for answer in result["replies"]:
    print(answer)

>>>
>>>     return degrees * (9/5) + 32

>>>
>>>     return round(degrees * (9.0 / 5.0) + 32, 1)

>>>
>>>     return 5 * (degrees - 32) /9
>>>
>>> def convert_temperature_back(degrees):
>>>     return 9 * (degrees / 5) + 32
```
