Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[OPIK-488] Fix NPE during span cost calculation #749

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;

@RequiredArgsConstructor
Expand Down Expand Up @@ -84,6 +85,6 @@ public static ModelPrice fromString(String modelName) {
}

public BigDecimal calculateCost(Map<String, Integer> usage) {
return calculator.apply(this, usage);
return calculator.apply(this, Optional.ofNullable(usage).orElse(Map.of()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import jakarta.ws.rs.core.Response;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;
import org.jdbi.v3.core.Jdbi;
Expand Down Expand Up @@ -3284,27 +3285,44 @@ void createAndGetById() {

@ParameterizedTest
@MethodSource
void createAndGetCost(BigDecimal expectedCost, String model, JsonNode metadata) {
void createAndGetCost(Map<String, Integer> usage, String model, JsonNode metadata) {
var expectedSpan = podamFactory.manufacturePojo(Span.class).toBuilder()
.model(model)
.metadata(metadata)
.usage(Map.of("prompt_tokens", 4000000, "completion_tokens", 3000000))
.usage(usage)
.build();

createAndAssert(expectedSpan, API_KEY, TEST_WORKSPACE);

BigDecimal expectedCost = ModelPrice.fromString(
StringUtils.isNotBlank(model) ?
model :
Optional.ofNullable(metadata)
.map(md -> md.get("model"))
.map(JsonNode::asText).orElse("")
).calculateCost(usage);

Span span = getAndAssert(expectedSpan, API_KEY, TEST_WORKSPACE);
assertThat(span.totalEstimatedCost()).isEqualTo(expectedCost);

assertThat(span.totalEstimatedCost())
.usingRecursiveComparison(RecursiveComparisonConfiguration.builder()
.withComparatorForType(BigDecimal::compareTo, BigDecimal.class)
.build())
.isEqualTo(expectedCost.compareTo(BigDecimal.ZERO) == 0 ? null : expectedCost);
}

static Stream<Arguments> createAndGetCost() {
Stream<Arguments> createAndGetCost() {
JsonNode metadata = JsonUtils
.getJsonNodeFromString(
"{\"created_from\":\"openai\",\"type\":\"openai_chat\",\"model\":\"gpt-3.5-turbo\"}");
return Stream.of(
Arguments.of(new BigDecimal("10.00000000"), "gpt-3.5-turbo-1106", null),
Arguments.of(new BigDecimal("10.00000000"), "gpt-3.5-turbo-1106", metadata),
Arguments.of(new BigDecimal("12.00000000"), "", metadata),
Arguments.of(Map.of("completion_tokens", Math.abs(podamFactory.manufacturePojo(Integer.class)),
"prompt_tokens", Math.abs(podamFactory.manufacturePojo(Integer.class))), "gpt-3.5-turbo-1106", null),
Arguments.of(Map.of("completion_tokens", Math.abs(podamFactory.manufacturePojo(Integer.class)),
"prompt_tokens", Math.abs(podamFactory.manufacturePojo(Integer.class))), "gpt-3.5-turbo-1106", metadata),
Arguments.of(Map.of("completion_tokens", Math.abs(podamFactory.manufacturePojo(Integer.class)),
"prompt_tokens", Math.abs(podamFactory.manufacturePojo(Integer.class))), "", metadata),
Arguments.of(null, "gpt-3.5-turbo-1106", null),
Arguments.of(null, "unknown-model", null),
Arguments.of(null, "", null));
}
Expand Down