modelpath.go 3.11 KB
Newer Older
Patrick Devine's avatar
Patrick Devine committed
1
2
3
package server

import (
4
	"errors"
Patrick Devine's avatar
Patrick Devine committed
5
6
7
	"fmt"
	"os"
	"path/filepath"
Michael Yang's avatar
Michael Yang committed
8
	"runtime"
Patrick Devine's avatar
Patrick Devine committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
	"strings"
)

type ModelPath struct {
	ProtocolScheme string
	Registry       string
	Namespace      string
	Repository     string
	Tag            string
}

const (
	DefaultRegistry       = "registry.ollama.ai"
	DefaultNamespace      = "library"
	DefaultTag            = "latest"
	DefaultProtocolScheme = "https"
)

27
28
29
30
31
32
33
34
35
36
37
38
39
40
var (
	ErrInvalidImageFormat = errors.New("invalid image format")
	ErrInvalidProtocol    = errors.New("invalid protocol scheme")
	ErrInsecureProtocol   = errors.New("insecure protocol http")
)

func ParseModelPath(name string, allowInsecure bool) (ModelPath, error) {
	mp := ModelPath{
		ProtocolScheme: DefaultProtocolScheme,
		Registry:       DefaultRegistry,
		Namespace:      DefaultNamespace,
		Repository:     "",
		Tag:            DefaultTag,
	}
Patrick Devine's avatar
Patrick Devine committed
41

42
43
44
45
46
47
48
49
50
51
52
53
54
	protocol, rest, didSplit := strings.Cut(name, "://")
	if didSplit {
		if protocol == "https" || protocol == "http" && allowInsecure {
			mp.ProtocolScheme = protocol
			name = rest
		} else if protocol == "http" && !allowInsecure {
			return ModelPath{}, ErrInsecureProtocol
		} else {
			return ModelPath{}, ErrInvalidProtocol
		}
	}

	slashParts := strings.Split(name, "/")
Patrick Devine's avatar
Patrick Devine committed
55
56
	switch len(slashParts) {
	case 3:
57
58
59
		mp.Registry = slashParts[0]
		mp.Namespace = slashParts[1]
		mp.Repository = slashParts[2]
Patrick Devine's avatar
Patrick Devine committed
60
	case 2:
61
62
		mp.Namespace = slashParts[0]
		mp.Repository = slashParts[1]
Patrick Devine's avatar
Patrick Devine committed
63
	case 1:
64
		mp.Repository = slashParts[0]
Patrick Devine's avatar
Patrick Devine committed
65
	default:
66
		return ModelPath{}, ErrInvalidImageFormat
Patrick Devine's avatar
Patrick Devine committed
67
68
	}

69
70
71
	if repo, tag, didSplit := strings.Cut(mp.Repository, ":"); didSplit {
		mp.Repository = repo
		mp.Tag = tag
Patrick Devine's avatar
Patrick Devine committed
72
73
	}

74
	return mp, nil
Patrick Devine's avatar
Patrick Devine committed
75
76
77
78
79
80
81
82
83
84
85
}

func (mp ModelPath) GetNamespaceRepository() string {
	return fmt.Sprintf("%s/%s", mp.Namespace, mp.Repository)
}

func (mp ModelPath) GetFullTagname() string {
	return fmt.Sprintf("%s/%s/%s:%s", mp.Registry, mp.Namespace, mp.Repository, mp.Tag)
}

func (mp ModelPath) GetShortTagname() string {
86
87
88
89
90
	if mp.Registry == DefaultRegistry {
		if mp.Namespace == DefaultNamespace {
			return fmt.Sprintf("%s:%s", mp.Repository, mp.Tag)
		}
		return fmt.Sprintf("%s/%s:%s", mp.Namespace, mp.Repository, mp.Tag)
Patrick Devine's avatar
Patrick Devine committed
91
	}
92
	return fmt.Sprintf("%s/%s/%s:%s", mp.Registry, mp.Namespace, mp.Repository, mp.Tag)
Patrick Devine's avatar
Patrick Devine committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
}

func (mp ModelPath) GetManifestPath(createDir bool) (string, error) {
	home, err := os.UserHomeDir()
	if err != nil {
		return "", err
	}

	path := filepath.Join(home, ".ollama", "models", "manifests", mp.Registry, mp.Namespace, mp.Repository, mp.Tag)
	if createDir {
		if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
			return "", err
		}
	}

	return path, nil
}

Patrick Devine's avatar
Patrick Devine committed
111
112
113
114
115
116
117
118
119
func GetManifestPath() (string, error) {
	home, err := os.UserHomeDir()
	if err != nil {
		return "", err
	}

	return filepath.Join(home, ".ollama", "models", "manifests"), nil
}

Patrick Devine's avatar
Patrick Devine committed
120
121
122
123
124
125
func GetBlobsPath(digest string) (string, error) {
	home, err := os.UserHomeDir()
	if err != nil {
		return "", err
	}

Michael Yang's avatar
Michael Yang committed
126
127
128
129
	if runtime.GOOS == "windows" {
		digest = strings.ReplaceAll(digest, ":", "-")
	}

Michael Yang's avatar
Michael Yang committed
130
	path := filepath.Join(home, ".ollama", "models", "blobs", digest)
Patrick Devine's avatar
Patrick Devine committed
131
132
133
134
	if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
		return "", err
	}

Michael Yang's avatar
Michael Yang committed
135
	return path, nil
Patrick Devine's avatar
Patrick Devine committed
136
}