Introduction
Chat GPT is a chatbot introduced by OpenAI in November 2022. It is built on LLM (large language model) which means the underlying algorithm has been trained on a lot of data. The actual model that it is based on is the GPT-3 which has 175 billion parameters. OpenAI has exposed the ChatGPT functionality via UI and API. In this blog we will explore how this API can be called from the Salesforce platform and the potential use cases of Chat GPT in Salesforce with an example.
Benefits of using ChatGPT
The revolutionary nature of ChatGPT stems from its ability to understand prompts given in natural language, i.e., it allows developers to prompt development tasks and get an output which is like a generated code template or formula for the problem mentioned in the prompt. This can be attributed ability of ChatGPT to parse, recognize and actionize based on given natural language prompts and generate an output.
ChatGPT has come up as a savior for developers. It allows developers to free up their bandwidth from repetitive, non-value adding tasks that can be completed by giving the input as a prompt to the ChatGPT and focusing in disruptive, innovative solutions instead, leading to better output and higher efficiency.
Akin to search engines, the quality of the prompt also makes a difference. The more self-explanatory, unambiguous the prompt is, the closer the output is to the desired outcome. An experienced developer can easily use the resultant code as a template and tweak it to achieve the desired function. This is true for Salesforce development (and configuration) work as well, allowing admins and developers to pair program with ChatGPT for higher throughput.
Using ChatGPT API to generate Salesforce code
Salesforce uses a proprietary programming language called Apex, which is loosely based on Java and has similar syntax and constructs. Using the ChatGPT API to generate an Apex class is straightforward.
1. Setup steps: Add the OpenAI completions endpoint https://api.openai.com/v1/completions to remote site settings in Salesforce
2. Authentication: The authentication is taken care by generation of an API key which can be generated from the OpenAI site (https://platform.openai.com/) and the token can be sent in the authorization header. Remember, usage is free only to a certain extent.
3. Making the request: To hit the API and get a response in text format, we need to hit the completions endpoint (https://api.openai.com/v1/completions). The HTTP request is a POST request and it expects an input (prompt) in JSON format. An example request can be like ‘{\”prompt\”:”Write an Apex batch class to deactivate users who have not logged in for last 90 days”,\”model\”:\”text-davinci-003\”,\”max_tokens\”:4000}’. The other parameters in the JSON request for e.g., model can be read about and understood from the documentation (https://platform.openai.com/docs/api-reference/introduction). This logic explained above can be included in an Apex class and can be called within Salesforce (by creating a UI on top of it. e.g., a Lightning web component using which developer can post the prompts) to generate meaningful output.
4. Output: Below is the output generated using the prompt “Write an Apex batch class to deactivate users who have not logged in for last 90 days”. The generated Apex code can be seen in the “text” tag of the response JSON as below:
Conclusion
ChatGPT will certainly make the work of developers easier by helping them generate boilerplate code for functionality they desire to achieve in Salesforce development. ChatGPT can generate formulas, Apex code, Lightning code etc. In the above example the output for the given prompt “Write an Apex batch class to deactivate users who have not logged in for last 90 days” is pretty much on target. A developer can easily use this code directly or modify if desired to include other business logic. As mentioned earlier, the more precise the prompt is, the better would be the output.
As we look ahead in near future, A UI built within Salesforce and integrated with ChatGPT at the backend opens new dimensions for developers and business users alike to work much more efficiently than ever on the Salesforce platform. Business users can benefit by fetching relevant information that may for example help them to close an opportunity faster.
The recently released Einstein GPT feature in Salesforce aims to capitalize on generative AI as well, with a similar aim of making life simpler for Salesforce developers. For a low-code / no-code platform, that is certainly an investment in the right direction.
Good article, explaining in simple and clear terms how Salesforce can integrate and leverage ChatGPT. Definite unlocks
potential for developers to use the technology to do bigger and better things in future!
I am a non technical person aged 68 but enjoy reading articles which help me to update my knowledge on technical subjects. I enjoyed reading the article, though many of the technical jargon were beyond my comprehension. All in all I could understand that chat GPT is the future.
I wish the author all success in his career and furtherance in his interests.