Skip to content

Commit

Permalink
History for console quick chat, icons for components
Browse files Browse the repository at this point in the history
  • Loading branch information
HemulGM committed Jul 31, 2024
1 parent b869117 commit 380f04b
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 107 deletions.
Binary file added Media/openai.bmp
Binary file not shown.
Binary file added Media/openai.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Media/openai_chat.bmp
Binary file not shown.
Binary file added Media/openai_chat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Media/openai_func.bmp
Binary file not shown.
Binary file added Media/openai_func.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions OpenAI.Utils.ChatHistory.pas
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ implementation
constructor TChatHistory.Create;
begin
inherited;
FAutoTrim := False;
FMaxTokensForQuery := DEFULT_MAX_TOKENS;
FMaxTokensOfModel := DEFULT_MODEL_TOKENS_LIMIT;
end;
Expand Down
22 changes: 21 additions & 1 deletion OpenAI.pas
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,9 @@ procedure print(const Text: string; const NewLine: Boolean = True);

implementation

uses
OpenAI.Utils.ChatHistory;

procedure print(const Text: string; const NewLine: Boolean);
begin
if NewLine then
Expand All @@ -515,19 +518,31 @@ function input(const Print: string): string;
readln(Result);
end;

var
ConsoleHistory: TChatHistory = nil;

function chat(const Token, Prompt: string; const Model: string): string;
begin
if ConsoleHistory = nil then
begin
ConsoleHistory := TChatHistory.Create;
ConsoleHistory.AutoTrim := True;
end;
try
ConsoleHistory.New(TMessageRole.User, Prompt, TGUID.NewGuid.ToString);
var API: IOpenAI := TOpenAI.Create(Token);
var Chat := API.Chat.Create(
procedure(Params: TChatParams)
begin
Params.Model(Model);
Params.Messages([TChatMessageBuild.User(Prompt)]);
Params.Messages(ConsoleHistory.ToArray);
end);
try
if Length(Chat.Choices) > 0 then
begin
Result := Chat.Choices[0].Message.Content;
ConsoleHistory.New(TMessageRole.Assistant, Result, TGUID.NewGuid.ToString);
end;
finally
Chat.Free;
end;
Expand Down Expand Up @@ -1022,5 +1037,10 @@ procedure TOpenAIComponent.THTTPHeader.SetValue(const Value: string);
FValue := Value;
end;

initialization

finalization
ConsoleHistory.Free;

end.

19 changes: 10 additions & 9 deletions OpenAIPackage.dpk
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package OpenAIPackage;

{$R *.res}
{$R 'hIcons.dcr'}
{$IFDEF IMPLICITBUILDING This IFDEF should not be used by users}
{$ALIGN 8}
{$ASSERTIONS ON}
Expand Down Expand Up @@ -32,15 +33,16 @@ requires
RESTComponents;

contains
OpenAI.API.Params in 'OpenAI.API.Params.pas',
OpenAI in 'OpenAI.pas',
OpenAI.Types in 'OpenAI.Types.pas',
OpenAI.API in 'OpenAI.API.pas',
OpenAI.API.Params in 'OpenAI.API.Params.pas',
OpenAI.Assistants in 'OpenAI.Assistants.pas',
OpenAI.Audio in 'OpenAI.Audio.pas',
OpenAI.Chat in 'OpenAI.Chat.pas',
OpenAI.Chat.Functions in 'OpenAI.Chat.Functions.pas',
OpenAI.Chat.Functions.Samples in 'OpenAI.Chat.Functions.Samples.pas',
OpenAI.Chat in 'OpenAI.Chat.pas',
OpenAI.Completions in 'OpenAI.Completions.pas',
OpenAI.Component.Chat in 'OpenAI.Component.Chat.pas',
OpenAI.Component.Reg in 'OpenAI.Component.Reg.pas',
OpenAI.Edits in 'OpenAI.Edits.pas',
OpenAI.Embeddings in 'OpenAI.Embeddings.pas',
OpenAI.Engines in 'OpenAI.Engines.pas',
Expand All @@ -51,13 +53,12 @@ contains
OpenAI.Images in 'OpenAI.Images.pas',
OpenAI.Models in 'OpenAI.Models.pas',
OpenAI.Moderations in 'OpenAI.Moderations.pas',
OpenAI in 'OpenAI.pas',
OpenAI.Utils.Base64 in 'OpenAI.Utils.Base64.pas',
OpenAI.Utils.ChatHistory in 'OpenAI.Utils.ChatHistory.pas',
OpenAI.Utils.ObjectHolder in 'OpenAI.Utils.ObjectHolder.pas',
OpenAI.Component.Chat in 'OpenAI.Component.Chat.pas',
OpenAI.Component.Functions in 'OpenAI.Component.Functions.pas',
OpenAI.Assistants in 'OpenAI.Assistants.pas',
OpenAI.Types in 'OpenAI.Types.pas',
OpenAI.Utils.Base64 in 'OpenAI.Utils.Base64.pas',
OpenAI.Utils.ObjectHolder in 'OpenAI.Utils.ObjectHolder.pas';
OpenAI.Component.Reg in 'OpenAI.Component.Reg.pas';

