UNPKG

mongodb-rag-core

Version:

Common elements used by MongoDB Chatbot Framework components.

69 lines (67 loc) 2.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.makeGenerateHighLevelDbDescriptions = void 0; const zod_1 = require("zod"); const getOpenAiFunctionResponse_1 = require("./getOpenAiFunctionResponse"); const prettyPrintMongoDbDocument_1 = require("../prettyPrintMongoDbDocument"); const braintrust_1 = require("braintrust"); const systemPrompt = `You are an expert MongoDB database architect. Your task is to analyze the provided database metadata and generate clear, concise descriptions. Database Metadata: - Database contains database name and information about the collections in the database - Each collection includes: - Schema information - Truncated sample documents - Index definitions Instructions: 1. Analyze the schema, sample documents, and indexes for each collection 2. Generate a high-level description of the entire database's purpose 3. For each collection, provide a clear description of: - What kind of data it stores - Its role in the overall database - Key relationships with other collections 4. Order collections logically, starting with the most fundamental ones that others depend on.`; const functionName = "get_high_level_db_descriptions"; /** Creates a Zod schema for database descriptions based on the actual collections in the database */ function createHighLevelDbDescriptionsSchema(databaseMetadata) { const collectionNames = databaseMetadata.collections.map((metadata) => metadata.collectionName); // Create a record schema where each key must be one of the collection names const collectionDescriptionsSchema = zod_1.z.array(zod_1.z.object({ collectionName: zod_1.z.enum(collectionNames), description: zod_1.z.string(), })); return zod_1.z.object({ databaseDescription: zod_1.z.string(), collectionDescriptions: collectionDescriptionsSchema, }); } /** Get high-level descriptions of the database and its collections. */ const makeGenerateHighLevelDbDescriptions = (openAiClient) => (0, braintrust_1.wrapTraced)(async function (databaseMetadata, llmOptions) { const schema = createHighLevelDbDescriptionsSchema(databaseMetadata); const messages = [ { role: "system", content: systemPrompt, }, { role: "user", content: `Database information: ${(0, prettyPrintMongoDbDocument_1.prettyPrintMongoDbDocument)(databaseMetadata)}`, }, ]; return await (0, getOpenAiFunctionResponse_1.getOpenAiFunctionResponse)({ messages, llmOptions, schema, functionName, functionDescription: "Generate high-level descriptions of the database and its collections based on the provided metadata", openAiClient, }); }, { name: "generateHighLevelDbDescriptions", }); exports.makeGenerateHighLevelDbDescriptions = makeGenerateHighLevelDbDescriptions; //# sourceMappingURL=generateHighLevelDbDescriptions.js.map