download.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma once
  2. #include <string>
  3. #include <vector>
  4. struct common_params_model;
  5. using common_header = std::pair<std::string, std::string>;
  6. using common_header_list = std::vector<common_header>;
  7. struct common_remote_params {
  8. common_header_list headers;
  9. long timeout = 0; // in seconds, 0 means no timeout
  10. long max_size = 0; // unlimited if 0
  11. };
  12. // get remote file content, returns <http_code, raw_response_body>
  13. std::pair<long, std::vector<char>> common_remote_get_content(const std::string & url, const common_remote_params & params);
  14. struct common_cached_model_info {
  15. std::string manifest_path;
  16. std::string user;
  17. std::string model;
  18. std::string tag;
  19. size_t size = 0; // GGUF size in bytes
  20. // return string representation like "user/model:tag"
  21. // if tag is "latest", it will be omitted
  22. std::string to_string() const {
  23. return user + "/" + model + (tag == "latest" ? "" : ":" + tag);
  24. }
  25. };
  26. struct common_hf_file_res {
  27. std::string repo; // repo name with ":tag" removed
  28. std::string ggufFile;
  29. std::string mmprojFile;
  30. };
  31. /**
  32. * Allow getting the HF file from the HF repo with tag (like ollama), for example:
  33. * - bartowski/Llama-3.2-3B-Instruct-GGUF:q4
  34. * - bartowski/Llama-3.2-3B-Instruct-GGUF:Q4_K_M
  35. * - bartowski/Llama-3.2-3B-Instruct-GGUF:q5_k_s
  36. * Tag is optional, default to "latest" (meaning it checks for Q4_K_M first, then Q4, then if not found, return the first GGUF file in repo)
  37. *
  38. * Return pair of <repo, file> (with "repo" already having tag removed)
  39. *
  40. * Note: we use the Ollama-compatible HF API, but not using the blobId. Instead, we use the special "ggufFile" field which returns the value for "hf_file". This is done to be backward-compatible with existing cache files.
  41. */
  42. common_hf_file_res common_get_hf_file(
  43. const std::string & hf_repo_with_tag,
  44. const std::string & bearer_token,
  45. bool offline,
  46. const common_header_list & headers = {}
  47. );
  48. // returns true if download succeeded
  49. bool common_download_model(
  50. const common_params_model & model,
  51. const std::string & bearer_token,
  52. bool offline,
  53. const common_header_list & headers = {}
  54. );
  55. // returns list of cached models
  56. std::vector<common_cached_model_info> common_list_cached_models();
  57. // download single file from url to local path
  58. // returns status code or -1 on error
  59. int common_download_file_single(const std::string & url,
  60. const std::string & path,
  61. const std::string & bearer_token,
  62. bool offline,
  63. const common_header_list & headers = {});
  64. // resolve and download model from Docker registry
  65. // return local path to downloaded model file
  66. std::string common_docker_resolve_model(const std::string & docker);