Connecting to Gemini Pro API
Published: 2023-06-13 16:45:06

**Introduction:**

In the fast-paced world of artificial intelligence, Google continues to push the boundaries of innovation with the release of Gemini Pro in their Generative AI Studio. This release marks a significant milestone in the development of chat applications, enabling developers to leverage advanced generative AI capabilities. In this article, we will explore the features and implications of Google Gemini Pro for chat applications.

Gemini Pro, the latest iteration of the Pattern-Attributed Language Model, represents a breakthrough in chat-based AI technology. It is designed to understand and generate human-like responses in natural language conversations. Powered by deep learning algorithms and trained on vast amounts of text data, Gemini Pro demonstrates remarkable fluency and contextual understanding, making it an invaluable tool for chat application developers.

Google's Gemini ProAPIs for chat provide developers with a powerful interface to integrate into their chat applications. These APIs offer a range of functionalities, including sentiment analysis, language translation, and response generation. By harnessing the power of Gemini Pro, developers can create chat applications that deliver more natural, contextually aware responses, thereby enhancing user experiences.

**Integration and Usage:**

You can start use Gemini Pro in Vertex AI, [Generative AI Studio](https://console.cloud.google.com/vertex-ai/generative/language/gallery) like this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmois6ljdabg1a8klk9m.png)

Developers can interact with the model in a similar way to ChatGPT, initiating conversations and receiving responses:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gxpa9ve8ul6v01t45v70.png)


API access is also available, allowing developers to integrate Gemini Pro into their applications. For example, in Node.JS, API access can be easily implemented by providing the filename of the Google Service account JSON file obtained from the Cloud Console:

```javascript
import { JWT } from "google-auth-library";

const API_ENDPOINT = "us-central1-aiplatform.googleapis.com";
const URL = `https://${API_ENDPOINT}/v1beta1/projects/${process.env.GOOGLE_KEY}/locations/us-central1/publishers/google/models/gemini-pro:streamGenerateContent`;

export const getIdToken = async () => {
    const client = new JWT({
        keyFile: "./google.json",
        scopes: [
            "https://www.googleapis.com/auth/cloud-platform",
        ],
    });
    const idToken = await client.authorize();
    return idToken.access_token;
};

export const getTextGemini = async (prompt, temperature) => {
    const headers = {
        Authorization: `Bearer ` + (await getIdToken()),
        "Content-Type": "application/json",
    };

    const data = {
        contents: [
            {
                role: "user",
                parts: [
                    {
                        text: prompt,
                    },
                ],
            },
        ],
        generation_config: {
            maxOutputTokens: 2048,
            temperature: temperature || 0.5,
            topP: 0.8,
        },
    };

    const response = await fetch(URL, {
        method: "POST",
        headers,
        body: JSON.stringify(data),
    });

    if (!response.ok) {
        console.error(response.statusText);
        throw new Error("Gemini Error:" + response.statusText);
    }

    const result = await response.json();
    return result.map((r) => r?.candidates?.[0]?.content?.parts?.[0]?.text).join("");
};
```
Please grant your service account permissions to run Vertex AI operations, like here:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1mo5f6y5meppiiub7icp.png)



The cost of using the Gemini Pro API is relatively affordable at $0.00025 per 1K characters. However, it's worth noting that Google is currently offering a full discount on the usage fees.

Testing and Impressive Results:

During testing, Gemini Pro demonstrated impressive capabilities, particularly in storytelling. The generated stories produced by  Gemini Pro are comparable to results achieved by models like GPT-4. This showcases the potential of Gemini Pro in generating high-quality narrative content:

https://mangatv.shop/story/futurama-lama-meets-the-gemini-and-the-mistral