modelpath.go 2.5 KB

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