| 598 | } |
| 599 | |
| 600 | common_hf_file_res common_get_hf_file(const std::string & hf_repo_with_tag, |
| 601 | const std::string & bearer_token, |
| 602 | bool offline, |
| 603 | const common_header_list & custom_headers) { |
| 604 | // the returned hf_repo is without tag |
| 605 | auto [hf_repo, tag] = common_download_split_repo_tag(hf_repo_with_tag); |
| 606 | |
| 607 | std::string url = get_model_endpoint() + "v2/" + hf_repo + "/manifests/" + tag; |
| 608 | |
| 609 | // headers |
| 610 | common_header_list headers = custom_headers; |
| 611 | headers.push_back({"Accept", "application/json"}); |
| 612 | if (!bearer_token.empty()) { |
| 613 | headers.push_back({"Authorization", "Bearer " + bearer_token}); |
| 614 | } |
| 615 | // Important: the User-Agent must be "llama-cpp" to get the "ggufFile" field in the response |
| 616 | // User-Agent header is already set in common_remote_get_content, no need to set it here |
| 617 | |
| 618 | // make the request |
| 619 | common_remote_params params; |
| 620 | params.headers = headers; |
| 621 | long res_code = 0; |
| 622 | std::string res_str; |
| 623 | bool use_cache = false; |
| 624 | std::string cached_response_path = get_manifest_path(hf_repo, tag); |
| 625 | if (!offline) { |
| 626 | try { |
| 627 | auto res = common_remote_get_content(url, params); |
| 628 | res_code = res.first; |
| 629 | res_str = std::string(res.second.data(), res.second.size()); |
| 630 | } catch (const std::exception & e) { |
| 631 | LOG_WRN("error: failed to get manifest at %s: %s\n", url.c_str(), e.what()); |
| 632 | } |
| 633 | } |
| 634 | if (res_code == 0) { |
| 635 | if (std::filesystem::exists(cached_response_path)) { |
| 636 | LOG_WRN("trying to read manifest from cache: %s\n", cached_response_path.c_str()); |
| 637 | res_str = read_file(cached_response_path); |
| 638 | res_code = 200; |
| 639 | use_cache = true; |
| 640 | } else { |
| 641 | throw std::runtime_error( |
| 642 | offline ? "error: failed to get manifest (offline mode)" |
| 643 | : "error: failed to get manifest (check your internet connection)"); |
| 644 | } |
| 645 | } |
| 646 | std::string ggufFile; |
| 647 | std::string mmprojFile; |
| 648 | |
| 649 | if (res_code == 200 || res_code == 304) { |
| 650 | try { |
| 651 | auto j = json::parse(res_str); |
| 652 | |
| 653 | if (j.contains("ggufFile") && j["ggufFile"].contains("rfilename")) { |
| 654 | ggufFile = j["ggufFile"]["rfilename"].get<std::string>(); |
| 655 | } |
| 656 | if (j.contains("mmprojFile") && j["mmprojFile"].contains("rfilename")) { |
| 657 | mmprojFile = j["mmprojFile"]["rfilename"].get<std::string>(); |
no test coverage detected