SAP AI SDK for Generative AI

6.7.0 · active · verified Thu Apr 16

The SAP Cloud SDK for AI (Python) is designed to simplify interactions with generative AI APIs and services, particularly within the SAP ecosystem. It provides unified interfaces for various large language models (LLMs), enabling developers to easily integrate AI capabilities into their applications. The library is currently at version 6.7.0 and maintains a regular release cadence with updates and new features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `GenerativeAI` client and use its `Completion` service. It relies on environment variables for API key configuration (e.g., `OPENAI_API_KEY`) and attempts to generate text using a specified model.

import os
from sap_ai_sdk.generative_ai import GenerativeAI
from sap_ai_sdk.generative_ai.completion import Completion

# Set your API key as an environment variable (e.g., OPENAI_API_KEY).
# For SAP AI Core, ensure AI_API_URL, AI_CLIENT_ID, AI_CLIENT_SECRET, AI_AUTH_URL are set.
# Example: os.environ['OPENAI_API_KEY'] = os.environ.get('OPENAI_API_KEY', '') # For demo, ensure it's loaded.

try:
    # Initialize the GenerativeAI client. It automatically detects configured providers.
    ai_client = GenerativeAI()

    # Get the completion service
    completion_service: Completion = ai_client.get_completion_service()

    prompt = "Explain the concept of quantum entanglement in simple terms."
    model_name = "gpt-3.5-turbo" # Use a model supported by your configured provider

    print(f"Requesting completion for model: {model_name}")
    response = completion_service.create(
        prompt=prompt,
        model_name=model_name,
        # provider="openai" # Optionally specify if multiple providers are configured
    )

    print("\nGenerated Text:")
    print(response.text)

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure your environment variables (e.g., OPENAI_API_KEY) are correctly set")
    print("and that a suitable provider is configured for the chosen model.")

view raw JSON →