-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathbuilder.go
More file actions
100 lines (88 loc) · 3.09 KB
/
builder.go
File metadata and controls
100 lines (88 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package builder
import (
"context"
"fmt"
"io"
"github.com/docker/model-runner/pkg/distribution/internal/gguf"
"github.com/docker/model-runner/pkg/distribution/internal/mutate"
"github.com/docker/model-runner/pkg/distribution/internal/partial"
"github.com/docker/model-runner/pkg/distribution/internal/safetensors"
"github.com/docker/model-runner/pkg/distribution/types"
)
// Builder builds a model artifact
type Builder struct {
model types.ModelArtifact
}
// FromGGUF returns a *Builder that builds a model artifacts from a GGUF file
func FromGGUF(path string) (*Builder, error) {
mdl, err := gguf.NewModel(path)
if err != nil {
return nil, err
}
return &Builder{
model: mdl,
}, nil
}
// FromSafetensorsWithConfig returns a *Builder that builds model artifacts from safetensors files with a config archive
func FromSafetensorsWithConfig(safetensorsPaths []string, configArchivePath string) (*Builder, error) {
mdl, err := safetensors.NewModelWithConfigArchive(safetensorsPaths, configArchivePath)
if err != nil {
return nil, err
}
return &Builder{
model: mdl,
}, nil
}
// WithLicense adds a license file to the artifact
func (b *Builder) WithLicense(path string) (*Builder, error) {
licenseLayer, err := partial.NewLayer(path, types.MediaTypeLicense)
if err != nil {
return nil, fmt.Errorf("license layer from %q: %w", path, err)
}
return &Builder{
model: mutate.AppendLayers(b.model, licenseLayer),
}, nil
}
func (b *Builder) WithContextSize(size uint64) *Builder {
return &Builder{
model: mutate.ContextSize(b.model, size),
}
}
// WithMultimodalProjector adds a Multimodal projector file to the artifact
func (b *Builder) WithMultimodalProjector(path string) (*Builder, error) {
mmprojLayer, err := partial.NewLayer(path, types.MediaTypeMultimodalProjector)
if err != nil {
return nil, fmt.Errorf("mmproj layer from %q: %w", path, err)
}
return &Builder{
model: mutate.AppendLayers(b.model, mmprojLayer),
}, nil
}
// WithChatTemplateFile adds a Jinja chat template file to the artifact which takes precedence over template from GGUF.
func (b *Builder) WithChatTemplateFile(path string) (*Builder, error) {
templateLayer, err := partial.NewLayer(path, types.MediaTypeChatTemplate)
if err != nil {
return nil, fmt.Errorf("chat template layer from %q: %w", path, err)
}
return &Builder{
model: mutate.AppendLayers(b.model, templateLayer),
}, nil
}
// WithConfigArchive adds a config archive (tar) file to the artifact
func (b *Builder) WithConfigArchive(path string) (*Builder, error) {
configLayer, err := partial.NewLayer(path, types.MediaTypeVLLMConfigArchive)
if err != nil {
return nil, fmt.Errorf("config archive layer from %q: %w", path, err)
}
return &Builder{
model: mutate.AppendLayers(b.model, configLayer),
}, nil
}
// Target represents a build target
type Target interface {
Write(context.Context, types.ModelArtifact, io.Writer) error
}
// Build finalizes the artifact and writes it to the given target, reporting progress to the given writer
func (b *Builder) Build(ctx context.Context, target Target, pw io.Writer) error {
return target.Write(ctx, b.model, pw)
}