forked from HemulGM/DelphiOpenAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenAI.Utils.ChatHistory.pas
198 lines (175 loc) · 5.27 KB
/
OpenAI.Utils.ChatHistory.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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, Tag: string);
procedure NewFunc(const FuncName, FuncResult, Tag: string);
procedure NewTool(const Content, ToolCallId, Name, Tag: string);
procedure NewAsistantFunc(const FuncName, Args, Tag: string);
procedure NewAsistantTool(const Content: string; const ToolCalls: TArray<TChatToolCallBuild>; const Tag: string);
procedure SetContentByTag(const Tag, Text: 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;
procedure DeleteByTag(const Tag: string);
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.DeleteByTag(const Tag: string);
var
i: Integer;
begin
for i := 0 to Count - 1 do
if Items[i].Tag = Tag then
begin
Delete(i);
Exit;
end;
end;
procedure TChatHistory.SetContentByTag(const Tag, Text: string);
var
i: Integer;
Item: TChatMessageBuild;
begin
for i := 0 to Count - 1 do
if Items[i].Tag = Tag then
begin
Item := Items[i];
Item.Content := Text;
Items[i] := Item;
Exit;
end;
end;
procedure TChatHistory.New(Role: TMessageRole; Content, Tag: string);
var
Item: TChatMessageBuild;
begin
Item := TChatMessageBuild.Create(Role, Content);
Item.Tag := Tag;
Add(Item);
end;
procedure TChatHistory.NewAsistantFunc(const FuncName, Args, Tag: string);
var
Item: TChatMessageBuild;
begin
Item := TChatMessageBuild.AssistantFunc(FuncName, Args);
Item.Tag := Tag;
Add(Item);
end;
procedure TChatHistory.NewAsistantTool(const Content: string; const ToolCalls: TArray<TChatToolCallBuild>; const Tag: string);
var
Item: TChatMessageBuild;
begin
Item := TChatMessageBuild.AssistantTool(Content, ToolCalls);
Item.Tag := Tag;
Add(Item);
end;
procedure TChatHistory.NewFunc(const FuncName, FuncResult, Tag: string);
var
Item: TChatMessageBuild;
begin
Item := TChatMessageBuild.Func(FuncResult, FuncName);
Item.Tag := Tag;
Add(Item);
end;
procedure TChatHistory.NewTool(const Content, ToolCallId, Name, Tag: string);
var
Item: TChatMessageBuild;
begin
Item := TChatMessageBuild.Tool(Content, ToolCallId, Name);
Item.Tag := Tag;
Add(Item);
end;
procedure TChatHistory.Notify(const Item: TChatMessageBuild; Action: TCollectionNotification);
var
LastItem: TChatMessageBuild;
NeedLen: Int64;
begin
inherited;
if FAutoTrim and (Action = TCollectionNotification.cnAdded) then
begin
while TextLength + FMaxTokensForQuery > FMaxTokensOfModel do
if Count = 1 then
begin
LastItem := Items[0];
NeedLen := LastItem.Content.Length - FMaxTokensForQuery;
LastItem.Content := LastItem.Content.Substring(LastItem.Content.Length - NeedLen, NeedLen);
Items[0] := LastItem;
end
else
Delete(0);
end;
end;
procedure TChatHistory.SetAutoTrim(const Value: Boolean);
begin
FAutoTrim := Value;
end;
procedure TChatHistory.SetMaxTokensForQuery(const Value: Int64);
begin
FMaxTokensForQuery := Value;
if FMaxTokensForQuery <= 0 then
FMaxTokensForQuery := DEFULT_MAX_TOKENS;
end;
procedure TChatHistory.SetMaxTokensOfModel(const Value: Int64);
begin
FMaxTokensOfModel := Value;
if FMaxTokensOfModel <= 0 then
FMaxTokensOfModel := DEFULT_MODEL_TOKENS_LIMIT;
end;
procedure TChatHistory.SetOnCalcContentTokens(const Value: TOnCalcTokens);
begin
FOnCalcContentTokens := Value;
end;
function TChatHistory.TextLength: Int64;
var
ItemTokenCount: Int64;
Item: TChatMessageBuild;
begin
Result := 0;
if Assigned(FOnCalcContentTokens) then
for Item in Self do
begin
ItemTokenCount := 0;
FOnCalcContentTokens(Self, Item.Content, ItemTokenCount);
Inc(Result, ItemTokenCount);
end
else
begin
for Item in Self do
Inc(Result, Item.Content.Length);
end;
end;
end.