# Multi-language libraries and samples for GenAI in Vertex AI

Author: [Mete Atamel](https://atamel.dev/) | Published: 2023-11-28 | Topics: GenAI, VertexAI, Google Cloud Platform | Canonical: https://atamel.dev/posts/2023/11-28_multilanguage_libs_samples_genai_vertexai/


You might think that you need to know Python to be able to use GenAI with
VertexAI. While Python is the dominant language in GenAI (and Vertex AI is no
exception in that regard), you can actually use GenAI in Vertex AI from other
languages such as Java, C#, Node.js, Go, and more.

Let’s take a look at the details.

## Vertex AI SDK for Python

The official SDK for Vertex AI is [Vertex AI SDK for
Python](https://cloud.google.com/vertex-ai/docs/python-sdk/use-vertex-ai-python-sdk)
and as expected, it’s in Python. You can initialize Vertex AI SDK with some
parameters and utilize GenAI models with a few lines of code:

```python
    vertexai.init(project=project_id, location=location)
    parameters = {
        "temperature": temperature,
        "max_output_tokens": 256,
        "top_p": 0.8,
        "top_k": 40
    }

    model = TextGenerationModel.from_pretrained("text-bison")
    response = model.predict(
        "Give me ten interview questions for the role of program manager.",
        **parameters,
    )
```

That’s great for Python but how can you do the same for other languages?

## REST API for Vertex AI

It’s worth nothing that Vertex AI has a REST API. For example, you can utilize
the same GenAI model via REST API as follows:

```sh
curl \
  -X POST \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  https://${REGION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${REGION}/publishers/google/models/text-bison:predict -d \
  $'{
    "instances": [
      { "prompt": "Give me ten interview questions for the role of program manager."}
    ],
    "parameters": {
      "temperature": 0.2,
      "maxOutputTokens": 256,
      "topK": 40,
      "topP": 0.8
    }
  }'
```

You can also do the same REST API call in code from any language. The only
complication is how you authenticate with Vertex AI and that depends on the
language you’re using.

For example, here’s a C# [console
app](https://github.com/meteatamel/genai-samples/blob/main/vertexai/palm2-text/console/csharp/rest/Program.cs)
and [Cloud
Functions](https://github.com/meteatamel/genai-samples/blob/main/vertexai/palm2-text/functions/csharp/rest/Function.cs)
utilizing the REST API and using application-default authentication.

## Client libraries for Vertex AI

The REST API is a good start but there are also auto-generated client libraries
for Vertex AI in multiple languages:

* [C# reference](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.AIPlatform.V1/latest)
* [Go reference](https://cloud.google.com/go/docs/reference/cloud.google.com/go/aiplatform/latest/apiv1)
* [Java reference](https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/overview)
* [Node.js reference](https://cloud.google.com/nodejs/docs/reference/aiplatform/latest)
* [Ruby reference](https://cloud.google.com/ruby/docs/reference/google-cloud-ai_platform/latest)

These client libraries take care of authentication and give you some strongly
typed classes to work with. However, they are auto-generated and not as
idiomatic to use as the Python SDK. They also use protobuf and you need to know
how to construct the right protobuf which can be unintuitive.

For example, here’s the same sample,
[predict-text-prompt.js](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/HEAD/ai-platform/snippets/predict-text-prompt.js),
using the Node.js client library:

```nodejs
  const {PredictionServiceClient} = aiplatform.v1;
  const {helpers} = aiplatform;
  const clientOptions = {
    apiEndpoint: 'us-central1-aiplatform.googleapis.com',
  };

  const publisher = 'google';
  const model = 'text-bison';

  const predictionServiceClient = new PredictionServiceClient(clientOptions);

  async function callPredict() {
    const endpoint = `projects/${project}/locations/${location}/publishers/${publisher}/models/${model}`;

    const prompt = {
      prompt:
        'Give me ten interview questions for the role of program manager.',
    };
    const instanceValue = helpers.toValue(prompt);
    const instances = [instanceValue];

    const parameter = {
      temperature: 0.2,
      maxOutputTokens: 256,
      topP: 0.95,
      topK: 40,
    };
    const parameters = helpers.toValue(parameter);

    const request = {
      endpoint,
      instances,
      parameters,
    };

    const response = await predictionServiceClient.predict(request);
    console.log(response);
  }
```

Definitely more verbose than the Vertex AI Python SDK but it works.

## Multi-language GenAI samples for Vertex AI

Last but not least, there’s a collection of [GenAI samples for Vertex
AI](https://cloud.google.com/vertex-ai/docs/samples?text=generative) in multiple
languages: Python, Node.js, Java, Go, C#, and Ruby.

![GenAI samples for Vertex AI](/img/2023/genai_samples_vertexai.png)

These samples show different GenAI use cases such as text classification,
extraction, summarization, sentiment analysis and more. They are work in
progress with some languages having more samples than others but they’re good
starting points with GenAI on Vertex AI in different languages.

---

In this blog post, I summarized the state of Vertex AI SDKs and libraries and
showed you a collection of samples for different languages.

SDK and client libraries:

* [Python reference](https://cloud.google.com/vertex-ai/docs/python-sdk/use-vertex-ai-python-sdk)
* [C# reference](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.AIPlatform.V1/latest)
* [Go reference](https://cloud.google.com/go/docs/reference/cloud.google.com/go/aiplatform/latest/apiv1)
* [Java reference](https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/overview)
* [Node.js reference](https://cloud.google.com/nodejs/docs/reference/aiplatform/latest)
* [Ruby reference](https://cloud.google.com/ruby/docs/reference/google-cloud-ai_platform/latest)

Samples:

[GenAI samples for Vertex AI](https://cloud.google.com/vertex-ai/docs/samples?text=generative)

Feel free to reach out to me on Twitter
@[meteatamel](https://twitter.com/meteatamel) for any feedback and questions.

