@arizeai/phoenix-client
Version:
A client for the Phoenix API
60 lines • 2.79 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPromptBySelector = getPromptBySelector;
const client_1 = require("../client");
const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
/**
* Get a prompt from the Phoenix API.
*
* if the input is a prompt id, fetch the latest prompt version from the client.
* if the input is a prompt version id, fetch that prompt version.
* if the input is a prompt tag and name, fetch the prompt version that has that tag and name.
* if the input is a prompt name, fetch the latest prompt version from the client.
*
* @param params - The parameters to get a prompt.
* @returns The nearest prompt version that matches the selector, or null if it does not exist.
*/
async function getPromptBySelector({ client: _client, prompt, }) {
var _a, _b, _c;
try {
const client = _client !== null && _client !== void 0 ? _client : (0, client_1.createClient)();
if ("promptId" in prompt) {
throw new Error("Prompt by id not implemented");
}
if ("versionId" in prompt) {
const response = await client.GET(`/v1/prompt_versions/{prompt_version_id}`, {
params: { path: { prompt_version_id: prompt.versionId } },
});
(0, tiny_invariant_1.default)((_a = response.data) === null || _a === void 0 ? void 0 : _a.data, `Prompt version ${prompt.versionId} not found`);
return response.data.data;
}
if ("tag" in prompt && "name" in prompt) {
const response = await client.GET(`/v1/prompts/{prompt_identifier}/tags/{tag_name}`, {
params: {
path: { prompt_identifier: prompt.name, tag_name: prompt.tag },
},
});
(0, tiny_invariant_1.default)((_b = response.data) === null || _b === void 0 ? void 0 : _b.data, `Prompt tag ${prompt.tag} not found`);
return response.data.data;
}
if ("name" in prompt) {
const response = await client.GET(`/v1/prompts/{prompt_identifier}/latest`, {
params: {
path: { prompt_identifier: prompt.name },
},
});
(0, tiny_invariant_1.default)((_c = response.data) === null || _c === void 0 ? void 0 : _c.data, `Prompt ${prompt.name} not found`);
return response.data.data;
}
throw new Error("Invalid prompt");
}
catch (error) {
// eslint-disable-next-line no-console
console.error(error);
return null;
}
}
//# sourceMappingURL=getPromptBySelector.js.map