Skip to content

Commit 8d8dc4b

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

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

cmd/build.go

Lines changed: 55 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,71 @@ 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+
} else if !filepath.IsAbs(modelfilePath) {
85+
modelfilePath = filepath.Join(workDir, modelfilePath)
86+
}
87+
88+
shouldGenerate := buildConfig.Regenerate
89+
90+
if shouldGenerate {
91+
fmt.Println("Regenerate flag detected. Regenerating Modelfile...")
92+
} else {
93+
_, err := os.Stat(modelfilePath)
94+
if os.IsNotExist(err) {
95+
fmt.Println("No Modelfile found. Generating automatically...")
96+
shouldGenerate = true
97+
} else if err != nil {
98+
return fmt.Errorf("error checking Modelfile at %s: %w", modelfilePath, err)
99+
}
100+
}
101+
102+
if shouldGenerate {
103+
genConfig := configmodelfile.NewGenerateConfig()
104+
105+
absWorkDir, err := filepath.Abs(workDir)
106+
if err != nil {
107+
return fmt.Errorf("failed to resolve workspace path: %w", err)
108+
}
109+
110+
genConfig.Workspace = absWorkDir
111+
genConfig.Overwrite = true
112+
113+
mf, err := modelfilegen.NewModelfileByWorkspace(genConfig.Workspace, genConfig)
114+
if err != nil {
115+
return fmt.Errorf("failed to auto-generate modelfile: %w", err)
116+
}
117+
118+
content := mf.Content()
119+
if err := os.WriteFile(modelfilePath, content, 0644); err != nil {
120+
return fmt.Errorf("failed to write modelfile: %w", err)
121+
}
122+
123+
fmt.Printf("Successfully generated %s\n", modelfilePath)
124+
}
125+
73126
b, err := backend.New(rootConfig.StoargeDir)
74127
if err != nil {
75128
return err
76129
}
77130

78-
if err := b.Build(ctx, buildConfig.Modelfile, workDir, buildConfig.Target, buildConfig); err != nil {
131+
if err := b.Build(ctx, modelfilePath, workDir, buildConfig.Target, buildConfig); err != nil {
79132
return err
80133
}
81134

82135
fmt.Printf("Successfully built model artifact: %s\n", buildConfig.Target)
83-
84136
return nil
85137
}

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)