#!/usr/bin/env node /** * SAP AI Provider - Document Grounding (RAG) Example * * This example demonstrates document grounding (Retrieval-Augmented Generation) * using the SAP AI Core Orchestration API's document grounding module. * * Document grounding allows you to ground LLM responses in your own documents * stored in a vector database, ensuring answers are based on your specific * knowledge base rather than the model's general training data. * * Prerequisites: * - A configured vector database in SAP AI Core (e.g., HANA Cloud Vector Engine) * - Documents indexed in the vector database * - Vector store ID (data repository ID) * * Authentication: * - On SAP BTP: Automatically uses service binding (VCAP_SERVICES) * - Locally: Set AICORE_SERVICE_KEY environment variable with your service key JSON */ // Load environment variables import "dotenv/config"; import { APICallError, LoadAPIKeyError, NoSuchModelError } from "@ai-sdk/provider"; import { generateText } from "ai"; // This example uses relative imports for local development within this repo. // In YOUR production project, use the published package instead: // import { createSAPAIProvider, buildDocumentGroundingConfig } from "@jerome-benoit/sap-ai-provider"; import { buildDocumentGroundingConfig, createSAPAIProvider } from "../src/index"; /** * */ async function documentGroundingExample() { console.log("šŸ“š SAP AI Document Grounding (RAG) Example\n"); // Verify AICORE_SERVICE_KEY is set for local development if (!process.env.AICORE_SERVICE_KEY && !process.env.VCAP_SERVICES) { console.warn("āš ļø Warning: AICORE_SERVICE_KEY environment variable not set."); console.warn(" Set it in your .env file or environment for local development.\n"); } // Check for vector store configuration const VECTOR_STORE_ID = process.env.VECTOR_STORE_ID ?? "vector-store-1"; console.log("šŸ“‹ Configuration:"); console.log(` Vector Store ID: ${VECTOR_STORE_ID}`); console.log(""); try { // Example 1: Basic document grounding configuration console.log("================================"); console.log("šŸ“– Example 1: Basic Document Grounding"); console.log("================================\n"); const basicGroundingConfig = buildDocumentGroundingConfig({ filters: [ { // Search across all repositories data_repositories: ["*"], id: VECTOR_STORE_ID, }, ], // Required: Define placeholders for input question and output placeholders: { input: ["?question"], output: "groundingOutput", }, }); const provider = createSAPAIProvider({ defaultSettings: { grounding: basicGroundingConfig, }, }); const model = provider("gpt-4.1"); console.log("šŸ“ Query: What are the key features of SAP AI Core?\n"); const { text } = await generateText({ messages: [ { content: "What are the key features of SAP AI Core?", role: "user", }, ], model, }); console.log("šŸ¤– Grounded Response:", text); console.log("\nšŸ“Œ Note: This response is grounded in your vector database documents."); // Example 2: Advanced grounding with metadata console.log("\n================================"); console.log("šŸ“Š Example 2: Grounding with Metadata"); console.log("================================\n"); const advancedGroundingConfig = buildDocumentGroundingConfig({ filters: [ { data_repositories: ["*"], id: VECTOR_STORE_ID, }, ], // Request metadata about the retrieved chunks metadata_params: ["file_name", "document_id", "chunk_id"], placeholders: { input: ["?question"], output: "groundingOutput", }, }); const providerAdvanced = createSAPAIProvider({ defaultSettings: { grounding: advancedGroundingConfig, }, }); const modelAdvanced = providerAdvanced("gpt-4.1"); console.log("šŸ“ Query: How do I deploy a model in SAP AI Core? Include sources.\n"); const { text: advancedText } = await generateText({ messages: [ { content: "How do I deploy a model in SAP AI Core? Please cite your sources with file names.", role: "user", }, ], model: modelAdvanced, }); console.log("šŸ¤– Grounded Response with Metadata:", advancedText); // Example 3: Comparison with and without grounding console.log("\n================================"); console.log("šŸ” Example 3: Grounded vs Ungrounded Comparison"); console.log("================================\n"); const providerNoGrounding = createSAPAIProvider(); const modelNoGrounding = providerNoGrounding("gpt-4.1"); const query = "What is the latest pricing for our enterprise plan?"; console.log(`šŸ“ Query: ${query}\n`); console.log("🌐 Response WITHOUT grounding (general knowledge):"); const { text: ungroundedText } = await generateText({ messages: [ { content: query, role: "user", }, ], model: modelNoGrounding, }); console.log(ungroundedText); console.log("\nšŸ“š Response WITH grounding (your documents):"); const { text: groundedText } = await generateText({ messages: [ { content: query, role: "user", }, ], model, }); console.log(groundedText); console.log("\nāœ… Document grounding example completed!"); console.log("\nšŸ’” Next Steps:"); console.log(" - Index your documents in SAP HANA Cloud Vector Engine"); console.log(" - Set VECTOR_STORE_ID environment variable"); console.log(" - Use document_metadata filters to restrict search to specific documents"); console.log(" - Use metadata_params to retrieve source information for citations"); } catch (error: unknown) { if (error instanceof LoadAPIKeyError) { console.error("āŒ Authentication Error:", error.message); } else if (error instanceof NoSuchModelError) { console.error("āŒ Model Not Found:", error.modelId); } else if (error instanceof APICallError) { console.error("āŒ API Call Error:", error.statusCode, error.message); // Parse SAP-specific metadata const sapError = JSON.parse(error.responseBody ?? "{}") as { error?: { code?: string; message?: string; request_id?: string }; }; if (sapError.error?.request_id) { console.error(" SAP Request ID:", sapError.error.request_id); console.error(" SAP Error Code:", sapError.error.code); console.error(" SAP Error Message:", sapError.error.message); } // Common errors if (error.statusCode === 400) { console.error("\nšŸ’” Vector store not found or not configured correctly."); console.error(" Make sure your vector database is set up in SAP AI Core."); } } else { const errorMessage = error instanceof Error ? error.message : String(error); console.error("āŒ Example failed:", errorMessage); } console.error("\nšŸ’” Troubleshooting tips:"); console.error(" - Ensure AICORE_SERVICE_KEY is set with valid credentials"); console.error(" - Check that your SAP AI Core instance is accessible"); console.error(" - Verify your vector database is configured and populated"); console.error(" - Ensure VECTOR_STORE_ID matches your actual vector store"); console.error(" - Check that documents are indexed in the vector database"); } } documentGroundingExample().catch(console.error); export { documentGroundingExample };