DocumentationAPI Reference📓 Tutorials🧑‍🍳 Cookbook🤝 Integrations💜 Discord

VertexAICodeGenerator

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

NameVertexAICodeGenerator
Pathhttps://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/google_vertex
Mandatory Input variables“prefix”: a string of code before the current point

”suffix”: an optional string of code after the current point
Output variables“replies”: a list of strings generated by the model

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.

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 or locally by running gcloud projects list in your terminal. For more info on the gcloud CLI, see its official documentation.

Usage

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

pip install google-vertex-haystack

On its own

Basic usage:

from haystack_integrations.components.generators.google_vertex import VertexAICodeGenerator


generator = VertexAICodeGenerator(project_id=project_id)

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:

from haystack_integrations.components.generators.google_vertex import VertexAICodeGenerator


generator = VertexAICodeGenerator(
	model="code-gecko",
	project_id=project_id,
	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

Related Links

Check out the API reference in the GitHub repo or in our docs: