You can enable users to make ChatGPT queries from within a screen flow:
When at the end of 2022 ChatGPT became all the hype, I wondered what could be done with it when integrated with Salesforce? Could you unlock new ways CRM data would be utilized when an integration is done? What would be potential use cases?
So, where did I start? A few weeks back I asked GPT3 to create some code for me integrating Salesforce with its API. The code looked good but took some time to fine tune. After all, I finally made it, writing an integration into OpenAI’s API. What were the main guidelines I was following when architecting the solution?
1. It should be suitable for low-code and no-code tools inside Salesforce
2. Although ChatGPT is great, it lacks some freedom when it comes to training your own model or even choosing which model you would like to use. There are differences in OpenAI models all along the road.
3. It should be as secure as you are used to working with Salesforce. So you should be able to define an organization and other parameters to have full control over the integration and data flow.
I set to work writing some APEX class that does exactly that – creating an invocable action so that GPT could be utilized from flow.
With that developed, I created a simple use case that will help service experts: decent knowledge management is one of the hardest things to achieve in a servicecenter. So, why not ask GPT about something you might not have in your knowledge base as of now?
With the integration of OpenAI API, one could start training a bespoke model with your company’s manuals and documentation frequently used at your service centers – without the need to write a knowledge article about it. Since the integration is being built in flow, you could even add additional record data to it. I. e.: “how to start a generator of series 3000?” -> After all, this will speed up things and may make your knowledge base so much more powerful.
I have provided a sample flow called OpenAI Connect Template. [NOTE: the flow is not included in the package because it requires the creation of a custom object and custom field. However, the flow is available in the source repository and can be deployed if you create those objects. See Virginia Leandro’s comments below). You can place it on every lightning record page you like. You can see how it is utilized in the short screencast below.
Establishing a connection to ChatGPT
Go to https://openai.com/ and register. Head over to your account, click “Settings”. Copy your Organization ID to some keyvault of your choice.
Next, head over to API Keys and create an API key. Copy that to the keyvault of your choice. We will need both values later when setting up credentials to connect to OpenAI API.
What you need to get started with flow:
- Create a permission set. We will need that to store the credentials. I haven’t added one in the package, feel free to provide information of your liking with it.
- Create an external credential (i.e. named “OpenAI”) to provide the configuration for your authentication. Make sure to define “Authentication Protocol” as “Custom”
- Use the created permission set to map the credentials to that are needed for your (company) account with OpenAI. Next to permission set in external credential, click new. Provide 2 parameters. One for API Key, one for organization. Value will be the keys we have fetched from OpenAIs site, stored in your keyvault before (or you copy them across on the fly). Make sure to define the mapping for identity type “Named Principal”.
- Once the external credential and permission set mapping is created, make sure to provide the right custom headers:
- Now, set up a named credential to access the right endpoint. You could actually define more than one named credential and access other endpoints. For now, the only endpoint supported is the “completions” endpoint by OpenAI. Head over to “Named Credentials” and click “new”. Fill in the values as stated below. Important, check “Allow Formulas in HTTP Header” and uncheck “Generate Authorization Header”. That is being done by our external credential using formulas. Therefore, relate the external credential we just created.
- Set up a remote site to allow access to openAI.
- DONE! You are done setting up the background configuration. Now, let’s configure the flow to finally go with the flow 😉 .
Now, let’s head over to flows and make some final configurations in the sample flow. That will help you get started as soon as possible.
- —-Sample Flow not included in package.
- Make sure the Open AI Endpoint Name matches the named credential you defined the completions endpoint with (there is one predefined, if you use the same values, it will work).
Once all those steps are done, you can save the flow, activate it and start your first debug!
In conclusion, by integrating OpenAI’s API with Salesforce, you can unlock new possibilities in utilizing CRM data. The integration, developed as a low-code or no-code solution within Salesforce, ensures security and allows you to control the data flow and parameters with ease. The integration allows you to utilize GPT directly from Salesforce flows, opening up new opportunities to enhance the capabilities of your CRM system. With the set up of external credentials, named credentials, and remote sites, you can connect to the OpenAI API and start utilizing the power of GPT within your Salesforce environment.
[Written by GPT]