@arizeai/phoenix-client
Version:
A client for the Phoenix API
75 lines • 3.43 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.toAI = void 0;
const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
const converters_1 = require("../../schemas/llm/converters");
const formatPromptMessages_1 = require("../../utils/formatPromptMessages");
/**
* Converts a Phoenix prompt to Vercel AI sdk params.
*
* - note: To use response format, you must pass `prompt.response_format.json` to generateObject or streamObject
* directly, through the `schema` argument.
*/
const toAI = ({ prompt, variables, }) => {
var _a, _b;
// eslint-disable-next-line no-console
console.warn("Prompt invocation parameters not currently supported in AI SDK, falling back to default invocation parameters");
try {
// parts of the prompt that can be directly converted to OpenAI params
const baseCompletionParams = {
// Invocation parameters are validated on the phoenix-side
};
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 vercelAIMessage = (0, converters_1.safelyConvertMessageToProvider)({
message,
targetProvider: "VERCEL_AI",
});
(0, tiny_invariant_1.default)(vercelAIMessage, "Message is not valid");
return vercelAIMessage;
});
// convert tools to Vercel AI tool set, which is a map of tool name to tool
let tools = (_a = prompt.tools) === null || _a === void 0 ? void 0 : _a.tools.reduce((acc, tool) => {
if (!tool.function.parameters) {
return acc;
}
const vercelAIToolDefinition = (0, converters_1.safelyConvertToolDefinitionToProvider)({
toolDefinition: tool,
targetProvider: "VERCEL_AI",
});
(0, tiny_invariant_1.default)(vercelAIToolDefinition, "Tool definition is not valid");
acc[tool.function.name] = vercelAIToolDefinition;
return acc;
}, {});
const hasTools = Object.keys(tools !== null && tools !== void 0 ? tools : {}).length > 0;
tools = hasTools ? tools : undefined;
let toolChoice = (0, converters_1.safelyConvertToolChoiceToProvider)({
toolChoice: (_b = prompt.tools) === null || _b === void 0 ? void 0 : _b.tool_choice,
targetProvider: "VERCEL_AI",
}) || undefined;
toolChoice = hasTools ? toolChoice : undefined;
// combine base and computed params
const completionParams = Object.assign(Object.assign({}, baseCompletionParams), { messages,
tools,
toolChoice });
return completionParams;
}
catch (error) {
// eslint-disable-next-line no-console
console.warn(`Failed to convert prompt to AI params`);
// eslint-disable-next-line no-console
console.error(error);
return null;
}
};
exports.toAI = toAI;
//# sourceMappingURL=toAI.js.map