-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep_model_pull.go
More file actions
141 lines (127 loc) · 3.96 KB
/
step_model_pull.go
File metadata and controls
141 lines (127 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package agent
import (
"context"
"fmt"
"os"
"github.com/GoCodeAlone/modular"
"github.com/GoCodeAlone/workflow-plugin-agent/provider"
"github.com/GoCodeAlone/workflow/module"
"github.com/GoCodeAlone/workflow/plugin"
)
// ModelPullStep ensures a model is available before agent execution.
// For "ollama" source it pulls from an Ollama server; for "huggingface"
// it downloads from HuggingFace Hub.
type ModelPullStep struct {
name string
source string // "ollama" or "huggingface"
model string // model name / HF repo (e.g. "org/repo")
file string // HuggingFace file name within the repo
outputDir string // HuggingFace local destination; defaults to ~/.cache/workflow/models
ollamaBase string // Ollama server base URL; defaults to http://localhost:11434
}
func (s *ModelPullStep) Name() string { return s.name }
func (s *ModelPullStep) Execute(ctx context.Context, _ *module.PipelineContext) (*module.StepResult, error) {
switch s.source {
case "ollama":
return s.pullOllama(ctx)
case "huggingface":
return s.pullHuggingFace(ctx)
default:
return &module.StepResult{
Output: map[string]any{
"status": "error",
"model_path": "",
"size_bytes": 0,
"error": fmt.Sprintf("unknown source %q: must be \"ollama\" or \"huggingface\"", s.source),
},
}, nil
}
}
func (s *ModelPullStep) pullOllama(ctx context.Context) (*module.StepResult, error) {
client := provider.NewOllamaClient(s.ollamaBase)
// Check if model is already available.
models, err := client.ListModels(ctx)
if err == nil {
for _, m := range models {
if m.ID == s.model || m.Name == s.model {
return &module.StepResult{
Output: map[string]any{
"status": "ready",
"model_path": s.model,
"size_bytes": 0,
},
}, nil
}
}
}
// Pull (download) the model.
if pullErr := client.Pull(ctx, s.model, nil); pullErr != nil {
return &module.StepResult{
Output: map[string]any{
"status": "error",
"model_path": "",
"size_bytes": 0,
"error": pullErr.Error(),
},
}, nil
}
return &module.StepResult{
Output: map[string]any{
"status": "downloaded",
"model_path": s.model,
"size_bytes": 0,
},
}, nil
}
func (s *ModelPullStep) pullHuggingFace(ctx context.Context) (*module.StepResult, error) {
path, err := provider.DownloadHuggingFaceFile(ctx, s.model, s.file, s.outputDir, nil)
if err != nil {
return &module.StepResult{
Output: map[string]any{
"status": "error",
"model_path": "",
"size_bytes": 0,
"error": err.Error(),
},
}, nil
}
// Determine whether the file was already present by checking mtime vs request timing.
// DownloadHuggingFaceFile returns early (skip) when the file already exists — we detect
// this by statting and checking size, which is simpler than an extra timestamp check.
fi, statErr := os.Stat(path)
var sizeBytes int64
if statErr == nil {
sizeBytes = fi.Size()
}
// DownloadHuggingFaceFile skips downloading when the file exists; we call it "ready".
// Without introspecting the function internals, we conservatively report "downloaded".
// A future optimisation could pass a flag via context or return value.
return &module.StepResult{
Output: map[string]any{
"status": "downloaded",
"model_path": path,
"size_bytes": sizeBytes,
},
}, nil
}
// newModelPullStepFactory returns a plugin.StepFactory for "step.model_pull".
func newModelPullStepFactory() plugin.StepFactory {
return func(name string, cfg map[string]any, _ modular.Application) (any, error) {
source, _ := cfg["provider"].(string) // "ollama" or "huggingface"
if source == "" {
source, _ = cfg["source"].(string)
}
model, _ := cfg["model"].(string)
file, _ := cfg["file"].(string)
outputDir, _ := cfg["output_dir"].(string)
ollamaBase, _ := cfg["base_url"].(string)
return &ModelPullStep{
name: name,
source: source,
model: model,
file: file,
outputDir: outputDir,
ollamaBase: ollamaBase,
}, nil
}
}