@@ -563,8 +563,8 @@ class LlamaData {
563563
564564 private:
565565#ifdef LLAMA_USE_CURL
566- int download (const std::string & url, const std::vector<std:: string> & headers , const std::string & output_file ,
567- const bool progress , std::string * response_str = nullptr ) {
566+ int download (const std::string & url, const std::string & output_file , const bool progress ,
567+ const std::vector<std::string> & headers = {} , std::string * response_str = nullptr ) {
568568 HttpClient http;
569569 if (http.init (url, headers, output_file, progress, response_str)) {
570570 return 1 ;
@@ -573,57 +573,95 @@ class LlamaData {
573573 return 0 ;
574574 }
575575#else
576- int download (const std::string &, const std::vector<std:: string> &, const std::string &, const bool ,
576+ int download (const std::string &, const std::string &, const bool , const std::vector<std:: string> & = {} ,
577577 std::string * = nullptr ) {
578578 printe (" %s: llama.cpp built without libcurl, downloading from an url not supported.\n " , __func__);
579579 return 1 ;
580580 }
581581#endif
582582
583- int huggingface_dl (const std::string & model, const std::vector<std::string> headers, const std::string & bn) {
583+ // Helper function to handle model tag extraction and URL construction
584+ std::pair<std::string, std::string> extract_model_and_tag (std::string & model, const std::string & base_url) {
585+ std::string model_tag = " latest" ;
586+ const size_t colon_pos = model.find (' :' );
587+ if (colon_pos != std::string::npos) {
588+ model_tag = model.substr (colon_pos + 1 );
589+ model = model.substr (0 , colon_pos);
590+ }
591+
592+ std::string url = base_url + model + " /manifests/" + model_tag;
593+
594+ return { model, url };
595+ }
596+
597+ // Helper function to download and parse the manifest
598+ int download_and_parse_manifest (const std::string & url, const std::vector<std::string> & headers,
599+ nlohmann::json & manifest) {
600+ std::string manifest_str;
601+ int ret = download (url, " " , false , headers, &manifest_str);
602+ if (ret) {
603+ return ret;
604+ }
605+
606+ manifest = nlohmann::json::parse (manifest_str);
607+
608+ return 0 ;
609+ }
610+
611+ int huggingface_dl (std::string & model, const std::string & bn) {
584612 // Find the second occurrence of '/' after protocol string
585613 size_t pos = model.find (' /' );
586614 pos = model.find (' /' , pos + 1 );
615+ std::string hfr, hff;
616+ std::vector<std::string> headers = { " User-Agent: llama-cpp" , " Accept: application/json" };
617+ std::string url;
618+
587619 if (pos == std::string::npos) {
588- return 1 ;
620+ auto [model_name, manifest_url] = extract_model_and_tag (model, " https://huggingface.co/v2/" );
621+ hfr = model_name;
622+
623+ nlohmann::json manifest;
624+ int ret = download_and_parse_manifest (manifest_url, headers, manifest);
625+ if (ret) {
626+ return ret;
627+ }
628+
629+ hff = manifest[" ggufFile" ][" rfilename" ];
630+ if (std::filesystem::exists (hff)) {
631+ return 0 ;
632+ }
633+ } else {
634+ hfr = model.substr (0 , pos);
635+ hff = model.substr (pos + 1 );
589636 }
590637
591- const std::string hfr = model.substr (0 , pos);
592- const std::string hff = model.substr (pos + 1 );
593- const std::string url = " https://huggingface.co/" + hfr + " /resolve/main/" + hff;
594- return download (url, headers, bn, true );
638+ url = " https://huggingface.co/" + hfr + " /resolve/main/" + hff;
639+ return download (url, bn, true , headers);
595640 }
596641
597- int ollama_dl (std::string & model, const std::vector<std::string> headers, const std::string & bn) {
642+ int ollama_dl (std::string & model, const std::string & bn) {
643+ const std::vector<std::string> headers = { " Accept: application/vnd.docker.distribution.manifest.v2+json" };
598644 if (model.find (' /' ) == std::string::npos) {
599645 model = " library/" + model;
600646 }
601647
602- std::string model_tag = " latest" ;
603- size_t colon_pos = model.find (' :' );
604- if (colon_pos != std::string::npos) {
605- model_tag = model.substr (colon_pos + 1 );
606- model = model.substr (0 , colon_pos);
607- }
608-
609- std::string manifest_url = " https://registry.ollama.ai/v2/" + model + " /manifests/" + model_tag;
610- std::string manifest_str;
611- const int ret = download (manifest_url, headers, " " , false , &manifest_str);
648+ auto [model_name, manifest_url] = extract_model_and_tag (model, " https://registry.ollama.ai/v2/" );
649+ nlohmann::json manifest;
650+ int ret = download_and_parse_manifest (manifest_url, {}, manifest);
612651 if (ret) {
613652 return ret;
614653 }
615654
616- nlohmann::json manifest = nlohmann::json::parse (manifest_str);
617- std::string layer;
655+ std::string layer;
618656 for (const auto & l : manifest[" layers" ]) {
619657 if (l[" mediaType" ] == " application/vnd.ollama.image.model" ) {
620658 layer = l[" digest" ];
621659 break ;
622660 }
623661 }
624662
625- std::string blob_url = " https://registry.ollama.ai/v2/" + model + " /blobs/" + layer;
626- return download (blob_url, headers, bn, true );
663+ std::string blob_url = " https://registry.ollama.ai/v2/" + model_name + " /blobs/" + layer;
664+ return download (blob_url, bn, true , headers );
627665 }
628666
629667 std::string basename (const std::string & path) {
@@ -653,22 +691,18 @@ class LlamaData {
653691 return ret;
654692 }
655693
656- const std::string bn = basename (model_);
657- const std::vector<std::string> headers = { " --header" ,
658- " Accept: application/vnd.docker.distribution.manifest.v2+json" };
694+ const std::string bn = basename (model_);
659695 if (string_starts_with (model_, " hf://" ) || string_starts_with (model_, " huggingface://" )) {
660696 rm_until_substring (model_, " ://" );
661- ret = huggingface_dl (model_, headers, bn);
697+ ret = huggingface_dl (model_, bn);
662698 } else if (string_starts_with (model_, " hf.co/" )) {
663699 rm_until_substring (model_, " hf.co/" );
664- ret = huggingface_dl (model_, headers, bn);
665- } else if (string_starts_with (model_, " ollama://" )) {
666- rm_until_substring (model_, " ://" );
667- ret = ollama_dl (model_, headers, bn);
700+ ret = huggingface_dl (model_, bn);
668701 } else if (string_starts_with (model_, " https://" )) {
669- ret = download (model_, headers, bn, true );
670- } else {
671- ret = ollama_dl (model_, headers, bn);
702+ ret = download (model_, bn, true );
703+ } else { // ollama:// or nothing
704+ rm_until_substring (model_, " ://" );
705+ ret = ollama_dl (model_, bn);
672706 }
673707
674708 model_ = bn;
0 commit comments