Solution
Use a VB or C# function that calls an OpenAI service via a web call.
Example We want to give the user the option of improving their entered long text via OpenAI, for example the description text in a notification:
After entering the description, the user can press the button in the bottom right corner to improve the text, including correcting typos.
:
In most cases, pressing the “Improve” button again will result in a variant of the text that the user may like better:
It is reasonable to include an "Undo" button as well. While it's not implemented in this example—since our focus is on the OpenAI interface—it can be added without much difficulty by saving and restoring the GuiXT text variables, see
Undo and Redo buttons.
// Customer complaint ?
if Q[Transaction=QM01] or Q[Transaction=QM02]
// notification type Q1 ?
Set V[QM_notification_type] "&F[RIWO00-QMARTE]"
if V[QM_notification_type=Q1]
// add button to improve text via OpenAI
PushButton X[Text]+(5,78) _
"@IY\QImprove the text via OpenAI@" _
process="qm_improve_text.txt" size=(1,2)
endif
endif
InputScript "qm_improve_text.txt":
GuiXT
CopyText fromScreen=X[Text] toText="QM_longtext"
CallVB result = openai.openaihelper.askGPT _
"Can you please improve the following notification text
and only return the new text? &text[QM_longtext]"
if V[result]
CopyText fromString="result" toText="QM_longtext"
CopyText fromText="QM_longtext" toScreen=X[Text]
endif
Return
VB implementation:
VB.NET
Imports System.Collections.Generic
Imports System.Net.Http
Imports System.Security.Authentication
Imports System.Text
Imports System.Web.Script.Serialization
Public Class OpenAIHelper
Public apiKey As String = "sk-proj-4drnVX..."
Public apiUrl As String =
"https://api.openai.com/v1/chat/completions"
Public Function askGPT(question) As String
Dim myclient = New HttpClient _
(New HttpClientHandler With {.SslProtocols = SslProtocols.Tls12})
' add our openai API key in request header
myclient.DefaultRequestHeaders.Add _
("Authorization", $"Bearer {apiKey}")
Dim webRequest = New HttpRequestMessage(HttpMethod.Post, apiUrl)
Dim jsonPayload As String = "
{
""model"": ""gpt-4.1-mini"",
""messages"": [
{
""role"": ""user"",
""content"": ""$$question""
}
]
}"
' duplicate " within question
Dim questionstring As String = question.replace("""", """""")
' replace the line breaks
questionstring = questionstring. _
replace(vbCrLf, "\n").replace(vbLf, "\n")
webRequest.Content =
New StringContent(jsonPayload.
Replace("$$question", questionstring),
Encoding.UTF8, "application/json")
' we use blocking calls, since we need to wait for the answer
Dim response As HttpResponseMessage =
myclient.SendAsync(webRequest).Result
Dim content As String =
response.Content.ReadAsStringAsync().Result
' return message content from JSON-type result string
Dim contentdict As Dictionary(Of String, Object) =
New JavaScriptSerializer().DeserializeObject(content)
Dim result = contentdict("choices")(0)("message")("content")
Return result
End Function
End Class
using System.Collections.Generic;
using System.Net.Http;
using System.Security.Authentication;
using System.Text;
using System.Web.Script.Serialization;
public class OpenAIHelper
{
public string apiKey = "sk-proj-4drnVXbqqTFix9AbK...";
public string apiUrl = "https://api.openai.com/v1/chat/completions";
public string askGPT(string question)
{
var myclient = new HttpClient(new HttpClientHandler()
{ SslProtocols = SslProtocols.Tls12 });
// add our openai API key in request header
myclient.DefaultRequestHeaders.Add
("Authorization", $"Bearer {apiKey}");
var webRequest = new HttpRequestMessage(HttpMethod.Post, apiUrl);
string jsonPayload = @"
{
""model"": ""gpt-4.1-mini"",
""messages"": [
{
""role"": ""user"",
""content"": ""$$question""
}
]
}";
// duplicate " within question
string questionstring = question.Replace("\"", "\"\"");
// replace the line breaks
questionstring = questionstring
.Replace("\r\n", @"\n").Replace("\n", @"\n");
webRequest.Content = new StringContent(jsonPayload
.Replace("$$question", questionstring), Encoding.UTF8,
"application/json");
// we use blocking calls, since we need to wait for the answer
HttpResponseMessage response = myclient.SendAsync(webRequest).Result;
string content = response.Content.ReadAsStringAsync().Result;
// return message content from JSON-type result string
var contentdict = new JavaScriptSerializer()
.Deserialize<Dictionary<string, object>>(content);
var choices = (System.Collections.ArrayList)contentdict["choices"];
var firstChoice = (Dictionary)choices[0];
var message = (Dictionary)firstChoice["message"];
string result = (string) message["content"];
// Replace vbLf with vbCrLf, but only vbLf NOT already part of vbCrLf
result = System.Text.RegularExpressions.Regex
.Replace(result, @"(?<!\r)\n", "\r\n");
return result;
}
}