-
Notifications
You must be signed in to change notification settings - Fork 28
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") | ||||||||||||||||||||||||||||||||
|
Member
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.
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||
|
Member
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 below function should be abstracted into one single responsibility function. |
||||||||||||||||||||||||||||||||
| modelfilePath := buildConfig.Modelfile | ||||||||||||||||||||||||||||||||
| if modelfilePath == "" { | ||||||||||||||||||||||||||||||||
| modelfilePath = filepath.Join(workDir, configmodelfile.DefaultModelfileName) | ||||||||||||||||||||||||||||||||
| } else if !filepath.IsAbs(modelfilePath) { | ||||||||||||||||||||||||||||||||
| modelfilePath = filepath.Join(workDir, modelfilePath) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+81
to
+86
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
Suggested change
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| 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.
Align the golang standard style, suggest to use
pkgmodelfile.