53b38ea15c
Refactor/split investment and client * refactor: clean up tests * Renamed tests/qcqa to tests/qa_qc for consistency * comment out faulty test - see docstring note at top * split to client/investment * add __init__.py to investment and client to avoid mypy confusion * fix imports * update imports for consistency across investment module * move back to one config * try changing import to relative * add init.py to src * try relative import * try one more sys.path.append * fix path for client main * fix imports in file_processing * make prompts common * move keywords out to `src` * move smart_chunking_funcs to `src` * fix mypy error * start moving to explicit imports * remove old keywords and fix imports for keywords and prompts * change all imports to full paths * fix unit tests * add readme for new way of running things * isort after all that import work * remove unused sys.path.append from some tests Approved-by: Katon Minhas
73 lines
2.8 KiB
Python
73 lines
2.8 KiB
Python
import pandas as pd
|
|
|
|
from src import config
|
|
|
|
|
|
def aggregate_costlog(df: pd.DataFrame) -> tuple[pd.DataFrame, pd.DataFrame]:
|
|
"""This function aggregates the costlog in two ways:
|
|
- by caller function
|
|
- by filename
|
|
And returns two dataframes, corresponding to each of these aggregation methods
|
|
|
|
Args:
|
|
df (pd.DataFrame): Input costlog. Each row should be one call to the LLM.
|
|
The columns are determined by `config.COST_LOG_FIELDS`
|
|
|
|
Returns:
|
|
tuple[pd.DataFrame, pd.DataFrame]: Aggregated dataframes by caller and filename
|
|
respectively.
|
|
"""
|
|
# check required input columns
|
|
missing_columns = set(config.COST_LOG_FIELDS) - set(df.columns)
|
|
if missing_columns:
|
|
raise ValueError(f"Missing required columns: {', '.join(missing_columns)}")
|
|
|
|
group_by_caller_function = (
|
|
df.groupby("Caller Function")
|
|
.agg(
|
|
count_invocations=pd.NamedAgg(column="Filename", aggfunc="count"),
|
|
unique_filenames=pd.NamedAgg(column="Filename", aggfunc="nunique"),
|
|
sum_input_prompt_length=pd.NamedAgg(
|
|
column="Input Prompt Length", aggfunc="sum"
|
|
),
|
|
sum_input_prompt_tokens=pd.NamedAgg(
|
|
column="Input Prompt Tokens", aggfunc="sum"
|
|
),
|
|
sum_input_cost=pd.NamedAgg(column="Input Cost", aggfunc="sum"),
|
|
sum_output_response_length=pd.NamedAgg(
|
|
column="Output Response Length", aggfunc="sum"
|
|
),
|
|
sum_output_response_tokens=pd.NamedAgg(
|
|
column="Output Response Tokens", aggfunc="sum"
|
|
),
|
|
sum_output_cost=pd.NamedAgg(column="Output Cost", aggfunc="sum"),
|
|
sum_total_cost=pd.NamedAgg(column="Total Cost", aggfunc="sum"),
|
|
)
|
|
.reset_index()
|
|
)
|
|
|
|
group_by_filename = (
|
|
df.groupby("Filename")
|
|
.agg(
|
|
count_llm_calls=pd.NamedAgg(column="Caller Function", aggfunc="count"),
|
|
sum_input_prompt_length=pd.NamedAgg(
|
|
column="Input Prompt Length", aggfunc="sum"
|
|
),
|
|
sum_input_prompt_tokens=pd.NamedAgg(
|
|
column="Input Prompt Tokens", aggfunc="sum"
|
|
),
|
|
sum_input_cost=pd.NamedAgg(column="Input Cost", aggfunc="sum"),
|
|
sum_output_response_length=pd.NamedAgg(
|
|
column="Output Response Length", aggfunc="sum"
|
|
),
|
|
sum_output_response_tokens=pd.NamedAgg(
|
|
column="Output Response Tokens", aggfunc="sum"
|
|
),
|
|
sum_output_cost=pd.NamedAgg(column="Output Cost", aggfunc="sum"),
|
|
sum_total_cost=pd.NamedAgg(column="Total Cost", aggfunc="sum"),
|
|
)
|
|
.reset_index()
|
|
)
|
|
|
|
return group_by_caller_function, group_by_filename
|