Skip to content

Commit

Permalink
fix(persistence): postgres json calculations (#2848)
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerHYang committed Apr 10, 2024
1 parent 3477bb9 commit 45f084d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
14 changes: 4 additions & 10 deletions src/phoenix/server/api/input_types/SpanSort.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pandas as pd
import strawberry
from openinference.semconv.trace import SpanAttributes
from sqlalchemy import Integer, cast, desc, nulls_last
from sqlalchemy import desc, nulls_last
from strawberry import UNSET
from typing_extensions import assert_never

Expand Down Expand Up @@ -37,15 +37,9 @@ class SpanColumn(Enum):
SpanColumn.startTime: models.Span.start_time,
SpanColumn.endTime: models.Span.end_time,
SpanColumn.latencyMs: models.Span.latency_ms,
SpanColumn.tokenCountTotal: cast(
models.Span.attributes[LLM_TOKEN_COUNT_TOTAL].as_string(), Integer
),
SpanColumn.tokenCountPrompt: cast(
models.Span.attributes[LLM_TOKEN_COUNT_PROMPT].as_string(), Integer
),
SpanColumn.tokenCountCompletion: cast(
models.Span.attributes[LLM_TOKEN_COUNT_COMPLETION].as_string(), Integer
),
SpanColumn.tokenCountTotal: models.Span.attributes[LLM_TOKEN_COUNT_TOTAL].as_float(),
SpanColumn.tokenCountPrompt: models.Span.attributes[LLM_TOKEN_COUNT_PROMPT].as_float(),
SpanColumn.tokenCountCompletion: models.Span.attributes[LLM_TOKEN_COUNT_COMPLETION].as_float(),
SpanColumn.cumulativeTokenCountTotal: models.Span.cumulative_llm_token_count_prompt
+ models.Span.cumulative_llm_token_count_completion,
SpanColumn.cumulativeTokenCountPrompt: models.Span.cumulative_llm_token_count_prompt,
Expand Down
11 changes: 4 additions & 7 deletions src/phoenix/server/api/types/Project.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import strawberry
from openinference.semconv.trace import SpanAttributes
from sqlalchemy import Integer, and_, cast, func, select
from sqlalchemy import and_, func, select
from sqlalchemy.orm import contains_eager
from sqlalchemy.sql.functions import coalesce
from strawberry import ID, UNSET
Expand Down Expand Up @@ -116,13 +116,10 @@ async def token_count_total(
info: Info[Context, None],
time_range: Optional[TimeRange] = UNSET,
) -> int:
prompt = models.Span.attributes[LLM_TOKEN_COUNT_PROMPT]
completion = models.Span.attributes[LLM_TOKEN_COUNT_COMPLETION]
prompt = models.Span.attributes[LLM_TOKEN_COUNT_PROMPT].as_float()
completion = models.Span.attributes[LLM_TOKEN_COUNT_COMPLETION].as_float()
stmt = (
select(
coalesce(func.sum(cast(prompt, Integer)), 0)
+ coalesce(func.sum(cast(completion, Integer)), 0)
)
select(coalesce(func.sum(prompt), 0) + coalesce(func.sum(completion), 0))
.join(models.Trace)
.join(models.Project)
.where(models.Project.name == self.name)
Expand Down

0 comments on commit 45f084d

Please sign in to comment.