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 2.0 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. namespace LLama.Examples;
  2. internal static class UserSettings
  3. {
  4. private static readonly string SettingsFilePath = Path.Join(AppContext.BaseDirectory, "DefaultModel.env");
  5. private static string? ReadDefaultModelPath()
  6. {
  7. if (!File.Exists(SettingsFilePath))
  8. return null;
  9. string path = File.ReadAllText(SettingsFilePath).Trim();
  10. if (!File.Exists(path))
  11. return null;
  12. return path;
  13. }
  14. private static void WriteDefaultModelPath(string path)
  15. {
  16. File.WriteAllText(SettingsFilePath, path);
  17. }
  18. public static string GetModelPath(bool alwaysPrompt = false)
  19. {
  20. string? defaultPath = ReadDefaultModelPath();
  21. return defaultPath is null || alwaysPrompt
  22. ? PromptUserForPath()
  23. : PromptUserForPathWithDefault(defaultPath);
  24. }
  25. private static string PromptUserForPath()
  26. {
  27. while (true)
  28. {
  29. Console.ForegroundColor = ConsoleColor.White;
  30. Console.Write("Please input your model path: ");
  31. string? path = Console.ReadLine();
  32. if (File.Exists(path))
  33. {
  34. WriteDefaultModelPath(path);
  35. return path;
  36. }
  37. Console.WriteLine("ERROR: invalid model file path\n");
  38. }
  39. }
  40. private static string PromptUserForPathWithDefault(string defaultPath)
  41. {
  42. while (true)
  43. {
  44. Console.ForegroundColor = ConsoleColor.White;
  45. Console.WriteLine($"Default model: {defaultPath}");
  46. Console.Write($"Please input a model path (or ENTER for default): ");
  47. string? path = Console.ReadLine();
  48. if (string.IsNullOrWhiteSpace(path))
  49. {
  50. return defaultPath;
  51. }
  52. if (File.Exists(path))
  53. {
  54. WriteDefaultModelPath(path);
  55. return path;
  56. }
  57. Console.WriteLine("ERROR: invalid model file path\n");
  58. }
  59. }
  60. }