-
Notifications
You must be signed in to change notification settings - Fork 51
Enhanced Model Management and SystemPrompt Integration in OllamaAgent #351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
2b67582
2d96206
a3b4a22
4279c88
c18b187
3d95100
1990232
153970a
9d1998f
6d66786
51a611e
c9f549e
db22595
ed5fb44
2cefb83
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,278 @@ | ||
using System.CommandLine; | ||
using System.CommandLine.Completions; | ||
using System.Threading.Tasks; | ||
using AIShell.Abstraction; | ||
|
||
namespace AIShell.Ollama.Agent; | ||
|
||
internal sealed class ConfigCommand : CommandBase | ||
{ | ||
private readonly OllamaAgent _agnet; | ||
public ConfigCommand(OllamaAgent agent) | ||
: base("config", "Command for config management within the 'ollama' agent.") | ||
{ | ||
_agnet = agent; | ||
|
||
var use = new Command("use", "Specify a config to use."); | ||
var useConfig = new Argument<string>( | ||
name: "Config", | ||
getDefaultValue: () => null, | ||
description: "Name of a configuration.").AddCompletions(ConfigNameCompleter); | ||
use.AddArgument(useConfig); | ||
use.SetHandler(UseConfigAction, useConfig); | ||
|
||
var list = new Command("list", "List a specific config, or all available configs."); | ||
var listConfig = new Argument<string>( | ||
name: "Config", | ||
getDefaultValue: () => null, | ||
description: "Name of a configuration.").AddCompletions(ConfigNameCompleter); | ||
list.AddArgument(listConfig); | ||
list.SetHandler(ListConfigAction, listConfig); | ||
|
||
AddCommand(list); | ||
AddCommand(use); | ||
} | ||
|
||
private void ListConfigAction(string name) | ||
{ | ||
IHost host = Shell.Host; | ||
|
||
// Reload the setting file if needed. | ||
_agnet.ReloadSettings(); | ||
|
||
Settings settings = _agnet.Settings; | ||
|
||
if (settings is null) | ||
{ | ||
host.WriteErrorLine("Invalid configuration."); | ||
return; | ||
} | ||
|
||
if (string.IsNullOrEmpty(name)) | ||
{ | ||
settings.ListAllConfigs(host); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
settings.ShowOneConfig(host, name); | ||
} | ||
catch (InvalidOperationException ex) | ||
{ | ||
string availableConfigNames = ConfigNamesAsString(); | ||
host.WriteErrorLine($"{ex.Message} Available cofiguration(s): {availableConfigNames}."); | ||
} | ||
} | ||
|
||
private async Task UseConfigAction(string name) | ||
{ | ||
// Reload the setting file if needed. | ||
_agnet.ReloadSettings(); | ||
|
||
var setting = _agnet.Settings; | ||
var host = Shell.Host; | ||
|
||
if (setting is null || setting.Configs.Count is 0) | ||
{ | ||
host.WriteErrorLine("No configs configured."); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
ModelConfig chosenConfig = (string.IsNullOrEmpty(name) | ||
? host.PromptForSelectionAsync( | ||
title: "[orange1]Please select a [Blue]Configuration[/] to use[/]:", | ||
choices: setting.Configs, | ||
converter: ConfigName, | ||
CancellationToken.None).GetAwaiter().GetResult() | ||
: setting.Configs.FirstOrDefault(c => c.Name == name)) ?? throw new InvalidOperationException($"The configuration '{name}' doesn't exist."); | ||
await setting.UseConfg(host, chosenConfig); | ||
Check failure on line 91 in shell/agents/AIShell.Ollama.Agent/Command.cs
|
||
host.MarkupLine($"Using the config [green]{chosenConfig.Name}[/]:"); | ||
} | ||
catch (InvalidOperationException ex) | ||
{ | ||
string availableConfigNames = ConfigNamesAsString(); | ||
host.WriteErrorLine($"{ex.Message} Available configurations: {availableConfigNames}."); | ||
} | ||
} | ||
|
||
private static string ConfigName(ModelConfig config) => config.Name.Any(Char.IsWhiteSpace) ? $"\"{config.Name}\"" : config.Name; | ||
private IEnumerable<string> ConfigNameCompleter(CompletionContext context) => _agnet.Settings?.Configs?.Select(ConfigName) ?? []; | ||
private string ConfigNamesAsString() => string.Join(", ", ConfigNameCompleter(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("Invalid configuration."); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
settings.ShowSystemPrompt(host); | ||
} | ||
catch (InvalidOperationException 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("Invalid configuration."); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
settings.SetSystemPrompt(host, prompt); | ||
} | ||
catch (InvalidOperationException ex) | ||
{ | ||
host.WriteErrorLine($"{ex.Message}."); | ||
} | ||
} | ||
} | ||
|
||
internal sealed class ModelCommand : CommandBase | ||
daxian-dbw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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 void 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("Invalid configuration."); | ||
return; | ||
} | ||
|
||
if (string.IsNullOrEmpty(name)) | ||
{ | ||
settings.ListAllModels(host).GetAwaiter().GetResult(); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
settings.ShowOneModel(host, name).GetAwaiter().GetResult(); | ||
} | ||
catch (InvalidOperationException ex) | ||
{ | ||
string availableModelNames = ModelNamesAsString(); | ||
host.WriteErrorLine($"{ex.Message} Available Models(s): {availableModelNames}."); | ||
} | ||
} | ||
|
||
private void UseModelAction(string name) | ||
{ | ||
// Reload the setting file if needed. | ||
_agnet.ReloadSettings(); | ||
|
||
var setting = _agnet.Settings; | ||
var host = Shell.Host; | ||
|
||
if (setting is null || setting.GetAllModels().GetAwaiter().GetResult().Count is 0) | ||
{ | ||
host.WriteErrorLine("No models configured."); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
string chosenModel = string.IsNullOrEmpty(name) | ||
? host.PromptForSelectionAsync( | ||
title: "[orange1]Please select a [Blue]Model[/] to use[/]:", | ||
choices: setting.GetAllModels().GetAwaiter().GetResult(), | ||
CancellationToken.None).GetAwaiter().GetResult() | ||
: setting.GetModelByName(name).GetAwaiter().GetResult(); | ||
|
||
setting.UseModel(chosenModel).GetAwaiter().GetResult(); | ||
host.MarkupLine($"Using the model [green]{chosenModel}[/]:"); | ||
} | ||
catch (InvalidOperationException ex) | ||
{ | ||
string availableModelNames = ModelNamesAsString(); | ||
host.WriteErrorLine($"{ex.Message} Available Modless: {availableModelNames}."); | ||
} | ||
} | ||
|
||
private IEnumerable<string> ModelNameCompleter(CompletionContext context) => _agnet.Settings?.GetAllModels().GetAwaiter().GetResult() ?? []; | ||
private string ModelNamesAsString() => string.Join(", ", ModelNameCompleter(null)); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.