Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cost, latency, tokens to playground #1096

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion agenta-web/src/components/Playground/Views/TestView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ interface BoxComponentProps {
inputParams: Parameter[] | null
testData: GenericObject
result: string
additionalData: {
cost: number | null
latency: number | null
usage: {completion_tokens: number; prompt_tokens: number; total_tokens: number} | null
}
onInputParamChange: (paramName: string, newValue: any) => void
onRun: () => void
onAddToTestset: (params: Record<string, string>) => void
Expand All @@ -105,6 +110,7 @@ const BoxComponent: React.FC<BoxComponentProps> = ({
inputParams,
testData,
result,
additionalData,
onInputParamChange,
onRun,
onAddToTestset,
Expand Down Expand Up @@ -155,6 +161,28 @@ const BoxComponent: React.FC<BoxComponentProps> = ({
imageSize="large"
/>
</Row>
{additionalData && (
<Space>
<p>
Tokens:{" "}
{additionalData.usage !== null
? JSON.stringify(additionalData.usage.total_tokens)
: 0}
</p>
<p>
Cost:{" "}
{additionalData.cost !== null
? `$${additionalData.cost.toFixed(4)}`
: "$0.00"}
</p>
<p>
Latency:{" "}
{additionalData.latency !== null
? `${Math.round(additionalData.latency * 1000)}ms`
: "0ms"}
</p>
</Space>
)}
<Row className={classes.row2} style={{marginBottom: isChatVariant ? 12 : 0}}>
<Col span={24} className={classes.row2Col}>
<Button
Expand Down Expand Up @@ -206,6 +234,13 @@ const App: React.FC<TestViewProps> = ({inputParams, optParams, variant, isChatVa
const [params, setParams] = useState<Record<string, string> | null>(null)
const classes = useStylesApp()
const rootRef = React.useRef<HTMLDivElement>(null)
const [additionalDataList, setAdditionalDataList] = useState<
Array<{
cost: number | null
latency: number | null
usage: {completion_tokens: number; prompt_tokens: number; total_tokens: number} | null
}>
>(testList.map(() => ({cost: null, latency: null, usage: null})))

useEffect(() => {
setResultsList((prevResultsList) => {
Expand Down Expand Up @@ -266,7 +301,12 @@ const App: React.FC<TestViewProps> = ({inputParams, optParams, variant, isChatVa
isChatVariant ? testItem.chat : [],
)

setResultForIndex(res, index)
setResultForIndex(res.message, index)
setAdditionalDataList((prev) => {
const newDataList = [...prev]
newDataList[index] = {cost: res.cost, latency: res.latency, usage: res.usage}
return newDataList
})
} catch (e) {
setResultForIndex(
"The code has resulted in the following error: \n\n --------------------- \n" +
Expand Down Expand Up @@ -348,6 +388,7 @@ const App: React.FC<TestViewProps> = ({inputParams, optParams, variant, isChatVa
?.content
: resultsList[index]
}
additionalData={additionalDataList[index]}
onInputParamChange={(paramName, value) =>
handleInputParamChange(paramName, value, index)
}
Expand Down
Loading