Skip to content

Commit

Permalink
Increase cost precision
Browse files Browse the repository at this point in the history
  • Loading branch information
Borys Tkachenko committed Nov 21, 2024
1 parent a4cb6eb commit b33fa7b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ INSERT INTO spans(
:metadata<item.index>,
:model<item.index>,
:provider<item.index>,
toDecimal32(:total_estimated_cost<item.index>, 6),
toDecimal32(:total_estimated_cost<item.index>, 7),
:total_estimated_cost_version<item.index>,
:tags<item.index>,
mapFromArrays(:usage_keys<item.index>, :usage_values<item.index>),
Expand Down Expand Up @@ -195,7 +195,7 @@ INSERT INTO spans(
new_span.provider
) as provider,
multiIf(
old_span.total_estimated_cost > toDecimal32(0, 6), old_span.total_estimated_cost,
old_span.total_estimated_cost > toDecimal32(0, 7), old_span.total_estimated_cost,
new_span.total_estimated_cost
) as total_estimated_cost,
multiIf(
Expand Down Expand Up @@ -235,7 +235,7 @@ old_span.total_estimated_cost > toDecimal32(0, 6), old_span.total_estimated_cost
:metadata as metadata,
:model as model,
:provider as provider,
toDecimal32(:total_estimated_cost, 6) as total_estimated_cost,
toDecimal32(:total_estimated_cost, 7) as total_estimated_cost,
:total_estimated_cost_version as total_estimated_cost_version,
:tags as tags,
mapFromArrays(:usage_keys, :usage_values) as usage,
Expand Down Expand Up @@ -297,7 +297,7 @@ INSERT INTO spans (
<if(metadata)> :metadata <else> metadata <endif> as metadata,
<if(model)> :model <else> model <endif> as model,
<if(provider)> :provider <else> provider <endif> as provider,
<if(total_estimated_cost)> toDecimal32(:total_estimated_cost, 6) <else> total_estimated_cost <endif> as total_estimated_cost,
<if(total_estimated_cost)> toDecimal32(:total_estimated_cost, 7) <else> total_estimated_cost <endif> as total_estimated_cost,
<if(total_estimated_cost_version)> :total_estimated_cost_version <else> total_estimated_cost_version <endif> as total_estimated_cost_version,
<if(tags)> :tags <else> tags <endif> as tags,
<if(usage)> CAST((:usageKeys, :usageValues), 'Map(String, Int64)') <else> usage <endif> as usage,
Expand Down Expand Up @@ -393,8 +393,8 @@ INSERT INTO spans(
new_span.provider
) as provider,
multiIf(
new_span.total_estimated_cost > toDecimal32(0, 6), new_span.total_estimated_cost,
old_span.total_estimated_cost > toDecimal32(0, 6), old_span.total_estimated_cost,
new_span.total_estimated_cost > toDecimal32(0, 7), new_span.total_estimated_cost,
old_span.total_estimated_cost > toDecimal32(0, 7), old_span.total_estimated_cost,
new_span.total_estimated_cost
) as total_estimated_cost,
multiIf(
Expand Down Expand Up @@ -437,7 +437,7 @@ old_span.total_estimated_cost > toDecimal32(0, 6), old_span.total_estimated_cost
<if(metadata)> :metadata <else> '' <endif> as metadata,
<if(model)> :model <else> '' <endif> as model,
<if(provider)> :provider <else> '' <endif> as provider,
<if(total_estimated_cost)> toDecimal32(:total_estimated_cost, 6) <else> toDecimal32(0, 6) <endif> as total_estimated_cost,
<if(total_estimated_cost)> toDecimal32(:total_estimated_cost, 7) <else> toDecimal32(0, 7) <endif> as total_estimated_cost,
<if(total_estimated_cost_version)> :total_estimated_cost_version <else> '' <endif> as total_estimated_cost_version,
<if(tags)> :tags <else> [] <endif> as tags,
<if(usage)> CAST((:usageKeys, :usageValues), 'Map(String, Int64)') <else> mapFromArrays([], []) <endif> as usage,
Expand Down Expand Up @@ -581,7 +581,7 @@ AND id in (
""";

private static final String ESTIMATED_COST_VERSION = "1.0";
private static final BigDecimal COST_NOT_AVAILABLE = new BigDecimal("0.000000");
private static final BigDecimal COST_NOT_AVAILABLE = new BigDecimal("0.0000000");

private final @NonNull ConnectionFactory connectionFactory;
private final @NonNull FeedbackScoreDAO feedbackScoreDAO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
ALTER TABLE ${ANALYTICS_DB_DATABASE_NAME}.spans
ADD COLUMN IF NOT EXISTS model String DEFAULT '',
ADD COLUMN IF NOT EXISTS provider String DEFAULT '',
ADD COLUMN IF NOT EXISTS total_estimated_cost Decimal32(6) DEFAULT toDecimal32(0, 6),
ADD COLUMN IF NOT EXISTS total_estimated_cost Decimal32(7) DEFAULT toDecimal32(0, 7),
ADD COLUMN IF NOT EXISTS total_estimated_cost_version String DEFAULT '';

--rollback ALTER TABLE ${ANALYTICS_DB_DATABASE_NAME}.spans DROP COLUMN IF EXISTS model, DROP COLUMN IF EXISTS provider, DROP COLUMN IF EXISTS total_estimated_cost, DROP COLUMN IF EXISTS total_estimated_cost_version;
Original file line number Diff line number Diff line change
Expand Up @@ -3217,9 +3217,9 @@ static Stream<Arguments> createAndGetCost() {
.getJsonNodeFromString(
"{\"created_from\":\"openai\",\"type\":\"openai_chat\",\"model\":\"gpt-3.5-turbo\"}");
return Stream.of(
Arguments.of(new BigDecimal("10.000000"), "gpt-3.5-turbo-1106", null),
Arguments.of(new BigDecimal("10.000000"), "gpt-3.5-turbo-1106", metadata),
Arguments.of(new BigDecimal("12.000000"), "", metadata),
Arguments.of(new BigDecimal("10.0000000"), "gpt-3.5-turbo-1106", null),
Arguments.of(new BigDecimal("10.0000000"), "gpt-3.5-turbo-1106", metadata),
Arguments.of(new BigDecimal("12.0000000"), "", metadata),
Arguments.of(null, "unknown-model", null),
Arguments.of(null, "", null));
}
Expand Down

0 comments on commit b33fa7b

Please sign in to comment.