Skip to content

Commit

Permalink
Merge pull request #1570 from Agenta-AI/total-cost
Browse files Browse the repository at this point in the history
add total latency
  • Loading branch information
aakrem authored Apr 28, 2024
2 parents a4518eb + a082cc2 commit 429da77
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class Evaluation(BaseModel):
status: Result
aggregated_results: List[AggregatedResult]
average_cost: Optional[Result]
total_cost: Optional[Result]
average_latency: Optional[Result]
created_at: datetime
updated_at: datetime
Expand Down
1 change: 1 addition & 0 deletions agenta-backend/agenta_backend/models/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ async def evaluation_db_to_pydantic(
created_at=evaluation_db.created_at,
updated_at=evaluation_db.updated_at,
average_cost=evaluation_db.average_cost,
total_cost=evaluation_db.total_cost,
average_latency=evaluation_db.average_latency,
)

Expand Down
1 change: 1 addition & 0 deletions agenta-backend/agenta_backend/models/db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ class EvaluationDB(Document):
evaluators_configs: List[PydanticObjectId]
aggregated_results: List[AggregatedResult]
average_cost: Optional[Result] = None
total_cost: Optional[Result] = None
average_latency: Optional[Result] = None
created_at: datetime = Field(default=datetime.now(timezone.utc))
updated_at: datetime = Field(default=datetime.now(timezone.utc))
Expand Down
26 changes: 26 additions & 0 deletions agenta-backend/agenta_backend/services/aggregation_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,29 @@ def aggregate_float_from_llm_app_response(
value=None,
error=Error(message=str(exc), stacktrace=str(traceback.format_exc())),
)


def sum_float_from_llm_app_response(
invocation_results: List[InvokationResult], key: Optional[str]
) -> Result:
try:
if not key:
raise ValueError("Key is required to aggregate InvokationResult objects.")

values = [
getattr(inv_result, key)
for inv_result in invocation_results
if hasattr(inv_result, key) and getattr(inv_result, key) is not None
]

if not values:
raise ValueError(f"No valid values found for {key} sum aggregation.")

total_value = sum(values)
return Result(type=key, value=total_value)
except Exception as exc:
return Result(
type="error",
value=None,
error=Error(message=str(exc), stacktrace=str(traceback.format_exc())),
)
9 changes: 8 additions & 1 deletion agenta-backend/agenta_backend/tasks/evaluations.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,17 @@ def evaluate(
average_cost = aggregation_service.aggregate_float_from_llm_app_response(
app_outputs, "cost"
)
total_cost = aggregation_service.sum_float_from_llm_app_response(
app_outputs, "cost"
)
loop.run_until_complete(
update_evaluation(
evaluation_id,
{"average_latency": average_latency, "average_cost": average_cost},
{
"average_latency": average_latency,
"average_cost": average_cost,
"total_cost": total_cost,
},
)
)

Expand Down
1 change: 1 addition & 0 deletions agenta-cli/agenta/client/backend/types/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Evaluation(pydantic.BaseModel):
status: Result
aggregated_results: typing.List[AggregatedResult]
average_cost: typing.Optional[Result]
total_cost: typing.Optional[Result]
average_latency: typing.Optional[Result]
created_at: dt.datetime
updated_at: dt.datetime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,20 +289,20 @@ const EvaluationResults: React.FC<Props> = () => {
{
flex: 1,
field: "average_latency",
headerName: "Latency",
headerName: "Avg. Latency",
hide: hiddenCols.includes("Latency"),
minWidth: 120,
...getFilterParams("number"),
valueGetter: (params) => getTypedValue(params?.data?.average_latency),
},
{
flex: 1,
field: "average_cost",
headerName: "Cost",
field: "total_cost",
headerName: "Total Cost",
hide: hiddenCols.includes("Cost"),
minWidth: 120,
...getFilterParams("number"),
valueGetter: (params) => getTypedValue(params?.data?.average_cost),
valueGetter: (params) => getTypedValue(params?.data?.total_cost),
},
{
flex: 1,
Expand Down
1 change: 1 addition & 0 deletions agenta-web/src/lib/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ export interface _Evaluation {
revisions: string[]
average_latency?: TypedValue & {error: null | EvaluationError}
average_cost?: TypedValue & {error: null | EvaluationError}
total_cost?: TypedValue & {error: null | EvaluationError}
variant_revision_ids: string[]
}

Expand Down
1 change: 1 addition & 0 deletions agenta-web/src/services/evaluations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ const evaluationTransformer = (item: any) => ({
variant_revision_ids: item.variant_revision_ids,
variant_ids: item.variant_ids,
average_cost: item.average_cost,
total_cost: item.total_cost,
average_latency: item.average_latency,
})
export const fetchAllEvaluations = async (appId: string) => {
Expand Down

0 comments on commit 429da77

Please sign in to comment.