async function cached_request()

in excel-addin/src/taskpane/taskpane.js [142:172]


async function cached_request({context_string, logprobs, length, temperature=0.0}) {
    if (context_string === undefined) {
        context_string = _build_field_query_context_string({topic: 'Edible fruits'});
    }
    if (logprobs === undefined) {
        logprobs = 0;
    }
    if (length === undefined) {
        length = 24;
    }
    const request_hash = _hash(context_string, logprobs, length);
    var cached_result = getWithExpiry(request_hash);
    if (cached_result === null) {
        // TODO handle non-200 code
        cached_result = make_request({context_string: context_string, logprobs: logprobs, length: length, temperature: temperature});
    }
    else {
        await sleep(Math.floor(Math.random() * 500) + 250);
        function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
    }
    try {
        setWithExpiry(request_hash, cached_result, 1800);
    }
    catch(err) {
        console.log('Issue caching, possible too large: ', err, cached_result.length);
        console.log(JSON.stringify(cached_result).length);
    }
    return cached_result;
}