fi.evolver.ai.spring.provider.openai.response.ORateLimitHeaders Maven / Gradle / Ivy
package fi.evolver.ai.spring.provider.openai.response;
import fi.evolver.ai.spring.util.RateLimitHeaders;
import java.net.http.HttpHeaders;
import java.time.Duration;
import java.time.OffsetDateTime;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
*/
public record ORateLimitHeaders(
Optional requestsLimit,
Optional requestsRemaining,
Optional requestsReset,
Optional tokensLimit,
Optional tokensRemaining,
Optional tokensReset
) implements RateLimitHeaders {
private static final Pattern OAIDURATION_PATTERN = Pattern.compile("(?:(\\d+)m)?(?:(\\d+)(?:\\.(\\d+))?s)?");
public static ORateLimitHeaders fromHttpHeaders(HttpHeaders headers) {
try {
OffsetDateTime now = OffsetDateTime.now();
return new ORateLimitHeaders(
headers.firstValue("x-ratelimit-limit-requests").map(Integer::parseInt),
headers.firstValue("x-ratelimit-remaining-requests").map(Integer::parseInt),
headers.firstValue("x-ratelimit-reset-requests").map(it -> ORateLimitHeaders.parseOAIDuration(it, now)),
headers.firstValue("x-ratelimit-limit-tokens").map(Integer::parseInt),
headers.firstValue("x-ratelimit-remaining-tokens").map(Integer::parseInt),
headers.firstValue("x-ratelimit-reset-tokens").map(it -> ORateLimitHeaders.parseOAIDuration(it, now))
);
}
catch (Exception e) {
return null;
}
}
private static OffsetDateTime parseOAIDuration(String duration, OffsetDateTime now) {
Matcher matcher = OAIDURATION_PATTERN.matcher(duration);
if (!matcher.matches()) {
return null;
}
String minutesPart = matcher.group(1);
String secondsPart = matcher.group(2);
String secondsDecimalPart = matcher.group(3);
long minutes = minutesPart != null ? Long.parseLong(minutesPart) : 0;
long seconds = secondsPart != null ? Long.parseLong(secondsPart) : 0;
long nanoSeconds = secondsDecimalPart != null
? Long.parseLong(secondsDecimalPart) * (long) Math.pow(10, 9 - secondsDecimalPart.length())
: 0;
return now.plus(Duration.ofMinutes(minutes).plusSeconds(seconds).plusNanos(nanoSeconds));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy