@arizeai/phoenix-client
Version:
A client for the Phoenix API
78 lines • 3.59 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDataset = createDataset;
const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
const client_1 = require("../client");
/**
* Create a new dataset with examples.
*
* @experimental this interface may change in the future
*
* @param params - The parameters for creating the dataset
* @param params.client - Optional Phoenix client instance
* @param params.name - The name of the dataset
* @param params.description - The description of the dataset
* @param params.examples - The examples to create in the dataset. Each example can include:
* - `input`: Required input data for the example
* - `output`: Optional expected output data
* - `metadata`: Optional metadata for the example
* - `splits`: Optional split assignment (string, array of strings, or null)
* - `spanId`: Optional OpenTelemetry span ID to link the example back to its source span
*
* @returns A promise that resolves to the created dataset ID
*
* @example
* ```ts
* // Create a dataset with span links
* const { datasetId } = await createDataset({
* name: "qa-dataset",
* description: "Q&A examples from traces",
* examples: [
* {
* input: { question: "What is AI?" },
* output: { answer: "Artificial Intelligence is..." },
* spanId: "abc123def456" // Links to the source span
* },
* {
* input: { question: "Explain ML" },
* output: { answer: "Machine Learning is..." },
* spanId: "789ghi012jkl"
* }
* ]
* });
* ```
*/
async function createDataset({ client: _client, name, description, examples, }) {
var _a;
const client = _client || (0, client_1.createClient)();
const inputs = examples.map((example) => example.input);
const outputs = examples.map((example) => { var _a; return (_a = example === null || example === void 0 ? void 0 : example.output) !== null && _a !== void 0 ? _a : {}; }); // Treat null as an empty object
const metadata = examples.map((example) => { var _a; return (_a = example === null || example === void 0 ? void 0 : example.metadata) !== null && _a !== void 0 ? _a : {}; });
const splits = examples.map((example) => (example === null || example === void 0 ? void 0 : example.splits) !== undefined ? example.splits : null);
// Extract span IDs from examples, preserving null/undefined as null
const spanIds = examples.map((example) => { var _a; return (_a = example === null || example === void 0 ? void 0 : example.spanId) !== null && _a !== void 0 ? _a : null; });
// Only include span_ids in the request if at least one example has a span ID
const hasSpanIds = spanIds.some((id) => id !== null);
const createDatasetResponse = await client.POST("/v1/datasets/upload", {
params: {
query: {
// TODO: parameterize this
sync: true,
},
},
body: Object.assign({ name,
description, action: "create", inputs,
outputs,
metadata,
splits }, (hasSpanIds ? { span_ids: spanIds } : {})),
});
(0, tiny_invariant_1.default)((_a = createDatasetResponse.data) === null || _a === void 0 ? void 0 : _a.data, "Failed to create dataset");
const datasetId = createDatasetResponse.data.data.dataset_id;
return {
datasetId,
};
}
//# sourceMappingURL=createDataset.js.map