UNPKG

parea-ai

Version:

Client SDK library to connect to Parea AI.

108 lines (107 loc) 3.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.experimentContext = void 0; const async_hooks_1 = require("async_hooks"); /** * Manages the context for experiments using the Singleton pattern. */ class ExperimentContext { constructor() { this.context = new async_hooks_1.AsyncLocalStorage(); } /** * Gets the singleton instance of ExperimentContext. * @returns The ExperimentContext instance. */ static getInstance() { if (!ExperimentContext.instance) { ExperimentContext.instance = new ExperimentContext(); } return ExperimentContext.instance; } /** * Runs a callback function within the context of an experiment. * @param experimentUUID - The UUID of the experiment. * @param callback - The function to run within the experiment context. * @returns The result of the callback function. */ runInContext(experimentUUID, callback) { const store = new Map(); return this.context.run(store, () => { store.set(experimentUUID, { logs: [], scores: [] }); return callback(); }); } /** * Adds a score to the experiment context. * @param experimentUUID - The UUID of the experiment. * @param score - The evaluation result to add. */ addScore(experimentUUID, score) { const store = this.context.getStore(); if (store) { const context = store.get(experimentUUID) || { logs: [], scores: [] }; context.scores.push(score); store.set(experimentUUID, context); } else { console.error(`Experiment context store not found for experiment ${experimentUUID}`); } } /** * Adds a score to the experiment context. * @param experimentUUID - The UUID of the experiment. * @param scores - The evaluation results to add. */ addScores(experimentUUID, scores) { const store = this.context.getStore(); if (store) { const context = store.get(experimentUUID) || { logs: [], scores: [] }; context.scores.push(...scores); store.set(experimentUUID, context); } else { console.error(`Experiment context store not found for experiment ${experimentUUID}`); } } /** * Adds a log to the experiment context. * @param experimentUUID - The UUID of the experiment. * @param log - The evaluated log to add. */ addLog(experimentUUID, log) { const store = this.context.getStore(); if (store) { const context = store.get(experimentUUID) || { logs: [], scores: [] }; context.logs.push(log); store.set(experimentUUID, context); } else { console.error(`Experiment context store not found for experiment ${experimentUUID}`); } } /** * Retrieves the scores for a specific experiment. * @param experimentUUID - The UUID of the experiment. * @returns An array of evaluation results. */ getScores(experimentUUID) { const store = this.context.getStore(); const context = store?.get(experimentUUID) || { logs: [], scores: [] }; return context.scores; } /** * Retrieves the logs for a specific experiment. * @param experimentUUID - The UUID of the experiment. * @returns An array of evaluated logs. */ getLogs(experimentUUID) { const store = this.context.getStore(); const context = store?.get(experimentUUID) || { logs: [], scores: [] }; return context.logs; } } /** * The singleton instance of ExperimentContext. */ exports.experimentContext = ExperimentContext.getInstance();