-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSophora.Messages.pas
160 lines (134 loc) · 3.2 KB
/
Sophora.Messages.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
{===============================================================================
___ _
/ __| ___ _ __| |_ ___ _ _ __ _ ™
\__ \/ _ \ '_ \ ' \/ _ \ '_/ _` |
|___/\___/ .__/_||_\___/_| \__,_|
|_|
AI Reasoning, Function-calling &
Knowledge Retrieval
Copyright © 2025-present tinyBigGAMES™ LLC
All Rights Reserved.
https://github.com/tinyBigGAMES/Sophora
See LICENSE file for license information
===============================================================================}
unit Sophora.Messages;
{$I Sophora.Defines.inc}
interface
uses
System.SysUtils,
System.StrUtils,
System.Classes,
Sophora.CLibs,
Sophora.Utils,
Sophora.Common;
const
soSystem = 'system';
soUser = 'user';
soAssistant = 'assistant';
soTool = 'tool';
type
{ TsoMessages }
TsoMessages = class(TsoBaseObject)
protected
FList: TStringList;
FLastUser: string;
FAddEnd: Boolean;
public
constructor Create(); override;
destructor Destroy(); override;
procedure Clear(); virtual;
procedure AddRaw(const AContent: string); virtual;
procedure Add(const ARole, AContent: string); virtual;
procedure AddEnd(); virtual;
function Prompt(): string; virtual;
function IsEmpty: Boolean; virtual;
function LastUser(): string;
end;
implementation
{ TsoMessages }
constructor TsoMessages.Create();
begin
inherited;
FList := TStringList.Create();
Clear();
end;
destructor TsoMessages.Destroy();
begin
FList.Free();
inherited;
end;
procedure TsoMessages.Clear();
begin
FList.Clear();
FLastUser := '';
FAddEnd := False;
end;
procedure TsoMessages.AddRaw(const AContent: string);
var
LContent: string;
begin
LContent := AContent.Trim();
if LContent.IsEmpty then Exit;
FList.Add(AContent.Trim);
end;
procedure TsoMessages.Add(const ARole, AContent: string);
var
LRole: string;
LContent: string;
LMsg: string;
begin
LRole := ARole.Trim();
LContent := AContent.Trim();
if LRole.IsEmpty then Exit;
if LContent.IsEmpty then Exit;
if SameText(LRole, soSystem) then
begin
LMsg := '<|start_header_id|>system<|end_header_id|>\n' + LContent + '<|eot_id|>';
FAddEnd := False;
end
else
if SameText(LRole, soUser) then
begin
LMsg := '<|start_header_id|>user<|end_header_id|>\n' + LContent + '<|eot_id|>';
FLastUser := LContent;
FAddEnd := True;
end
else
if SameText(LRole, soAssistant) then
begin
LMsg := '<|start_header_id|>assistant<|end_header_id|>\n' + LContent + '<|eot_id|>';
FAddEnd := False;
end
else
if SameText(LRole, soTool) then
begin
LMsg := '<|start_header_id|>tool<|end_header_id|>\n' + LContent + '<|eot_id|>';
FAddEnd := True;
end;
AddRaw(LMsg);
end;
procedure TsoMessages.AddEnd();
begin
if FAddEnd then
AddRaw('<|start_header_id|>assistant<|end_header_id|>');
end;
function TsoMessages.Prompt(): string;
var
LItem: string;
begin
Result := '';
for LItem in FList do
begin
Result := Result + LItem + ' ';
end;
Result := Result.Trim();
end;
function TsoMessages.IsEmpty: Boolean;
begin
Result := Prompt().IsEmpty;
end;
function TsoMessages.LastUser(): string;
begin
Result := FLastUser;
end;
end.