forked from HuichanKIM/Ollama-Delphi-GUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unit_AliveOllama.pas
171 lines (149 loc) · 4.49 KB
/
Unit_AliveOllama.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
unit Unit_AliveOllama;
interface
uses
Winapi.Windows,
Winapi.Messages,
System.SysUtils,
System.Variants,
System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.StdCtrls,
Vcl.Buttons;
type
TForm_AliveOllama = class(TForm)
GroupBox1: TGroupBox;
Edit1: TEdit;
Memo_Alive: TMemo;
Button_OK: TButton;
SpeedButton_Check: TSpeedButton;
procedure FormShow(Sender: TObject);
procedure FormKeyPress(Sender: TObject; var Key: Char);
procedure SpeedButton_CheckClick(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
private
procedure LogReturn(const S: String);
public
IsCkeckedFlag: Boolean;
end;
procedure CheckAlive_Ollama(const AFlag: Integer = 0);
function Get_ListModels_Ollama(const ARequestURI: string): string;
implementation
uses
Vcl.Themes,
Unit_Common,
Unit_Main,
System.Threading,
System.NetConsts,
System.Net.HttpClient,
System.Net.URLClient;
{$R *.dfm}
const
C_OllamaAddress = 'http://localhost:11434';
procedure CheckAlive_Ollama(const AFlag: Integer);
begin
TTask.Run( // Prevent Locking for Too Slow Response at First time ...
procedure // When Ollama_server(ollama_llama_server.exe) not started, Yet.
begin
var _response: string := '';
var _HTTP := THTTPClient.Create;
_HTTP.ProtocolVersion := THTTPProtocolVersion.HTTP_1_1;
try
var _HttpResponse := _HTTP.Get(C_OllamaAddress);
if _HttpResponse.StatusCode = 200 then
begin
_response := LowerCase(_HttpResponse.ContentAsString());
GV_AliveOllamaFlag := (Pos('ollama', _response) > 0) and (Pos('running', _response) > 1);
end;
finally
_HTTP.Free;
end;
PostMessage(Form_RestOllama.Handle, WM_NETHTTP_MESSAGE, WM_NETHTTP_MESSAGE_ALIVE, Ord(GV_AliveOllamaFlag));
end);
end;
function Get_ListModels_Ollama(const ARequestURI: string): string;
begin
Result := '';
if GV_AliveOllamaFlag then
try
var _HTTP := THTTPClient.Create;
_HTTP.ProtocolVersion := THTTPProtocolVersion.HTTP_1_1;
_HTTP.Accept := 'application/json, text/javascript, */*; q=0.01';
_HTTP.ContentType := 'application/json';
try
var _HttpResponse := _HTTP.Get(ARequestURI);
if _HttpResponse.StatusCode = 200 then
begin
Result := _HttpResponse.ContentAsString();
end;
finally
_HTTP.Free;
end;
except
on E: Exception do
ShowMessage(E.ClassName + ': ' + E.Message);
end;
end;
procedure TForm_AliveOllama.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
CanClose := IsCkeckedFlag;
end;
procedure TForm_AliveOllama.FormKeyPress(Sender: TObject; var Key: Char);
begin
if Key = #27 then
begin
ModalResult := mrCancel;
end;
end;
procedure TForm_AliveOllama.FormShow(Sender: TObject);
begin
if TStyleManager.IsCustomStyleActive then
begin
Memo_Alive.StyleElements := [seBorder];
Memo_Alive.Color := StyleServices.GetStyleColor(scWindow);
end;
IsCkeckedFlag := True;
Memo_Alive.Clear;
end;
procedure TForm_AliveOllama.LogReturn(const S: String);
begin
Memo_Alive.lines.Add(S);
end;
procedure TForm_AliveOllama.SpeedButton_CheckClick(Sender: TObject);
const
c_Warning = 'Check Ollama is installed and running on local computer.';
begin
IsCkeckedFlag := False;
Memo_Alive.lines.Clear;
try
var _response: string := '';
var _HTTP := THTTPClient.Create;
_HTTP.ProtocolVersion := THTTPProtocolVersion.HTTP_1_1;
try
var _HttpResponse := _HTTP.Get(C_OllamaAddress);
if _HttpResponse.StatusCode = 200 then
begin
_response := LowerCase(_HttpResponse.ContentAsString());
GV_AliveOllamaFlag := (Pos('ollama', _response) > 0) and (Pos('running', _response) > 1);
end;
finally
_HTTP.Free;
end;
PostMessage(Form_RestOllama.Handle, WM_NETHTTP_MESSAGE, WM_NETHTTP_MESSAGE_ALIVE, Ord(GV_AliveOllamaFlag));
LogReturn(_response+GC_CRLF+ IIF.CastBool<string>(GV_AliveOllamaFlag, 'Alive On', 'Not Alive'));
if not GV_AliveOllamaFlag then
begin
Memo_Alive.lines.Add(c_Warning);
Memo_Alive.lines.Add(GC_CRLF+'* On Restart, Checking Ollama Alive - On.');
end;
except
on E: Exception do
LogReturn(E.ClassName + ': ' + E.Message);
end;
if Button_OK.CanFocus then
Button_OK.SetFocus;
IsCkeckedFlag := True;
end;
end.