UNPKG

@arizeai/phoenix-client

Version:
116 lines 5.42 kB
"use strict"; 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"); const serverRequirements_1 = require("../constants/serverRequirements"); const serverVersionUtils_1 = require("../utils/serverVersionUtils"); /** * Create a dataset with the given examples. * * If a dataset with the same name already exists, it is updated to match the * provided examples. Re-running with the same inputs is a no-op. * * @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); // Extract example IDs from examples, preserving null/undefined as null const exampleIds = examples.map((example) => { var _a; return (_a = example === null || example === void 0 ? void 0 : example.id) !== null && _a !== void 0 ? _a : null; }); // Only include example_ids in the request if at least one example has an ID const hasExampleIds = exampleIds.some((id) => id !== null); const post = (action) => client.POST("/v1/datasets/upload", { params: { query: { // TODO: parameterize this sync: true, }, }, body: Object.assign(Object.assign({ name, description, action, inputs, outputs, metadata, splits }, (hasSpanIds ? { span_ids: spanIds } : {})), (hasExampleIds ? { example_ids: exampleIds } : {})), }); if (hasExampleIds) { await (0, serverVersionUtils_1.ensureServerCapability)({ client, requirement: serverRequirements_1.DATASET_UPLOAD_EXAMPLE_IDS, }); } let createDatasetResponse = await post("update"); if (isUnsupportedUpdateActionResponse(createDatasetResponse)) { warnUpdateFallback(); createDatasetResponse = await post("create"); } (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, }; } function isUnsupportedUpdateActionResponse(result) { var _a; var _b; if (((_a = result.response) === null || _a === void 0 ? void 0 : _a.status) !== 422) return false; const body = typeof result.error === "string" ? result.error : JSON.stringify((_b = result.error) !== null && _b !== void 0 ? _b : ""); return (body.includes("Invalid dateset action") || body.includes("Invalid dataset action")); } function warnUpdateFallback() { // eslint-disable-next-line no-console console.warn("Phoenix server does not support declarative update semantics. " + "Upgrade to Phoenix v15 or later."); } //# sourceMappingURL=createDataset.js.map