-
Notifications
You must be signed in to change notification settings - Fork 27
Add Feature(build): auto-generate Modelfile and add --regenerate flag #414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,12 +19,17 @@ package cmd | |||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||||||||
| "path/filepath" | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| "github.com/spf13/cobra" | ||||||||||||||||||||||||||||||||
| "github.com/spf13/viper" | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| "github.com/modelpack/modctl/pkg/backend" | ||||||||||||||||||||||||||||||||
| "github.com/modelpack/modctl/pkg/config" | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| configmodelfile "github.com/modelpack/modctl/pkg/config/modelfile" | ||||||||||||||||||||||||||||||||
| modelfilegen "github.com/modelpack/modctl/pkg/modelfile" | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| var buildConfig = config.NewBuild() | ||||||||||||||||||||||||||||||||
|
|
@@ -62,24 +67,71 @@ func init() { | |||||||||||||||||||||||||||||||
| flags.BoolVar(&buildConfig.Raw, "raw", true, "turning on this flag will build model artifact layers in raw format") | ||||||||||||||||||||||||||||||||
| flags.BoolVar(&buildConfig.Reasoning, "reasoning", false, "turning on this flag will mark this model as reasoning model in the config") | ||||||||||||||||||||||||||||||||
| 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") | ||||||||||||||||||||||||||||||||
| flags.BoolVar(&buildConfig.Regenerate, "regenerate", false, "force regenerate Modelfile before building") | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if err := viper.BindPFlags(flags); err != nil { | ||||||||||||||||||||||||||||||||
| panic(fmt.Errorf("bind cache list flags to viper: %w", err)) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // runBuild runs the build modctl. | ||||||||||||||||||||||||||||||||
| func runBuild(ctx context.Context, workDir string) error { | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // Determine modelfile path | ||||||||||||||||||||||||||||||||
| modelfilePath := buildConfig.Modelfile | ||||||||||||||||||||||||||||||||
| if modelfilePath == "" { | ||||||||||||||||||||||||||||||||
| modelfilePath = filepath.Join(workDir, configmodelfile.DefaultModelfileName) | ||||||||||||||||||||||||||||||||
| } else if !filepath.IsAbs(modelfilePath) { | ||||||||||||||||||||||||||||||||
| modelfilePath = filepath.Join(workDir, modelfilePath) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| shouldGenerate := buildConfig.Regenerate | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if shouldGenerate { | ||||||||||||||||||||||||||||||||
| fmt.Println("Regenerate flag detected. Regenerating Modelfile...") | ||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||
| _, err := os.Stat(modelfilePath) | ||||||||||||||||||||||||||||||||
| if os.IsNotExist(err) { | ||||||||||||||||||||||||||||||||
| fmt.Println("No Modelfile found. Generating automatically...") | ||||||||||||||||||||||||||||||||
| shouldGenerate = true | ||||||||||||||||||||||||||||||||
| } else if err != nil { | ||||||||||||||||||||||||||||||||
| return fmt.Errorf("error checking Modelfile at %s: %w", modelfilePath, err) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if shouldGenerate { | ||||||||||||||||||||||||||||||||
| genConfig := configmodelfile.NewGenerateConfig() | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| absWorkDir, err := filepath.Abs(workDir) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| return fmt.Errorf("failed to resolve workspace path: %w", err) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+103
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The setup for
Suggested change
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| genConfig.Workspace = absWorkDir | ||||||||||||||||||||||||||||||||
| genConfig.Overwrite = true | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| mf, err := modelfilegen.NewModelfileByWorkspace(genConfig.Workspace, genConfig) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| return fmt.Errorf("failed to auto-generate modelfile: %w", err) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| content := mf.Content() | ||||||||||||||||||||||||||||||||
| if err := os.WriteFile(modelfilePath, content, 0644); err != nil { | ||||||||||||||||||||||||||||||||
| return fmt.Errorf("failed to write modelfile: %w", err) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| fmt.Printf("Successfully generated %s\n", modelfilePath) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| b, err := backend.New(rootConfig.StoargeDir) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if err := b.Build(ctx, buildConfig.Modelfile, workDir, buildConfig.Target, buildConfig); err != nil { | ||||||||||||||||||||||||||||||||
| if err := b.Build(ctx, modelfilePath, workDir, buildConfig.Target, buildConfig); err != nil { | ||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| fmt.Printf("Successfully built model artifact: %s\n", buildConfig.Target) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
modelfilePathis not correctly resolved against theworkDir. When a relative path is used for the modelfile (which is the default),os.Stat(modelfilePath)will check for the file in the current working directory of themodctlprocess, not in the specifiedworkDir. This can lead to incorrect behavior, such as failing to find an existingModelfileor generating a new one in the wrong location. The path should be explicitly joined withworkDirif it's relative.