Custom Server Connect module - ChatGPT
Introduction
Welcome to my first revision of a Custom Server Connect module for node facilitating interfacing with ChatGPT. This module can use "gpt-3.5-turbo" or "gpt-4o". I hope to add more options later.
This server connect module allows the user to pass text to ChatGPT and returns a text response.
Extension location and installation
The extension can be found in the npm repository here:
The extension is installed in accordance with this post
Don't forget to do a full Wappler restart to activate the extension
Using the extension
Once installed a new icon will appear in the API connector group of server connect
Adding the Chat GPT API key
Firstly we must define out ChatGPT API key
You can get an API key from:
https://platform.openai.com/api-keys
and add credit via
https://platform.openai.com/settings/organization/billing/overview
We do so by creating a Environment variable within Workflows server connect settings
We add an environment variable. This MUST be called "OPENAI_API_KEY" and we add the API key
Adding the component
Create an API action and add the component
Add a name
In this tutorial i am going to pass the enquiry from a page as a $_POST variable so i add this to the Inputs. The data source could alternatively be static text or dynamic such as the output of a database query.
Configuration
Add it to as the request body
Tokens
It is important to set the number of tokens for the enquiry. Tokens roughly align to characters.
Each character in the enquiry and the response contribute to the tokens total.
As the pricing for chat GPT is based on the number of tokens used, then setting a limit is a good way of managing costs.
In this case i set the maximum number of tokens to 100 and check output to enable the response
Chat GPT Engine
I select the desired Chat GPT engine from the dropdown
Token Pricing based on chat engine
Open AI token process vary between engines, basically the more advance engine, the higher the cost. I have included gpt-3.5-.5-turbo as token prices are around 5% of the cost of gpt-4o tokens. While less sophisticated, using gpt-3.5-turbo can significantly reduce costs.
Output
Finally we enable output to ensure the response is available.
Example use
I now create a content page with a simple server connect form with a text input and submit button.
I name the text input to match the $_POST input variable.
I connect the server connect for to the chat API action i have just created
I add a further row/column and paragraph to display the results of the action and set the text via dynamic attributes to Inner text and select the returned value form the form data
I then open the page in a browser and add a question, in this case "what is wappler"
The Chat GPT response is displayed in the lower paragraph