@arizeai/phoenix-client
Version:
A client for the Phoenix API
71 lines • 3.47 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.toAnthropic = void 0;
const converters_1 = require("../../schemas/llm/converters");
const formatPromptMessages_1 = require("../../utils/formatPromptMessages");
const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
/**
* Convert a Phoenix prompt to Anthropic client sdk's message create parameters
*/
const toAnthropic = ({ prompt, variables, }) => {
var _a, _b, _c;
try {
let invocationParameters;
if (prompt.invocation_parameters.type === "anthropic") {
invocationParameters = prompt.invocation_parameters.anthropic;
}
else {
// eslint-disable-next-line no-console
console.warn("Prompt is not an Anthropic prompt, falling back to default Anthropic invocation parameters");
invocationParameters = { max_tokens: 1024 };
}
// parts of the prompt that can be directly converted to Anthropic params
const baseCompletionParams = Object.assign({ model: prompt.model_name }, invocationParameters);
if (!("messages" in prompt.template)) {
return null;
}
let formattedMessages = prompt.template.messages;
if (variables) {
formattedMessages = (0, formatPromptMessages_1.formatPromptMessages)(prompt.template_format, formattedMessages, variables);
}
const messages = formattedMessages.map((message) => {
const anthropicMessage = (0, converters_1.safelyConvertMessageToProvider)({
message,
targetProvider: "ANTHROPIC",
});
(0, tiny_invariant_1.default)(anthropicMessage, "Message is not valid");
return anthropicMessage;
});
let tools = (_a = prompt.tools) === null || _a === void 0 ? void 0 : _a.tools.map((tool) => {
const anthropicToolDefinition = (0, converters_1.safelyConvertToolDefinitionToProvider)({
toolDefinition: tool,
targetProvider: "ANTHROPIC",
});
(0, tiny_invariant_1.default)(anthropicToolDefinition, "Tool definition is not valid");
return anthropicToolDefinition;
});
tools = ((_b = tools === null || tools === void 0 ? void 0 : tools.length) !== null && _b !== void 0 ? _b : 0) > 0 ? tools : undefined;
let tool_choice = (0, converters_1.safelyConvertToolChoiceToProvider)({
toolChoice: (_c = prompt === null || prompt === void 0 ? void 0 : prompt.tools) === null || _c === void 0 ? void 0 : _c.tool_choice,
targetProvider: "ANTHROPIC",
}) || undefined;
tool_choice = (tools === null || tools === void 0 ? void 0 : tools.length) ? tool_choice : undefined;
// combine base and computed params
const completionParams = Object.assign(Object.assign({}, baseCompletionParams), { messages,
tools,
tool_choice });
return completionParams;
}
catch (e) {
// eslint-disable-next-line no-console
console.warn(`Failed to convert prompt to Anthropic params`);
// eslint-disable-next-line no-console
console.error(e);
return null;
}
};
exports.toAnthropic = toAnthropic;
//# sourceMappingURL=toAnthropic.js.map