MuleSoft Integration with ChatGPT — Part 1

Shubham Chaurasia
Another Integration Blog
5 min readMar 24, 2023
MuleSoft Integration with ChatGPT Logo

What is ChatGPT in Layman Language?

ChatGPT is a computer program that can talk with people like you and me by understanding our messages and responding with relevant information. It has been trained on a vast amount of text data to be able to answer questions and carry out conversations on a wide range of topics. In simple terms, it’s a virtual assistant that can chat with you!

Next, you’ll need to decide on the specific use case for your integration. For example, you may want to use ChatGPT to provide customer service through a chat bot or virtual assistant. Alternatively, you may want to use ChatGPT to automate internal business processes that require natural language processing.

Once you have a clear use case in mind, you can start building your integration. You have two options, either you can use ChatGPT API with HTTP Request Connector or Public/Custom MuleSoft ChatGPT Connector.

Once your integration is up and running, you can start testing it and refining it to meet the specific needs of your organization. With the power of MuleSoft and ChatGPT combined, you can automate complex business processes and provide a seamless customer experience that leverages the latest in natural language processing technology.

❒ Meetup Session/ YouTube Recording

I have delivered a meetup on this topic. You can watch it on YouTube below

👉 MuleSoft Integration with ChatGPT — Part 1

Generating API Key

To get started with integrating ChatGPT with MuleSoft, you’ll first need to create an account with OpenAI and obtain an API key. Once you have your API key, you can use it to authenticate your requests to the ChatGPT API.

1. Sign up here.
2. To obtain an API key, visit here.
3. Click on the Circle at the top right.
4. In the drop-down menu, click View API Keys
5. Click on + Create a new secret key.
6. A dialog box will appear with the message API Key Generated
7. Copy your API Key.

Keep this API key safe, we will be using this in later stages. We can retrieve it again.

We will be looking at 2 ways to integrate MuleSoft with ChatGPT

MuleSoft Integration with ChatGPT API

  1. Create a ‘New Project’ in Studio and create the following flow:
MuleSoft Integration with ChatGPT API Flow

2. In ‘HTTP Request Configuration’, go to the ‘Settings’ tab, and on the default headers option, from the drop-down option, select ‘Edit inline’, and configure the API key.

HTTP Request Authorization Configuration

3. Protocol will be HTTPS, and the Host:- api.openai.com

4. For HTTP request, input the values as shown in the image below:

Method:- POST
Path:- /v1/chat/completions

5. Run the project, hit the ‘HTTP Listener’ endpoint from Postman and pass the following in the body

{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "Describe MuleSoft in layman terms in 100 words"
}
]
}

6. You’ll receive the response to your question in the content key in the message.

7. If you need to access the response only, use the following DataWeave in the transform message.

%dw 2.0
output application/json
---
payload.choices[0].message.content

The code for MuleSoft ChatGPT API is available on below Git Repository.

Github Repo Link:- MuleSoft_with_chatGPT_API

MuleSoft Integration with OpenAI Connector

For this, we`ll be using the custom connector created by Dejim Juang (dejimj@gmail.com). Thank you Dejim for sharing it with the community.

  1. First, we need to download the code from the repo 🔗
  2. Run the command mvn clean install -DskipTests=true in cmd inside the folder which will download the connector in our local repo.
  3. You may get HTTP repositories blocked error while running the command. Paste the below code in the settings.xml file of your maven in the mirrors section.
<mirror> 
<id>maven-default-http-blocker</id>
<mirrorOf>mulesoft-plugin-releases</mirrorOf>
<name>Repository to mirror external repositories</name>
<url>http://repository.mulesoft.org/releases</url>
<blocked>false</blocked>
</mirror>

4. Next, create a new project and include the dependency in our project’s dependencies section in pom.xml.

<dependency>
<groupId>com.mulesoft.platform_se</groupId>
<artifactId>openai</artifactId>
<version>1.0.12</version>
<classifier>mule-plugin</classifier>
</dependency>

5. This will download the connector from the local repo, and will make it available in the palette section.

6. Create the following flow:

7. In the OpenAI connector configuration, configure the API key.

8. In Create Chat Completion Operation, select the model as “gpt-3.5-turbo”, and Messages will be payload. We will be sending the request through the POST method in the body.

Create Chat Completion Operation Configuration

9. Run the project and hit the ‘HTTP Listener’ endpoint with POST method from Postman and pass the following in the body

[
{
"role": "user",
"content": "Who is Elon Musk"
}
]

You’ll receive the response to your question in the content key in the message.

The code for MuleSoft OpenAI Connector is available on below Git Repository.

Github Repo Link:- MuleSoft_with_chatGPT_Connector

This completes the demonstration of the MuleSoft integration with the ChatGPT API & OpenAI Connector.

➣You can read about the other OpenAI API endpoints which you can use to generate images, convert audio to text, and fine-tune your documents.

Important Link For References

In the next blog, we will be looking at an interesting use case of ChatGPT.

Please provide feedback. It will help to improve the content.

Thank you for reading this article! If you found this response useful, please consider giving it a ‘clap’ and follow me for more helpful articles and insights in the future.

--

--

Shubham Chaurasia
Another Integration Blog

MuleSoft Ambassador | Mule Certified Architect & Developer | 11x Salesforce| 4x AWS | 2x GCP | 2x Solace | 2xAzure | https://linkedin.com/in/shubhamchaurasia1/