model

package
v0.1.37 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package model contains types and utilities for parsing, validating, and working with model names and digests.

Index

Constants

View Source
const MissingPart = "!MISSING!"

MissingPart is used to indicate any part of a name that was "promised" by the presence of a separator, but is missing.

The value was chosen because it is deemed unlikely to be set by a user, not a valid part name valid when checked by Name.IsValid, and easy to spot in logs.

Variables

View Source
var (
	// ErrUnqualifiedName represents an error where a name is not fully
	// qualified. It is not used directly in this package, but is here
	// to avoid other packages inventing their own error type.
	// Additionally, it can be conveniently used via [Unqualified].
	ErrUnqualifiedName = errors.New("unqualified name")
)

Errors

Functions

func Unqualified added in v0.1.33

func Unqualified(n Name) error

Unqualified is a helper function that returns an error with ErrUnqualifiedName as the cause and the name as the message.

Types

type Command added in v0.1.34

type Command struct {
	Name string
	Args string
}

func (Command) String added in v0.1.34

func (c Command) String() string

type Digest

type Digest struct {
	Type DigestType
	Sum  [32]byte
}

func ParseDigest

func ParseDigest(s string) (Digest, error)

func (Digest) IsValid

func (d Digest) IsValid() bool

func (Digest) String

func (d Digest) String() string

type DigestType added in v0.1.33

type DigestType byte
const (
	DigestTypeInvalid DigestType = iota
	DigestTypeSHA256
)

func (DigestType) String added in v0.1.33

func (t DigestType) String() string

type File added in v0.1.34

type File struct {
	Commands []Command
}

func ParseFile added in v0.1.34

func ParseFile(r io.Reader) (*File, error)

func (File) String added in v0.1.34

func (f File) String() string

type Name

type Name struct {
	Host      string
	Namespace string
	Model     string
	Tag       string
	RawDigest string
}

Name is a structured representation of a model name string, as defined by [ParseNameNoDefaults].

It is not guaranteed to be valid. Use Name.IsValid to check if the name is valid.

func DefaultName added in v0.1.33

func DefaultName() Name

DefaultName returns a name with the default values for the host, namespace, and tag parts. The model and digest parts are empty.

  • The default host is ("registry.ollama.ai")
  • The default namespace is ("library")
  • The default tag is ("latest")

func Merge added in v0.1.33

func Merge(a, b Name) Name

Merge merges the host, namespace, and tag parts of the two names, preferring the non-empty parts of a.

func ParseName

func ParseName(s string) Name

ParseName parses and assembles a Name from a name string. The format of a valid name string is:

  s:
	  { host } "/" { namespace } "/" { model } ":" { tag } "@" { digest }
	  { host } "/" { namespace } "/" { model } ":" { tag }
	  { host } "/" { namespace } "/" { model } "@" { digest }
	  { host } "/" { namespace } "/" { model }
	  { namespace } "/" { model } ":" { tag } "@" { digest }
	  { namespace } "/" { model } ":" { tag }
	  { namespace } "/" { model } "@" { digest }
	  { namespace } "/" { model }
	  { model } ":" { tag } "@" { digest }
	  { model } ":" { tag }
	  { model } "@" { digest }
	  { model }
	  "@" { digest }
  host:
      pattern: { alphanum | "_" } { alphanum | "-" | "_" | "." | ":" }*
      length:  [1, 350]
  namespace:
      pattern: { alphanum | "_" } { alphanum | "-" | "_" }*
      length:  [1, 80]
  model:
      pattern: { alphanum | "_" } { alphanum | "-" | "_" | "." }*
      length:  [1, 80]
  tag:
      pattern: { alphanum | "_" } { alphanum | "-" | "_" | "." }*
      length:  [1, 80]
  digest:
      pattern: { alphanum | "_" } { alphanum | "-" | ":" }*
      length:  [1, 80]

Most users should use ParseName instead, unless need to support different defaults than DefaultName.

The name returned is not guaranteed to be valid. If it is not valid, the field values are left in an undefined state. Use Name.IsValid to check if the name is valid.

func ParseNameBare added in v0.1.33

func ParseNameBare(s string) Name

ParseNameBare parses s as a name string and returns a Name. No merge with DefaultName is performed.

func ParseNameFromFilepath

func ParseNameFromFilepath(s string) (n Name)

ParseNameFromFilepath parses a 4-part filepath as a Name. The parts are expected to be in the form:

{ host } "/" { namespace } "/" { model } "/" { tag }

func (Name) DisplayShortest

func (n Name) DisplayShortest() string

DisplayShort returns a short string version of the name.

func (Name) Filepath

func (n Name) Filepath() string

Filepath returns a canonical filepath that represents the name with each part from host to tag as a directory in the form:

{host}/{namespace}/{model}/{tag}

It uses the system's filepath separator and ensures the path is clean.

It panics if the name is not fully qualified. Use Name.IsFullyQualified to check if the name is fully qualified.

func (Name) IsFullyQualified added in v0.1.33

func (n Name) IsFullyQualified() bool

IsFullyQualified returns true if all parts of the name are present and valid without the digest.

func (Name) IsValid

func (n Name) IsValid() bool

IsValid reports whether all parts of the name are present and valid. The digest is a special case, and is checked for validity only if present.

func (Name) LogValue

func (n Name) LogValue() slog.Value

LogValue returns a slog.Value that represents the name as a string.

func (Name) String added in v0.1.33

func (n Name) String() string

String returns the name string, in the format that [ParseNameNoDefaults] accepts as valid, if Name.IsValid reports true; otherwise the empty string is returned.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL