json: cache externally fetched refs (forever for now)
This commit is contained in:
parent
ae058954c7
commit
3b24739071
1 changed files with 9 additions and 2 deletions
|
@ -1057,7 +1057,14 @@ std::string json_schema_to_grammar(const json & schema) {
|
||||||
std::function<json(const std::string &)> fetch_json = [](const std::string &) { return json::object(); };
|
std::function<json(const std::string &)> fetch_json = [](const std::string &) { return json::object(); };
|
||||||
|
|
||||||
#if defined(LLAMA_USE_CURL)
|
#if defined(LLAMA_USE_CURL)
|
||||||
fetch_json = [](const std::string & url) {
|
// TODO: implement HTTP caching semantics.
|
||||||
|
std::unordered_map<std::string, json> cache;
|
||||||
|
|
||||||
|
fetch_json = [&](const std::string & url) {
|
||||||
|
auto it = cache.find(url);
|
||||||
|
if (it != cache.end()) {
|
||||||
|
return it->second;
|
||||||
|
}
|
||||||
std::unique_ptr<CURL, decltype(&curl_easy_cleanup)> curl(curl_easy_init(), &curl_easy_cleanup);
|
std::unique_ptr<CURL, decltype(&curl_easy_cleanup)> curl(curl_easy_init(), &curl_easy_cleanup);
|
||||||
if (!curl) {
|
if (!curl) {
|
||||||
fprintf(stderr, "%s: error initializing libcurl\n", __func__);
|
fprintf(stderr, "%s: error initializing libcurl\n", __func__);
|
||||||
|
@ -1082,7 +1089,7 @@ std::string json_schema_to_grammar(const json & schema) {
|
||||||
throw std::runtime_error("Failed to fetch " + url + ": " + curl_easy_strerror(res));
|
throw std::runtime_error("Failed to fetch " + url + ": " + curl_easy_strerror(res));
|
||||||
}
|
}
|
||||||
response << '\0';
|
response << '\0';
|
||||||
return json::parse(response.str());
|
return cache[url] = json::parse(response.str());
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue