Skip to content

Commit

Permalink
Merge pull request #1714 from Agenta-AI/AGE-264/-added-fallback-value…
Browse files Browse the repository at this point in the history
…-for-undefined-parameters

Added fallback value for undefined parameters
  • Loading branch information
mmabrouk authored May 29, 2024
2 parents a5095bc + 2f12645 commit 953d239
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 34 deletions.
21 changes: 3 additions & 18 deletions agenta-web/src/components/Playground/Views/TestView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,24 +224,9 @@ const BoxComponent: React.FC<BoxComponentProps> = ({
</Row>
{additionalData?.cost || additionalData?.latency ? (
<Space>
<p>
Tokens:{" "}
{additionalData.usage !== null
? formatTokenUsage(additionalData.usage.total_tokens)
: 0}
</p>
<p>
Cost:{" "}
{additionalData.cost !== null
? formatCurrency(additionalData.cost)
: "$0.00"}
</p>
<p>
Latency:{" "}
{additionalData.latency !== null
? formatLatency(additionalData.latency)
: "0ms"}
</p>
<p>Tokens: {formatTokenUsage(additionalData?.usage?.total_tokens)}</p>
<p>Cost: {formatCurrency(additionalData?.cost)}</p>
<p>Latency: {formatLatency(additionalData?.latency)}</p>
</Space>
) : (
""
Expand Down
36 changes: 20 additions & 16 deletions agenta-web/src/lib/helpers/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,30 @@ const intlCurrency = new Intl.NumberFormat("en-US", {
maximumFractionDigits: 6,
})

export const formatNumber = (value = 0) => {
return intlNumber.format(value)
}

export const formatCurrency = (value: number) => {
if (value === null) {
return "-"
const handleNullOrUndefined = <T, R extends string>(
value: T | undefined | null,
callback: (v: T) => R,
defaultValue: R = "-" as R,
): R => {
if (value == null || (typeof value === "number" && isNaN(value))) {
return defaultValue
} else {
return intlCurrency.format(value)
return callback(value)
}
}

export const formatLatency = (value = 0) => {
return `${Math.round(value * 1000)}ms`
export const formatNumber = (value: number | undefined | null) => {
return handleNullOrUndefined(value, intlNumber.format)
}

export const formatTokenUsage = (value: number) => {
if (value === null) {
return "-"
} else {
return value
}
export const formatCurrency = (value: number | undefined | null) => {
return handleNullOrUndefined(value, intlCurrency.format)
}

export const formatLatency = (value: number | undefined | null) => {
return handleNullOrUndefined(value, (v) => `${Math.round(v * 1000)}ms`)
}

export const formatTokenUsage = (value: number | undefined | null) => {
return handleNullOrUndefined(value, (v) => v.toString())
}

0 comments on commit 953d239

Please sign in to comment.