@maximai/maxim-js
Version:
Maxim AI JS SDK. Visit https://getmaxim.ai for more info.
271 lines ⢠10.2 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.main = main;
const uuid_1 = require("uuid");
const ai_1 = require("ai");
const openai_1 = require("@ai-sdk/openai");
const v3_1 = require("zod/v3");
const dotenv = __importStar(require("dotenv"));
const vercel_1 = require("src/lib/logger/vercel");
const maxim_1 = require("src/lib/maxim");
// Load environment variables
dotenv.config();
// Initialize Maxim SDK
async function initializeMaxim() {
const apiKey = process.env["MAXIM_API_KEY"];
const repoId = process.env["MAXIM_LOG_REPO_ID"];
const baseUrl = process.env["MAXIM_BASE_URL"];
if (!apiKey || !repoId) {
throw new Error("MAXIM_API_KEY or MAXIM_LOG_REPO_ID is not defined in the environment variables");
}
const maxim = new maxim_1.Maxim({ baseUrl, apiKey, debug: true });
const logger = await maxim.logger({
id: repoId,
});
if (!logger) {
throw new Error("Logger is not available");
}
return { maxim, logger };
}
// Define calculator tool
const calculator = (0, ai_1.tool)({
description: "Perform basic arithmetic operations",
inputSchema: v3_1.z.object({
operation: v3_1.z.enum(["add", "subtract", "multiply", "divide"]).describe("The arithmetic operation to perform"),
a: v3_1.z.number().describe("First number"),
b: v3_1.z.number().describe("Second number"),
}),
execute: async ({ operation, a, b }) => {
console.log(`[CALCULATOR TOOL] š§ Executing ${operation}(${a}, ${b})`);
switch (operation) {
case "add":
return { result: a + b };
case "subtract":
return { result: a - b };
case "multiply":
return { result: a * b };
case "divide":
if (b === 0) {
return { result: "Cannot divide by zero", error: true };
}
return { result: a / b };
default:
return { result: "Invalid operation", error: true };
}
},
});
// Define weather tool
const getWeather = (0, ai_1.tool)({
description: "Get current weather information for a city",
inputSchema: v3_1.z.object({
city: v3_1.z.string().describe("Name of the city"),
unit: v3_1.z.enum(["celsius", "fahrenheit"]).describe("Temperature unit"),
}),
execute: async ({ city, unit }) => {
console.log(`[WEATHER TOOL] š§ Getting weather for ${city} in ${unit}`);
// Mock weather data
const mockWeather = {
city,
temperature: unit === "celsius" ? 22 : 72,
unit,
condition: "sunny",
humidity: 65,
};
return mockWeather;
},
});
async function main() {
var _a;
try {
const { maxim, logger } = await initializeMaxim();
const openAIKey = process.env["OPENAI_API_KEY"];
if (!openAIKey) {
throw new Error("OPENAI_API_KEY is not defined in the environment variables");
}
// Wrap the model with Maxim logging
const model = (0, vercel_1.wrapMaximAISDKModel)(openai_1.openai.chat("gpt-4o-mini"), logger);
// Test 1: Calculator tool call - Trace 1
console.log("\nš§® Test 1: Calculator Tool Call");
console.log("Prompt: Calculate 15 multiplied by 8, then add 25 to the result.\n");
const session = logger.session({
id: (0, uuid_1.v4)(),
name: "Non-Streaming Tool Call Test",
tags: {
test_type: "non_streaming_tool_calls",
model: "gpt-4o-mini",
},
});
// const trace1 = session.trace({
// id: uuid(),
// name: "Calculator Tool Call Test",
// tags: {
// test_type: "non_streaming_tool_calls",
// model: "gpt-4o-mini",
// },
// });
const result1 = await (0, ai_1.generateText)({
model: model,
tools: {
calculator,
},
prompt: "Calculate 15 multiplied by 8",
providerOptions: {
maxim: {
sessionId: session.id,
traceName: "Calculator Tool Call Test",
generationName: "Calculator Operations",
generationTags: {
tool_used: "calculator",
test_number: "1",
},
},
},
stopWhen: (0, ai_1.stepCountIs)(5),
});
console.log("ā
Result:", result1.text);
console.log("š Tool Calls:", ((_a = result1.toolCalls) === null || _a === void 0 ? void 0 : _a.length) || 0, "tool call(s)");
if (result1.toolCalls && result1.toolCalls.length > 0) {
result1.toolCalls.forEach((tc, idx) => {
console.log(` Tool Call ${idx + 1}:`, JSON.stringify(tc, null, 2));
});
}
console.log("\n");
// Wait a bit between calls
await new Promise((resolve) => setTimeout(resolve, 2000));
// // Test 2: Weather tool call - Trace 2
// console.log("š¤ļø Test 2: Weather Tool Call");
// console.log("Prompt: What's the weather like in San Francisco?\n");
//
// const trace2 = session.trace({
// id: uuid(),
// name: "Weather Tool Call Test",
// tags: {
// test_type: "non_streaming_tool_calls",
// model: "gpt-4o-mini",
// },
// });
//
// const result2 = await generateText({
// model: model,
// tools: {
// getWeather,
// },
// prompt: "What's the weather like in San Francisco? Use Celsius.",
// providerOptions: {
// maxim: {
// traceId: trace2.id,
// traceName: "Weather Tool Call Test",
// generationName: "Weather Query",
// generationTags: {
// tool_used: "getWeather",
// test_number: "2",
// },
// } as MaximVercelProviderMetadata,
// },
// stopWhen: stepCountIs(5),
// });
//
// trace2.end();
//
// console.log("ā
Result:", result2.text);
// console.log("š Tool Calls:", result2.toolCalls?.length || 0, "tool call(s)");
// if (result2.toolCalls && result2.toolCalls.length > 0) {
// result2.toolCalls.forEach((tc, idx) => {
// console.log(` Tool Call ${idx + 1}:`, JSON.stringify(tc, null, 2));
// });
// }
// console.log("\n");
//
// // Test 3: Multiple tool calls - Trace 3
// console.log("š¢ Test 3: Multiple Tool Calls");
// console.log("Prompt: What's 10 + 5? Also, what's the weather in New York?\n");
//
// const trace3 = session.trace({
// id: uuid(),
// name: "Multiple Tool Calls Test",
// tags: {
// test_type: "non_streaming_tool_calls",
// model: "gpt-4o-mini",
// },
// });
//
// const result3 = await generateText({
// model: model,
// tools: {
// calculator,
// getWeather,
// },
// prompt: "What's 10 + 5? Also, what's the weather in New York?",
// providerOptions: {
// maxim: {
// traceId: trace3.id,
// traceName: "Multiple Tool Calls Test",
// generationName: "Multiple Operations",
// generationTags: {
// tool_used: "calculator,getWeather",
// test_number: "3",
// },
// } as MaximVercelProviderMetadata,
// },
// stopWhen: stepCountIs(5),
// });
//
// trace3.end();
//
// console.log("ā
Result:", result3.text);
// console.log("š Tool Calls:", result3.toolCalls?.length || 0, "tool call(s)");
// if (result3.toolCalls && result3.toolCalls.length > 0) {
// result3.toolCalls.forEach((tc, idx) => {
// console.log(` Tool Call ${idx + 1}:`, JSON.stringify(tc, null, 2));
// });
// }
// console.log("\n");
console.log("⨠All tests completed!");
console.log("š Each test created its own trace automatically.\n");
session.end();
// Cleanup
await maxim.cleanup();
}
catch (error) {
console.error("ā Error:", error);
process.exit(1);
}
}
// Run the test
if (require.main === module) {
main();
}
//# sourceMappingURL=non_streaming_tool_call_test.js.map