judgeval
Version:
Judgment SDK for TypeScript/JavaScript
143 lines • 4.32 kB
JavaScript
export class Example {
constructor(options) {
var _a;
this.input = options.input;
this.actualOutput = options.actualOutput;
this.expectedOutput = options.expectedOutput;
this.context = options.context;
this.retrievalContext = options.retrievalContext;
this.additionalMetadata = options.additionalMetadata;
this.toolsCalled = options.toolsCalled;
this.expectedTools = options.expectedTools;
this.name = options.name;
this.exampleId = options.exampleId || this.generateUUID();
this.exampleIndex = options.exampleIndex;
this.timestamp = options.timestamp || new Date().toISOString();
this.traceId = options.traceId;
this.example = (_a = options.example) !== null && _a !== void 0 ? _a : true;
}
/**
* Generate a UUID for the example ID
*/
generateUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = Math.random() * 16 | 0;
const v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
/**
* Builder pattern for creating an Example
*/
static builder() {
return new ExampleBuilder();
}
/**
* Convert the example to a plain object
*/
toJSON() {
var _a;
return {
input: this.input,
actual_output: this.actualOutput,
expected_output: this.expectedOutput,
context: this.context,
retrieval_context: this.retrievalContext,
additional_metadata: this.additionalMetadata,
tools_called: this.toolsCalled,
expected_tools: this.expectedTools,
name: this.name || "example",
example_id: this.exampleId,
example_index: this.exampleIndex,
timestamp: this.timestamp,
trace_id: this.traceId,
example: (_a = this.example) !== null && _a !== void 0 ? _a : true,
};
}
}
/**
* Builder for creating Example instances
*/
export class ExampleBuilder {
constructor() {
this._input = '';
}
input(input) {
this._input = input;
return this;
}
actualOutput(actualOutput) {
this._actualOutput = actualOutput;
return this;
}
expectedOutput(expectedOutput) {
this._expectedOutput = expectedOutput;
return this;
}
context(context) {
this._context = context;
return this;
}
retrievalContext(retrievalContext) {
this._retrievalContext = retrievalContext;
return this;
}
additionalMetadata(additionalMetadata) {
this._additionalMetadata = additionalMetadata;
return this;
}
toolsCalled(toolsCalled) {
this._toolsCalled = toolsCalled;
return this;
}
expectedTools(expectedTools) {
this._expectedTools = expectedTools;
return this;
}
name(name) {
this._name = name;
return this;
}
exampleId(exampleId) {
this._exampleId = exampleId;
return this;
}
exampleIndex(exampleIndex) {
this._exampleIndex = exampleIndex;
return this;
}
timestamp(timestamp) {
this._timestamp = timestamp;
return this;
}
traceId(traceId) {
this._traceId = traceId;
return this;
}
example(example) {
this._example = example;
return this;
}
build() {
if (!this._input) {
throw new Error('Input is required for an Example');
}
return new Example({
input: this._input,
actualOutput: this._actualOutput,
expectedOutput: this._expectedOutput,
context: this._context,
retrievalContext: this._retrievalContext,
additionalMetadata: this._additionalMetadata,
toolsCalled: this._toolsCalled,
expectedTools: this._expectedTools,
name: this._name,
exampleId: this._exampleId,
exampleIndex: this._exampleIndex,
timestamp: this._timestamp,
traceId: this._traceId,
example: this._example,
});
}
}
//# sourceMappingURL=example.js.map