@maximai/maxim-js
Version:
Maxim AI JS SDK. Visit https://getmaxim.ai for more info.
157 lines • 5.77 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 });
const uuid_1 = require("uuid");
const ai_1 = require("ai");
const google_1 = require("@ai-sdk/google");
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 };
}
// Generate text response
async function generateInitialText(model, prompt, trace, spanId) {
const { text: rawOutput } = await (0, ai_1.generateText)({
model,
prompt,
providerOptions: {
maxim: {
traceName: "City Prediction",
traceId: trace.id,
spanId,
},
},
});
return rawOutput;
}
async function streamInitialText(model, prompt, trace, spanId) {
const response = (0, ai_1.streamText)({
model,
prompt,
providerOptions: {
maxim: {
traceName: "City Prediction",
traceId: trace.id,
spanId,
},
},
});
const result = await response.text;
return result;
}
// Define the schema for our city data
const CityPredictionSchema = v3_1.z.object({
name: v3_1.z.string().describe("the name of the city"),
country: v3_1.z.string().describe("the name of the country"),
reason: v3_1.z.string().describe("the reason why the city will be one of the largest cities by 2050"),
estimatedPopulation: v3_1.z.number(),
});
// Extract structured data
async function extractStructuredData(model, rawOutput, trace, spanId) {
const result = await (0, ai_1.generateText)({
model,
prompt: "Extract the desired information from this text: \n" + rawOutput,
output: ai_1.Output.object({
schema: CityPredictionSchema,
}),
providerOptions: {
maxim: {
traceId: trace.id,
spanId,
},
},
});
return result.output;
}
// Format the final output
async function formatOutput(model, object, trace) {
const { text: output } = await (0, ai_1.generateText)({
model,
prompt: `Format this into a human-readable format: ${JSON.stringify(object)}`,
providerOptions: {
maxim: {
traceId: trace.id,
},
},
});
return output;
}
// Main function
async function main() {
const { logger } = await initializeMaxim();
try {
const model = (0, vercel_1.wrapMaximAISDKModel)((0, google_1.google)("gemini-2.5-flash"), logger);
const spanId = (0, uuid_1.v4)();
const trace = logger.sessionTrace("12fde580-df42-4cca-9d38-85808eb56579", { id: (0, uuid_1.v4)(), name: "City Prediction Demo" });
const prompt = "Predict the top 3 largest city by 2050. For each, return the name, the country, the reason why it will be on the list, and the estimated population in millions.";
trace.input(prompt);
// Step 1: Generate initial text response
const rawOutput = await streamInitialText(model, prompt, trace, spanId);
// Step 2: Extract structured data
const structuredData = await extractStructuredData(model, rawOutput, trace, spanId);
// Step 3: Format the final output
const formattedOutput = await formatOutput(model, structuredData, trace);
trace.end();
logger.sessionEnd("12fde580-df42-4cca-9d38-85808eb56579");
console.log("Final formatted response:", formattedOutput);
}
catch (error) {
console.error("Error in city prediction demo:", error);
}
finally {
await logger.cleanup();
}
}
// Run the main function
main();
//# sourceMappingURL=agent_testing.js.map