Skip to content

Commit 6f4e703

Browse files
committed
add-TokenUsageTracking
1 parent 992c0e5 commit 6f4e703

6 files changed

Lines changed: 1476 additions & 33 deletions

File tree

Samples/TTS_STT_Translate/README.md

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
object TokenUsageTrackingMainView: TTokenUsageTrackingMainView
2+
Left = 0
3+
Top = 0
4+
Caption = 'AI in practice: Token Usage Tracking'
5+
ClientHeight = 698
6+
ClientWidth = 845
7+
Color = clBtnFace
8+
Font.Charset = DEFAULT_CHARSET
9+
Font.Color = clWindowText
10+
Font.Height = -12
11+
Font.Name = 'Segoe UI'
12+
Font.Style = []
13+
KeyPreview = True
14+
Position = poScreenCenter
15+
OnCreate = FormCreate
16+
TextHeight = 15
17+
object Splitter1: TSplitter
18+
Left = 0
19+
Top = 314
20+
Width = 845
21+
Height = 8
22+
Cursor = crVSplit
23+
Align = alTop
24+
Beveled = True
25+
ExplicitTop = 161
26+
ExplicitWidth = 780
27+
end
28+
object pnTop: TPanel
29+
Left = 0
30+
Top = 0
31+
Width = 845
32+
Height = 273
33+
Align = alTop
34+
BevelInner = bvLowered
35+
TabOrder = 0
36+
object pnButtonsTop: TPanel
37+
Left = 2
38+
Top = 2
39+
Width = 841
40+
Height = 39
41+
Align = alTop
42+
TabOrder = 0
43+
object ProgressBar1: TProgressBar
44+
AlignWithMargins = True
45+
Left = 5
46+
Top = 9
47+
Width = 129
48+
Height = 21
49+
Margins.Left = 4
50+
Margins.Top = 8
51+
Margins.Right = 4
52+
Margins.Bottom = 8
53+
Align = alLeft
54+
Smooth = True
55+
Style = pbstMarquee
56+
SmoothReverse = True
57+
State = pbsPaused
58+
TabOrder = 0
59+
ExplicitLeft = 197
60+
end
61+
end
62+
object gBoxTanscription: TGroupBox
63+
Left = 2
64+
Top = 41
65+
Width = 841
66+
Height = 230
67+
Align = alClient
68+
Caption = ' Audio transcription '
69+
Padding.Left = 1
70+
Padding.Right = 1
71+
Padding.Bottom = 1
72+
TabOrder = 1
73+
object mmTanscription: TMemo
74+
Left = 3
75+
Top = 17
76+
Width = 835
77+
Height = 210
78+
Align = alClient
79+
BorderStyle = bsNone
80+
Lines.Strings = (
81+
'')
82+
TabOrder = 0
83+
end
84+
end
85+
end
86+
object gBoxResponse: TGroupBox
87+
Left = 0
88+
Top = 322
89+
Width = 845
90+
Height = 376
91+
Align = alClient
92+
Caption = ' Response '
93+
Padding.Left = 1
94+
Padding.Right = 1
95+
Padding.Bottom = 1
96+
TabOrder = 1
97+
object mmResponse: TMemo
98+
Left = 3
99+
Top = 17
100+
Width = 839
101+
Height = 333
102+
Align = alClient
103+
BorderStyle = bsNone
104+
ScrollBars = ssVertical
105+
TabOrder = 0
106+
end
107+
object pnResponseDetails: TPanel
108+
Left = 3
109+
Top = 350
110+
Width = 839
111+
Height = 23
112+
Align = alBottom
113+
BevelEdges = [beTop]
114+
BevelKind = bkTile
115+
BevelOuter = bvNone
116+
TabOrder = 1
117+
object Label9: TLabel
118+
Left = 174
119+
Top = 0
120+
Width = 93
121+
Height = 21
122+
Hint = 'N'#186' de tokens que a resposta cont'#233'm: '
123+
Align = alLeft
124+
Caption = 'Tokens response: '
125+
ParentShowHint = False
126+
ShowHint = True
127+
Layout = tlCenter
128+
ExplicitHeight = 15
129+
end
130+
object lbNumTokensResponse: TLabel
131+
Left = 267
132+
Top = 0
133+
Width = 90
134+
Height = 21
135+
Align = alLeft
136+
AutoSize = False
137+
Caption = '0'
138+
Layout = tlCenter
139+
ExplicitLeft = 370
140+
end
141+
object Label10: TLabel
142+
Left = 0
143+
Top = 0
144+
Width = 84
145+
Height = 21
146+
Align = alLeft
147+
Caption = 'Prompt tokens: '
148+
Layout = tlCenter
149+
ExplicitHeight = 15
150+
end
151+
object lbPromptTokens: TLabel
152+
Left = 84
153+
Top = 0
154+
Width = 90
155+
Height = 21
156+
Align = alLeft
157+
AutoSize = False
158+
Caption = '0'
159+
Layout = tlCenter
160+
ExplicitLeft = 83
161+
end
162+
object Label11: TLabel
163+
Left = 517
164+
Top = 0
165+
Width = 90
166+
Height = 21
167+
Align = alLeft
168+
Caption = 'Model response: '
169+
Layout = tlCenter
170+
ExplicitHeight = 15
171+
end
172+
object lbServiceModel: TLabel
173+
Left = 607
174+
Top = 0
175+
Width = 5
176+
Height = 21
177+
Align = alLeft
178+
Caption = '-'
179+
Layout = tlCenter
180+
ExplicitHeight = 15
181+
end
182+
object Label12: TLabel
183+
Left = 357
184+
Top = 0
185+
Width = 70
186+
Height = 21
187+
Align = alLeft
188+
Caption = 'Total tokens: '
189+
Layout = tlCenter
190+
ExplicitHeight = 15
191+
end
192+
object lbTotalTokens: TLabel
193+
Left = 427
194+
Top = 0
195+
Width = 90
196+
Height = 21
197+
Align = alLeft
198+
AutoSize = False
199+
Caption = '0'
200+
Layout = tlCenter
201+
ExplicitLeft = 675
202+
end
203+
end
204+
end
205+
object pnOpcoesTranslate: TPanel
206+
Left = 0
207+
Top = 273
208+
Width = 845
209+
Height = 41
210+
Align = alTop
211+
Padding.Top = 6
212+
Padding.Bottom = 6
213+
TabOrder = 2
214+
end
215+
object TMSMCPCloudAI1: TTMSMCPCloudAI
216+
Service = aiOpenAI
217+
Settings.GeminiModel = 'gemini-1.5-flash-latest'
218+
Settings.OpenAIModel = 'gpt-4o'
219+
Settings.OpenAISoundModel = 'gpt-4o-mini-tts'
220+
Settings.OpenAITranscribeModel = 'whisper-1'
221+
Settings.GrokModel = 'grok-beta'
222+
Settings.ClaudeModel = 'claude-3-5-sonnet-20241022'
223+
Settings.OllamaModel = 'llama3.2:latest'
224+
Settings.DeepSeekModel = 'deepseek-chat'
225+
Settings.PerplexityModel = 'llama-3.1-sonar-small-128k-online'
226+
Settings.OllamaHost = 'localhost'
227+
Settings.OllamaPath = '/api/chat'
228+
Settings.MistralModel = 'mistral-large-latest'
229+
Settings.MistralTranscribeModel = 'voxtral-mini-2507'
230+
Tools = <>
231+
OnExecuted = TMSMCPCloudAI1Executed
232+
Left = 624
233+
Top = 136
234+
end
235+
object OpenDialog1: TOpenDialog
236+
DefaultExt = 'mp3'
237+
Filter = 'Arquivos MP3 (*.mp3)|*.mp3|Todos os arquivos (*.*)|*.*'
238+
Left = 624
239+
Top = 72
240+
end
241+
end
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
unit TokenUsageTracking.Main.View;
2+
3+
interface
4+
5+
uses
6+
Winapi.Windows,
7+
Winapi.Messages,
8+
System.SysUtils,
9+
System.Variants,
10+
System.Classes,
11+
Vcl.Graphics,
12+
Vcl.Controls,
13+
Vcl.Forms,
14+
Vcl.Dialogs,
15+
Vcl.ExtCtrls,
16+
Vcl.StdCtrls,
17+
Vcl.ComCtrls,
18+
TMS.MCP.CustomComponent,
19+
TMS.MCP.CloudBase,
20+
TMS.MCP.CloudAI;
21+
22+
type
23+
TTokenUsageTrackingMainView = class(TForm)
24+
pnTop: TPanel;
25+
pnButtonsTop: TPanel;
26+
Splitter1: TSplitter;
27+
ProgressBar1: TProgressBar;
28+
TMSMCPCloudAI1: TTMSMCPCloudAI;
29+
gBoxResponse: TGroupBox;
30+
mmResponse: TMemo;
31+
pnResponseDetails: TPanel;
32+
Label9: TLabel;
33+
lbNumTokensResponse: TLabel;
34+
Label10: TLabel;
35+
lbPromptTokens: TLabel;
36+
Label11: TLabel;
37+
lbServiceModel: TLabel;
38+
Label12: TLabel;
39+
lbTotalTokens: TLabel;
40+
gBoxTanscription: TGroupBox;
41+
mmTanscription: TMemo;
42+
OpenDialog1: TOpenDialog;
43+
pnOpcoesTranslate: TPanel;
44+
procedure FormCreate(Sender: TObject);
45+
procedure TMSMCPCloudAI1Executed(Sender: TObject; AResponse: TTMSMCPCloudAIResponse; AHttpStatusCode: Integer;
46+
AHttpResult: string);
47+
private
48+
procedure ClearResponse;
49+
public
50+
51+
end;
52+
53+
var
54+
TokenUsageTrackingMainView: TTokenUsageTrackingMainView;
55+
56+
implementation
57+
58+
{$R *.dfm}
59+
60+
procedure TTokenUsageTrackingMainView.FormCreate(Sender: TObject);
61+
begin
62+
ReportMemoryLeaksOnShutdown := True;
63+
64+
TMSMCPCloudAI1.APIKeys.LoadFromFile('..\..\Files\aikeys.cfg', 'PasswordTest');
65+
TMSMCPCloudAI1.Service := aiOpenAI;
66+
TMSMCPCloudAI1.Settings.WebSearch := True;
67+
end;
68+
69+
procedure TTokenUsageTrackingMainView.ClearResponse;
70+
begin
71+
mmResponse.Lines.Clear;
72+
lbPromptTokens.Caption := '0';
73+
lbNumTokensResponse.Caption := '0';
74+
lbTotalTokens.Caption := '0';
75+
lbServiceModel.Caption := '';
76+
end;
77+
78+
procedure TTokenUsageTrackingMainView.TMSMCPCloudAI1Executed(Sender: TObject; AResponse: TTMSMCPCloudAIResponse; AHttpStatusCode: Integer;
79+
AHttpResult: string);
80+
begin
81+
if AHttpStatusCode div 100 <> 2 then
82+
begin
83+
mmResponse.Lines.Text := 'Error: ' + AHttpStatusCode.ToString + sLineBreak + AHttpResult;
84+
Exit;
85+
end;
86+
87+
mmResponse.Lines.Text := AResponse.Content.Text;
88+
lbPromptTokens.Caption := AResponse.PromptTokens.ToString;
89+
lbNumTokensResponse.Caption := AResponse.CompletionTokens.ToString;
90+
lbTotalTokens.Caption := AResponse.TotalTokens.ToString;
91+
lbServiceModel.Caption := AResponse.ServiceModel;
92+
end;
93+
94+
end.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
program TokenUsageTracking;
2+
3+
uses
4+
Vcl.Forms,
5+
TokenUsageTracking.Main.View in 'Src\TokenUsageTracking.Main.View.pas' {TokenUsageTrackingMainView};
6+
7+
{$R *.res}
8+
9+
begin
10+
Application.Initialize;
11+
Application.MainFormOnTaskbar := True;
12+
Application.CreateForm(TTokenUsageTrackingMainView, TokenUsageTrackingMainView);
13+
Application.Run;
14+
end.

0 commit comments

Comments
 (0)