2025 Mar 11 7:20 PM - edited 2025 Mar 17 9:31 PM
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?
Request clarification before answering.
Hi,
The AI SDK automatically reads the AI Core service credentials from the VCAP_SERVICES variable in Cloud Foundry. It also takes care of fetching the necessary access tokens. If you want to test your app locally, you can create a .env file at the root level in your project and add the credentials as shown here. (Note: This is however not recommended for productive use cases).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @deekshasinha
Thank you for your response.
How is it going to be customized for this Standalone SDK Configuration (For testing AI Hub APIs) script to make use of the available SDK API calls?
Now I'm able to generate the access token successfully using this Node.js setup. However, I'm facing challenges while trying to retrieve the list of Scenarios from the AI Core - even though the Scenarios are already created and available under the given resourceGroup.
async function getScenarios(resourceGroup) {
try {
const { ScenarioApi } = await import('@sap-ai-sdk/ai-api');
const token = await getAccessToken();
const result = await ScenarioApi.scenarioQuery(
{ 'AI-Resource-Group': resourceGroup }, // Query parameters
{ headers: { 'Authorization': `Bearer ${token}`} }); // Headers
console.log("Scenarios Retrieved:", result);
return result;
} catch (error) {
console.error('Error in getScenarios:', error);
throw error; // Re-throw the error to be handled by the caller
}
}
Response:
Scenarios Retrieved: OpenApiRequestBuilder {
method: 'get',
pathPattern: '/lm/scenarios',
parameters: { headerParameters: { 'AI-Resource-Group': 'service-res-group-1' } },
basePath: undefined,
_fetchCsrfToken: true,
customHeaders: {},
customRequestConfiguration: {},
_middlewares: []
}
Scenario Value: undefined
Most of the available examples and references I came across are based on Python SDK usage. If there are any best practices or official sample repositories showcasing such an implementation (preferably in BAS / CAP JS stack), kindly share.
Best Regards.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
88 | |
10 | |
9 | |
8 | |
7 | |
6 | |
6 | |
5 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.