feat: add download features to llm models
This commit is contained in:
@ -7,31 +7,51 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ollamaProvider struct {
|
||||
// OllamaModelInfo holds detailed info about an installed Ollama model.
|
||||
type OllamaModelInfo struct {
|
||||
Name string `json:"name"`
|
||||
Size int64 `json:"size"`
|
||||
ModifiedAt string `json:"modified_at"`
|
||||
Details struct {
|
||||
ParameterSize string `json:"parameter_size"`
|
||||
QuantizationLevel string `json:"quantization_level"`
|
||||
Family string `json:"family"`
|
||||
} `json:"details"`
|
||||
}
|
||||
|
||||
// OllamaProvider implements Provider for Ollama and also exposes model management operations.
|
||||
type OllamaProvider struct {
|
||||
endpoint string
|
||||
model string
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func newOllama(endpoint, model string) *ollamaProvider {
|
||||
func newOllama(endpoint, model string) *OllamaProvider {
|
||||
if endpoint == "" {
|
||||
endpoint = "http://ollama:11434"
|
||||
}
|
||||
if model == "" {
|
||||
model = "llama3"
|
||||
}
|
||||
return &ollamaProvider{
|
||||
return &OllamaProvider{
|
||||
endpoint: endpoint,
|
||||
model: model,
|
||||
client: &http.Client{},
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ollamaProvider) Name() string { return "ollama" }
|
||||
// NewOllamaManager creates an OllamaProvider for model management (pull/delete/list).
|
||||
func NewOllamaManager(endpoint string) *OllamaProvider {
|
||||
return newOllama(endpoint, "")
|
||||
}
|
||||
|
||||
func (p *ollamaProvider) Summarize(ctx context.Context, prompt string, opts GenOptions) (string, error) {
|
||||
|
||||
func (p *OllamaProvider) Name() string { return "ollama" }
|
||||
|
||||
func (p *OllamaProvider) Summarize(ctx context.Context, prompt string, opts GenOptions) (string, error) {
|
||||
numCtx := 32768
|
||||
if opts.NumCtx > 0 {
|
||||
numCtx = opts.NumCtx
|
||||
@ -72,7 +92,19 @@ func (p *ollamaProvider) Summarize(ctx context.Context, prompt string, opts GenO
|
||||
return result.Response, nil
|
||||
}
|
||||
|
||||
func (p *ollamaProvider) ListModels(ctx context.Context) ([]string, error) {
|
||||
func (p *OllamaProvider) ListModels(ctx context.Context) ([]string, error) {
|
||||
infos, err := p.ListModelsDetailed(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
names := make([]string, len(infos))
|
||||
for i, m := range infos {
|
||||
names[i] = m.Name
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
func (p *OllamaProvider) ListModelsDetailed(ctx context.Context) ([]OllamaModelInfo, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, p.endpoint+"/api/tags", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -85,16 +117,52 @@ func (p *ollamaProvider) ListModels(ctx context.Context) ([]string, error) {
|
||||
|
||||
raw, _ := io.ReadAll(resp.Body)
|
||||
var result struct {
|
||||
Models []struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"models"`
|
||||
Models []OllamaModelInfo `json:"models"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var models []string
|
||||
for _, m := range result.Models {
|
||||
models = append(models, m.Name)
|
||||
}
|
||||
return models, nil
|
||||
return result.Models, nil
|
||||
}
|
||||
|
||||
// PullModel pulls (downloads) a model from Ollama Hub. Blocks until complete.
|
||||
func (p *OllamaProvider) PullModel(ctx context.Context, name string) error {
|
||||
body, _ := json.Marshal(map[string]interface{}{"name": name, "stream": false})
|
||||
// Use a long-timeout client since model downloads can take many minutes
|
||||
client := &http.Client{Timeout: 60 * time.Minute}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, p.endpoint+"/api/pull", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
raw, _ := io.ReadAll(resp.Body)
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("ollama pull error %d: %s", resp.StatusCode, raw)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteModel removes a model from local storage.
|
||||
func (p *OllamaProvider) DeleteModel(ctx context.Context, name string) error {
|
||||
body, _ := json.Marshal(map[string]string{"name": name})
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, p.endpoint+"/api/delete", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := p.client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
|
||||
raw, _ := io.ReadAll(resp.Body)
|
||||
return fmt.Errorf("ollama delete error %d: %s", resp.StatusCode, raw)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user