ChatGPT & Salesforce - Basics
Article authored by Srikant Poddar

ChatGPT & Salesforce - Basics

ChatGPT is one of the most trending words in the last couple of months and you will hear most of the business tycoons, IT intellectuals and your colleagues speaking about it. Well, we already have had google to answer to our queries with millions of results but what made the essence & significance of having ChatGPT.

In ChatGPT, GPT stands for "Generative Pre-training Transformer”.

For anyone who is yet to explore ChatGPT, please go to this link https://chat.openai.com/chat and you will be seconds or minutes away to understand the impact and how it can be a gamechanger in the AI era.

ChatGPT UI

No alt text provided for this image
As you see it in browser.

What is ChatGPT?

Think of ChatGPT as your friend with whom you can communicate, feed input & get output, that too, a meaningful one.

Technically, it is a language model developed by OpenAI that uses machine learning to generate human-like text based on the input provided. It's trained on a large corpus of text data to generate responses to various prompts, including questions and statements. The goal of ChatGPT is to provide an AI-powered conversational agent capable of understanding and generating human-like text.

Normally, you can also understand it as a trained model that interacts in a conversational way. Therefore, you can have a dialogue between you and ChatGPT interface. The dialogue format makes it possible for ChatGPT to answer follow-up questions, admit its mistakes, challenge incorrect premises, and reject inappropriate requests.

To know more about the model and build behind ChatGPT you may visit the link: https://openai.com/blog/chatgpt/

Now, let’s view one basic example where ChatGPT is making the difference:

Google answer to my sample question.

No alt text provided for this image
Search result.

???????

ChatGPT answer to my same sample question - Video Demo(click play button)

ChatGPT and Salesforce:

Now, as a Salesforce developer/consultant you might be thinking, if ChatGPT supports integration with Salesforce and the answer is, yes.

How to integrate ChatGPT API with Salesforce?

Step 1: Create an openAI platform account: https://platform.openai.com/account/

Step 2: Generate secret key for openAI platform account using Create new secret key button: https://platform.openai.com/account/api-keys

No alt text provided for this image

Step 3: Login to your Salesforce org & in the setup create remote site settings using URL: https://api.openai.com

No alt text provided for this image


Step 4: Now you are ready to test your Salesforce Apex code, go to developer console and execute below code in anonymous window.

/*
Author:??Srikant Poddar
Date:???February 4, 2023
Email:[email protected]
Objective:?SFDC Apex code for ChatGPT API callout from Salesforce platform.

Important pre-steps:
Step 1: Create an openAI platform account - https://platform.openai.com/account/
Step 2: generate your openAI account secret key - https://platform.openai.com/account/api-keys 
*/

//Example text query to ChatGPT will use as an input
String myQuery= 'How are you ChatGPT?';

//openAI(ChatGPT) http endpoint
String endpoint = 'https://api.openai.com/v1/completions';

//refer to Step 1 & Step 2 in above comment section
String httpKey = '********'; //hidden my openAI platform secret key

//Instantiate http method
Http http = new Http();

/*
Build Request body for API call
Request Body has 3 attributes
Attribute 1. model - text-davinci-003
Attribute 2. prompt - It will be text query e.g. myQuery in below lines.
Attribute 3. max_tokens - It sets an upper bound on how many tokens the API will return.
Attribute 4. temperature - What sampling temperature to use. Higher values means the model will take more risks.
Attribute 5. stream - true for the API to stream back text.
Attribute 6. top_p - e.g. 0.1 means only the tokens comprising the top 10% probability mass are considered.
*/

String reqBody = '{'+

??????????'"model": "text-davinci-003",'+

??????????'"prompt":"'+myQuery+'",'+

??????????'"max_tokens": 3000,'+

??????????'"temperature": 0,'+

??????????'"stream": false,'+

??????????'"top_p": 0.5'+

????????'}';
?

/*
Instantiate http request method, set endpoint, authorization & content headers & request body
and then, make a callout.
*/

HttpRequest request = new HttpRequest();

request.setEndpoint(endpoint);

request.setMethod('POST');

request.setHeader('Authorization', 'Bearer '+String.escapeSingleQuotes(httpKey).trim()); 

request.setTimeout(120000);

request.setHeader('Content-Type', 'application/json;charset=UTF-8');

request.setBody(reqBody);

HttpResponse response = http.send(request);

System.debug(response);

//Create map to hold the desrialized response

Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());

//Iterate map to check all the key-pair value of response

for(String responseKey : results.keyset()){

? ? system.debug(responseKey +' : ' + results.get(responseKey));

}
?        

Step 5: Execution result:

No alt text provided for this image
Annoymous execute: debug log.

or, visit the URL https://youtu.be/09MczKdgyf4 or click on play button to view execution at developer console in Salesforce.

Thank you!

Kaushik Mukhopadhyay

Delivery and Solutions Leader | Salesforce.com Practice | Bringing efficiency and excellence to solutions and delivery operations

1 年

Loved it Srikant Poddar!

Anirban Roy

Salesforce.Blockchain.AI.Helping Businesses Grow Globally

1 年

Good write up Srikant. The possibilities shall open wide open from here on

Good write-up Srikant; appreciate your experimentation and dissemination

要查看或添加评论,请登录

Srikant P.的更多文章

社区洞察

其他会员也浏览了