-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ettore Di Giacinto <[email protected]>
- Loading branch information
Showing
5 changed files
with
129 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,88 @@ | ||
package oci | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// 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) error { | ||
_, repository, imageNoTag := ParseImageParts(image) | ||
|
||
blobID, err := OllamaModelBlob(image) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println("Downloading blob", blobID) | ||
fmt.Println("Downloading to", output) | ||
fmt.Println("Downloading from", fmt.Sprintf("registry.ollama.ai/%s/%s", repository, imageNoTag)) | ||
|
||
return FetchImageBlob(fmt.Sprintf("registry.ollama.ai/%s/%s", repository, imageNoTag), blobID, output) | ||
} |
This file contains 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,16 @@ | ||
package oci_test | ||
|
||
import ( | ||
. "github.com/go-skynet/LocalAI/pkg/oci" // Update with your module path | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("OCI", func() { | ||
Context("ollama", func() { | ||
FIt("pulls model files", func() { | ||
err := OllamaFetchModel("gemma:2b", "/tmp/foo") | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
}) | ||
}) |