UNPKG

@arizeai/phoenix-client

Version:
79 lines 3.31 kB
import invariant from "tiny-invariant"; import { safelyConvertMessageToProvider, safelyConvertToolChoiceToProvider, safelyConvertToolDefinitionToProvider, } from "../../schemas/llm/converters.js"; import { isPromptToolRaw } from "../../types/prompts.js"; import { formatPromptMessages } from "../../utils/formatPromptMessages.js"; /** * Convert a Phoenix prompt to Anthropic client sdk's message create parameters */ export const toAnthropic = ({ prompt, variables, }) => { 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 = { model: prompt.model_name, ...invocationParameters, }; if (!("messages" in prompt.template)) { return null; } let formattedMessages = prompt.template.messages; if (variables) { formattedMessages = formatPromptMessages(prompt.template_format, formattedMessages, variables); } const messages = formattedMessages.map((message) => { const anthropicMessage = safelyConvertMessageToProvider({ message, targetProvider: "ANTHROPIC", }); invariant(anthropicMessage, "Message is not valid"); return anthropicMessage; }); const toolsList = prompt.tools?.tools ?? []; // 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 (isPromptToolRaw(tool)) { return tool.raw; } const anthropicToolDefinition = safelyConvertToolDefinitionToProvider({ toolDefinition: tool, targetProvider: "ANTHROPIC", }); invariant(anthropicToolDefinition, "Tool definition is not valid"); return anthropicToolDefinition; }); const tool_choice = tools ? (safelyConvertToolChoiceToProvider({ toolChoice: prompt?.tools?.tool_choice, targetProvider: "ANTHROPIC", }) ?? undefined) : undefined; // combine base and computed params const completionParams = { ...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; } }; //# sourceMappingURL=toAnthropic.js.map