end.

Expand Down
46 changes: 13 additions & 33 deletions OpenAIPackage.dproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<ProjectGuid>{AEF0734F-C1E6-4A7A-BC68-D170EF3FE99F}</ProjectGuid>
<MainSource>OpenAIPackage.dpk</MainSource>
<ProjectVersion>20.1</ProjectVersion>
<ProjectVersion>20.2</ProjectVersion>
<FrameworkType>None</FrameworkType>
<Base>True</Base>
<Config Condition="'$(Config)'==''">Debug</Config>
Expand Down Expand Up @@ -164,17 +164,19 @@
<DelphiCompile Include="$(MainSource)">
<MainSource>MainSource</MainSource>
</DelphiCompile>
<DCCReference Include="hIcons.dcr"/>
<DCCReference Include="rtl.dcp"/>
<DCCReference Include="RESTComponents.dcp"/>
<DCCReference Include="OpenAI.API.Params.pas"/>
<DCCReference Include="OpenAI.pas"/>
<DCCReference Include="OpenAI.Types.pas"/>
<DCCReference Include="OpenAI.API.pas"/>
<DCCReference Include="OpenAI.API.Params.pas"/>
<DCCReference Include="OpenAI.Assistants.pas"/>
<DCCReference Include="OpenAI.Audio.pas"/>
<DCCReference Include="OpenAI.Chat.pas"/>
<DCCReference Include="OpenAI.Chat.Functions.pas"/>
<DCCReference Include="OpenAI.Chat.Functions.Samples.pas"/>
<DCCReference Include="OpenAI.Chat.pas"/>
<DCCReference Include="OpenAI.Completions.pas"/>
<DCCReference Include="OpenAI.Component.Chat.pas"/>
<DCCReference Include="OpenAI.Component.Reg.pas"/>
<DCCReference Include="OpenAI.Edits.pas"/>
<DCCReference Include="OpenAI.Embeddings.pas"/>
<DCCReference Include="OpenAI.Engines.pas"/>
Expand All @@ -185,13 +187,12 @@
<DCCReference Include="OpenAI.Images.pas"/>
<DCCReference Include="OpenAI.Models.pas"/>
<DCCReference Include="OpenAI.Moderations.pas"/>
<DCCReference Include="OpenAI.pas"/>
<DCCReference Include="OpenAI.Utils.ChatHistory.pas"/>
<DCCReference Include="OpenAI.Component.Functions.pas"/>
<DCCReference Include="OpenAI.Assistants.pas"/>
<DCCReference Include="OpenAI.Types.pas"/>
<DCCReference Include="OpenAI.Utils.Base64.pas"/>
<DCCReference Include="OpenAI.Utils.ChatHistory.pas"/>
<DCCReference Include="OpenAI.Utils.ObjectHolder.pas"/>
<DCCReference Include="OpenAI.Component.Chat.pas"/>
<DCCReference Include="OpenAI.Component.Functions.pas"/>
<DCCReference Include="OpenAI.Component.Reg.pas"/>
<BuildConfiguration Include="Base">
<Key>Base</Key>
</BuildConfiguration>
Expand All @@ -213,7 +214,7 @@
<Source Name="MainSource">OpenAIPackage.dpk</Source>
</Source>
</Delphi.Personality>
<Deployment Version="4">
<Deployment Version="5">
<DeployFile LocalName="$(BDS)\Redist\iossimulator\libcgunwind.1.0.dylib" Class="DependencyModule">
<Platform Name="iOSSimulator">
<Overwrite>true</Overwrite>
Expand All @@ -229,12 +230,7 @@
<Overwrite>true</Overwrite>
</Platform>
</DeployFile>
<DeployFile LocalName="C:\Users\Public\Documents\Embarcadero\Studio\23.0\Bpl\OpenAIPackage.bpl" Configuration="Debug" Class="ProjectOutput">
<Platform Name="Win32">
<RemoteName>OpenAIPackage.bpl</RemoteName>
<Overwrite>true</Overwrite>
</Platform>
</DeployFile>
<DeployFile LocalName="C:\Users\Public\Documents\Embarcadero\Studio\23.0\Bpl\OpenAIPackage.bpl" Configuration="Debug" Class="ProjectOutput"/>
<DeployClass Name="AdditionalDebugSymbols">
<Platform Name="OSX32">
<Operation>1</Operation>
Expand All @@ -243,16 +239,6 @@
<Operation>0</Operation>
</Platform>
</DeployClass>
<DeployClass Name="AndroidClasses">
<Platform Name="Android">
<RemoteDir>classes</RemoteDir>
<Operation>64</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>classes</RemoteDir>
<Operation>64</Operation>
</Platform>
</DeployClass>
<DeployClass Name="AndroidFileProvider">
<Platform Name="Android">
<RemoteDir>res\xml</RemoteDir>
Expand All @@ -263,12 +249,6 @@
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="AndroidGDBServer">
<Platform Name="Android">
<RemoteDir>library\lib\armeabi-v7a</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="AndroidLibnativeArmeabiFile">
<Platform Name="Android">
<RemoteDir>library\lib\armeabi</RemoteDir>
Expand Down
10 changes: 6 additions & 4 deletions Samples/Chat/Chat.Main.fmx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ object FormChat: TFormChat
Viewport.Width = 501.000000000000000000
Viewport.Height = 92.000000000000000000
end
object Button1: TButton
object ButtonAttach: TButton
Hint = 'Attach file'
Position.X = 521.000000000000000000
Position.Y = 233.000000000000000000
Size.Width = 24.000000000000000000
Expand All @@ -89,9 +90,10 @@ object FormChat: TFormChat
TabOrder = 12
Text = '+'
TextSettings.Trimming = None
OnClick = Button1Click
OnClick = ButtonAttachClick
end
object Button2: TButton
object ButtonRemoveAttach: TButton
Hint = 'Remove attach'
Position.X = 521.000000000000000000
Position.Y = 265.000000000000000000
Size.Width = 24.000000000000000000
Expand Down Expand Up @@ -9145,7 +9147,7 @@ object FormChat: TFormChat
'ype": "string", "description": "The city and state, e.g. San Fra' +
'ncisco, CA"},'#13#10' "unit": {"type": "string", "enum": ["celsiu' +
's", "fahrenheit"]}'#13#10' },'#13#10' "required": ["location"]'#13#10'}'
OnFunctionExecute = OpenAIChatFunctions1Items0FunctionExecute
OnFunctionExecute = FuncGetCurrentWeather
end>
Left = 384
Top = 104
Expand Down
14 changes: 6 additions & 8 deletions Samples/Chat/Chat.Main.pas
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ TFormChat = class(TForm)
AniIndicatorBusy: TAniIndicator;
OpenAIChatFunctions1: TOpenAIChatFunctions;
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
ButtonAttach: TButton;
ButtonRemoveAttach: TButton;
StyleBook1: TStyleBook;
OpenDialogImg: TOpenDialog;
procedure FormCreate(Sender: TObject);
Expand All @@ -33,8 +33,8 @@ TFormChat = class(TForm)
procedure OpenAIChat1BeginWork(Sender: TObject);
procedure OpenAIChat1ChatDelta(Sender: TObject; Event: TChat; IsDone: Boolean);
procedure OpenAIChat1EndWork(Sender: TObject);
procedure OpenAIChatFunctions1Items0FunctionExecute(Sender: TObject; const Args: string; out Result: string);
procedure Button1Click(Sender: TObject);
procedure FuncGetCurrentWeather(Sender: TObject; const Args: string; out Result: string);
procedure ButtonAttachClick(Sender: TObject);
private
{ Private declarations }
public
Expand All @@ -51,7 +51,7 @@ implementation

