Skip to content

Commit

Permalink
Class to store chat history - TChatHistory
Browse files Browse the repository at this point in the history
  • Loading branch information
HemulGM committed Mar 3, 2023
1 parent 1da2395 commit 8475ec1
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions OpenAI.Utils.ChatHistory.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
unit OpenAI.Utils.ChatHistory;

interface

uses
System.SysUtils, System.Generics.Collections, OpenAI.Chat;

type
TOnCalcTokens = procedure(Sender: TObject; const Content: string; var TokenCount: Int64) of object;

/// <summary>
/// This class is used to store chat history.
/// It can automatically delete previous messages if the total message size exceeds the specified number of tokens.
/// <br>
/// Use the ToArray method to pass the history to the Chat.Create request parameters
/// </summary>
TChatHistory = class(TList<TChatMessageBuild>)
private
FAutoTrim: Boolean;
FMaxTokensForQuery: Int64;
FMaxTokensOfModel: Int64;
FOnCalcContentTokens: TOnCalcTokens;
procedure SetAutoTrim(const Value: Boolean);
procedure SetMaxTokensForQuery(const Value: Int64);
procedure SetMaxTokensOfModel(const Value: Int64);
procedure SetOnCalcContentTokens(const Value: TOnCalcTokens);
protected
procedure Notify(const Item: TChatMessageBuild; Action: TCollectionNotification); override;
public
procedure New(Role: TMessageRole; Content: string);
function TextLength: Int64;
property AutoTrim: Boolean read FAutoTrim write SetAutoTrim;
property MaxTokensForQuery: Int64 read FMaxTokensForQuery write SetMaxTokensForQuery;
property MaxTokensOfModel: Int64 read FMaxTokensOfModel write SetMaxTokensOfModel;
property OnCalcContentTokens: TOnCalcTokens read FOnCalcContentTokens write SetOnCalcContentTokens;
constructor Create;
end;

const
DEFULT_MAX_TOKENS = 1024;
DEFULT_MODEL_TOKENS_LIMIT = 4096;

implementation

{ TChatHistory }

constructor TChatHistory.Create;
begin
inherited;
FMaxTokensForQuery := DEFULT_MAX_TOKENS;
FMaxTokensOfModel := DEFULT_MODEL_TOKENS_LIMIT;
end;

procedure TChatHistory.New(Role: TMessageRole; Content: string);
begin
Add(TChatMessageBuild.Create(Role, Content));
end;

procedure TChatHistory.Notify(const Item: TChatMessageBuild; Action: TCollectionNotification);
begin
inherited;
if FAutoTrim and (Action = TCollectionNotification.cnAdded) then
begin
while TextLength + FMaxTokensForQuery > FMaxTokensOfModel do
Delete(0);
end;
end;

procedure TChatHistory.SetAutoTrim(const Value: Boolean);
begin
FAutoTrim := Value;
end;

procedure TChatHistory.SetMaxTokensForQuery(const Value: Int64);
begin
FMaxTokensForQuery := Value;
end;

procedure TChatHistory.SetMaxTokensOfModel(const Value: Int64);
begin
FMaxTokensOfModel := Value;
end;

procedure TChatHistory.SetOnCalcContentTokens(const Value: TOnCalcTokens);
begin
FOnCalcContentTokens := Value;
end;

function TChatHistory.TextLength: Int64;
var
ItemTokenCount: Int64;
begin
Result := 0;
if Assigned(FOnCalcContentTokens) then
for var Item in Self do
begin
ItemTokenCount := 0;
FOnCalcContentTokens(Self, Item.Content, ItemTokenCount);
end
else
begin
for var Item in Self do
Inc(Result, Item.Content.Length);
end;
end;

end.

0 comments on commit 8475ec1

Please sign in to comment.