cancel
Showing results for 
Search instead for 
Did you mean: 

Invoke Generative AI Hub Models using @sap-ai-sdk/ai-api library in BAS

MKM
Active Participant
0 Kudos
330

Hello Experts

I am working with the @SAP-ai-sdk/ai-api library to connect to a configured Generative AI Hub and query the available models. However, I'm having trouble identifying the correct API or function within the SDK to use in order to retrieve the results.

 

 

 

 

 

async function main() {
    try {

        const { GenerativeAIHubClient } = await import('@sap-ai-sdk/ai-api'); // Dynamic import
        const client = new GenerativeAIHubClient({
            uaa: {
                url: process.env.GENAI_HUB_TOKEN_URL, // UAA URL from service key
                clientId: process.env.GENAI_HUB_CLIENT_ID, // Client ID from service key
                clientSecret: process.env.GENAI_HUB_CLIENT_SECRET, // Client secret from service key
            },
            baseUrl: process.env.GENAI_HUB_URL, // Base URL from service key or env variable.
        });

        // List Models
        const models = await client.listModels();
        console.log("Available Models:", models);

        // Invoke Model
        const invokeResponse = await GenerativeAIHubClient.invokeModel({
            modelId: 'sap/generative-text', // Model ID
            prompt: "Explain SAP BTP in simple terms."
        });
        console.log("AI Response:", invokeResponse.output);

    } catch (error) {
        console.error("Error:", error);
        if (error.response) {
            console.error("Response Data:", error.response.data);
        }
    }
}

main();

 

 

 

 

Getting this error as this api is not available in the sdk export list.

 

 

 

 

 

ERROR    TypeError: GenerativeAIHubClient is not a constructor

 

 

 

 

Could someone please guide me on the proper method/function/approach (Not using Python) to invoke and get the desired output from the Generative AI Hub?

@TomFrenken @MirelaB @ZhongpinWang @deekshasinha 

View Entire Topic
Vitaliy-R
Developer Advocate
Developer Advocate
0 Kudos

I would need to check how it is done in JavaScript SDK, because in Python SDK it is enough to set the environment variables or a config file `~/.aicore/config.json` as per https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/README_sphynx.html#configurati....

Then the code automatically initiates a client with credentials from those configurations, without an explicit read from env variables or from that config file in your code.

But that's how it is done in Python, and I would need to check how it is done in JS.