-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Class to store chat history - TChatHistory
- Loading branch information
Showing
1 changed file
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|