UNPKG

@arizeai/phoenix-client

Version:
80 lines 3.75 kB
"use strict"; 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 utils_1 = require("../../schemas/llm/utils"); 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_schema.schema` to generateObject or streamObject * via `jsonSchema()`, through the `schema` argument. */ const toAI = ({ prompt, variables, }) => { var _a, _b, _c; // 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; }); let tools; if (((_a = prompt.tools) === null || _a === void 0 ? void 0 : _a.tools) && prompt.tools.tools.length > 0) { const toolsRecord = {}; for (const tool of prompt.tools.tools) { const name = (0, utils_1.findToolDefinitionName)(tool); (0, tiny_invariant_1.default)(name, "Tool definition name is not valid"); const converted = (0, converters_1.safelyConvertToolDefinitionToProvider)({ toolDefinition: tool, targetProvider: "VERCEL_AI", }); (0, tiny_invariant_1.default)(converted, "Tool definition is not valid"); toolsRecord[name] = converted; } tools = Object.keys(toolsRecord).length > 0 ? toolsRecord : undefined; } let toolChoice; if (tools && ((_b = prompt.tools) === null || _b === void 0 ? void 0 : _b.tool_choice)) { toolChoice = (_c = (0, converters_1.safelyConvertToolChoiceToProvider)({ toolChoice: prompt.tools.tool_choice, targetProvider: "VERCEL_AI", })) !== null && _c !== void 0 ? _c : undefined; } // combine base and computed params const completionParams = Object.assign(Object.assign(Object.assign(Object.assign({}, baseCompletionParams), { messages }), (tools !== undefined && { tools })), (toolChoice !== undefined && { 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