Friday, February 17, 2023

Salesforce and Open AI's GPT-3 (Chat GPT) Integration Sample Apex Code.

 I have implemented a simple use case to communicated between Salesforce and Open AI's GPT-3 (Chat GPT).

Click on the below link to get the full video details,

Salesforce ChatGPT Use Case

Use Case Details:
Lets create a Salesforce Screen where User can see multiple dog breed images in a left panel, which will be dynamically fetch from salesforce record (say the object name is "Dog Breed") and the image will be set in static resource. Now click on any breed image, it's details will be display in right panel.
But, the main aim is that the details will not be collected from any Salesforce Database. It should come from Open AI's GPT-3 (Chat GPT).

Solution:
As solution you have to perform few steps as mentioned below,
1) Create a App Builder name as "Dog Breed" (with Header and Right Side Bar) for above page layout design.
2) Check the "Lightning Page Tab" for above created App Builder (Dog Breed).
3) Create a Lightning Console App (Dog Breed App) and add the above tab as navigation item.
4) Create a "Dog Breed" custom object with 2 fields ,
     i. Name (Capture the Breed Name)
    ii. Image Url (Capture the static resource image location)
5. Create 2 LWC Component
     i. One to show the list of Breed Image (collected from Dog Breed records)
    ii. Two to display the breed details response collected from Open AI's GPT-3
6. Set Remote Site Settings in Salesforce Org (https://api.openai.com).
7. Create an account on Open AI (Signup link: https://openai.com/api/).

8. Login to Open AI and go to the below link 
https://platform.openai.com/docs/quickstart/build-your-application
and scroll down to below section to create your Secret Key,















Note:
1. In this example, I am not going to show you how to create and design the above 2 LWC component, but I am going to share how to connect with Open AI's GPT-3 to take the response (dog breed details).

2. Also as I just did the R&D to communicate between Salesforce and Open AI - so, I am just trying to concentrate on the code how to connect with these two platform, but you can modify the code base by following best practice.

So, here is your Webservice call sample code,

public with sharing class ChatGPTConnectHelper {
    static String END_POINT = 'https://api.openai.com/v1/completions';
    // Pass the breedName parameter's value from UI level, once user clicks on any breed image.
    public static String getChatGPTResponse(String breedName){
        try{
            HttpRequest request = new HttpRequest();

            request.setEndpoint(END_POINT);
            request.setMethod('POST');
            //Use Secreate Key
            request.setHeader('Authorization', 'Bearer '+String.escapeSingleQuotes('Put Your Secret Key').trim());
            request.setTimeout(120000);
            request.setHeader('Content-Type', 'application/json;charset=UTF-8');
           
            String reqQuery = 'Give me ' + breedName + ' Dog Breed Details';
            String reqBody = '{"model": "text-davinci-003","prompt":"'
                +reqQuery+
                '","max_tokens": 4000,"temperature": 0.5,'
                +'"stream": false,"top_p": 1}';
            request.setBody(reqBody);

            Http http = new Http();
            HttpResponse response = http.send(request);          
            //return response.getBody();
            //TODO: Instead below code use proper JSON Formatting to collect the breed details.

            // For simplicity I am using the below formatting.
            String result = String.valueOf( JSON.deserializeUntyped(response.getBody()) );
            Integer firstInd = result.indexOf('text');
            Integer lstInd = result.indexOf('created');
            result = result.substring(firstInd+5, lstInd-4);
            return result;
        } catch(System.CalloutException e){
          System.debug('Sorry, You have an error ' + e.getMessage());
        }
        return null;
    }
}

Click on the blow link to check what is ChatGPT and how to create an account:

ChatGPT Quick Overview

How to create a ChatGPT Account

LWC to LWC Communication using Lightning Messaging Service (Part - 2)

In my previous post ( previous post link ) we have learn how to create a Lightning Messaging Service with 6 steps and today we will use the ...