UNPKG

@arizeai/phoenix-client

Version:
82 lines 3.87 kB
"use strict"; 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 tiny_invariant_1 = __importDefault(require("tiny-invariant")); const converters_1 = require("../../schemas/llm/converters"); const prompts_1 = require("../../types/prompts"); const formatPromptMessages_1 = require("../../utils/formatPromptMessages"); /** * Convert a Phoenix prompt to Anthropic client sdk's message create parameters */ const toAnthropic = ({ prompt, variables, }) => { var _a, _b; var _c, _d; 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; }); const toolsList = (_c = (_a = prompt.tools) === null || _a === void 0 ? void 0 : _a.tools) !== null && _c !== void 0 ? _c : []; // Cast: raw tools are `Record<string, unknown>` straight from the prompt // store. We trust the upstream caller to have stored a shape Anthropic's // SDK accepts; no validation here. const tools = toolsList.length === 0 ? undefined : toolsList.map((tool) => { if ((0, prompts_1.isPromptToolRaw)(tool)) { return tool.raw; } const anthropicToolDefinition = (0, converters_1.safelyConvertToolDefinitionToProvider)({ toolDefinition: tool, targetProvider: "ANTHROPIC", }); (0, tiny_invariant_1.default)(anthropicToolDefinition, "Tool definition is not valid"); return anthropicToolDefinition; }); const tool_choice = tools ? ((_d = (0, converters_1.safelyConvertToolChoiceToProvider)({ toolChoice: (_b = prompt === null || prompt === void 0 ? void 0 : prompt.tools) === null || _b === void 0 ? void 0 : _b.tool_choice, targetProvider: "ANTHROPIC", })) !== null && _d !== void 0 ? _d : undefined) : 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