You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserSettings.cs 1.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using Spectre.Console;
  2. namespace LLama.Examples;
  3. internal static class UserSettings
  4. {
  5. private static readonly string SettingsFilePath = Path.Join(AppContext.BaseDirectory, "DefaultModel.env");
  6. private static string? ReadDefaultModelPath()
  7. {
  8. if (!File.Exists(SettingsFilePath))
  9. return null;
  10. string path = File.ReadAllText(SettingsFilePath).Trim();
  11. if (!File.Exists(path))
  12. return null;
  13. return path;
  14. }
  15. private static void WriteDefaultModelPath(string path)
  16. {
  17. File.WriteAllText(SettingsFilePath, path);
  18. }
  19. public static string GetModelPath(bool alwaysPrompt = false)
  20. {
  21. var defaultPath = ReadDefaultModelPath();
  22. var path = defaultPath is null || alwaysPrompt
  23. ? PromptUserForPath()
  24. : PromptUserForPathWithDefault(defaultPath);
  25. if (File.Exists(path))
  26. WriteDefaultModelPath(path);
  27. return path;
  28. }
  29. private static string PromptUserForPath()
  30. {
  31. return AnsiConsole.Prompt(
  32. new TextPrompt<string>("Please input your model path:")
  33. .PromptStyle("white")
  34. .Validate(File.Exists, "[red]ERROR: invalid model file path - file does not exist[/]")
  35. );
  36. }
  37. private static string PromptUserForPathWithDefault(string defaultPath)
  38. {
  39. return AnsiConsole.Prompt(
  40. new TextPrompt<string>("Please input your model path (or ENTER for default):")
  41. .DefaultValue(defaultPath)
  42. .PromptStyle("white")
  43. .Validate(File.Exists, "[red]ERROR: invalid model file path - file does not exist[/]")
  44. );
  45. }
  46. }