Google Gemini Salesforce Integration using Apex

In the rapidly evolving landscape of Artificial Intelligence, the collaboration between Salesforce and Google Gemini might represent a significant leap in developing advance AI related Application in Salesforce. Today I will show you a simple Google Gemini Salesforce Integration using Apex. So, without any detailed theory on Google Gemini I will start with the interesting stuff.

Interested Read: Salesforce ChatGPT Integration

Google Gemini

Google Gemini is multimodal large language models developed by Google DeepMind. Positioned as a successor to LaMDA and PaLM 2, it was announced on December 6, 2023, and is seen as a competitor to OpenAI’s GPT-4.

Gemini Salesforce Integration

Remote Site Setting

First thing first. Open your Salesforce Org and Add the below URL in Remote Site Setting.

https://generativelanguage.googleapis.com

Gemini Salesforce Integration

Google Gemini API Key

Now, Signup for Google Gemini in Google AI Studio by visiting this URL https://aistudio.google.com

Gemini Salesforce Integration

Click on ‘Get API Key’.

Gemini Salesforce Integration

This will create the API key. Now, copy the API key from the prompt. Keep this API key somewhere safe. It will be used in the Google Gemini Salesforce Integration Apex code.

Google Gemini Salesforce Integration Apex code

Copy Paste the below code. Make sure you replace “API_KEY” with your API key in the code.

public class GeminiCallout {
    public static void makePostCallout() {
        String endpoint = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent';
        String apiKey = 'API_KEY';
        endpoint+= '?key=' + apiKey;
        HttpRequest req = new HttpRequest();
        req.setEndpoint(endpoint);
        req.setMethod('POST');
        req.setTimeout(120000);
        req.setHeader('Content-Type', 'application/json');
        String requestBody = '{\"contents\":[{\"parts\":[{\"text\":\"Write a story in 3 lines about a girl.\"}]}]}';
        req.setBody(requestBody);
        Http http = new Http();
        HttpResponse res = http.send(req);
        if (res.getStatusCode() == 200) {
            String jsonResponse = res.getBody();
			
            // Parse the JSON response
            Map<String, Object> parsedResponse = 
            (Map<String, Object>)JSON.deserializeUntyped(jsonResponse);
            
            // Navigate through the nested structure to get to the "candidates" list
            List<Object> candidates = (List<Object>)parsedResponse.get('candidates');
            
            // Assuming there's at least one candidate and you want the first one
            Map<String, Object> firstCandidate = (Map<String, Object>)candidates[0];
            
            // Navigate to "content" -> "parts"
            Map<String, Object> content = (Map<String, Object>)firstCandidate.get('content');
            List<Object> parts = (List<Object>)content.get('parts');
            
            // Assuming there's at least one part and you want the text from the first part
            Map<String, Object> firstPart = (Map<String, Object>)parts[0];
            String text = (String)firstPart.get('text');
            System.debug('Callout Result - ' + text);
		} else {
            System.debug('HTTP error: ' + res.getStatusCode() + ' ' + res.getStatus());
        }
	}
}

To run the above code, just open Anonymous Window and paste this:

GeminiCallout.makePostCallout();

I hope this will give you an idea of Google Gemini Salesforce Integration using Apex. If you have any queries, please reach out to me on LinkedIn, Kamal Thakur.

Share: