File size: 2,365 Bytes
651d019
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package oci

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"

	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

// Define the main struct for the JSON data
type Manifest struct {
	SchemaVersion int           `json:"schemaVersion"`
	MediaType     string        `json:"mediaType"`
	Config        Config        `json:"config"`
	Layers        []LayerDetail `json:"layers"`
}

// Define the struct for the "config" section
type Config struct {
	Digest    string `json:"digest"`
	MediaType string `json:"mediaType"`
	Size      int    `json:"size"`
}

// Define the struct for each item in the "layers" array
type LayerDetail struct {
	Digest    string `json:"digest"`
	MediaType string `json:"mediaType"`
	Size      int    `json:"size"`
}

func OllamaModelManifest(image string) (*Manifest, error) {
	// parse the repository and tag from `image`. `image` should be for e.g. gemma:2b, or foobar/gemma:2b

	// if there is a : in the image, then split it
	// if there is no : in the image, then assume it is the latest tag
	tag, repository, image := ParseImageParts(image)

	// get e.g. https://registry.ollama.ai/v2/library/llama3/manifests/latest
	req, err := http.NewRequest("GET", "https://registry.ollama.ai/v2/"+repository+"/"+image+"/manifests/"+tag, nil)
	if err != nil {
		return nil, err
	}
	req.Header.Set("Accept", "application/vnd.docker.distribution.manifest.v2+json")
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}

	// parse the JSON response
	var manifest Manifest
	err = json.NewDecoder(resp.Body).Decode(&manifest)
	if err != nil {
		return nil, err
	}

	return &manifest, nil
}

func OllamaModelBlob(image string) (string, error) {
	manifest, err := OllamaModelManifest(image)
	if err != nil {
		return "", err
	}
	// find a application/vnd.ollama.image.model in the mediaType

	for _, layer := range manifest.Layers {
		if layer.MediaType == "application/vnd.ollama.image.model" {
			return layer.Digest, nil
		}
	}

	return "", nil
}

func OllamaFetchModel(image string, output string, statusWriter func(ocispec.Descriptor) io.Writer) error {
	_, repository, imageNoTag := ParseImageParts(image)

	blobID, err := OllamaModelBlob(image)
	if err != nil {
		return err
	}

	return FetchImageBlob(fmt.Sprintf("registry.ollama.ai/%s/%s", repository, imageNoTag), blobID, output, statusWriter)
}