From Clomosy Docs
It is an object that provides access to OpenAI's artificial intelligence services. This type of component interacts with the OpenAI API to perform tasks such as natural language processing, text generation, or other AI-based operations.
| Feature | Use of | Definition |
|---|---|---|
| TclOpenAIEngine | OpenAIEngine1:TclOpenAIEngine; | A variable belonging to the TclOpenAIEngine class is created. |
| Create | OpenAIEngine1 = TclOpenAIEngine.Create(Self); | It creates an object of the TclOpenAIEngine class. |
| ParentForm | OpenAIEngine1.ParentForm = MyForm; | It is a property of the TclOpenAIEngine class, and this property represents the form to which the object is attached. |
| SetToken | OpenAIEngine1.SetToken(setToken : String); | The SetToken method must be defined for an object and the value provided as a parameter to this method represents a token used for authentication with the OpenAI service. |
| OnNewMessageEvent | OpenAIEngine1.OnNewMessageEvent = 'OnNewMessageEvent'; | It allows the assignment of an event handler to the 'OnNewMessageEvent' event. The 'OnNewMessageEvent' event provides the event that will be triggered when a new message is received from the object. |
| OnOpenAIExceptionEvent | OpenAIEngine1.OnOpenAIExceptionEvent = 'OnOpenAIExceptionEvent'; | It is an event handler that will be called when an error, exception, or special condition occurs. |
| SendAIMessage | OpenAIEngine1.SendAIMessage(MemMsg.Text); | The purpose of this method is to send an AI (artificial intelligence) message to the OpenAI service. The expression MemMsg.Text represents the content of the text to be sent, and this text forms the content of the artificial intelligence message. |
| NewMessageContent | OpenAIEngine1.NewMessageContent |
The NewMessageContent property represents the content of the latest artificial intelligence message received from the OpenAI service. |
The SetToken method must be defined for an object, and the value provided as a parameter to this method represents a token used for authentication with OpenAI services. For this reason, a token can be obtained for free by logging in at https://openai.com.
Example
var
MyForm:TclForm;
BtnSend:TclProButton;
MemMsg:TclMemo;
chatSectionMemo:TclMemo;
MyOpenAIEngine:TclOpenAIEngine;
bigPanel,middlePanel:TclProPanel;
bigLyt:TClLayout;
MyMQTT : TclMQTT;
void BtnSendClick;
{
if (MemMsg.Text == '')
{
ShowMessage('Write a message!');
}
else
{
MyOpenAIEngine.SendAIMessage(MemMsg.Text);
MemMsg.Text = '';
}
}
void OnNewMessageEvent;
{
if (not MyMQTT.ReceivedAlright)
{
chatSectionMemo.Lines.Add('');
MyMQTT.Send(MyOpenAIEngine.NewMessageContent);
chatSectionMemo.Lines.Add(MyOpenAIEngine.NewMessageContent);
chatSectionMemo.ScrollTo(0,chatSectionMemo.Lines.Count*chatSectionMemo.Lines.Count,True);
}
}
void MyMQTTPublishReceived;
{
If (MyMQTT.ReceivedAlright)
{
chatSectionMemo.Lines.Add('');
chatSectionMemo.Lines.Add(' ' + MyMQTT.ReceivedMessage);
chatSectionMemo.ScrollTo(0,chatSectionMemo.Lines.Count*chatSectionMemo.Lines.Count,True);
}
}
{
MyForm = TclForm.Create(Self);
bigLyt = MyForm.AddNewLayout(MyForm,'bigLyt');
bigLyt.Align=alContents;
bigLyt.Margins.Left=10;
bigLyt.Margins.Right=10;
bigLyt.Margins.Top=10;
bigLyt.Margins.Bottom=30;
MyMQTT = MyForm.AddNewMQTTConnection(MyForm,'MyMQTT');
MyForm.AddNewEvent(MyMQTT,tbeOnMQTTPublishReceived,'MyMQTTPublishReceived');
MyMQTT.Channel = 'ChatAI';
MyMQTT.Connect;
middlePanel=MyForm.AddNewProPanel(bigLyt,'middlePanel');
middlePanel.Align = AlTop;
middlePanel.Width = 300;
middlePanel.Height = 50;
middlePanel.Margins.Right = 10;
middlePanel.Margins.Top = 30;
middlePanel.Margins.Left = 10;
middlePanel.clProSettings.IsRound = True;
middlePanel.clProSettings.RoundHeight = 10;
middlePanel.clProSettings.RoundWidth = 10;
middlePanel.clProSettings.BorderColor = clAlphaColor.clHexToColor('#008000');
middlePanel.clProSettings.BorderWidth = 3;
middlePanel.SetclProSettings(middlePanel.clProSettings);
MemMsg= MyForm.AddNewMemo(middlePanel,'MemMsg','');
MemMsg.Align = alTop;
MemMsg.Margins.Right=10;
MemMsg.Margins.Left=10;
MemMsg.Margins.Top=10;
MemMsg.Margins.Bottom= 10;
bigPanel=MyForm.AddNewProPanel(bigLyt,'bigPanel');
bigPanel.Align = AlClient;
bigPanel.Margins.Right = 10;
bigPanel.Margins.Left = 10;
bigPanel.clProSettings.IsRound = True;
bigPanel.clProSettings.RoundHeight = 10;
bigPanel.clProSettings.RoundWidth = 10;
bigPanel.clProSettings.BorderColor = clAlphaColor.clHexToColor('#008000');
bigPanel.clProSettings.BorderWidth = 3;
bigPanel.SetclProSettings(bigPanel.clProSettings);
chatSectionMemo= MyForm.AddNewMemo(bigPanel,'chatSectionMemo','');
chatSectionMemo.Align = alClient;
chatSectionMemo.ReadOnly = True;
chatSectionMemo.Margins.Top= 10;
chatSectionMemo.Margins.Left= 10;
chatSectionMemo.Margins.Right =10;
chatSectionMemo.Margins.Bottom =10;
chatSectionMemo.TextSettings.Font.Size=26;
chatSectionMemo.TextSettings.WordWrap = True;
chatSectionMemo.EnabledScroll = True;
MyOpenAIEngine=TclOpenAIEngine.Create(Self);
MyOpenAIEngine.ParentForm = MyForm;
MyOpenAIEngine.SetToken('AIToken');
MyOpenAIEngine.OnNewMessageEvent = 'OnNewMessageEvent';
BtnSend = MyForm.AddNewProButton(bigLyt,'BtnSend','SEND');
BtnSend.Align = AlTop;
BtnSend.Margins.Right = 100;
BtnSend.Margins.Left = 100;
BtnSend.Margins.Bottom = 8;
BtnSend.Margins.Top = 8;
BtnSend.clProSettings.TextSettings.Font.Style = [fsBold];
BtnSend.clProSettings.IsRound = True;
BtnSend.clProSettings.RoundHeight = 2;
BtnSend.clProSettings.RoundWidth = 2;
BtnSend.clProSettings.BorderColor = clAlphaColor.clHexToColor('#808080');
BtnSend.clProSettings.BorderWidth = 2;
BtnSend.SetclProSettings(BtnSend.clProSettings);
MyForm.AddNewEvent(BtnSend,tbeOnClick,'BtnSendClick');
MyForm.Run;
}