How to use Cloud SDK for AI in an existing CAP NodeJS Project in CommonJS

Estimated read time 3 min read

I have an existing SAP Cloud Application Program Project in CommonJs. I want to add custom AI function in it . So need to use SAP Cloud SDK for AI in my project. But SAP Cloud SDK for AI only support native ESM.  Convert my existing project into ESM? I think it is too risky.  I need to use the ESM module in the CommonJs project.  At last, it works. Let me share the steps which maybe be helpful for you.

Steps:

1,  Install esbuild in the root path of my project:

npm i esbuild

2.  Create  folder named esm under  srv, then create a file named index.js under folder esm with the following code .

import { OrchestrationClient, buildAzureContentSafetyFilter } from ‘@sap-ai-sdk/orchestration’;
import { AzureOpenAiEmbeddingClient } from ‘@sap-ai-sdk/langchain’

export { OrchestrationClient, buildAzureContentSafetyFilter, AzureOpenAiEmbeddingClient };

2.  Add following script in my package.json file in project root path.

“deps”: “esbuild –platform=node srv/esm/index.js –bundle –outfile=srv/esm/bundle.js”

3.  Run the following command in the project root path in terminal.

npm run deps

4.  Adjust the code of importing the module of SAP Cloud SDK for AI as the following.

// const { OrchestrationClient, buildAzureContentSafetyFilter } = require(‘@sap-ai-sdk/orchestration’);
const { OrchestrationClient, buildAzureContentSafetyFilter } = require(‘../esm/bundle’);
// const { AzureOpenAiEmbeddingClient } = require(‘@sap-ai-sdk/langchain’);
const { AzureOpenAiEmbeddingClient } = require(‘../esm/bundle’);

 

 

The ends!

Thanks for your time!

Jacky Liu

   

 

​ I have an existing SAP Cloud Application Program Project in CommonJs. I want to add custom AI function in it . So need to use SAP Cloud SDK for AI in my project. But SAP Cloud SDK for AI only support native ESM.  Convert my existing project into ESM? I think it is too risky.  I need to use the ESM module in the CommonJs project.  At last, it works. Let me share the steps which maybe be helpful for you.Steps:1,  Install esbuild in the root path of my project:npm i esbuild2.  Create  folder named esm under  srv, then create a file named index.js under folder esm with the following code .import { OrchestrationClient, buildAzureContentSafetyFilter } from ‘@sap-ai-sdk/orchestration’;
import { AzureOpenAiEmbeddingClient } from ‘@sap-ai-sdk/langchain’

export { OrchestrationClient, buildAzureContentSafetyFilter, AzureOpenAiEmbeddingClient };2.  Add following script in my package.json file in project root path. “deps”: “esbuild –platform=node srv/esm/index.js –bundle –outfile=srv/esm/bundle.js”3.  Run the following command in the project root path in terminal.npm run deps4.  Adjust the code of importing the module of SAP Cloud SDK for AI as the following.// const { OrchestrationClient, buildAzureContentSafetyFilter } = require(‘@sap-ai-sdk/orchestration’);
const { OrchestrationClient, buildAzureContentSafetyFilter } = require(‘../esm/bundle’);
// const { AzureOpenAiEmbeddingClient } = require(‘@sap-ai-sdk/langchain’);
const { AzureOpenAiEmbeddingClient } = require(‘../esm/bundle’);  The ends!Thanks for your time!Jacky Liu      Read More Technology Blog Posts by SAP articles 

#SAP

#SAPTechnologyblog

You May Also Like

More From Author