-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathOllamaConsole.cs
98 lines (71 loc) · 2.89 KB
/
OllamaConsole.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
using System.Text;
using OllamaSharp;
using Spectre.Console;
namespace OllamaApiConsole;
public abstract class OllamaConsole(IOllamaApiClient ollama)
{
private const char MULTILINE_OPEN = '[';
private const char MULTILINE_CLOSE = ']';
public static string HintTextColor { get; } = "gray";
public static string AccentTextColor { get; } = "blue";
public static string WarningTextColor { get; } = "yellow";
public static string ErrorTextColor { get; } = "red";
public static string AiTextColor { get; } = "cyan";
public static string START_NEW_COMMAND { get; } = "/new";
public static string EXIT_COMMAND { get; } = "/exit";
public IOllamaApiClient Ollama { get; } = ollama ?? throw new ArgumentNullException(nameof(ollama));
public abstract Task Run();
public static string ReadInput(string prompt = "", string additionalInformation = "")
{
if (!string.IsNullOrEmpty(prompt))
AnsiConsole.MarkupLine(prompt);
if (!string.IsNullOrEmpty(additionalInformation))
AnsiConsole.MarkupLine(additionalInformation);
var builder = new StringBuilder();
bool? isMultiLineActive = null;
var needsCleaning = false;
while (!isMultiLineActive.HasValue || isMultiLineActive.Value)
{
AnsiConsole.Markup($"[{AccentTextColor}]> [/]");
var input = Console.ReadLine() ?? "";
if (!isMultiLineActive.HasValue)
{
isMultiLineActive = input.TrimStart().StartsWith(MULTILINE_OPEN);
needsCleaning = isMultiLineActive.GetValueOrDefault();
}
builder.AppendLine(input);
if (input.TrimEnd().EndsWith(MULTILINE_CLOSE) && isMultiLineActive.GetValueOrDefault())
isMultiLineActive = false;
}
if (needsCleaning)
return builder.ToString().Trim().TrimStart(MULTILINE_OPEN).TrimEnd(MULTILINE_CLOSE);
return builder.ToString().TrimEnd();
}
protected void WriteChatInstructionHint()
{
AnsiConsole.MarkupLine($"[{HintTextColor}]Enter [{AccentTextColor}]{START_NEW_COMMAND}[/] to start over or [{AccentTextColor}]{EXIT_COMMAND}[/] to leave.[/]");
AnsiConsole.MarkupLine($"[{HintTextColor}]Begin with [{AccentTextColor}]{Markup.Escape(MULTILINE_OPEN.ToString())}[/] to start multiline input. Sumbmit it by ending with [{AccentTextColor}]{Markup.Escape(MULTILINE_CLOSE.ToString())}[/].[/]");
}
protected async Task<string> SelectModel(string prompt, string additionalInformation = "")
{
const string BACK = "..";
var models = await Ollama.ListLocalModelsAsync();
var modelsWithBackChoice = models.OrderBy(m => m.Name).Select(m => m.Name).ToList();
if (modelsWithBackChoice.Count == 1)
{
return modelsWithBackChoice[0];
}
else
{
modelsWithBackChoice.Insert(0, BACK);
if (!string.IsNullOrEmpty(additionalInformation))
AnsiConsole.MarkupLine(additionalInformation);
var answer = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.PageSize(10)
.Title(prompt)
.AddChoices(modelsWithBackChoice));
return answer == BACK ? "" : answer;
}
}
}