In this article i am going to show how you can integrate with openai to leverage AI in salesforce.
Go to profile of openai account and create a secret key.
Now get the organization key from openAI account by navigating to settings
Now create an apex class with below code :-
public class openAIclass{
@AuraEnabled
public static string textcompletionCeck(string texttoCheck){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.openai.com/v1/completions');
request.setMethod('POST');
request.setHeader('Authorization', 'Bearer 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');
request.setHeader('OpenAI-Organization', 'org-c3zJbRvGR38GBMAg8IbX1Zm4');
request.setHeader('Content-Type','application/json');
string requestBody = '{"model":"text-davinci-003","prompt":"'+texttoCheck+'","max_tokens":4000,"temperature":0}';
request.setBody(requestBody);
request.setTimeout(120000);
HttpResponse response = http.send(request);
fromJSON responseRetrived = fromJSON.parse(string.valueof(response.getBody()));
system.debug('response check --> '+responseRetrived.choices[0].text);
//return string.valueof(response.getBody());
return responseRetrived.choices[0].text;
}
}
FromJSON Class :-
public class fromJSON{
public String id; //cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7
public Integer created; //1589478378
public String model; //text-davinci-003
public cls_choices[] choices;
public cls_usage usage;
public class cls_choices {
public String text;
public Integer index; //0
public cls_logprobs logprobs;
public String finish_reason; //length
}
public class cls_logprobs {
}
public class cls_usage {
public Integer prompt_tokens; //5
public Integer completion_tokens; //7
public Integer total_tokens; //12
}
public static fromJSON parse(String json){
return (fromJSON) System.JSON.deserialize(json, fromJSON.class);
}
}
In the above we are doing a post callout to completions endpoint. the secret key and organisation id is used for authorization followed by request body which contains the model name and prompt to get the AI generated response.
Create a LWC component like below:-
HTML :-
<template>
<lightning-layout>
<lightning-layout-item size="9">
<lightning-input variant="label-hidden" value={texttocheck} onchange={assignData}></lightning-input>
</lightning-layout-item>
<lightning-layout-item size="3">
<lightning-button label="Search" onclick={doSearch}></lightning-button>
</lightning-layout-item>
</lightning-layout><br/><br/>
{responseReturned}
</template>
JS :-
import { LightningElement,track,api } from 'lwc';
import fetchCompletion from '@salesforce/apex/openAIclass.textcompletionCeck';
export default class Textcompletioncomponent extends LightningElement {
@track texttocheck;
@track responseReturned;
assignData(event){
this.texttocheck = event.target.value;
}
doSearch(){
fetchCompletion({texttoCheck:this.texttocheck}).then(result=>{
this.responseReturned = result;
}).catch(error=>{
alert(JSON.stringify(error));
});
}
}
Now when you give input like ‘What is salesforce?‘, the apex class will hit openai integration and get the fine tuned answer created by AI.
Click here of live session of connecting salesforce with openAI.
Hey there, I tried implementing this but I am receiving an error when creating the Apex Class that “Error: Compile Error: Invalid type: fromJSON at line 15 column 5” any thoughts on what might be causing this?
I will update the code. There is a slight one line miss
Hi @Alok,
I am also getting some errors in apex. Please upload the correct sample code which you used in the video.
Thanks!
Updated code plz
Code updated. Please check
hey Can you update the code. This blog post is very helpful if you can make that change