forked from SciSharp/LLamaSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserSettings.cs
More file actions
57 lines (46 loc) · 1.65 KB
/
Copy pathUserSettings.cs
File metadata and controls
57 lines (46 loc) · 1.65 KB
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
using Spectre.Console;
namespace LLama.Examples;
internal static class UserSettings
{
private static readonly string SettingsFilePath = Path.Join(AppContext.BaseDirectory, "DefaultModel.env");
private static string? ReadDefaultModelPath()
{
if (!File.Exists(SettingsFilePath))
return null;
string path = File.ReadAllText(SettingsFilePath).Trim();
if (!File.Exists(path))
return null;
return path;
}
private static void WriteDefaultModelPath(string path)
{
File.WriteAllText(SettingsFilePath, path);
}
public static string GetModelPath(bool alwaysPrompt = false)
{
var defaultPath = ReadDefaultModelPath();
var path = defaultPath is null || alwaysPrompt
? PromptUserForPath()
: PromptUserForPathWithDefault(defaultPath);
if (File.Exists(path))
WriteDefaultModelPath(path);
return path;
}
private static string PromptUserForPath()
{
return AnsiConsole.Prompt(
new TextPrompt<string>("Please input your model path:")
.PromptStyle("white")
.Validate(File.Exists, "[red]ERROR: invalid model file path - file does not exist[/]")
);
}
private static string PromptUserForPathWithDefault(string defaultPath)
{
return AnsiConsole.Prompt(
new TextPrompt<string>("Please input your model path (or ENTER for default):")
.DefaultValue(defaultPath)
.PromptStyle("white")
.Validate(File.Exists, "[red]ERROR: invalid model file path - file does not exist[/]")
);
}
}