Skip to content

Commit 51a611e

Browse files
committed
Some refactoring and updates
1 parent 6d66786 commit 51a611e

File tree

3 files changed

+64
-68
lines changed

3 files changed

+64
-68
lines changed

shell/agents/AIShell.Ollama.Agent/Command.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ private async Task UsePresetAction(string name)
8989
try
9090
{
9191
ModelConfig chosenPreset = (string.IsNullOrEmpty(name)
92-
? host.PromptForSelectionAsync(
92+
? await host.PromptForSelectionAsync(
9393
title: "[orange1]Please select a [Blue]Preset[/] to use[/]:",
9494
choices: setting.Presets,
9595
converter: PresetName,
96-
CancellationToken.None).GetAwaiter().GetResult()
96+
CancellationToken.None)
9797
: setting.Presets.FirstOrDefault(c => c.Name == name)) ?? throw new InvalidOperationException($"The preset '{name}' doesn't exist.");
9898
await setting.UsePreset(host, chosenPreset);
9999
host.MarkupLine($"Using the preset [green]{chosenPreset.Name}[/]:");
@@ -155,7 +155,7 @@ private void ShowSystemPromptAction()
155155
}
156156
catch (InvalidOperationException ex)
157157
{
158-
host.WriteErrorLine($"{ex.Message}");
158+
host.WriteErrorLine(ex.Message);
159159
}
160160
}
161161

@@ -181,7 +181,7 @@ private void SetSystemPromptAction(string prompt)
181181
}
182182
catch (InvalidOperationException ex)
183183
{
184-
host.WriteErrorLine($"{ex.Message}.");
184+
host.WriteErrorLine(ex.Message);
185185
}
186186
}
187187
}
@@ -215,7 +215,7 @@ public ModelCommand(OllamaAgent agent)
215215
AddCommand(use);
216216
}
217217

218-
private void ListModelAction(string name)
218+
private async Task ListModelAction(string name)
219219
{
220220
IHost host = Shell.Host;
221221

@@ -233,19 +233,19 @@ private void ListModelAction(string name)
233233
{
234234
if (string.IsNullOrEmpty(name))
235235
{
236-
settings.ListAllModels(host).GetAwaiter().GetResult();
236+
await settings.ListAllModels(host);
237237
return;
238238
}
239239

240-
settings.ShowOneModel(host, name).GetAwaiter().GetResult();
240+
await settings.ShowOneModel(host, name);
241241
}
242242
catch (InvalidOperationException ex)
243243
{
244244
host.WriteErrorLine(ex.Message);
245245
}
246246
}
247247

