parea-ai
Version:
Client SDK library to connect to Parea AI.
108 lines (107 loc) • 3.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExperimentResult = exports.TrialResult = void 0;
const types_1 = require("../types");
/**
* Represents the result of a single trial in an experiment.
* @template T - The type of the input
* @template R - The type of the output
*/
class TrialResult {
/**
* Creates a new TrialResult instance.
* @param input - The input of the trial
* @param output - The output of the trial (null if error occurred)
* @param error - The error that occurred during the trial (null if successful)
* @param state - The status of the trial
* @param scores - The evaluation scores of the trial (null if not available)
* @param logs - The logs generated during the trial (null if not available)
*/
constructor(input, output, error, state, scores, logs) {
this.input = input;
this.output = output;
this.error = error;
this.state = state;
this.scores = scores;
this.logs = logs;
}
}
exports.TrialResult = TrialResult;
/**
* Represents the aggregated results of an experiment.
* @template T - The type of the input parameters
* @template R - The type of the output
*/
class ExperimentResult {
/**
* Creates a new ExperimentResult instance.
* @param name - The name of the experiment
* @param results - An array of TrialResult instances
* @param metadata - Additional metadata for the experiment
*/
constructor(name, results, metadata) {
this.name = name;
this.results = results;
this.metadata = metadata;
}
/**
* Calculates the success rate of the experiment.
* @returns The percentage of successful trials
*/
getSuccessRate() {
const successfulTrials = this.results.filter((r) => r.state === types_1.ExperimentStatus.COMPLETED);
return (successfulTrials.length / this.results.length) * 100;
}
/**
* Retrieves all logs from successful trials.
* @returns An array of EvaluatedLog objects
*/
getLogs() {
return this.results
.filter((r) => r.logs !== null)
.map((r) => r.logs)
.flat();
}
/**
* Retrieves all errors from failed trials.
* @returns An array of Error objects
*/
getErrors() {
return this.results
.filter((r) => r.error !== null)
.map((r) => r.error)
.filter((error) => error !== null);
}
/**
* Retrieves error messages from all failed trials.
* @returns A string containing all error messages, separated by commas
*/
getErrorsString() {
return this.getErrors()
.map((e) => e.message)
.join(', ');
}
/**
* Calculates the average scores across all trials.
* @returns An object containing average scores for each evaluation metric
*/
getAverageScores() {
const scoreMap = {};
for (const result of this.results) {
if (result.scores) {
for (const score of result.scores) {
if (!scoreMap[score.name]) {
scoreMap[score.name] = [];
}
scoreMap[score.name].push(score.score);
}
}
}
const averageScores = {};
for (const [name, scores] of Object.entries(scoreMap)) {
averageScores[name] = scores.reduce((a, b) => a + b, 0) / scores.length;
}
return averageScores;
}
}
exports.ExperimentResult = ExperimentResult;