-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathCommand.cs
309 lines (257 loc) · 8.88 KB
/
Command.cs
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
using System.CommandLine;
using System.CommandLine.Completions;
using System.Threading.Tasks;
using AIShell.Abstraction;
namespace AIShell.Ollama.Agent;
internal sealed class PresetCommand : CommandBase
{
private readonly OllamaAgent _agnet;
public PresetCommand(OllamaAgent agent)
: base("preset", "Command for preset management within the 'ollama' agent.")
{
_agnet = agent;
var use = new Command("use", "Specify a preset to use.");
var usePreset = new Argument<string>(
name: "Preset",
getDefaultValue: () => null,
description: "Name of a preset.").AddCompletions(PresetNameCompleter);
use.AddArgument(usePreset);
use.SetHandler(UsePresetAction, usePreset);
var list = new Command("list", "List a specific preset, or all configured presets.");
var listPreset = new Argument<string>(
name: "Preset",
getDefaultValue: () => null,
description: "Name of a preset.").AddCompletions(PresetNameCompleter);
list.AddArgument(listPreset);
list.SetHandler(ListPresetAction, listPreset);
AddCommand(list);
AddCommand(use);
}
private void ListPresetAction(string name)
{
IHost host = Shell.Host;
// Reload the setting file if needed.
_agnet.ReloadSettings();
Settings settings = _agnet.Settings;
if (settings is null)
{
host.WriteErrorLine("Error loading the configuration.");
return;
}
try
{
if (string.IsNullOrEmpty(name))
{
settings.ListAllPresets(host);
return;
}
settings.ShowOnePreset(host, name);
}
catch (Exception ex)
{
string availablePresetNames = PresetNamesAsString();
host.WriteErrorLine($"{ex.Message} Available preset(s): {availablePresetNames}.");
}
}
private async Task UsePresetAction(string name)
{
// Reload the setting file if needed.
_agnet.ReloadSettings();
var setting = _agnet.Settings;
var host = Shell.Host;
if (setting is null)
{
host.WriteErrorLine("Error loading the configuration.");
return;
}
if (setting.Presets.Count is 0)
{
host.WriteErrorLine("There are no presets configured.");
return;
}
try
{
ModelConfig chosenPreset = (string.IsNullOrEmpty(name)
? await host.PromptForSelectionAsync(
title: "[orange1]Please select a [Blue]Preset[/] to use[/]:",
choices: setting.Presets,
converter: PresetName,
CancellationToken.None)
: setting.Presets.FirstOrDefault(c => c.Name == name)) ?? throw new InvalidOperationException($"The preset '{name}' doesn't exist.");
await setting.UsePreset(host, chosenPreset);
host.MarkupLine($"Using the preset [green]{chosenPreset.Name}[/]:");
}
catch (Exception ex)
{
string availablePresetNames = PresetNamesAsString();
host.WriteErrorLine($"{ex.Message} Available presets: {availablePresetNames}.");
}
}
private static string PresetName(ModelConfig preset) => preset.Name.Any(Char.IsWhiteSpace) ? $"\"{preset.Name}\"" : preset.Name;
private IEnumerable<string> PresetNameCompleter(CompletionContext context) => _agnet.Settings?.Presets?.Select(PresetName) ?? [];
private string PresetNamesAsString() => string.Join(", ", PresetNameCompleter(null));
}
internal sealed class SystemPromptCommand : CommandBase
{
private readonly OllamaAgent _agnet;
public SystemPromptCommand(OllamaAgent agent)
: base("system-prompt", "Command for system prompt management within the 'ollama' agent.")
{
_agnet = agent;
var show = new Command("show", "Show the current system prompt.");
show.SetHandler(ShowSystemPromptAction);
var set = new Command("set", "Sets the system prompt.");
var systemPromptModel = new Argument<string>(
name: "System-Prompt",
getDefaultValue: () => null,
description: "The system prompt");
set.AddArgument(systemPromptModel);
set.SetHandler(SetSystemPromptAction, systemPromptModel);
AddCommand(show);
AddCommand(set);
}
private void ShowSystemPromptAction()
{
IHost host = Shell.Host;
// Reload the setting file if needed.
_agnet.ReloadSettings();
Settings settings = _agnet.Settings;
if (settings is null)
{
host.WriteErrorLine("Error loading the configuration.");
return;
}
try
{
settings.ShowSystemPrompt(host);
}
catch (Exception ex)
{
host.WriteErrorLine(ex.Message);
}
}
private void SetSystemPromptAction(string prompt)
{
IHost host = Shell.Host;
// Reload the setting file if needed.
_agnet.ReloadSettings();
_agnet.ResetContext();
Settings settings = _agnet.Settings;
if (settings is null)
{
host.WriteErrorLine("Error loading the configuration.");
return;
}
try
{
settings.SetSystemPrompt(host, prompt);
}
catch (Exception ex)
{
host.WriteErrorLine(ex.Message);
}
}
}
internal sealed class ModelCommand : CommandBase
{
private readonly OllamaAgent _agnet;
public ModelCommand(OllamaAgent agent)
: base("model", "Command for model management within the 'ollama' agent.")
{
_agnet = agent;
var use = new Command("use", "Specify a model to use, or choose one from the available models.");
var useModel = new Argument<string>(
name: "Model",
getDefaultValue: () => null,
description: "Name of a model.").AddCompletions(ModelNameCompleter);
use.AddArgument(useModel);
use.SetHandler(UseModelAction, useModel);
var list = new Command("list", "List a specific model, or all available models.");
var listModel = new Argument<string>(
name: "Model",
getDefaultValue: () => null,
description: "Name of a model.").AddCompletions(ModelNameCompleter);
list.AddArgument(listModel);
list.SetHandler(ListModelAction, listModel);
AddCommand(list);
AddCommand(use);
}
private async Task ListModelAction(string name)
{
IHost host = Shell.Host;
// Reload the setting file if needed.
_agnet.ReloadSettings();
Settings settings = _agnet.Settings;
if (settings is null)
{
host.WriteErrorLine("Error loading the configuration.");
return;
}
try
{
if (string.IsNullOrEmpty(name))
{
await settings.ListAllModels(host);
return;
}
await settings.ShowOneModel(host, name);
}
catch (Exception ex)
{
host.WriteErrorLine(ex.Message);
}
}
private async Task UseModelAction(string name)
{
// Reload the setting file if needed.
_agnet.ReloadSettings();
var settings = _agnet.Settings;
var host = Shell.Host;
if (settings is null)
{
host.WriteErrorLine("Error loading the configuration.");
return;
}
try
{
bool success = await settings.PerformSelfcheck(host, checkEndpointOnly: true);
if (!success)
{
return;
}
var allModels = await settings.GetAllModels();
if (allModels.Count is 0)
{
host.WriteErrorLine($"No models found from '{settings.Endpoint}'.");
return;
}
if (string.IsNullOrEmpty(name))
{
name = await host.PromptForSelectionAsync(
title: "[orange1]Please select a [Blue]Model[/] to use[/]:",
choices: allModels,
CancellationToken.None);
}
await settings.UseModel(host, name);
host.MarkupLine($"Using the model [green]{name}[/]");
}
catch (Exception ex)
{
host.WriteErrorLine(ex.Message);
}
}
private IEnumerable<string> ModelNameCompleter(CompletionContext context)
{
try
{
// Model retrieval may throw.
var results = _agnet.Settings?.GetAllModels().Result;
if (results is not null)
{
return results;
}
}
catch (Exception) { }
return [];
}
}