modelpath.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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(name, ":")
  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 && mp.Namespace == DefaultNamespace {
  64. return fmt.Sprintf("%s:%s", mp.Repository, mp.Tag)
  65. }
  66. return fmt.Sprintf("%s/%s:%s", mp.Namespace, mp.Repository, mp.Tag)
  67. }
  68. func (mp ModelPath) GetManifestPath(createDir bool) (string, error) {
  69. home, err := os.UserHomeDir()
  70. if err != nil {
  71. return "", err
  72. }
  73. path := filepath.Join(home, ".ollama", "models", "manifests", mp.Registry, mp.Namespace, mp.Repository, mp.Tag)
  74. if createDir {
  75. if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
  76. return "", err
  77. }
  78. }
  79. return path, nil
  80. }
  81. func GetManifestPath() (string, error) {
  82. home, err := os.UserHomeDir()
  83. if err != nil {
  84. return "", err
  85. }
  86. return filepath.Join(home, ".ollama", "models", "manifests"), nil
  87. }
  88. func GetBlobsPath(digest string) (string, error) {
  89. home, err := os.UserHomeDir()
  90. if err != nil {
  91. return "", err
  92. }
  93. if runtime.GOOS == "windows" {
  94. digest = strings.ReplaceAll(digest, ":", "-")
  95. }
  96. path := filepath.Join(home, ".ollama", "models", "blobs", digest)
  97. if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
  98. return "", err
  99. }
  100. return path, nil
  101. }