Integrating Salesforce with OpenAI can unlock a wealth of possibilities for businesses looking to optimize their operations and improve customer service. By combining the power of Salesforce’s CRM platform with the advanced natural language processing capabilities of OpenAI, companies can automate and streamline many common tasks, such as data entry, customer support, and lead generation.
In this blog i will show how you can use moderation of the openai in salesforce to analyze the sentiments of the text.
First create an account in openai. you have many option like google, microsoft etc.
Go to personal -> view api keys. generate a key via clicking on Create New Secret Key button. You will get a long key.
Now got to settings and copy organization Id.
Now create an apex class in salesforce with below code. This code Will do a rest callout with api key and organization id to do authorization in openai from salesforce. Also capture the response and convert it in the form of wrapper class.
openAIclass.cls is the callout class
public class openAIclass{
@auraenabled
public static openAiparser fetchSentiments(string texttocheck){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.openai.com/v1/moderations');
request.setMethod('POST');
request.setHeader('Authorization', 'Bearer Your_API_KEY');
request.setHeader('OpenAI-Organization', 'ORG_ID');
request.setHeader('Content-Type','application/json');
//request.setBody('{"prompt": "A cute baby sea otter","n": 2,"size": "1024x1024"}');
request.setBody('{"input": "'+texttocheck+'"}');
HttpResponse response = http.send(request);
system.debug('response code --> '+response.getStatusCode());
system.debug('body check --> '+response.getBody());
//openAiparser op = new openAiparser();
openAiparser op = openAiparser.parse(response.getBody());
return op;
}
}
openAiparser is the parsing class :-
public class openAiparser{
@Auraenabled
public String id; //modr-6WPfknO10QQbtp8belmocZopyNDzF
@Auraenabled
public String model; //text-moderation-004
@Auraenabled
public cls_results[] results;
class cls_results {
@Auraenabled
public cls_categories categories;
@Auraenabled
public cls_category_scores category_scores;
@Auraenabled
public boolean flagged;
}
class cls_categories {
@Auraenabled
public boolean hate;
//public boolean hate/threatening;
//public boolean self-harm;
@Auraenabled
public boolean sexual;
//public boolean sexual/minors;
@Auraenabled
public boolean violence;
//public boolean violence/graphic;
}
class cls_category_scores {
@Auraenabled
public Double hate; //0.0015514851547777653
//public Double hate/threatening; //3.54246168399186e-7
//public Double self-harm; //8.704522969082973e-8
@Auraenabled
public Double sexual; //0.000004727971372631146
//public Double sexual/minors; //2.2640367358661706e-9
@Auraenabled
public Double violence; //0.9857796430587769
//public Double violence/graphic; //6.306476620920876e-7
}
public static openAiparser parse(String json){
return (openAiparser) System.JSON.deserialize(json, openAiparser.class);
}
}
Here is the sample video of the working model of the analysis.
This is one of the thousands of the use by using open ai models in salesforce.