UNPKG

@arizeai/phoenix-client

Version:

A client for the Phoenix API

83 lines 3.9 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 converters_1 = require("../../schemas/llm/converters"); const formatPromptMessages_1 = require("../../utils/formatPromptMessages"); const tiny_invariant_1 = __importDefault(require("tiny-invariant")); /** * 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, _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; }); // convert tools to Vercel AI tool set, which is a map of tool name to tool // TODO: Vercel AI SDK 5 has complex tool schema // let tools: ToolSet | undefined = prompt.tools?.tools.reduce((acc, tool) => { // if (!tool.function.parameters) { // return acc; // } // const vercelAIToolDefinition = safelyConvertToolDefinitionToProvider({ // toolDefinition: tool, // targetProvider: "VERCEL_AI", // }); // invariant(vercelAIToolDefinition, "Tool definition is not valid"); // // TODO: get the symbol working here for validators // acc[tool.function.name] = vercelAIToolDefinition as unknown as Tool; // return acc; // }, {} as ToolSet); // const hasTools = Object.keys(tools ?? {}).length > 0; // tools = hasTools ? tools : undefined; const hasTools = false; const tools = undefined; if (((_a = prompt.tools) === null || _a === void 0 ? void 0 : _a.tools) && ((_b = prompt.tools) === null || _b === void 0 ? void 0 : _b.tools.length)) { // eslint-disable-next-line no-console console.warn("Prompt tools not currently supported in the AI SDK, falling back to no tools"); } let toolChoice = (0, converters_1.safelyConvertToolChoiceToProvider)({ toolChoice: (_c = prompt.tools) === null || _c === void 0 ? void 0 : _c.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