Skip to content

Commit e093354

Browse files
committed
Add Feature(build):- auto-generate Modelfile and add --regenerate flag
Signed-off-by: Aravind <gmarav005@gmail.com>
1 parent 06ac8b1 commit e093354

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

cmd/build.go

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@ package cmd
1919
import (
2020
"context"
2121
"fmt"
22+
"os"
23+
"path/filepath"
2224

2325
"github.com/spf13/cobra"
2426
"github.com/spf13/viper"
2527

2628
"github.com/modelpack/modctl/pkg/backend"
2729
"github.com/modelpack/modctl/pkg/config"
30+
31+
configmodelfile "github.com/modelpack/modctl/pkg/config/modelfile"
32+
modelfilegen "github.com/modelpack/modctl/pkg/modelfile"
2833
)
2934

3035
var buildConfig = config.NewBuild()
@@ -62,24 +67,64 @@ func init() {
6267
flags.BoolVar(&buildConfig.Raw, "raw", true, "turning on this flag will build model artifact layers in raw format")
6368
flags.BoolVar(&buildConfig.Reasoning, "reasoning", false, "turning on this flag will mark this model as reasoning model in the config")
6469
flags.BoolVar(&buildConfig.NoCreationTime, "no-creation-time", false, "turning on this flag will not set createdAt in the config, which will be helpful for repeated builds")
70+
flags.BoolVar(&buildConfig.Regenerate, "regenerate", false, "force regenerate Modelfile before building")
6571

6672
if err := viper.BindPFlags(flags); err != nil {
6773
panic(fmt.Errorf("bind cache list flags to viper: %w", err))
6874
}
75+
6976
}
7077

71-
// runBuild runs the build modctl.
7278
func runBuild(ctx context.Context, workDir string) error {
79+
80+
// Determine modelfile path
81+
modelfilePath := buildConfig.Modelfile
82+
if modelfilePath == "" {
83+
modelfilePath = filepath.Join(workDir, configmodelfile.DefaultModelfileName)
84+
}
85+
86+
shouldGenerate := false
87+
88+
if buildConfig.Regenerate {
89+
fmt.Println("Regenerate flag detected. Regenerating Modelfile...")
90+
shouldGenerate = true
91+
} else if _, err := os.Stat(modelfilePath); os.IsNotExist(err) {
92+
fmt.Println("No Modelfile found. Generating automatically...")
93+
shouldGenerate = true
94+
}
95+
96+
if shouldGenerate {
97+
genConfig := configmodelfile.NewGenerateConfig()
98+
genConfig.Workspace = workDir
99+
genConfig.Output = workDir
100+
genConfig.Overwrite = true
101+
102+
if err := genConfig.Convert(workDir); err != nil {
103+
return fmt.Errorf("failed to prepare modelfile generation: %w", err)
104+
}
105+
106+
mf, err := modelfilegen.NewModelfileByWorkspace(genConfig.Workspace, genConfig)
107+
if err != nil {
108+
return fmt.Errorf("failed to auto-generate modelfile: %w", err)
109+
}
110+
111+
content := mf.Content()
112+
if err := os.WriteFile(modelfilePath, content, 0644); err != nil {
113+
return fmt.Errorf("failed to write modelfile: %w", err)
114+
}
115+
116+
fmt.Printf("Successfully generated %s\n", modelfilePath)
117+
}
118+
73119
b, err := backend.New(rootConfig.StoargeDir)
74120
if err != nil {
75121
return err
76122
}
77123

78-
if err := b.Build(ctx, buildConfig.Modelfile, workDir, buildConfig.Target, buildConfig); err != nil {
124+
if err := b.Build(ctx, modelfilePath, workDir, buildConfig.Target, buildConfig); err != nil {
79125
return err
80126
}
81127

82128
fmt.Printf("Successfully built model artifact: %s\n", buildConfig.Target)
83-
84129
return nil
85130
}

pkg/config/build.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Build struct {
3636
Raw bool
3737
Reasoning bool
3838
NoCreationTime bool
39+
Regenerate bool
3940
}
4041

4142
func NewBuild() *Build {
@@ -52,6 +53,7 @@ func NewBuild() *Build {
5253
Raw: false,
5354
Reasoning: false,
5455
NoCreationTime: false,
56+
Regenerate: false,
5557
}
5658
}
5759

0 commit comments

Comments
 (0)