OpenAI REST API in Mendix
Integrating OpenAI with Mendix via OpenAI’s REST API enables us to create apps that quickly evaluate user questions and produce output that is similar to it.
For instance, if you have any doubts while coding in any programming languages like C, C++, Java, or JavaScript, the exact output with syntax can be provided by OpenAI based on a query entered by the user. The OpenAI platform has other uses besides code completion though.
In my use case, I have to send a Project Delay Letter to the client, but I don’t have the time to type the content. In this case, OpenAI’s completion API is beneficial. All you have to do is enter the subject: “Project Delay letter to client” as a query. It will return text that you can edit and use as a template for the letter as an Output. You could then send it to the client by using the email module with templates or the Email Connector module.
What is OpenAI?
OpenAI is an organization founded in 2015 that creates and conducts research on friendly artificial intelligence (AI) that generally benefits all of humanity.
Since its creation, OpenAI has created several AI powered tools such as ChatGPT (Complex Language Processing), Codex(Natural Speech to Code), and DALL·E (Image Generation).
Integrating OpenAI’s completion API in Mendix
Implementing OpenAI’s API with Mendix takes around five minutes.
You will need an account on OpenAI in order to access the API. Create the OpenAI account here https://openai.com/api/ and generate the API key for authorization.
After creating an account click on settings, then click the view API keys to find your key
Next, we need to create the domain model. As the completion API only expects a single query and returns the response, we only need two fields on our entity and then we can generate the overview pages for the requests.
After creating the domain model and generating the overview pages, we need to make the JSON structure for the data mapping.
Click anywhere in or on the module and select Add other →JSON structure
{
“id”: “cmpl-6Cm8D63RYyATUsgTvDqhVmoJGMIGO”,
“object”: “text_completion”,
“created”: 1668502973,
“model”: “text-davinci-002”,
“choices”: [
{
“text”: “in\n\n#include <iostream>\nusing namespace std;\n\nint main() {\n int num1, num2;\n\n cout << \”Please enter two numbers: \”;\n cin >> num1 >> num2;\n cout << \”The sum is \” << num1 + num2 << endl;\n\n return 0;\n}”,
“index”: 0,
“logprobs”: null,
“finish_reason”: “stop”
}
],
“usage”: {
“prompt_tokens”: 7,
“completion_tokens”: 79,
“total_tokens”: 86
}
}
Paste it in the JSON snippet and click format →Expand all → Refresh and click OK to complete.
After creating the JSON structure right-click on the module and Select Add other →Import mapping
Then we need to create a new microflow to call the API from our app.
Below you can see an example of my implementation:
Add this microflow to the SUBMIT button to get the response.
Call Rest Service Activity Configuration
Method
POST
Location
https://api.openai.com/v1/completions
Headers
Content-Type: “application/json”
Authorization: “Bearer sk-YfkyhiTMudra40*********yhjsnm,”
Body
{
“model”: “text-davinci-002”,
“prompt”:”’ + $OpenAI/Query +’”,
“temperature”: 0.7,
“max_tokens”: 256,
“top_p”: 1,
“frequency_penalty”: 0,
“presence_penalty”: 0
}
Response
Conclusion
You can input a query on the OpenAI overview page, and when you click the submit button, the REST API analyses the query, and returns an answer with a string that you can use as a template for emails.
OpenAI Output in Mendix
I’ll include a few screenshots that show coding and a letter.
I hope this article helped you to understand how to use OpenAI in Mendix using OpenAI’s REST API.
Additionally, you can configure the Email Template Module to send emails.
Thank You for reading this!
Read more
From the Publisher -
If you enjoyed this article you can find more like it on our Medium page. For great videos and live sessions, you can go to MxLive or our community Youtube page.
For the makers looking to get started, you can sign up for a free account, and get instant access to learning with our Academy.
Interested in getting more involved with our community? Join us in our Slack community channel.