#!/usr/bin/env node /** * SAP AI Provider - Simple Chat Completion Example * * This example demonstrates basic chat completion using the SAP AI Provider * powered by `@sap-ai-sdk/orchestration` and `@sap-ai-sdk/foundation-models`. * * 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"; // This example uses relative imports for local development within this repo. // In YOUR production project, use the published package instead: // import { createSAPAIProvider } from "@jerome-benoit/sap-ai-provider"; import { createSAPAIProvider } from "../src/index"; /** * */ async function simpleTest() { console.log("๐Ÿงช Simple SAP AI Chat Completion Example\n"); try { // 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"); } console.log("๐Ÿ”„ Creating SAP AI provider..."); // Create provider - authentication is handled automatically by SAP AI SDK const provider = createSAPAIProvider({ resourceGroup: "default", // Optional: specify resource group }); console.log("๐Ÿ“ Testing text generation with gpt-4.1..."); const model = provider("gpt-4.1", { modelParams: { maxTokens: 1000, temperature: 0.7, }, }); const result = await model.doGenerate({ prompt: [ { content: [{ text: "How to cook a delicious chicken recipe?", type: "text" }], role: "user", }, ], }); // Extract text from content array const text = result.content .filter((item) => item.type === "text") .map((item) => item.text) .join(""); console.log("โœ… Success!"); console.log("๐Ÿ“„ Generated text:", text); console.log( "๐Ÿ“Š Usage:", `${String(result.usage.inputTokens.total)} prompt + ${String(result.usage.outputTokens.total)} completion tokens`, ); console.log("๐Ÿ Finish reason:", result.finishReason); console.log(""); } catch (error: unknown) { if (error instanceof LoadAPIKeyError) { // 401/403: Authentication or permission issue console.error("โŒ Authentication Error:", error.message); } else if (error instanceof NoSuchModelError) { // 404: Model or deployment not found 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; 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); } } else { const errorMessage = error instanceof Error ? error.message : String(error); console.error("โŒ Test 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 the model is available in your deployment"); } } simpleTest().catch(console.error); export { simpleTest };