Integrate ChatGPT with Salesforce.

Integrate ChatGPT with Salesforce

What is ChatGPT--

The language model ChatGPT, developed by Open AI, can produce text and interactions that resemble those of a human.

The free tool creates interactions with the user that seem plausible.

ChatGPT can:-

*Answer questions or search queries in a manner similar to that of Google Create writing, including
*copy and creative suggestions
*Research Assistance

In today's fast-paced business environment, customer support has become more critical than ever. With increasing competition, businesses need to provide exceptional customer service to differentiate themselves from the competition. One way to do that is by integrating a chatbot with Salesforce, which can provide real-time assistance to customers.

Dotsquares is a Salesforce partner that offers seamless integration of Chat GPT Salesforce with. The integration of these two platforms can enhance customer experience and improve the efficiency of customer service.

Here are some of the benefits of integrating Salesforce with Chat GPT:

Automated Customer Service: By integrating Chat GPT with Salesforce, businesses can provide automated customer service, which can save time and resources. Chat GPT can answer common customer queries and provide quick solutions, freeing up customer support staff to handle more complex issues.

Improved Customer Engagement: Chat GPT can provide real-time customer service, improving engagement with customers. By answering queries quickly, businesses can increase customer satisfaction and improve their reputation.

Increased Efficiency: Integrating Salesforce with Chat GPT can improve the efficiency of customer service. Chat GPT can handle multiple queries simultaneously, which can reduce wait times for customers and improve the overall experience.

24/7 Customer Support: Chat GPT can provide 24/7 customer support, which can enhance the customer experience. Customers can get answers to their queries at any time, which can improve their satisfaction and loyalty.

Steps for Integrating ChatGPT with Salesforce: -

1. Apex Class Code:-

 
    public with sharing class ChatGptAi {
private static String CHAT_GPT_KEY = '     ';//Use your own API key provided by the ChatGpt. 
private static final String ENDPOINT ='https://api.openai.com/v1/completions';    
@AuraEnabled
public static String getQueryData(String searchString){
    try{
        String seachQueryEscaped = (searchString).trim();
        Http http = new Http();
        String reqBody = '{"model": "text-davinci-003","prompt":"'
            +seachQueryEscaped+
            '","max_tokens": 4000,"temperature": 0,'
            +'"stream": false,"top_p": 0.5}';
        System.debug('Query '+seachQueryEscaped+' '+reqBody);
        HttpRequest request = new HttpRequest();
        request.setEndpoint(ENDPOINT);
        request.setMethod('POST');
        request.setHeader('Authorization', 'Bearer '+String.escapeSingleQuotes(CHAT_GPT_KEY).trim());
        request.setTimeout(120000);
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        request.setBody(reqBody);
        HttpResponse response = http.send(request);
        if(response.getStatusCode() != 200) {
            System.debug('The status code returned was not expected: ' + response.getStatusCode() + ' ' + response.getBody());
            return response.getBody();
        }
        return response.getBody();
    }catch(Exception ex){
        System.debug('Exception in Catch of Server-Side Controller '+ex);
        throw new AuraHandledException(ex.getMessage());
        }
    }
}
      

**Note--Use API key with New openai Account.

 

2. For Creating LWC Component

HTML File: -

                    
<template>
	<div class="slds-card slds-scrollable  slds-float_left" style ="width: 400px;"  >
		<header class="slds-card__header slds-grid">
			<div class="slds-media slds-media_center slds-has-flexi-truncate">
				<div class="slds-media__figure">
					<img src={imageUrl} width="50" height="50" alt="Avatar"  />
				</div>
				<div class="slds-media__body">
					<h2 class="slds-card__header-title">
						<a href="javascript:void(0);" class="slds-card__header-link slds-truncate" title="Card Title">
							Chat GPT
						</a>
					</h2>
					<p class="slds-card__header-subtitle slds-truncate" title="Card Subtitle">Dotsquares Technologies</p>
				</div>
			</div>
		</header>
		<div class="scrollable-content slds-card__body slds-card__body_inner " >
			<template if:true={responseData}>
				<lightning-formatted-rich-text disable-linkify value={responseData}></lightning-formatted-rich-text>
			</template>
			<template if:true={showSpace}>
				<br/>
				<br/>
				<br/>
			</template>
			<template if:true={showSpinner}>
				<lightning-spinner alternative-text="Loading" size="large"></lightning-spinner>
			</template>		
		</div>
		<footer class="slds-card__footer" >
			<div class="slds-form-element">
				<div class="slds-form-element__control slds-input-has-icon slds-input-has-icon_right">
					<input type="text" id="search-input-footer" class="slds-input" placeholder="Enter search term" onkeydown={handleKeyDown} />
					 <span class="slds-icon_container slds-icon-utility-down slds-input__icon slds-input__icon_right multi-select-combobox__icon"
                		   title="Click to open the dropdown"
              		 ><div class="slds-p-left_xx-small">
                		<lightning-icon
                  			icon-name="utility:right"
                  			size="xx-small"
                  			alternative-text="Click here"
                  			class="slds-icon slds-icon--selected slds-icon--x-small slds-icon-text-default"
                  
                		>
                		</lightning-icon>
                	</div>
                  </span>
				</div>
			</div>
		</footer>
	</div>
</template>                    

3. JavaScript Code: -

    
import { LightningElement,track,api } from 'lwc';
import getQueryData from '@salesforce/apex/ChatGptAi.getQueryData';
import IMAGE from '@salesforce/resourceUrl/ChatBot';


export default class ChatGPT extends LightningElement {
  @track searchResults = [];
  @track searchTerm = '';
  @api imageUrl = IMAGE;
  @track showSpace = true ;
  @track showSpinner = false
  @track responseData 

  handleKeyDown(event) {
    
      if (event.keyCode === 13) {
    
        this.searchTerm = event.target.value;
        this.showSpinner = true
        this.searchResults = [];
        getQueryData({searchString:this.searchTerm})
         .then(result=>{
           this.showSpinner = false
           let response = JSON.parse(JSON.stringify(JSON.parse(result)));
           if (response.error) {
                    this.responseData = response.error.message;
            } else if (response.choices[0].text) {
                    this.responseData = response.choices[0].text;
                    this.responseData = this.responseData.replace(/\n/g, "
"); let tempScriptData = '' tempScriptData = (response.choices[0].text.includes()) ? 'JS File: ' + response.choices[0].text.split('')[1] : ''; tempScriptData = this.responseTextLWCJS.replace(/\n/g, "
"); this.responseData = this.responseData + this.responseTextLWCJS; this.responseData = (this.responseData.includes('XML File:')) ? this.responseData.split('XML File:')[0] : this.responseData; this.responseData.trim(); } console.log('ss',JSON.stringify(responseData)) }) .catch(error=>{ this.showSpinner = false console.log('error is '+error) }) if(this.searchResults.length > 0 ) this.showSpace =false } } }

4. XML Code:-

                
<?xml version="1.0"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
	<apiVersion>55.0</apiVersion>
	<isExposed>true</isExposed>
	<targets>
		<target>lightning__RecordPage</target>
		<target>lightning__AppPage</target>
		<target>lightning__HomePage</target>
	</targets>
</LightningComponentBundle>                

You can add this Component in your org.

 

In conclusion, integrating Chat GPT with Salesforce, Dotsquares/Forcesquares can provide businesses with a competitive advantage by enhancing customer service, improving efficiency, and increasing customer engagement. With the right integration partner, businesses can take advantage of the benefits of these two platforms and achieve their customer service goals.