Documentation ¶
Index ¶
- Constants
- Variables
- func IsValidNamePart(kind PartKind, s string) bool
- type Digest
- type Name
- func (r Name) Build() string
- func (r Name) CompareFold(o Name) int
- func (r Name) Digest() Digest
- func (r Name) DisplayLong() string
- func (r Name) DisplayLongest() string
- func (r Name) DisplayShortest(mask string) string
- func (r Name) DisplayURLPath() string
- func (r Name) EqualFold(o Name) bool
- func (r Name) Filepath() string
- func (r Name) FilepathNoBuild() string
- func (r Name) GoString() string
- func (r Name) Host() string
- func (r Name) IsComplete() bool
- func (r Name) IsCompleteNoBuild() bool
- func (r Name) IsResolved() bool
- func (r Name) IsValid() bool
- func (r Name) IsZero() bool
- func (r Name) LogValue() slog.Value
- func (r Name) MapHash() uint64
- func (r Name) Model() string
- func (r Name) Namespace() string
- func (r Name) Tag() string
- func (r Name) URLPath() string
- func (r Name) WithBuild(build string) Name
- func (r Name) WithDigest(digest Digest) Name
- type PartKind
Examples ¶
Constants ¶
const ( // MaskDefault is the default mask used by [Name.DisplayShortest]. MaskDefault = "registry.ollama.ai/library/?:latest" // MaskNothing is a mask that masks nothing. MaskNothing = "?/?/?:?" // DefaultFill is the default fill used by [ParseName]. FillDefault = "registry.ollama.ai/library/?:latest+Q4_0" // FillNothing is a fill that fills nothing. FillNothing = "?/?/?:?+?" )
Defaults
const MaxNamePartLen = 128
Variables ¶
var ( // ErrInvalidName, ErrIncompleteName, and ErrInvalidDigest are not // used by this package, but are exported so that other packages can // use them, instead of defining their own errors for them. ErrInvalidName = errors.New("invalid model name") ErrIncompleteName = errors.New("incomplete model name") ErrInvalidDigest = errors.New("invalid digest") )
Errors
Functions ¶
func IsValidNamePart ¶
IsValidNamePart reports if s contains all valid characters for the given part kind and is under MaxNamePartLen bytes.
Types ¶
type Digest ¶
type Digest struct {
// contains filtered or unexported fields
}
Digest represents a digest of a model Manifest. It is a comparable value type and is immutable.
The zero Digest is not a valid digest.
func MustParseDigest ¶
func ParseDigest ¶
ParseDigest parses a string in the form of "<digest-type>-<digest>" into a Digest.
func (Digest) IsValid ¶
IsValid returns true if the digest is valid (not zero).
A valid digest may be created only by ParseDigest, or ParseName(name).Digest().
type Name ¶
type Name struct {
// contains filtered or unexported fields
}
Name is an opaque reference to a model. It holds the parts of a model with the case preserved, but is not directly comparable with other Names since model names can be represented with different casing depending on the use case. For instance, "Mistral" and "mistral" are the same model but each version may have come from different sources (e.g. copied from a Web page, or from a file path).
Valid Names can ONLY be constructed by calling ParseName.
A Name is valid if and only if is have a valid Model part. The other parts are optional.
A Name is considered "complete" if it has all parts present. To check if a Name is complete, use Name.IsComplete.
To compare two names in a case-insensitive manner, use Name.EqualFold.
The parts of a Name are:
- Host: the domain of the model (optional)
- Namespace: the namespace of the model (optional)
- Model: the name of the model (required)
- Tag: the tag of the model (optional)
- Build: the build of the model; usually the quantization or "file type" (optional)
The parts can be obtained in their original form by calling [Name.Parts].
To check if a Name has at minimum a valid model part, use Name.IsValid.
Example (CompleteAndResolved) ¶
for _, s := range []string{ "x/y/z:latest+q4_0@sha123-abc", "x/y/z:latest+q4_0", "@sha123-abc", } { name := ParseNameFill(s, FillNothing) fmt.Printf("complete:%v resolved:%v digest:%s\n", name.IsComplete(), name.IsResolved(), name.Digest()) }
Output: complete:true resolved:true digest:sha123-abc complete:true resolved:false digest: complete:false resolved:true digest:sha123-abc
func MustParseName ¶
func ParseName ¶
ParseName parses s into a Name, and returns the result of filling it with FillDefault. The input string must be a valid string representation of a model
func ParseNameFill ¶
ParseName parses s into a Name, and returns the result of filling it with defaults. The input string must be a valid string representation of a model name in the form:
[host/][namespace/]<model>[:tag][+build][@<digest-type>-<digest>]
The name part is required, all others are optional. If a part is missing, it is left empty in the returned Name. If a part is invalid, the zero Ref value is returned.
The build part is normalized to uppercase.
Examples of valid paths:
"example.com/library/mistral:7b+x" "example.com/eva/mistral:7b+Q4_0" "mistral:7b+x" "example.com/mike/mistral:latest+Q4_0" "example.com/bruce/mistral:latest" "example.com/pdevine/thisisfine:7b+Q4_0@sha256-1234567890abcdef"
Examples of invalid paths:
"example.com/mistral:7b+" "example.com/mistral:7b+Q4_0+" "x/y/z/z:8n+I" ""
It returns the zero value if any part is invalid.
Fills ¶
For any valid s, the fill string is used to fill in missing parts of the Name. The fill string must be a valid Name with the exception that any part may be the string ("?"), which will not be considered for filling.
func ParseNameFromFilepath ¶
ParseNameFromFilepath parses a file path into a Name. The input string must be a valid file path representation of a model name in the form:
host/namespace/model/tag/build
The zero valid is returned if s does not contain all path elements leading up to the model part, or if any path element is an invalid part for the its corresponding part kind.
The fill string is used to fill in missing parts of any constructed Name. See ParseName for more information on the fill string.
func ParseNameFromURLPath ¶
ParseNameFromURLPath parses forms of a URL path into a Name. Specifically, it trims any leading "/" and then calls ParseName with fill.
func (Name) CompareFold ¶
CompareFold performs a case-insensitive cmp.Compare on r and o.
This can be used with slices.SortFunc.
For simple equality checks, use Name.EqualFold.
Example (Sort) ¶
names := []Name{ ParseNameFill("mistral:latest", FillNothing), ParseNameFill("mistRal:7b+q4", FillNothing), ParseNameFill("MIstral:7b", FillNothing), } slices.SortFunc(names, Name.CompareFold) for _, n := range names { fmt.Println(n.DisplayLong()) }
Output: MIstral:7b mistRal:7b+q4 mistral:latest
func (Name) Digest ¶
Digest returns the digest part of the Name, if any.
If Digest returns a non-empty string, then Name.IsResolved will return true, and digest is considered valid.
func (Name) DisplayLong ¶
DisplayLong returns the fullest possible display string in form:
<host>/<namespace>/<model>:<tag>+<build>
If any part is missing, it is omitted from the display string.
func (Name) DisplayLongest ¶
DisplayLongest returns the result of r.DisplayShortest(MaskNothing).
func (Name) DisplayShortest ¶
DisplayShortest returns the shortest possible, masked display string in form:
[host/][<namespace>/]<model>[:<tag>]
Masks ¶
The mask is a string that specifies which parts of the name to omit based on case-insensitive comparison. Name.DisplayShortest omits parts of the name that are the same as the mask, moving from left to right until the first unequal part is found. It then moves right to left until the first unequal part is found. The result is the shortest possible display string.
Unlike a Name the mask can contain "?" characters which are treated as wildcards. A "?" will never match a part of the name, since a valid name can never contain a "?" character.
For example: Given a Name ("registry.ollama.ai/library/mistral:latest") masked with ("registry.ollama.ai/library/?:latest") will produce the display string ("mistral").
If mask is the empty string, then MaskDefault is used.
DisplayShortest panics if the mask is not the empty string, MaskNothing, and invalid.
Builds ¶
For now, DisplayShortest does consider the build or return one in the result. We can lift this restriction when needed.
Example ¶
name := ParseNameFill("example.com/jmorganca/mistral:latest+Q4_0", FillNothing) fmt.Println(name.DisplayShortest("example.com/jmorganca/_:latest")) fmt.Println(name.DisplayShortest("example.com/_/_:latest")) fmt.Println(name.DisplayShortest("example.com/_/_:_")) fmt.Println(name.DisplayShortest("_/_/_:_")) // Default name = ParseNameFill("registry.ollama.ai/library/mistral:latest+Q4_0", FillNothing) fmt.Println(name.DisplayShortest(""))
Output: mistral jmorganca/mistral jmorganca/mistral:latest example.com/jmorganca/mistral:latest mistral
func (Name) DisplayURLPath ¶
URLPath returns a complete, canonicalized, relative URL path using the parts of a complete Name.
The parts maintain their original case.
Example:
ParseName("example.com/namespace/model:tag+build").URLPath() // returns "/example.com/namespace/model:tag"
func (Name) EqualFold ¶
EqualFold reports whether r and o are equivalent model names, ignoring case.
func (Name) Filepath ¶
Filepath returns a complete, canonicalized, relative file path using the parts of a complete Name.
Each parts is downcased, except for the build part which is upcased.
Example:
ParseName("example.com/namespace/model:tag+build").Filepath() // returns "example.com/namespace/model/tag/BUILD"
func (Name) FilepathNoBuild ¶
FilepathNoBuild returns a complete, canonicalized, relative file path using the parts of a complete Name, but without the build part.
func (Name) GoString ¶
GoString implements fmt.GoStringer. It returns a string suitable for debugging and logging. It is similar to Name.DisplayLong but it always returns a string that includes all parts of the Name, with missing parts replaced with a ("?").
func (Name) IsComplete ¶
IsComplete reports whether the Name is fully qualified. That is it has a domain, namespace, name, tag, and build.
func (Name) IsCompleteNoBuild ¶
IsCompleteNoBuild is like Name.IsComplete but it does not require the build part to be present.
func (Name) IsResolved ¶
IsResolved reports true if the Name has a valid digest.
It is possible to have a valid Name, or a complete Name that is not resolved.
func (Name) MapHash ¶
MapHash returns a case insensitive hash for use in maps and equality checks. For a convenient way to compare names, use Name.EqualFold.
Example ¶
m := map[uint64]bool{} // key 1 m[ParseNameFill("mistral:latest+q4", FillNothing).MapHash()] = true m[ParseNameFill("miSTRal:latest+Q4", FillNothing).MapHash()] = true m[ParseNameFill("mistral:LATest+Q4", FillNothing).MapHash()] = true // key 2 m[ParseNameFill("mistral:LATest", FillNothing).MapHash()] = true fmt.Println(len(m))
Output: 2
func (Name) URLPath ¶
URLPath returns a complete, canonicalized, relative URL path using the parts of a complete Name in the form:
<host>/<namespace>/<model>/<tag>
The parts are downcased.