@@ -563,8 +563,8 @@ class LlamaData {
563
563
564
564
private:
565
565
#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 ) {
568
568
HttpClient http;
569
569
if (http.init (url, headers, output_file, progress, response_str)) {
570
570
return 1 ;
@@ -573,28 +573,45 @@ class LlamaData {
573
573
return 0 ;
574
574
}
575
575
#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> & = {} ,
577
577
std::string * = nullptr ) {
578
578
printe (" %s: llama.cpp built without libcurl, downloading from an url not supported.\n " , __func__);
579
579
return 1 ;
580
580
}
581
581
#endif
582
582
583
- int huggingface_dl (const std::string & model, const std::vector<std::string> headers, const std:: string & bn) {
583
+ int huggingface_dl (const std::string & model, const std::string & bn) {
584
584
// Find the second occurrence of '/' after protocol string
585
585
size_t pos = model.find (' /' );
586
586
pos = model.find (' /' , pos + 1 );
587
+ std::string hfr, hff;
588
+ std::vector<std::string> headers = { " User-Agent: llama-cpp" , " Accept: application/json" };
589
+ std::string url;
587
590
if (pos == std::string::npos) {
588
- return 1 ;
591
+ std::string tag = " latest" ;
592
+ url = " https://huggingface.co/v2/" + model + " /manifests/" + tag;
593
+ std::string manifest_str;
594
+ const int ret = download (url, " " , false , headers, &manifest_str);
595
+ if (ret) {
596
+ return ret;
597
+ }
598
+
599
+ nlohmann::json manifest = nlohmann::json::parse (manifest_str);
600
+ hfr = model;
601
+ hff = manifest[" ggufFile" ][" rfilename" ];
602
+ } else {
603
+ hfr = model.substr (0 , pos);
604
+ hff = model.substr (pos + 1 );
605
+ printf (" %s\n %s\n " , hfr.c_str (), hff.c_str ());
606
+ url = " https://huggingface.co/" + hfr + " /resolve/main/" + hff;
589
607
}
590
608
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 );
609
+ url = " https://huggingface.co/" + hfr + " /resolve/main/" + hff;
610
+ return download (url, bn, true , headers);
595
611
}
596
612
597
- int ollama_dl (std::string & model, const std::vector<std::string> headers, const std::string & bn) {
613
+ int ollama_dl (std::string & model, const std::string & bn) {
614
+ const std::vector<std::string> headers = { " Accept: application/vnd.docker.distribution.manifest.v2+json" };
598
615
if (model.find (' /' ) == std::string::npos) {
599
616
model = " library/" + model;
600
617
}
@@ -608,7 +625,7 @@ class LlamaData {
608
625
609
626
std::string manifest_url = " https://registry.ollama.ai/v2/" + model + " /manifests/" + model_tag;
610
627
std::string manifest_str;
611
- const int ret = download (manifest_url, headers, " " , false , &manifest_str);
628
+ const int ret = download (manifest_url, " " , false , {} , &manifest_str);
612
629
if (ret) {
613
630
return ret;
614
631
}
@@ -623,7 +640,7 @@ class LlamaData {
623
640
}
624
641
625
642
std::string blob_url = " https://registry.ollama.ai/v2/" + model + " /blobs/" + layer;
626
- return download (blob_url, headers, bn, true );
643
+ return download (blob_url, bn, true , headers );
627
644
}
628
645
629
646
std::string basename (const std::string & path) {
@@ -653,22 +670,18 @@ class LlamaData {
653
670
return ret;
654
671
}
655
672
656
- const std::string bn = basename (model_);
657
- const std::vector<std::string> headers = { " --header" ,
658
- " Accept: application/vnd.docker.distribution.manifest.v2+json" };
673
+ const std::string bn = basename (model_);
659
674
if (string_starts_with (model_, " hf://" ) || string_starts_with (model_, " huggingface://" )) {
660
675
rm_until_substring (model_, " ://" );
661
- ret = huggingface_dl (model_, headers, bn);
676
+ ret = huggingface_dl (model_, bn);
662
677
} else if (string_starts_with (model_, " hf.co/" )) {
663
678
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);
679
+ ret = huggingface_dl (model_, bn);
668
680
} else if (string_starts_with (model_, " https://" )) {
669
- ret = download (model_, headers, bn, true );
670
- } else {
671
- ret = ollama_dl (model_, headers, bn);
681
+ ret = download (model_, bn, true );
682
+ } else { // ollama:// or nothing
683
+ rm_until_substring (model_, " ://" );
684
+ ret = ollama_dl (model_, bn);
672
685
}
673
686
674
687
model_ = bn;
0 commit comments