248-
private void UseModelAction(string name)
248+
private async Task UseModelAction(string name)
249249
{
250250
// Reload the setting file if needed.
251251
_agnet.ReloadSettings();
@@ -261,26 +261,28 @@ private void UseModelAction(string name)
261261

262262
try
263263
{
264-
if (!settings.PerformSelfcheck(host))
264+
bool success = await settings.PerformSelfcheck(host, checkEndpointOnly: true);
265+
if (!success)
265266
{
266267
return;
267268
}
268269

269-
if (settings.GetAllModels().GetAwaiter().GetResult().Count is 0)
270+
var allModels = await settings.GetAllModels();
271+
if (allModels.Count is 0)
270272
{
271-
host.WriteErrorLine("No models found.");
273+
host.WriteErrorLine($"No models found from '{settings.Endpoint}'.");
272274
return;
273275
}
274276

275277
if (string.IsNullOrEmpty(name))
276278
{
277-
name = host.PromptForSelectionAsync(
279+
name = await host.PromptForSelectionAsync(
278280
title: "[orange1]Please select a [Blue]Model[/] to use[/]:",
279-
choices: settings.GetAllModels(host).GetAwaiter().GetResult(),
280-
CancellationToken.None).GetAwaiter().GetResult();
281+
choices: allModels,
282+
CancellationToken.None);
281283
}
282284

283-
settings.UseModel(host, name).GetAwaiter().GetResult();
285+
await settings.UseModel(host: null, name);
284286
host.MarkupLine($"Using the model [green]{name}[/]");
285287
}
286288
catch (InvalidOperationException ex)

shell/agents/AIShell.Ollama.Agent/OllamaAgent.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,21 +170,22 @@ public async Task<bool> ChatAsync(string input, IShell shell)
170170
// Reload the setting file if needed.
171171
ReloadSettings();
172172

173-
if (!_settings.PerformSelfcheck(host))
173+
bool success = await _settings.PerformSelfcheck(host);
174+
if (!success)
174175
{
175176
return false;
176177
}
177178

178-
var activeModel = await _settings.GetActiveModel(host).ConfigureAwait(false);
179+
ModelConfig config = _settings.RunningConfig;
179180

180181
// Prepare request
181182
_request.Prompt = input;
182-
_request.Model = activeModel;
183+
_request.Model = config.ModelName;
183184
_request.Stream = _settings.Stream;
184185

185-
if (!string.IsNullOrWhiteSpace(_settings.RunningConfig.SystemPrompt))
186+
if (!string.IsNullOrWhiteSpace(config.SystemPrompt))
186187
{
187-
_request.System = _settings.RunningConfig.SystemPrompt;
188+
_request.System = config.SystemPrompt;
188189
}
189190

190191
try
@@ -249,7 +250,7 @@ public async Task<bool> ChatAsync(string input, IShell shell)
249250
catch (HttpRequestException e)
250251
{
251252
host.WriteErrorLine($"{e.Message}");
252-
host.WriteErrorLine($"Ollama active model: \"{activeModel}\"");
253+
host.WriteErrorLine($"Ollama active model: \"{config.ModelName}\"");
253254
host.WriteErrorLine($"Ollama endpoint: \"{_settings.Endpoint}\"");
254255
host.WriteErrorLine($"Ollama settings: \"{SettingFile}\"");
255256
}

shell/agents/AIShell.Ollama.Agent/Settings.cs

Lines changed: 40 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,14 @@ private async Task<bool> EnsureModelsInitialized(IHost host, CancellationToken c
5252
return true;
5353
}
5454

55-
if (!PerformSelfcheck(host))
55+
// Skip the self check when host is null.
56+
if (host is not null)
5657
{
57-
return false;
58+
bool success = await PerformSelfcheck(host, checkEndpointOnly: true);
59+
if (!success)
60+
{
61+
return false;
62+
}
5863
}
5964

6065
using OllamaApiClient client = new(Endpoint);
@@ -77,6 +82,7 @@ internal async Task<ICollection<string>> GetAllModels(IHost host = null, Cancell
7782
internal void EnsureModelNameIsValid(string name)
7883
{
7984
ArgumentException.ThrowIfNullOrEmpty(name);
85+
8086
if (!_availableModels.Contains(name.AddLatestTagIfNecessery()))
8187
{
8288
throw new InvalidOperationException($"A model with the name '{name}' doesn't exist. The available models are: [{string.Join(", ", _availableModels)}].");
@@ -94,7 +100,7 @@ internal void SetSystemPrompt(IHost host, string prompt)
94100
}
95101

96102
private static List<IRenderElement<string>> GetRenderModelElements(Func<string, bool> isActive) => [
97-
new CustomElement<string>(label: "Model Name", m => m),
103+
new CustomElement<string>(label: "Name", m => m),
98104
new CustomElement<string>(label: "Active", m => isActive(m) ? "true" : string.Empty)
99105
];
100106

@@ -165,64 +171,50 @@ internal void ShowOnePreset(IHost host, string name)
165171
]);
166172
}
167173

