modelpath.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package server
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "runtime"
  7. "strings"
  8. )
  9. type ModelPath struct {
  10. ProtocolScheme string
  11. Registry string
  12. Namespace string
  13. Repository string
  14. Tag string
  15. }
  16. const (
  17. DefaultRegistry = "registry.ollama.ai"
  18. DefaultNamespace = "library"
  19. DefaultTag = "latest"
  20. DefaultProtocolScheme = "https"
  21. )
  22. func ParseModelPath(name string) ModelPath {
  23. slashParts := strings.Split(name, "/")
  24. var registry, namespace, repository, tag string
  25. switch len(slashParts) {
  26. case 3:
  27. registry = slashParts[0]
  28. namespace = slashParts[1]
  29. repository = strings.Split(slashParts[2], ":")[0]
  30. case 2:
  31. registry = DefaultRegistry
  32. namespace = slashParts[0]
  33. repository = strings.Split(slashParts[1], ":")[0]
  34. case 1:
  35. registry = DefaultRegistry
  36. namespace = DefaultNamespace
  37. repository = strings.Split(slashParts[0], ":")[0]
  38. default:
  39. fmt.Println("Invalid image format.")
  40. return ModelPath{}
  41. }
  42. colonParts := strings.Split(slashParts[len(slashParts)-1], ":")
  43. if len(colonParts) == 2 {
  44. tag = colonParts[1]
  45. } else {
  46. tag = DefaultTag
  47. }
  48. return ModelPath{
  49. ProtocolScheme: DefaultProtocolScheme,
  50. Registry: registry,
  51. Namespace: namespace,
  52. Repository: repository,
  53. Tag: tag,
  54. }
  55. }
  56. func (mp ModelPath) GetNamespaceRepository() string {
  57. return fmt.Sprintf("%s/%s", mp.Namespace, mp.Repository)
  58. }
  59. func (mp ModelPath) GetFullTagname() string {
  60. return fmt.Sprintf("%s/%s/%s:%s", mp.Registry, mp.Namespace, mp.Repository, mp.Tag)
  61. }
  62. func (mp ModelPath) GetShortTagname() string {
  63. if mp.Registry == DefaultRegistry {
  64. if mp.Namespace == DefaultNamespace {
  65. return fmt.Sprintf("%s:%s", mp.Repository, mp.Tag)
  66. }
  67. return fmt.Sprintf("%s/%s:%s", mp.Namespace, mp.Repository, mp.Tag)
  68. }
  69. return fmt.Sprintf("%s/%s/%s:%s", mp.Registry, mp.Namespace, mp.Repository, mp.Tag)
  70. }
  71. func (mp ModelPath) GetManifestPath(createDir bool) (string, error) {
  72. home, err := os.UserHomeDir()
  73. if err != nil {
  74. return "", err
  75. }
  76. path := filepath.Join(home, ".ollama", "models", "manifests", mp.Registry, mp.Namespace, mp.Repository, mp.Tag)
  77. if createDir {
  78. if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
  79. return "", err
  80. }
  81. }
  82. return path, nil
  83. }
  84. func GetManifestPath() (string, error) {
  85. home, err := os.UserHomeDir()
  86. if err != nil {
  87. return "", err
  88. }
  89. return filepath.Join(home, ".ollama", "models", "manifests"), nil
  90. }
  91. func GetBlobsPath(digest string) (string, error) {
  92. home, err := os.UserHomeDir()
  93. if err != nil {
  94. return "", err
  95. }
  96. if runtime.GOOS == "windows" {
  97. digest = strings.ReplaceAll(digest, ":", "-")
  98. }
  99. path := filepath.Join(home, ".ollama", "models", "blobs", digest)
  100. if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
  101. return "", err
  102. }
  103. return path, nil
  104. }