ChatGPT-4: Webnmethods.io Connector

ChatGPT-4: Webnmethods.io Connector

Earlier in the series, we defined a custom connector for the new X API and established that a predefined connector does not work with the latest OpenAI models. Let's create a custom REST connector for the latest GPT models.

The most logical way to define a custom connector would be to create a custom REST connector, as we've done for X API version 2. The primary difference in the configuration process is the authentication and authorization methods. But let's get started.

  1. Log in to your Webmethods.io environment and select the "Connectors" tab. On the left navigation pane, choose "REST" connectors.
  2. Start the new connector Wizard with the "Add Connector" button next to the search field.
  3. Fill out the form with the parameters below:
    • Name: GPT4_Connector
    • Default Endpoint URL: https://api.openai.com/
    • Description: Custom Connector to access the latest OpenAI models.
    • Connector Icon: Keep it as is or upload a custom image
    • Authentication Type: Credentials
OpenAI RESTful Connector
  1. Click the Next button to continue.
  2. Click the lonely "Add resource" button in the right corner to define services.
  3. Describe a completions resource with the parameters below:
    • Name: ChatCompletions
    • Path: /v1/chat/completions
    • Description: Put some meaningful description
New Resource Defintion
  1. Click on the POST check to unlock the method definition form.
  2. Under the Request tab, locate the "Document Type" drop-down and click the + icon.
  3. You could define the document structure manually, but the easiest way is to Load an existing JSON document and adjust it to your satisfaction. Click the "Load JSON" button on the "Data Fields" toolbar.
  4. Copy and paste the sample document below, adding or removing optional parameters. When ready, click the "Load JSON" button.
{
  "model": "gpt-4o-mini",
  "temperature": 0.7,
  "max_completion_tokens": 250,
  "stream": false,
  "messages": [
    {
      "role": "user",
      "content": "Say this is a test!"
    }
  ]
}

Load JSON Template Document

  1. Adjust data fields and make all but model and messages optional.
  2. Set the document type name, i.e., "GPT4_Request_Doc," and save the new document type.
Request Document Structure
  1. Click the "Save" button.
  2. Use the drop-down control to set the "Document Type" field to the GPT_Request_Doc.
  3. Click "Next" to describe the resource response.
  4. Set "HTTP Code" to the 200.
  5. Click on the + icon to define the resource response data structure. Use "Load JSON" to load the sample method reply from the OpenAI API documentation.
{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "gpt-4o-mini",
  "system_fingerprint": "fp_44709d6fcb",
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "\n\nHello there, how may I assist you today?"
    },
    "logprobs": null,
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21,
    "completion_tokens_details": {
      "reasoning_tokens": 0
    }
  }
}

Sample Chat Completion Response

  1. Name the new document type "GPT_Response_Doc," and save the reply structure.
💡
I skip the error response description, but it would be a good idea to define new document types for different error codes. When connector is saved you could return and improve the resource definition.
  1. Click the "Next" button to finish the resource description.
Completion POST Resource
  1. Click "Save" to store the custom connector changes.

Click on the "REST Connectors" breadcrumb to see our new connector line. Let's define a new authentication method and operation.

  1. Click on the icon next to the "GPT4_Connector" and then the "New Account" button on the right.
  2. Ensure that "Authorization Type" is set to "none" and click the "Add" button.
  3. Click on the "Operations" button on the right.
  4. Click the "New Operation" button to start the new operation detention.
  5. Select our authentication account "GPT4_Connector_1."
  6. Set the operation name, i.e, "askGPT", and click the "Next" button.
  7. Activate the radio button against the "postChatCompletions" and click on the "Headers" button.
Map a Custom Operation to Connector Resource.
  1. Add Headers as below and make sure they are active:
    • Content-Type: application/json
    • Authorization:
Custom Operation Headers
  1. Click the "Next" button twice to advance to the confirmation screen.
  2. Click the "Done" button to save the definition.

You could test the new operation if you have your OpenAI API key handy.

  1. In the list of operations, click on the play icon ▶️ for the "askGPT" operation.
  2. Select the connection account and fill out the input fields. It's important to set the Authorization header value to "Bearer ${Your API Key}".
  3. Put the model name into the "model" field.
  4. Add an entry to the "messages" list and set the "role" to "user" and your question or task for the model into content.
Set Operation Input Paramters
  1. Click the "Run" button to execute an operation. If you completed all operations correctly you should have reply similar to the screenshot below
Operation Output.

You may notice that copying and pasting your API key is not a secure approach, and you are absolutely correct.

That's why I keep my API key as Project parameter, defined as a password field. This approach allows you disassociate your workflows and connectors from credentials and accounts.

  1. Drop your new GPT4 connector to the workflow, select account and advance to the action configuration.
  2. Copy API Key from the project parameters to the Authorization header.
Mapping API Key to Header
  1. Set the operation input parameters, dynamic or otherwise.
  2. Click the "Next" button and test the connector.

As you may see Webmethods.io masking the API key value from all logs and user screens.

Masked API Key Value

This small exercise proves that even you do not have a predefined connector, you can always create a custom one and use it for your integration projects.