-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from parea-ai/PAI-583-experiment-in-typescript-…
…easy-auto-evals feat(tracer): experiment, evals, new context manager
- Loading branch information
Showing
8 changed files
with
474 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { DataItem, ExperimentStatsSchema, TraceStatsSchema } from '../types'; | ||
import { Parea } from '../client'; | ||
import { asyncPool } from '../helpers'; | ||
|
||
function calculateAvgAsString(values: number[] | undefined): string { | ||
if (!values || values.length === 0) { | ||
return 'N/A'; | ||
} | ||
const filteredValues = values.filter((x) => x !== null); | ||
const avg = filteredValues.reduce((acc, curr) => acc + curr, 0) / filteredValues.length; | ||
return avg.toFixed(2); | ||
} | ||
|
||
function calculateAvgStdForExperiment(experimentStats: ExperimentStatsSchema): { [key: string]: string } { | ||
const traceStats: TraceStatsSchema[] = experimentStats.parent_trace_stats; | ||
const latencyValues = traceStats.map((traceStat) => traceStat.latency || 0); | ||
const inputTokensValues = traceStats.map((traceStat) => traceStat.input_tokens || 0); | ||
const outputTokensValues = traceStats.map((traceStat) => traceStat.output_tokens || 0); | ||
const totalTokensValues = traceStats.map((traceStat) => traceStat.total_tokens || 0); | ||
const costValues = traceStats.map((traceStat) => traceStat.cost || 0); | ||
const scoreNameToValues: { [key: string]: number[] } = {}; | ||
|
||
traceStats.forEach((traceStat) => { | ||
traceStat.scores?.forEach((score) => { | ||
if (!scoreNameToValues[score.name]) { | ||
scoreNameToValues[score.name] = []; | ||
} | ||
scoreNameToValues[score.name].push(score.score); | ||
}); | ||
}); | ||
|
||
const result: { [key: string]: string } = { | ||
latency: calculateAvgAsString(latencyValues), | ||
input_tokens: calculateAvgAsString(inputTokensValues), | ||
output_tokens: calculateAvgAsString(outputTokensValues), | ||
total_tokens: calculateAvgAsString(totalTokensValues), | ||
cost: calculateAvgAsString(costValues), | ||
}; | ||
|
||
Object.keys(scoreNameToValues).forEach((scoreName) => { | ||
result[scoreName] = calculateAvgAsString(scoreNameToValues[scoreName]); | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
export async function experiment( | ||
name: string, | ||
data: Iterable<Record<string, any>>, | ||
func: (...args: any[]) => Promise<any>, | ||
p: Parea, | ||
maxParallelCalls: number = 10, | ||
): Promise<ExperimentStatsSchema> { | ||
const experimentSchema = await p.createExperiment({ name }); | ||
const experimentUUID = experimentSchema.uuid; | ||
process.env.PAREA_OS_ENV_EXPERIMENT_UUID = experimentUUID; | ||
|
||
const tasksGenerator = asyncPool(maxParallelCalls, data, async (dataInput) => { | ||
return func(dataInput); | ||
}); | ||
|
||
for await (const _ of tasksGenerator) { | ||
// Purposely ignore. Result not needed | ||
void _; | ||
} | ||
|
||
const experimentStats: ExperimentStatsSchema = await p.finishExperiment(experimentUUID); | ||
const statNameToAvgStd = calculateAvgStdForExperiment(experimentStats); | ||
console.log(`Experiment stats:\n${JSON.stringify(statNameToAvgStd, null, 2)}\n\n`); | ||
console.log(`View experiment & its traces at: https://app.parea.ai/experiments/${experimentUUID}\n`); | ||
return experimentStats; | ||
} | ||
|
||
export class Experiment { | ||
name: string; | ||
data: Iterable<DataItem>; | ||
func: (dataItem: DataItem) => Promise<any>; | ||
p: Parea; | ||
experimentStats?: ExperimentStatsSchema; | ||
|
||
constructor(name: string, data: Iterable<DataItem>, func: (dataItem: DataItem) => Promise<any>, p: Parea) { | ||
this.name = name; | ||
this.data = data; | ||
this.func = func; | ||
this.p = p; | ||
} | ||
|
||
async run(): Promise<void> { | ||
this.experimentStats = await experiment(this.name, this.data, this.func, this.p); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.