Skip to content

Commit

Permalink
feat(cost-tracking): trace all usage keys (#1021)
Browse files Browse the repository at this point in the history
  • Loading branch information
hassiebp authored Dec 16, 2024
1 parent 4945318 commit cf9ec3e
Show file tree
Hide file tree
Showing 20 changed files with 349 additions and 66 deletions.
4 changes: 4 additions & 0 deletions langfuse/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
ObservationsView,
ObservationsViews,
OpenAiUsage,
OpenAiUsageSchema,
OptionalObservationBody,
PaginatedDatasetItems,
PaginatedDatasetRuns,
Expand Down Expand Up @@ -133,6 +134,7 @@
UpdateSpanEvent,
Usage,
UsageByModel,
UsageDetails,
comments,
commons,
dataset_items,
Expand Down Expand Up @@ -240,6 +242,7 @@
"ObservationsView",
"ObservationsViews",
"OpenAiUsage",
"OpenAiUsageSchema",
"OptionalObservationBody",
"PaginatedDatasetItems",
"PaginatedDatasetRuns",
Expand Down Expand Up @@ -286,6 +289,7 @@
"UpdateSpanEvent",
"Usage",
"UsageByModel",
"UsageDetails",
"comments",
"commons",
"dataset_items",
Expand Down
4 changes: 4 additions & 0 deletions langfuse/api/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
ObservationBody,
ObservationType,
OpenAiUsage,
OpenAiUsageSchema,
OptionalObservationBody,
ScoreBody,
ScoreEvent,
Expand All @@ -105,6 +106,7 @@
UpdateObservationEvent,
UpdateSpanBody,
UpdateSpanEvent,
UsageDetails,
)
from .media import (
GetMediaResponse,
Expand Down Expand Up @@ -237,6 +239,7 @@
"ObservationsView",
"ObservationsViews",
"OpenAiUsage",
"OpenAiUsageSchema",
"OptionalObservationBody",
"PaginatedDatasetItems",
"PaginatedDatasetRuns",
Expand Down Expand Up @@ -283,6 +286,7 @@
"UpdateSpanEvent",
"Usage",
"UsageByModel",
"UsageDetails",
"comments",
"commons",
"dataset_items",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CreateCommentRequest(pydantic_v1.BaseModel):

content: str = pydantic_v1.Field()
"""
The content of the comment. May include markdown. Currently limited to 500 characters.
The content of the comment. May include markdown. Currently limited to 3000 characters.
"""

author_user_id: typing.Optional[str] = pydantic_v1.Field(
Expand Down
16 changes: 15 additions & 1 deletion langfuse/api/resources/commons/types/observation.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Observation(pydantic_v1.BaseModel):

usage: typing.Optional[Usage] = pydantic_v1.Field(default=None)
"""
The usage data of the observation
(Deprecated. Use usageDetails and costDetails instead.) The usage data of the observation
"""

level: ObservationLevel = pydantic_v1.Field()
Expand All @@ -111,6 +111,20 @@ class Observation(pydantic_v1.BaseModel):
The prompt ID associated with the observation
"""

usage_details: typing.Optional[typing.Dict[str, int]] = pydantic_v1.Field(
alias="usageDetails", default=None
)
"""
The usage details of the observation. Key is the name of the usage metric, value is the number of units consumed. The total key is the sum of all (non-total) usage metrics or the total value ingested.
"""

cost_details: typing.Optional[typing.Dict[str, float]] = pydantic_v1.Field(
alias="costDetails", default=None
)
"""
The cost details of the observation. Key is the name of the cost metric, value is the cost in USD. The total key is the sum of all (non-total) cost metrics or the total value ingested.
"""

def json(self, **kwargs: typing.Any) -> str:
kwargs_with_defaults: typing.Any = {
"by_alias": True,
Expand Down
6 changes: 3 additions & 3 deletions langfuse/api/resources/commons/types/observations_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,21 @@ class ObservationsView(Observation):
alias="calculatedInputCost", default=None
)
"""
The calculated cost of the input in USD
(Deprecated. Use usageDetails and costDetails instead.) The calculated cost of the input in USD
"""

calculated_output_cost: typing.Optional[float] = pydantic_v1.Field(
alias="calculatedOutputCost", default=None
)
"""
The calculated cost of the output in USD
(Deprecated. Use usageDetails and costDetails instead.) The calculated cost of the output in USD
"""

calculated_total_cost: typing.Optional[float] = pydantic_v1.Field(
alias="calculatedTotalCost", default=None
)
"""
The calculated total cost in USD
(Deprecated. Use usageDetails and costDetails instead.) The calculated total cost in USD
"""

latency: typing.Optional[float] = pydantic_v1.Field(default=None)
Expand Down
2 changes: 1 addition & 1 deletion langfuse/api/resources/commons/types/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class Usage(pydantic_v1.BaseModel):
"""
Standard interface for usage and cost
(Deprecated. Use usageDetails and costDetails instead.) Standard interface for usage and cost
"""

input: typing.Optional[int] = pydantic_v1.Field(default=None)
Expand Down
4 changes: 4 additions & 0 deletions langfuse/api/resources/ingestion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
ObservationBody,
ObservationType,
OpenAiUsage,
OpenAiUsageSchema,
OptionalObservationBody,
ScoreBody,
ScoreEvent,
Expand All @@ -40,6 +41,7 @@
UpdateObservationEvent,
UpdateSpanBody,
UpdateSpanEvent,
UsageDetails,
)

__all__ = [
Expand Down Expand Up @@ -69,6 +71,7 @@
"ObservationBody",
"ObservationType",
"OpenAiUsage",
"OpenAiUsageSchema",
"OptionalObservationBody",
"ScoreBody",
"ScoreEvent",
Expand All @@ -82,4 +85,5 @@
"UpdateObservationEvent",
"UpdateSpanBody",
"UpdateSpanEvent",
"UsageDetails",
]
4 changes: 4 additions & 0 deletions langfuse/api/resources/ingestion/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from .observation_body import ObservationBody
from .observation_type import ObservationType
from .open_ai_usage import OpenAiUsage
from .open_ai_usage_schema import OpenAiUsageSchema
from .optional_observation_body import OptionalObservationBody
from .score_body import ScoreBody
from .score_event import ScoreEvent
Expand All @@ -41,6 +42,7 @@
from .update_observation_event import UpdateObservationEvent
from .update_span_body import UpdateSpanBody
from .update_span_event import UpdateSpanEvent
from .usage_details import UsageDetails

__all__ = [
"BaseEvent",
Expand Down Expand Up @@ -69,6 +71,7 @@
"ObservationBody",
"ObservationType",
"OpenAiUsage",
"OpenAiUsageSchema",
"OptionalObservationBody",
"ScoreBody",
"ScoreEvent",
Expand All @@ -82,4 +85,5 @@
"UpdateObservationEvent",
"UpdateSpanBody",
"UpdateSpanEvent",
"UsageDetails",
]
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ...commons.types.map_value import MapValue
from .create_span_body import CreateSpanBody
from .ingestion_usage import IngestionUsage
from .usage_details import UsageDetails


class CreateGenerationBody(CreateSpanBody):
Expand All @@ -19,6 +20,12 @@ class CreateGenerationBody(CreateSpanBody):
alias="modelParameters", default=None
)
usage: typing.Optional[IngestionUsage] = None
usage_details: typing.Optional[UsageDetails] = pydantic_v1.Field(
alias="usageDetails", default=None
)
cost_details: typing.Optional[typing.Dict[str, float]] = pydantic_v1.Field(
alias="costDetails", default=None
)
prompt_name: typing.Optional[str] = pydantic_v1.Field(
alias="promptName", default=None
)
Expand Down
46 changes: 46 additions & 0 deletions langfuse/api/resources/ingestion/types/open_ai_usage_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# This file was auto-generated by Fern from our API Definition.

import datetime as dt
import typing

from ....core.datetime_utils import serialize_datetime
from ....core.pydantic_utilities import deep_union_pydantic_dicts, pydantic_v1


class OpenAiUsageSchema(pydantic_v1.BaseModel):
prompt_tokens: int
completion_tokens: int
total_tokens: int
prompt_tokens_details: typing.Optional[typing.Dict[str, int]] = None
completion_tokens_details: typing.Optional[typing.Dict[str, int]] = None

def json(self, **kwargs: typing.Any) -> str:
kwargs_with_defaults: typing.Any = {
"by_alias": True,
"exclude_unset": True,
**kwargs,
}
return super().json(**kwargs_with_defaults)

def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
kwargs_with_defaults_exclude_unset: typing.Any = {
"by_alias": True,
"exclude_unset": True,
**kwargs,
}
kwargs_with_defaults_exclude_none: typing.Any = {
"by_alias": True,
"exclude_none": True,
**kwargs,
}

return deep_union_pydantic_dicts(
super().dict(**kwargs_with_defaults_exclude_unset),
super().dict(**kwargs_with_defaults_exclude_none),
)

class Config:
frozen = True
smart_union = True
extra = pydantic_v1.Extra.allow
json_encoders = {dt.datetime: serialize_datetime}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ...commons.types.map_value import MapValue
from .ingestion_usage import IngestionUsage
from .update_span_body import UpdateSpanBody
from .usage_details import UsageDetails


class UpdateGenerationBody(UpdateSpanBody):
Expand All @@ -22,6 +23,12 @@ class UpdateGenerationBody(UpdateSpanBody):
prompt_name: typing.Optional[str] = pydantic_v1.Field(
alias="promptName", default=None
)
usage_details: typing.Optional[UsageDetails] = pydantic_v1.Field(
alias="usageDetails", default=None
)
cost_details: typing.Optional[typing.Dict[str, float]] = pydantic_v1.Field(
alias="costDetails", default=None
)
prompt_version: typing.Optional[int] = pydantic_v1.Field(
alias="promptVersion", default=None
)
Expand Down
7 changes: 7 additions & 0 deletions langfuse/api/resources/ingestion/types/usage_details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file was auto-generated by Fern from our API Definition.

import typing

from .open_ai_usage_schema import OpenAiUsageSchema

UsageDetails = typing.Union[typing.Dict[str, int], OpenAiUsageSchema]
6 changes: 4 additions & 2 deletions langfuse/api/resources/media/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def get_upload_url(
Examples
--------
from langfuse.api import GetMediaUploadUrlRequest
from langfuse.api import GetMediaUploadUrlRequest, MediaContentType
from langfuse.api.client import FernLangfuse
client = FernLangfuse(
Expand All @@ -212,6 +212,7 @@ def get_upload_url(
request=GetMediaUploadUrlRequest(
trace_id="string",
observation_id="string",
content_type=MediaContentType.IMAGE_PNG,
content_length=1,
sha_256_hash="string",
field="string",
Expand Down Expand Up @@ -446,7 +447,7 @@ async def get_upload_url(
--------
import asyncio
from langfuse.api import GetMediaUploadUrlRequest
from langfuse.api import GetMediaUploadUrlRequest, MediaContentType
from langfuse.api.client import AsyncFernLangfuse
client = AsyncFernLangfuse(
Expand All @@ -464,6 +465,7 @@ async def main() -> None:
request=GetMediaUploadUrlRequest(
trace_id="string",
observation_id="string",
content_type=MediaContentType.IMAGE_PNG,
content_length=1,
sha_256_hash="string",
field="string",
Expand Down
Loading

0 comments on commit cf9ec3e

Please sign in to comment.