{$R *.fmx}

procedure TFormChat.Button1Click(Sender: TObject);
procedure TFormChat.ButtonAttachClick(Sender: TObject);
begin
if OpenDialogImg.Execute then
begin
Expand Down Expand Up @@ -90,8 +90,6 @@ procedure TFormChat.ButtonStreamSendClick(Sender: TObject);

procedure TFormChat.FormCreate(Sender: TObject);
begin
//
//OpenAIClient1.Assistants.Retrieve('')
OpenAIClient1.Token := {$INCLUDE token.txt};
end;

Expand Down Expand Up @@ -132,7 +130,7 @@ procedure TFormChat.OpenAIChat1Error(Sender: TObject; Error: Exception);
MemoMessages.Lines.Add('');
end;

procedure TFormChat.OpenAIChatFunctions1Items0FunctionExecute(Sender: TObject; const Args: string; out Result: string);
procedure TFormChat.FuncGetCurrentWeather(Sender: TObject; const Args: string; out Result: string);
var
JSON: TJSONObject;
Location: string;
Expand Down
38 changes: 6 additions & 32 deletions Samples/Chat/SimpleChat.dproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectGuid>{BFDEE0F1-0647-4DC4-B516-5699A40BC0F4}</ProjectGuid>
<ProjectVersion>20.1</ProjectVersion>
<ProjectVersion>20.2</ProjectVersion>
<FrameworkType>FMX</FrameworkType>
<Base>True</Base>
<Config Condition="'$(Config)'==''">Debug</Config>
Expand Down Expand Up @@ -116,7 +116,7 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Base_Android)'!=''">
<DCC_UsePackage>fmx;DbxCommonDriver;bindengine;IndyIPCommon;emsclient;FireDACCommonDriver;FrameStandPackage_11_1;IndyProtocols;Skia.Package.RTL;RadiantShapesFmx_Design;IndyIPClient;dbxcds;FmxTeeUI;bindcompfmx;FireDACSqliteDriver;DbxClientDriver;soapmidas;fmxFireDAC;dbexpress;inet;DataSnapCommon;FMXAudioHGM;fmxase;DzHTMLText_FMX;dbrtl;FireDACDBXDriver;Skia.Package.FMX;CustomIPTransport;DBXInterBaseDriver;IndySystem;RadiantShapesFmx;bindcomp;FireDACCommon;VLCPlayer;IndyCore;RESTBackendComponents;bindcompdbx;rtl;RESTComponents;DBXSqliteDriver;IndyIPServer;dsnapxml;DataSnapClient;DataSnapProviderClient;DataSnapFireDAC;emsclientfiredac;FireDAC;FireDACDSDriver;xmlrtl;tethering;dsnap;CloudService;DataSnapNativeClient;FMXTee;soaprtl;soapserver;FireDACIBDriver;$(DCC_UsePackage)</DCC_UsePackage>
<VerInfo_Keys>package=com.embarcadero.$(MSBuildProjectName);label=$(MSBuildProjectName);versionCode=1;versionName=1.0.0;persistent=False;restoreAnyVersion=False;installLocation=auto;largeHeap=False;theme=TitleBar;hardwareAccelerated=true;apiKey=</VerInfo_Keys>
<VerInfo_Keys>package=com.embarcadero.$(MSBuildProjectName);label=$(MSBuildProjectName);versionCode=1;versionName=1.0.0;persistent=False;restoreAnyVersion=False;installLocation=auto;largeHeap=False;theme=TitleBar;hardwareAccelerated=true;apiKey=;minSdkVersion=23;targetSdkVersion=34</VerInfo_Keys>
<BT_BuildType>Debug</BT_BuildType>
<VerInfo_IncludeVerInfo>true</VerInfo_IncludeVerInfo>
<Android_LauncherIcon36>$(BDS)\bin\Artwork\Android\FM_LauncherIcon_36x36.png</Android_LauncherIcon36>
Expand All @@ -138,7 +138,7 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Base_Android64)'!=''">
<DCC_UsePackage>fmx;DbxCommonDriver;bindengine;IndyIPCommon;emsclient;FireDACCommonDriver;FrameStandPackage_11_1;IndyProtocols;Skia.Package.RTL;RadiantShapesFmx_Design;IndyIPClient;dbxcds;FmxTeeUI;bindcompfmx;FireDACSqliteDriver;DbxClientDriver;soapmidas;fmxFireDAC;dbexpress;inet;DataSnapCommon;FMXAudioHGM;DzHTMLText_FMX;dbrtl;FireDACDBXDriver;Skia.Package.FMX;CustomIPTransport;DBXInterBaseDriver;IndySystem;RadiantShapesFmx;bindcomp;FireDACCommon;VLCPlayer;IndyCore;RESTBackendComponents;bindcompdbx;rtl;RESTComponents;DBXSqliteDriver;IndyIPServer;dsnapxml;DataSnapClient;DataSnapProviderClient;DataSnapFireDAC;emsclientfiredac;FireDAC;FireDACDSDriver;xmlrtl;tethering;dsnap;CloudService;DataSnapNativeClient;FMXTee;soaprtl;soapserver;FireDACIBDriver;$(DCC_UsePackage)</DCC_UsePackage>
<VerInfo_Keys>package=com.embarcadero.$(MSBuildProjectName);label=$(MSBuildProjectName);versionCode=1;versionName=1.0.0;persistent=False;restoreAnyVersion=False;installLocation=auto;largeHeap=False;theme=TitleBar;hardwareAccelerated=true;apiKey=</VerInfo_Keys>
<VerInfo_Keys>package=com.embarcadero.$(MSBuildProjectName);label=$(MSBuildProjectName);versionCode=1;versionName=1.0.0;persistent=False;restoreAnyVersion=False;installLocation=auto;largeHeap=False;theme=TitleBar;hardwareAccelerated=true;apiKey=;minSdkVersion=23;targetSdkVersion=34</VerInfo_Keys>
<BT_BuildType>Debug</BT_BuildType>
<VerInfo_IncludeVerInfo>true</VerInfo_IncludeVerInfo>
<Android_LauncherIcon36>$(BDS)\bin\Artwork\Android\FM_LauncherIcon_36x36.png</Android_LauncherIcon36>
Expand Down Expand Up @@ -331,7 +331,7 @@
<Source Name="MainSource">SimpleChat.dpr</Source>
</Source>
</Delphi.Personality>
<Deployment Version="4">
<Deployment Version="5">
<DeployFile LocalName="$(BDS)\Redist\iossimulator\libcgunwind.1.0.dylib" Class="DependencyModule">
<Platform Name="iOSSimulator">
<Overwrite>true</Overwrite>
Expand All @@ -347,18 +347,8 @@
<Overwrite>true</Overwrite>
</Platform>
</DeployFile>
<DeployFile LocalName="Win32\Debug\SimpleChat.exe" Configuration="Debug" Class="ProjectOutput">
<Platform Name="Win32">
<RemoteName>SimpleChat.exe</RemoteName>
<Overwrite>true</Overwrite>
</Platform>
</DeployFile>
<DeployFile LocalName="token.txt" Configuration="Debug" Class="ProjectFile">
<Platform Name="Win32">
<RemoteDir>.\</RemoteDir>
<Overwrite>true</Overwrite>
</Platform>
</DeployFile>
<DeployFile LocalName="Win32\Debug\SimpleChat.exe" Configuration="Debug" Class="ProjectOutput"/>
<DeployFile LocalName="token.txt" Configuration="Debug" Class="ProjectFile"/>
<DeployClass Name="AdditionalDebugSymbols">
<Platform Name="iOSSimulator">
<Operation>1</Operation>
Expand All @@ -371,16 +361,6 @@
<Operation>0</Operation>
</Platform>
</DeployClass>
<DeployClass Name="AndroidClasses">
<Platform Name="Android">
<RemoteDir>classes</RemoteDir>
<Operation>64</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>classes</RemoteDir>
<Operation>64</Operation>
</Platform>
</DeployClass>
<DeployClass Name="AndroidFileProvider">
<Platform Name="Android">
<RemoteDir>res\xml</RemoteDir>
Expand All @@ -391,12 +371,6 @@
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="AndroidGDBServer">
<Platform Name="Android">
<RemoteDir>library\lib\armeabi-v7a</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="AndroidLibnativeArmeabiFile">
<Platform Name="Android">
<RemoteDir>library\lib\armeabi</RemoteDir>
Expand Down
2 changes: 1 addition & 1 deletion Samples/QuickChat/QuickChat.dpr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
uses OpenAI;

begin
repeat print('GPT: ' + chat('insert token', input('You: '))) until False;
repeat print('GPT: ' + chat('token', input('You: '))) until False;
end.
Loading

0 comments on commit 380f04b

Please sign in to comment.