-
Notifications
You must be signed in to change notification settings - Fork 125
add modelfile support to docker model package #903
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
Draft
areebahmeddd
wants to merge
4
commits into
docker:main
Choose a base branch
from
areebahmeddd:feat/modelfile-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "slices" | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| // modelfileAliases maps Modelfile instruction aliases to their canonical names. | ||
| var modelfileAliases = map[string]string{ | ||
| "SAFETENSORS-DIR": "SAFETENSORS_DIR", | ||
| "CHAT-TEMPLATE": "CHAT_TEMPLATE", | ||
| "MM-PROJ": "MMPROJ", | ||
| "CTX": "CONTEXT", | ||
| "CONTEXT-SIZE": "CONTEXT", | ||
| } | ||
|
|
||
| // modelfilePathInstructions is the set of instructions whose value is a file or directory path. | ||
| var modelfilePathInstructions = map[string]struct{}{ | ||
| "GGUF": {}, | ||
| "SAFETENSORS_DIR": {}, | ||
| "DDUF": {}, | ||
| "LICENSE": {}, | ||
| "CHAT_TEMPLATE": {}, | ||
| "MMPROJ": {}, | ||
| } | ||
|
|
||
| // applyModelfile reads opts.modelfile and applies its directives to opts. | ||
| // CLI flags take precedence over Modelfile values. | ||
| func applyModelfile(opts *packageOptions) error { | ||
| if opts.modelfile == "" { | ||
| return nil | ||
| } | ||
|
|
||
| absModelfile, err := filepath.Abs(opts.modelfile) | ||
| if err != nil { | ||
| return fmt.Errorf("resolve Modelfile path %q: %w", opts.modelfile, err) | ||
| } | ||
| baseDir := filepath.Dir(absModelfile) | ||
|
|
||
| f, err := os.Open(absModelfile) | ||
| if err != nil { | ||
| return fmt.Errorf("open Modelfile %q: %w", opts.modelfile, err) | ||
| } | ||
| defer f.Close() | ||
|
|
||
| scanner := bufio.NewScanner(f) | ||
| lineNum := 0 | ||
|
|
||
| for scanner.Scan() { | ||
| lineNum++ | ||
| line := strings.TrimSpace(scanner.Text()) | ||
| if line == "" || strings.HasPrefix(line, "#") { | ||
| continue | ||
| } | ||
|
|
||
| fields := strings.Fields(line) | ||
| if len(fields) < 2 { | ||
| return fmt.Errorf("Modelfile line %d: expected an instruction and a value, got: %q", lineNum, line) | ||
| } | ||
|
|
||
| instruction := strings.ToUpper(fields[0]) | ||
| if canonical, ok := modelfileAliases[instruction]; ok { | ||
| instruction = canonical | ||
| } | ||
|
|
||
| value := strings.Join(fields[1:], " ") | ||
|
|
||
| var absPath string | ||
| if _, isPath := modelfilePathInstructions[instruction]; isPath { | ||
| absPath, err = modelfileResolvePath(value, baseDir) | ||
| if err != nil { | ||
| return fmt.Errorf("Modelfile line %d: invalid path for %s: %w", lineNum, instruction, err) | ||
| } | ||
|
|
||
| info, statErr := os.Stat(absPath) | ||
| if statErr != nil { | ||
| return fmt.Errorf("Modelfile line %d: path for %s not found: %q", lineNum, instruction, absPath) | ||
| } | ||
|
|
||
| switch instruction { | ||
| case "SAFETENSORS_DIR": | ||
| if !info.IsDir() { | ||
| return fmt.Errorf("Modelfile line %d: SAFETENSORS_DIR must be a directory: %q", lineNum, absPath) | ||
| } | ||
| case "GGUF", "DDUF", "LICENSE", "CHAT_TEMPLATE", "MMPROJ": | ||
| if info.IsDir() { | ||
| return fmt.Errorf("Modelfile line %d: %s must be a file, not a directory: %q", lineNum, instruction, absPath) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| switch instruction { | ||
| // Model sources | ||
| case "FROM": | ||
| if opts.fromModel == "" { | ||
| if strings.HasPrefix(value, "./") || strings.HasPrefix(value, "../") || filepath.IsAbs(value) { | ||
| return fmt.Errorf("Modelfile line %d: FROM takes a model reference, not a file path; use GGUF or SAFETENSORS_DIR instead", lineNum) | ||
| } | ||
| opts.fromModel = value | ||
| } | ||
|
|
||
| case "GGUF": | ||
| if opts.ggufPath == "" { | ||
| opts.ggufPath = absPath | ||
| } | ||
|
|
||
| case "SAFETENSORS_DIR": | ||
| if opts.safetensorsDir == "" { | ||
| opts.safetensorsDir = absPath | ||
| } | ||
|
|
||
| case "DDUF": | ||
| if opts.ddufPath == "" { | ||
| opts.ddufPath = absPath | ||
| } | ||
|
|
||
| // Optional assets | ||
| case "LICENSE": | ||
| if !slices.Contains(opts.licensePaths, absPath) { | ||
| opts.licensePaths = append(opts.licensePaths, absPath) | ||
| } | ||
|
|
||
| case "CHAT_TEMPLATE": | ||
| if opts.chatTemplatePath == "" { | ||
| opts.chatTemplatePath = absPath | ||
| } | ||
|
|
||
| case "MMPROJ": | ||
| if opts.mmprojPath == "" { | ||
| opts.mmprojPath = absPath | ||
| } | ||
|
|
||
| // Parameters | ||
| case "CONTEXT": | ||
| if opts.contextSize == 0 { | ||
| v, parseErr := strconv.ParseUint(value, 10, 64) | ||
| if parseErr != nil || v == 0 { | ||
| return fmt.Errorf("Modelfile line %d: invalid CONTEXT value %q: must be a positive integer", lineNum, value) | ||
| } | ||
| opts.contextSize = v | ||
| opts.contextSizeSet = true | ||
| } | ||
|
|
||
| default: | ||
| return fmt.Errorf("Modelfile line %d: unknown instruction %q", lineNum, instruction) | ||
|
areebahmeddd marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| if err := scanner.Err(); err != nil { | ||
| return fmt.Errorf("read Modelfile %q: %w", opts.modelfile, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // modelfileResolvePath returns path as an absolute cleaned path, resolved | ||
| // relative to baseDir when path is not already absolute. | ||
| func modelfileResolvePath(path, baseDir string) (string, error) { | ||
| if !filepath.IsAbs(path) { | ||
| path = filepath.Join(baseDir, path) | ||
| } | ||
| abs, err := filepath.Abs(path) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return filepath.Clean(abs), nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.