168-
internal async Task<string> GetActiveModel(IHost host, CancellationToken cancellationToken = default)
174+
internal async Task<bool> PerformSelfcheck(IHost host, bool checkEndpointOnly = false)
169175
{
170-
if (_runningConfigChecked is false)
171-
{
172-
if (await EnsureModelsInitialized(host, cancellationToken).ConfigureAwait(false))
173-
{
174-
if (string.IsNullOrEmpty(RunningConfig.ModelName))
175-
{
176-
// There is no model set, so use the first one available.
177-
if (_availableModels.Count == 0)
178-
{
179-
throw new InvalidOperationException($"No models are available to use from '{Endpoint}'.");
180-
}
181-
182-
RunningConfig = RunningConfig with { ModelName = _availableModels.First() };
183-
host.MarkupLine($"No Ollama model is configured. Using the first available model [green]'{RunningConfig.ModelName}'[/].");
184-
}
185-
else
186-
{
187-
EnsureModelNameIsValid(RunningConfig.ModelName);
188-
}
189-
190-
_runningConfigChecked = true;
191-
}
192-
else
193-
{
194-
throw new InvalidOperationException($"Error initializing models from '{Endpoint}'.");
195-
}
196-
}
197-
198-
return RunningConfig.ModelName;
199-
}
176+
_isRunningLocalHost ??= IsLocalHost().IsMatch(new Uri(Endpoint).Host);
200177

201-
internal bool PerformSelfcheck(IHost host)
202-
{
203-
if (_isRunningLocalHost is null)
178+
if (_isRunningLocalHost is true && Process.GetProcessesByName("ollama").Length is 0)
204179
{
205-
var endpointUri = new Uri(Endpoint);
206-
_isRunningLocalHost = IsLocalHost().IsMatch(endpointUri.Host);
180+
host.WriteErrorLine("Please be sure the Ollama is installed and server is running. Check all the prerequisites in the README of this agent are met.");
181+
return false;
207182
}
208183

209-
if (_isRunningLocalHost is true && Process.GetProcessesByName("ollama").Length is 0)
184+
if (!checkEndpointOnly && !_runningConfigChecked)
210185
{
211-
var message = "Please be sure the Ollama is installed and server is running. Check all the prerequisites in the README of this agent are met.";
212-
if (host is null)
186+
await EnsureModelsInitialized(host: null).ConfigureAwait(false);
187+
if (string.IsNullOrEmpty(RunningConfig.ModelName))
213188
{
214-
throw new InvalidOperationException(message);
189+
// There is no model set, so use the first one available.
190+
if (_availableModels.Count is 0)
191+
{
192+
host.WriteErrorLine($"No models are available to use from '{Endpoint}'.");
193+
return false;
194+
}
195+
196+
RunningConfig = RunningConfig with { ModelName = _availableModels.First() };
197+
host.MarkupLine($"No Ollama model is configured. Using the first available model [green]'{RunningConfig.ModelName}'[/].");
215198
}
216199
else
217200
{
218-
host.WriteErrorLine(message);
219-
return false;
201+
try
202+
{
203+
EnsureModelNameIsValid(RunningConfig.ModelName);
204+
}
205+
catch (InvalidOperationException e)
206+
{
207+
host.WriteErrorLine(e.Message);
208+
return false;
209+
}
220210
}
211+
212+
_runningConfigChecked = true;
221213
}
222214

223215
return true;
224216
}
225-
217+
226218
/// <summary>
227219
/// Defines a generated regular expression to match localhost addresses
228220
/// "localhost", "127.0.0.1" and "[::1]" with case-insensitivity.
@@ -286,5 +278,6 @@ internal partial class SourceGenerationContext : JsonSerializerContext { }
286278

287279
static class TagExtensions
288280
{
289-
public static string AddLatestTagIfNecessery(this string model) => model.IndexOf(":") == -1 ? string.Concat(model, ":latest") : model;
281+
public static string AddLatestTagIfNecessery(this string model) =>
282+
model.Contains(':') ? model : string.Concat(model, ":latest");
290283
}

0 commit comments

Comments
 (0)