@arizeai/phoenix-client
Version:
A client for the Phoenix API
192 lines • 8.82 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.findToolDefinitionDescription = exports.findToolDefinitionName = exports.detectToolDefinitionProvider = exports.detectToolChoiceProvider = exports.detectToolCallProvider = exports.detectMessagePartProvider = exports.detectMessageProvider = exports.makeSDKConverters = void 0;
const isObject_1 = require("../../utils/isObject");
const messagePartSchemas_1 = require("./anthropic/messagePartSchemas");
const messageSchemas_1 = require("./anthropic/messageSchemas");
const toolCallSchemas_1 = require("./anthropic/toolCallSchemas");
const toolChoiceSchemas_1 = require("./anthropic/toolChoiceSchemas");
const toolSchemas_1 = require("./anthropic/toolSchemas");
const messagePartSchemas_2 = require("./openai/messagePartSchemas");
const messageSchemas_2 = require("./openai/messageSchemas");
const toolCallSchemas_2 = require("./openai/toolCallSchemas");
const toolChoiceSchemas_2 = require("./openai/toolChoiceSchemas");
const toolSchemas_2 = require("./openai/toolSchemas");
const messagePartSchemas_3 = require("./phoenixPrompt/messagePartSchemas");
const messageSchemas_3 = require("./phoenixPrompt/messageSchemas");
const toolCallSchemas_3 = require("./phoenixPrompt/toolCallSchemas");
const toolChoiceSchemas_3 = require("./phoenixPrompt/toolChoiceSchemas");
const toolSchemas_3 = require("./phoenixPrompt/toolSchemas");
const messageSchemas_4 = require("./vercel/messageSchemas");
const toolSchemas_4 = require("./vercel/toolSchemas");
const schemas_1 = require("./schemas");
const makeSDKConverters = ({ messages, messageParts, toolChoices, toolCalls, toolDefinitions, responseFormat, }) => {
return {
messages,
messageParts,
toolChoices,
toolCalls,
toolDefinitions,
responseFormat,
};
};
exports.makeSDKConverters = makeSDKConverters;
/**
* Detect the provider of a message object
*/
const detectMessageProvider = (message) => {
const { success: openaiSuccess, data: openaiData } = messageSchemas_2.openAIMessageSchema.safeParse(message);
if (openaiSuccess) {
return {
// we cannot disambiguate between azure openai and openai here
provider: "OPENAI",
validatedMessage: openaiData,
};
}
const { success: anthropicSuccess, data: anthropicData } = messageSchemas_1.anthropicMessageSchema.safeParse(message);
if (anthropicSuccess) {
return {
provider: "ANTHROPIC",
validatedMessage: anthropicData,
};
}
const { success: vercelSuccess, data: vercelData } = messageSchemas_4.vercelAIMessageSchema.safeParse(message);
if (vercelSuccess) {
return { provider: "VERCEL_AI", validatedMessage: vercelData };
}
const { success: phoenixSuccess, data: phoenixData } = messageSchemas_3.phoenixMessageSchema.safeParse(message);
if (phoenixSuccess) {
return { provider: "PHOENIX", validatedMessage: phoenixData };
}
return { provider: null, validatedMessage: null };
};
exports.detectMessageProvider = detectMessageProvider;
const detectMessagePartProvider = (part) => {
const { success: openaiSuccess, data: openaiData } = messagePartSchemas_2.openaiChatPartSchema.safeParse(part);
if (openaiSuccess) {
return {
provider: "OPENAI",
validatedMessage: openaiData,
};
}
const { success: anthropicSuccess, data: anthropicData } = messagePartSchemas_1.anthropicMessagePartSchema.safeParse(part);
if (anthropicSuccess) {
return {
provider: "ANTHROPIC",
validatedMessage: anthropicData,
};
}
const { success: phoenixSuccess, data: phoenixData } = messagePartSchemas_3.phoenixContentPartSchema.safeParse(part);
if (phoenixSuccess) {
return { provider: "PHOENIX", validatedMessage: phoenixData };
}
return { provider: null, validatedMessage: null };
};
exports.detectMessagePartProvider = detectMessagePartProvider;
/**
* Detect the provider of a tool call object
*/
const detectToolCallProvider = (toolCall) => {
const { success: openaiSuccess, data: openaiData } = toolCallSchemas_2.openAIToolCallSchema.safeParse(toolCall);
if (openaiSuccess) {
// we cannot disambiguate between azure openai and openai here
return { provider: "OPENAI", validatedToolCall: openaiData };
}
const { success: anthropicSuccess, data: anthropicData } = toolCallSchemas_1.anthropicToolCallSchema.safeParse(toolCall);
if (anthropicSuccess) {
return { provider: "ANTHROPIC", validatedToolCall: anthropicData };
}
const { success: phoenixSuccess, data: phoenixData } = toolCallSchemas_3.phoenixToolCallSchema.safeParse(toolCall);
if (phoenixSuccess) {
return { provider: "PHOENIX", validatedToolCall: phoenixData };
}
return { provider: null, validatedToolCall: null };
};
exports.detectToolCallProvider = detectToolCallProvider;
/**
* Detects the provider of a tool choice
* @param toolChoice the tool choice to detect the provider of
* @returns the provider of the tool choice
*/
const detectToolChoiceProvider = (toolChoice) => {
const { success: openAISuccess, data: openAIData } = toolChoiceSchemas_2.openAIToolChoiceSchema.safeParse(toolChoice);
if (openAISuccess) {
return { provider: "OPENAI", toolChoice: openAIData };
}
const { success: anthropicSuccess, data: anthropicData } = toolChoiceSchemas_1.anthropicToolChoiceSchema.safeParse(toolChoice);
if (anthropicSuccess) {
return { provider: "ANTHROPIC", toolChoice: anthropicData };
}
const { success: phoenixSuccess, data: phoenixData } = toolChoiceSchemas_3.phoenixToolChoiceSchema.safeParse(toolChoice);
if (phoenixSuccess) {
return { provider: "PHOENIX", toolChoice: phoenixData };
}
return { provider: null, toolChoice: null };
};
exports.detectToolChoiceProvider = detectToolChoiceProvider;
/**
* Detect the provider of a tool call object
*/
const detectToolDefinitionProvider = (toolDefinition) => {
const { success: openaiSuccess, data: openaiData } = toolSchemas_2.openAIToolDefinitionSchema.safeParse(toolDefinition);
if (openaiSuccess) {
return {
// we cannot disambiguate between azure openai and openai here
provider: "OPENAI",
validatedToolDefinition: openaiData,
};
}
const { success: anthropicSuccess, data: anthropicData } = toolSchemas_1.anthropicToolDefinitionSchema.safeParse(toolDefinition);
if (anthropicSuccess) {
return {
provider: "ANTHROPIC",
validatedToolDefinition: anthropicData,
};
}
const { success: phoenixSuccess, data: phoenixData } = toolSchemas_3.phoenixToolDefinitionSchema.safeParse(toolDefinition);
if (phoenixSuccess) {
return { provider: "PHOENIX", validatedToolDefinition: phoenixData };
}
const { success: vercelSuccess, data: vercelData } = toolSchemas_4.vercelAIToolDefinitionSchema.safeParse(toolDefinition);
if (vercelSuccess) {
return { provider: "VERCEL_AI", validatedToolDefinition: vercelData };
}
return { provider: null, validatedToolDefinition: null };
};
exports.detectToolDefinitionProvider = detectToolDefinitionProvider;
const findToolDefinitionName = (toolDefinition) => {
const parsed = schemas_1.llmProviderToolDefinitionSchema.safeParse(toolDefinition);
if (!parsed.success || parsed.data === null || !(0, isObject_1.isObject)(parsed.data)) {
return null;
}
if ("function" in parsed.data &&
(0, isObject_1.isObject)(parsed.data.function) &&
"name" in parsed.data.function &&
typeof parsed.data.function.name === "string") {
return parsed.data.function.name;
}
if ("name" in parsed.data && typeof parsed.data.name === "string") {
return parsed.data.name;
}
return null;
};
exports.findToolDefinitionName = findToolDefinitionName;
const findToolDefinitionDescription = (toolDefinition) => {
const parsed = schemas_1.llmProviderToolDefinitionSchema.safeParse(toolDefinition);
if (!parsed.success || parsed.data === null || !(0, isObject_1.isObject)(parsed.data)) {
return null;
}
if ("function" in parsed.data &&
(0, isObject_1.isObject)(parsed.data.function) &&
"description" in parsed.data.function &&
typeof parsed.data.function.description === "string") {
return parsed.data.function.description;
}
if ("description" in parsed.data &&
typeof parsed.data.description === "string") {
return parsed.data.description;
}
return null;
};
exports.findToolDefinitionDescription = findToolDefinitionDescription;
//# sourceMappingURL=utils.js.map