webMethods.io: Transformation Service, part 2
We continue to build our transformation service for ChatGPT completion response. We can define service steps with the input and output documents in place.
Let's reiterate what are It's a good time to take a break and design transformation steps:
- We check if the incoming message contains ChatGPT completion responses and throw an error if it does not.
- For each completion message in the response list, complete the transformations:
- Trim trailing and leading spaces;
- Remove the outmost pair of quotes, single or double;
- Trim extra spaces after that.
- Drop all service attributes and return only optimized reply strings.
Let's replicate this little algorithm as a flow service.
- Open your webMethods.io integration project, switch to the Flow services tab on the left, and select the service we saved before.
- Start typing "if.." to populate the drop-down menu with matching options. Select the second one, "If ... Else ...". Your new line transforms into the conditional flow.
- Click on the "Choose from options" drop-down and select "choices" from the list of all objects. Pick "not exists" from the second drop-down to test if the list of messages is in the income message. The context menu icon on the right brings up the advanced expression builder to help you with complex evaluations.
- To finish execution, if the "choices" list does not exist, add the "Throw error ..." operator and put the error message to notify the caller that something has gone wrong.
- Every ChatGPT API call returns more than one completion to make announcements unique. To process all of them, click on step #4 and find the operation "Repeat."
- Keep it as "Repeat for each item in the list" and select "choices" from the drop-down.
- Step #5 is the innermost step in the loop and will be executed for each item in the completion response. Click on it and locate the "trim" function.
- Click the "View/Edit Pipeline" icon on the right to configure parameters.
- Expand the pipeline input on the right until you locate the "/choices/message/content" string.
- With the left mouse button, grab the "content" and drag it to the "inString" input parameter.
- Pull the output "value" to the same field on the left in the pipeline output.
- Let's save the service (the "floppy drive" icon in the main menu) and close the function pipeline ( the "✖️" icon in the right corner of the pipeline panel).
- Hover the mouse pointer over the loop line until a small blue ➕ icon appears. Click on it to add the new inner step.
- Find and select a function "replace." Save the service before we continue.
- Open the "replace" string pipeline configuration with the View/edit Pipeline icon on the right.
- Expand the pipeline input on the right until you locate the "/choices/message/content" string.
- Drag the "content" to the "inString" parameter and connect them.
- Map output "value" to the pipeline output "/choices/message/content."
- Set constants for the other parameters by right-clicking on the parameter name and choosing "Set value." Set parameters values as below:
- searchString =
^['"](.*?)["']?
- replaceString =
$1
- useRegex =
true
- Expand pipeline output data structures to see all completion fields. Using the "Expand/Collapse" controls right under pipeline headers, you could expand and collapse all items at once.
- Select all pipeline output documents and attributes except the "/choices/message/content" and drop them from the pipeline output. To do so
- Select a document or attribute and then use the drop filed ⤵︎ action.
- Or, right-click on the element name and select the "Drop field."
- Close the Replace pipeline and save your service.
- Right-click on step #5 and select "Duplicate" from the service context menu. You should have two identical function calls with all parameters in place.
- Right-click on the new line and select "Move step down." At this point, the repeat loop contains three pipelines.
- Select the "Exit" for step #8 by signaling "Success."
- Save your project progress.
I use three single-function pipelines instead of a single transform pipeline with three function calls because I need to apply those operations in a specific order. Separate service steps are numbered, and the engine guarantees the execution order. On the contrary, the transform pipeline steps are unordered, and the service engine will eventually do its best to execute them all.
Previous posts in the Flow service series: