### Install SDK with Support for All Models Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/README_sphynx.html Install the SDK with support for all models (OpenAI, Amazon, Google) and LangChain. This is the recommended installation for full functionality. ```bash pip install "sap-ai-sdk-gen[all]" Copy code ``` -------------------------------- ### Configuration File Example (X.509 Certificate String) Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/README_sphynx.html Example JSON structure for a configuration file using X.509 certificate strings for authentication. ```json { "AICORE_AUTH_URL": "https://* * * .authentication.cert.sap.hana.ondemand.com", "AICORE_CLIENT_ID": "* * * ", "AICORE_CERT_STR": "* * *", "AICORE_KEY_STR": "* * *", "AICORE_RESOURCE_GROUP": "* * * ", "AICORE_BASE_URL": "https://api.ai.* * *.cfapps.sap.hana.ondemand.com/v2" } ``` -------------------------------- ### setup Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.evaluations.html One-time setup function to create object store secrets and orchestration deployment URLs if they are not already provided. ```APIDOC ## setup ### Description One time setup function which does object store secrets creation and orchestration deployment url creation if not provided. ### Parameters * **input_secret_body** (dict | None) - Optional dictionary for input secret configuration. * **default_secret_body** (dict | None) - Optional dictionary for default secret configuration. * **replace_existing** (bool) - Flag to replace existing configurations, defaults to False. ``` -------------------------------- ### Install Default SDK (OpenAI Models) Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/README_sphynx.html Install the default version of the SDK, which includes support for OpenAI models and LangChain. This is the minimal installation. ```bash pip install sap-ai-sdk-gen Copy code ``` -------------------------------- ### Configuration File Example (Client Secret) Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/README_sphynx.html Example JSON structure for a configuration file using client ID and secret for authentication. ```json { "AICORE_AUTH_URL": "https://* * * .authentication.sap.hana.ondemand.com/oauth/token", "AICORE_CLIENT_ID": "* * * ", "AICORE_CLIENT_SECRET": "* * * ", "AICORE_RESOURCE_GROUP": "* * * ", "AICORE_BASE_URL": "https://api.ai.* * *.cfapps.sap.hana.ondemand.com/v2" } ``` -------------------------------- ### Configuration File Example (X.509 Certificate File Path) Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/README_sphynx.html Example JSON structure for a configuration file using X.509 certificate file paths for authentication. ```json { "AICORE_AUTH_URL": "https://* * * .authentication.cert.sap.hana.ondemand.com", "AICORE_CLIENT_ID": "* * * ", "AICORE_CERT_FILE_PATH": "* * */cert.pem", "AICORE_KEY_FILE_PATH": "* * */key.pem", "AICORE_RESOURCE_GROUP": "* * * ", "AICORE_BASE_URL": "https://api.ai.* * *.cfapps.sap.hana.ondemand.com/v2" } ``` -------------------------------- ### Install SDK with Subset of Extra Libraries Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/README_sphynx.html Install a subset of extra libraries, such as Google and Amazon model support, along with LangChain. Specify the desired libraries in square brackets. ```bash pip install "sap-ai-sdk-gen[google, amazon]" Copy code ``` -------------------------------- ### Async Google GenAI Native Example Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/async-examples.html Shows how to use the Google GenAI native client asynchronously to generate content. This example uses the `gemini-2.5-flash` model. ```python from gen_ai_hub.proxy.native.google_genai import Client from gen_ai_hub.proxy import get_proxy_client proxy_client = get_proxy_client('gen-ai-hub') async with Client(proxy_client=proxy_client,).aio as aclient: response = await aclient.models.generate_content( model="gemini-2.5-flash", contents="Explain the relativity theory in simple terms." ) response ``` -------------------------------- ### Async Google GenAI Chat Example Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/async-examples.html Illustrates an asynchronous chat interaction with Google GenAI using the `gemini-2.5-flash` model. This example demonstrates creating a chat session and sending multiple messages. ```python from gen_ai_hub.proxy.native.google_genai import Client from gen_ai_hub.proxy import get_proxy_client proxy_client = get_proxy_client('gen-ai-hub') async with Client(proxy_client=proxy_client).aio as aclient: chat_session = aclient.chats.create( model="gemini-2.5-flash" ) model_response = await chat_session.send_message("Hello.") print("Response 1:", model_response.text) model_response = await chat_session.send_message( "What is your opinion about latest Gemini model?" ) print("Response 2:", model_response.text) ``` -------------------------------- ### Vertex AI with API Key Authentication Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.proxy.langchain.html Demonstrates how to use Vertex AI with API key authentication for simpler setup. This method is useful when you want to leverage Vertex AI without relying on service account JSON files. ```bash export GEMINI_API_KEY='your-api-key' export GOOGLE_GENAI_USE_VERTEXAI=true export GOOGLE_CLOUD_PROJECT='your-project-id' ``` -------------------------------- ### OpenAI Completions Integration Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/gen_ai_hub.html Example usage of the Completions API, equivalent to openai.Completions. Suitable for models supporting the legacy completion endpoint. ```python from gen_ai_hub.proxy.native.openai import completions response = completions.create( model_name="gpt-4o-mini", prompt="The Answer to the Ultimate Question of Life, the Universe, and Everything is", max_tokens=20, temperature=0 ) print(response) ``` -------------------------------- ### Get Help on EvaluationConfig Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/evaluations.html Use the `help()` function to inspect the docstring of `EvaluationConfig` for detailed parameter information. ```python from gen_ai_hub.evaluations import EvaluationConfig print(help(EvaluationConfig)) ``` -------------------------------- ### Initialize and use Langchain ChatOpenAI with proxy Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/gen_ai_hub.html Initializes a Langchain ChatOpenAI model using the Generative AI Hub proxy client. This example demonstrates setting up a chat prompt with system, human, and AI messages, and invoking the chain. ```python from langchain.prompts.chat import ( AIMessagePromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, ) from gen_ai_hub.proxy.langchain import ChatOpenAI from gen_ai_hub.proxy import get_proxy_client proxy_client = get_proxy_client('gen-ai-hub') chat_llm = ChatOpenAI(proxy_model_name='gpt-4o-mini', proxy_client=proxy_client) template = 'You are a helpful assistant that translates english to pirate.' system_message_prompt = SystemMessagePromptTemplate.from_template(template) example_human = HumanMessagePromptTemplate.from_template('Hi') example_ai = AIMessagePromptTemplate.from_template('Ahoy!') human_template = '{text}' human_message_prompt = HumanMessagePromptTemplate.from_template(human_template) chat_prompt = ChatPromptTemplate.from_messages( [system_message_prompt, example_human, example_ai, human_message_prompt]) chain = chat_prompt | chat_llm response = chain.invoke({'text': 'I love planking.'}) print(response.content) ``` -------------------------------- ### Configure Evaluation with TemplateRef by Scenario, Name, and Version Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.evaluations.html Example of setting up an EvaluationConfig using a TemplateRef identified by scenario, name, and version. This allows for more specific template referencing. ```python from gen_ai_hub.orchestration_v2.models.template_ref import TemplateRefByScenarioNameVersion config = EvaluationConfig( dataset_config=Dataset("data/test.jsonl"), metrics=[MetricConfig(name="accuracy")], llm=LLM(name="gpt-4", version="latest", params={"temperature": 0.7}), template=TemplateRef(template_ref=TemplateRefByScenarioNameVersion( scenario="foundation-models", name="prompt1", version="1.0" )), test_row_count=100 ) ``` -------------------------------- ### Initiate Evaluation Runs Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/evaluations.html Starts one or more evaluation runs based on the provided configuration list. The result is a list of evaluation run objects. ```python evaluation_runs = client.evaluate(evaluation_config_list) ``` -------------------------------- ### Create Orchestration Configuration Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/orchestration-service.html Defines the configuration for an orchestration setup by integrating a template and a language model. Specifies how these components interact. ```python from gen_ai_hub.orchestration.models.config import OrchestrationConfig config = OrchestrationConfig( template=template, llm=llm, ) ``` -------------------------------- ### Stream Langchain Response with Amazon Nova-Premier Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/streaming.html Demonstrates streaming a response from the 'amazon--nova-premier' model. This example showcases integration with Amazon's LLM offerings for streaming capabilities. ```python stream_langchain("How do airplanes stay in the air?", model_name='amazon--nova-premier') ``` -------------------------------- ### Create EvaluationConfig with TemplateRef by Scenario, Name, and Version Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.evaluations.models.html Example of creating an EvaluationConfig using TemplateRef with scenario, name, and version. This configuration includes LLM parameters like temperature. ```python >>> from gen_ai_hub.orchestration_v2.models.template_ref import TemplateRefByScenarioNameVersion >>> config = EvaluationConfig( ... dataset_config=Dataset("data/test.jsonl"), ... metrics=[MetricConfig(name="accuracy")], ... llm=LLM(name="gpt-4", version="latest", params={"temperature": 0.7}), ... template=TemplateRef(template_ref=TemplateRefByScenarioNameVersion( ... scenario="foundation-models", name="prompt1", version="1.0" ... )), ... test_row_count=100 ... ) ``` -------------------------------- ### Async Amazon Titan Embedding Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/async-examples.html Demonstrates how to asynchronously invoke the Amazon Titan embedding model to get text embeddings. Ensure you have the necessary session and client setup. ```python async def async_amazon_titan_embedding(model_name): session = AsyncSession() bedrock = await session.async_client(model_name=model_name) body = json.dumps( { "inputText": "Please recommend books with a theme similar to the movie 'Inception'.", } ) response = await bedrock.invoke_model(body=body) response_body = json.loads(await response.get("body").read()) print("Response Metadata:", response["ResponseMetadata"]) print("Embedding:", response_body["embedding"]) await bedrock.close() ``` ```python # Replace with the desired model name await async_amazon_titan_embedding("amazon--titan-embed-text") ``` -------------------------------- ### Basic Usage: Generate Single Embedding Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/orchestration-service2.html Generates a vector embedding for a single text string using minimal configuration. This is the simplest way to get started with text embeddings. ```APIDOC ## Basic Usage: Generate Single Embedding ### Description Generate an embedding for a single text string with minimal configuration. ### Method `service.embed` ### Parameters #### Request Body - **config** (EmbeddingsOrchestrationConfig) - Required - Configuration for the embeddings module. - **input** (EmbeddingsInput) - Required - The input text to embed. - **text** (string) - Required - The text string to generate an embedding for. ### Request Example ```python from gen_ai_hub.orchestration_v2 import ( OrchestrationService, EmbeddingsOrchestrationConfig, EmbeddingsModuleConfigs, EmbeddingsModelConfig, EmbeddingsModelDetails, EmbeddingsInput ) service = OrchestrationService() # Minimal configuration - just specify the model embeddings_config = EmbeddingsOrchestrationConfig( modules=EmbeddingsModuleConfigs( embeddings=EmbeddingsModelConfig( model=EmbeddingsModelDetails(name="text-embedding-3-large") ) ) ) response = service.embed( config=embeddings_config, input=EmbeddingsInput(text="Hello World!") ) embedding = response.final_result.data[0].embedding print(f"Embedding dimensions: {len(embedding)}") print(f"First 5 values: {embedding[:5]}") ``` ### Response #### Success Response - **final_result.data** (list) - A list containing the embedding results. - **embedding** (list of float) - The generated vector embedding. - **index** (int) - The index of the input text in the batch. #### Response Example ```json { "final_result": { "data": [ { "embedding": [0.00123, -0.00456, ...], "index": 0 } ] } } ``` ``` -------------------------------- ### Sentiment Analysis Examples Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/orchestration-service2.html Provides a list of example message pairs for sentiment analysis. Each pair consists of a UserMessage and an AssistantMessage representing a sentiment classification. ```python sentiment_examples = [ (UserMessage(content="I love this product!"), AssistantMessage(content="Positive")), (UserMessage(content="This is terrible service."), AssistantMessage(content="Negative")), (UserMessage(content="The weather is okay today."), AssistantMessage(content="Neutral")), ] ``` -------------------------------- ### _ChatBedrock Initialization Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.proxy.langchain.html Drop-in replacement for LangChain ChatBedrock, extending AICoreBedrockBaseModel. ```APIDOC ## __init__ _ChatBedrock ### Description Initializes the AICoreBedrockBaseModel with AICore specific parameters. Extends the constructor of the base class with aicore specific parameters. ### Parameters #### Keyword Arguments * **model_id** (str, optional) - the model identifier, defaults to "" * **deployment_id** (str, optional) - the deployment identifier, defaults to "" * **model_name** (str, optional) - the model name, defaults to "" * **config_id** (str, optional) - the configuration identifier, defaults to "" * **config_name** (str, optional) - the configuration name, defaults to "" * **proxy_client** (Optional[BaseProxyClient], optional) - the proxy client to use, defaults to None ``` -------------------------------- ### Dataset Initialization with String Path Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.evaluations.html Create a Dataset instance using a string representing the file path. ```python >>> Dataset("data/sample.json") ``` -------------------------------- ### Define Sentiment Analysis Examples Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/orchestration-service.html Provides a list of example message pairs for sentiment analysis. Each pair consists of a UserMessage and its corresponding sentiment classification (Positive, Negative, or Neutral). ```python sentiment_examples = [ (UserMessage("I love this product!"), AssistantMessage("Positive")), (UserMessage("This is terrible service."), AssistantMessage("Negative")), (UserMessage("The weather is okay today."), AssistantMessage("Neutral")), ] ``` -------------------------------- ### BedrockLLM Initialization with Guardrails and Tracing Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.proxy.langchain.html Shows how to enable tracing for guardrails by setting the 'trace' key to True and passing a callback handler. This is useful for monitoring guardrail interventions. ```APIDOC ## BedrockLLM Initialization with Guardrails and Tracing ### Description Initializes the BedrockLLM class with guardrails and enables tracing for monitoring guardrail interventions. This involves setting the 'trace' key to True and providing a callback handler. ### Parameters #### `model_id` (str) - Required Id of the model to call, e.g., 'amazon.titan-text-express-v1'. For custom and provisioned models, an ARN value is expected. #### `client` (Any) - Required The Bedrock client object. #### `model_kwargs` (Dict[str, Any]) - Optional Keyword arguments to pass to the model. #### `guardrails` (Optional[Mapping[str, str]]) - Optional An optional dictionary to configure guardrails for Bedrock. This field consists of 'guardrailId' and 'guardrailVersion', which should be strings. To enable tracing, include `"trace": True`. #### `callbacks` (List[BaseCallbackHandler]) - Optional A list of callback handlers to use for tracing and logging. ### Request Example ```python from langchain_aws import BedrockLLM from langchain_core.callbacks import BedrockAsyncCallbackHandler llm = BedrockLLM( model_id="", client=, model_kwargs={}, guardrails={ "guardrailId": "", "guardrailVersion": "", "trace": True }, callbacks=[BedrockAsyncCallbackHandler()] ) ``` ### Further Information For more information on callback handlers, refer to: https://python.langchain.com/docs/concepts/callbacks/ ``` -------------------------------- ### Initialize Prompt Registry Client Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/prompt-registry.html Initializes the client to interact with the Prompt Registry. Ensure the proxy client is correctly configured. ```python from gen_ai_hub.proxy import get_proxy_client from gen_ai_hub.prompt_registry import OrchestrationConfigClient proxy_client = get_proxy_client(proxy_version="gen-ai-hub") prompt_registry_client = OrchestrationConfigClient(proxy_client=proxy_client) ``` -------------------------------- ### RPTClient Classification Task Example Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/gen_ai_hub.html Shows how to use RPTClient with a dictionary for a classification task. This example utilizes a dictionary format for the request body, including prediction configuration, columns, and data schema. ```python example_request_by_columns_dict = { "prediction_config": { "target_columns": [ { "name": "COSTCENTER", "prediction_placeholder": "[PREDICT]", "task_type": "classification" } ] }, "columns": { "PRODUCT": ["Couch", "Office Chair", "Server Rack"], "PRICE": [999.99, 150.8, 2200.00], "ORDERDATE": ["28-11-2025", "02-11-2025", "01-11-2025"], "ID": ["35", "44", "104"], "COSTCENTER": ["[PREDICT]", "Office Furniture", "Data Infrastructure"] }, "data_schema": { "PRODUCT": { "dtype": "string" }, "PRICE": { "dtype": "numeric" }, "ORDERDATE": { "dtype": "date" }, "ID": { "dtype": "string" }, "COSTCENTER": { "dtype": "string" } } } response = client.predict(body=example_request_by_columns_dict, model_name="sap-rpt-1-small") print(response.predictions) ``` -------------------------------- ### _init_client Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.proxy.html Initializes the client with provided data, preparing it for subsequent operations. ```APIDOC ## _init_client ### Description Initializes the client with the provided data. This method is used internally to set up the client's state based on input data. ### Method classmethod ### Parameters * **data** (_Any_) - Required - Input data for client initialization. ### Returns * Initialized data. Return type: Any ``` -------------------------------- ### init_conf Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.evaluations.html Initializes the configuration, optionally using a specified profile. ```APIDOC ## Function init_conf ### Description Initializes the SDK configuration, optionally specifying a profile to load. ### Parameters * **profile** (str) - The name of the profile to initialize the configuration with. ``` -------------------------------- ### RPTClient Regression Task Example Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/gen_ai_hub.html Demonstrates using RPTClient with pydantic models for a regression task. It shows how to set up the request body with target columns and rows, and how to make a prediction. Includes an example with and without specifying the model version. ```python from gen_ai_hub.proxy.native.sap import RPTRequest, PredictionConfig, TargetColumn, RPTClient rows_regression = [ { "PRODUCT": "Couch", "PRICE": 999.99, "ORDERDATE": "28-11-2025", "ID": "35", "DISCOUNT_RATE": "[PREDICT]", }, { "PRODUCT": "Office Chair", "PRICE": 150.80, "ORDERDATE": "02-11-2025", "ID": "44", "DISCOUNT_RATE": 0.12, }, { "PRODUCT": "Server Rack", "PRICE": 2200.00, "ORDERDATE": "01-11-2025", "ID": "104", "DISCOUNT_RATE": 0.05, }, { "PRODUCT": "Standing Desk", "PRICE": 640.00, "ORDERDATE": "05-11-2025", "ID": "205", "DISCOUNT_RATE": 0.10, }, { "PRODUCT": "Monitor 27 inch", "PRICE": 289.99, "ORDERDATE": "08-11-2025", "ID": "306", "DISCOUNT_RATE": "[PREDICT]", }, ] client = RPTClient() body = RPTRequest( prediction_config=PredictionConfig( target_columns=[ TargetColumn(name="DISCOUNT_RATE", task_type="regression") ]), rows=rows_regression ) response = client.predict(body=body, model_name="sap-rpt-1-small") print(response.predictions) #example with model_version response = client.predict(body=body, model_name="sap-rpt-1-small", model_version="latest") print(response.predictions) ``` -------------------------------- ### list_prompt_variables Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.evaluations.utils.html Get all fields (parameters) of the form {{ ?param_name }} from the template. ```APIDOC ## list_prompt_variables ### Description Get all fields (parameters) of the form {{ ?param_name }} from the template. Optionally return the raw field names without stripping spaces and '?'. ### Parameters #### Path Parameters - **format_string** (str) - Required - The template string to parse. ### Return type List[str] ``` -------------------------------- ### init_conf Function Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.evaluations.html Initializes configuration, potentially related to credentials. ```APIDOC ## Function: init_conf ### Description Initializes configuration settings. The specific configuration details are not provided. ### Parameters (No parameters are explicitly documented for this function in the source.) ### Returns (The return type is not explicitly documented in the source.) ``` -------------------------------- ### Initialize OrchestrationService Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/orchestration-service.html Instantiate the OrchestrationService with configuration details to interact with the service. ```python from gen_ai_hub.orchestration.service import OrchestrationService orchestration_service = OrchestrationService(config=config) ``` -------------------------------- ### _get_corresponding_model_id Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.proxy.langchain.html Gets the corresponding model ID for a given model name. ```APIDOC ## _get_corresponding_model_id(_model_name_) ### Description Gets the corresponding model ID for a given model name. ### Parameters #### Path Parameters * **model_name** (str) - the model name ### Raises * **ValueError** - if the model name is not supported ### Returns the corresponding model ID ### Return Type str ``` -------------------------------- ### Get Data Repository by ID Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.document_grounding.html Retrieves a single data repository by its unique identifier. ```APIDOC ## GET /retrieval/data_repositories/{repository_id} ### Description Get a single data repository by its unique ID. Retrieves a single data repository by its unique identifier. ### Method GET ### Endpoint /retrieval/data_repositories/{repository_id} ### Parameters #### Path Parameters - **repository_id** (str) - Required - the unique identifier of the data repository ### Response #### Success Response (200) - **DataRepository model representing the data repository** (DataRepository) - DataRepository model representing the data repository ``` -------------------------------- ### ImageUrl.__init__ Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.orchestration.models.html Initializes an ImageUrl instance with a URL and an optional detail level. ```APIDOC ## ImageUrl.__init__ ### Description A data structure holding the URL and detail level for an image. ### Method `__init__(_url_ , _detail =None_)` ### Parameters #### Path Parameters - **url** (str) - Required - The location of the image, as a standard or data URL. - **detail** (ImageDetailLevel | None) - Optional - The processing detail level for the image. ``` -------------------------------- ### get_current_deployment Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.proxy.native.amazon.html Gets the current deployment from the context. This is useful for retrieving the active deployment configuration. ```APIDOC ## get_current_deployment ### Description Gets the current deployment from the context. ### Returns - **Any** - The current deployment. ``` -------------------------------- ### Initialize Prompt Registry Client Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/prompt-registry.html Initializes the client to interact with the Prompt Registry API. Ensure you have provided the necessary credentials beforehand. ```python from gen_ai_hub.proxy import get_proxy_client from gen_ai_hub.prompt_registry import PromptTemplateClient proxy_client = get_proxy_client(proxy_version="gen-ai-hub") prompt_registry_client = PromptTemplateClient(proxy_client=proxy_client) ``` -------------------------------- ### Instantiate Orchestration Service Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/orchestration-service2.html Sets up the necessary configurations and instantiates the OrchestrationService for use. This includes defining system and user messages, model details, and module configurations. ```python from gen_ai_hub.orchestration_v2 import ( SystemMessage, UserMessage, Template, PromptTemplatingModuleConfig, LLMModelDetails, OrchestrationConfig, ModuleConfig ) from IPython.display import display, Markdown # just for pretty print in jupyter template = Template( template=[ SystemMessage(content="This is a system message."), UserMessage(content="Write a markdown cheatsheet!"), ] ) llm=LLMModelDetails(name="gemini-2.0-flash") prompt_template = PromptTemplatingModuleConfig(prompt=template, model=llm) module_config = ModuleConfig(prompt_templating=prompt_template) config = OrchestrationConfig(modules=module_config) # Instantiate the orchestration service. from gen_ai_hub.orchestration_v2 import OrchestrationService orchestration_service = OrchestrationService(config=config) ``` -------------------------------- ### PipelineAPIClient.trigger_pipeline Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.document_grounding.clients.html Trigger the execution of a pipeline. This method starts a new run for a specified pipeline. ```APIDOC ## PipelineAPIClient.trigger_pipeline ### Description Trigger the execution of a pipeline. ### Parameters #### Path Parameters - **pipeline_id** (str) - Required - The ID of the pipeline to trigger. #### Request Body - **request** (TriggerPipelineRequest) - Optional - The object containing parameters for triggering the pipeline. ### Response #### Success Response (200) - **PipelineExecutionResponse** - A PipelineExecutionResponse object containing details of the triggered execution. ``` -------------------------------- ### Chat Completions with Model Version Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/gen_ai_hub.html Example of using Chat Completions, specifying both model_name and model_version. ```python #example where model_name is passed with model_version parameter from gen_ai_hub.proxy.native.openai import chat messages = [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Does Azure OpenAI support customer managed keys?"}, {"role": "assistant", "content": "Yes, customer managed keys are supported by Azure OpenAI."}, {"role": "user", "content": "Do other Azure Cognitive Services support this too?"}] response = chat.completions.create(model_name='gpt-4o-mini', model_version="latest", messages=messages) print(response) ``` -------------------------------- ### Dataset Initialization with Path Object Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.evaluations.html Create a Dataset instance using a Path object for the data source. ```python >>> Dataset(Path("data/sample.json")) ``` -------------------------------- ### Initialize EvaluationClient with Environment Variables Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/evaluations.html Loads credentials from an .env file and initializes the EvaluationClient using environment variables. This client is used for interacting with generative AI services for evaluation purposes. Ensure all necessary environment variables are set. ```python # Loading the credentials from the env file from gen_ai_hub.evaluations import EvaluationClient from dotenv import load_dotenv import os load_dotenv(override=True) AICORE_BASE_URL = os.getenv("AICORE_BASE_URL") AICORE_RESOURCE_GROUP = os.getenv("AICORE_RESOURCE_GROUP") AICORE_AUTH_URL = os.getenv("AICORE_AUTH_URL") AICORE_CLIENT_ID = os.getenv("AICORE_CLIENT_ID") AICORE_CLIENT_SECRET = os.getenv("AICORE_CLIENT_SECRET") AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID") AWS_BUCKET_ID = os.getenv("AWS_BUCKET_ID") AWS_REGION = os.getenv("AWS_REGION") AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY") ORCHESTRATION_URL = os.getenv("ORCHESTRATION_URL") client = EvaluationClient( # direct ai_core_client can be added as a parameter if already created base_url=AICORE_BASE_URL, auth_url=AICORE_AUTH_URL, client_id=AICORE_CLIENT_ID, client_secret=AICORE_CLIENT_SECRET, resource_group=AICORE_RESOURCE_GROUP, aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY, orchestration_url=ORCHESTRATION_URL ) # One more way to initialize the client # client = EvaluationClient.from_env() print(client.base_url) print(client.ai_core_client.object_store_secrets.query(top=10,resource_group=AICORE_RESOURCE_GROUP)) ``` -------------------------------- ### Get Pipeline Status Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.document_grounding.html Retrieves the current status of a pipeline identified by its unique pipeline ID. ```APIDOC ## GET /pipelines/{pipeline_id}/status ### Description Get pipeline status by pipeline id. Retrieves the current status of a pipeline identified by its unique pipeline ID. ### Method GET ### Endpoint /pipelines/{pipeline_id}/status ### Parameters #### Path Parameters - **pipeline_id** (str) - Required - Pipeline ID ### Response #### Success Response (200) - **Status of the pipeline** (GetPipelineStatusResponse) - Status of the pipeline ``` -------------------------------- ### _Service Class Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.evaluations.html Represents a service within an environment, providing methods to get configuration values. ```APIDOC ## Class _Service ### Description Represents a service with an associated environment, allowing retrieval of configuration values. ### Parameters * **env** (Dict[str, Any]) - A dictionary representing the environment configuration. ### Methods * **get**(_key_, _default= _) - Retrieves a value from the environment by key. ### Properties * **label**: str | None * **name**: str | None ``` -------------------------------- ### Completions.create Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.proxy.native.openai.html This method creates a completion based on the provided parameters. It uses a proxy client to select a deployment and then calls the create method of the parent class to generate a completion. ```APIDOC ## Completions.create ### Description This method creates a completion based on the provided parameters. It uses a proxy client to select a deployment and then calls the create method of the parent class to generate a completion. ### Method POST (inferred) ### Endpoint /v1/completions (inferred) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **prompt** (str) - Required - The prompt to use for completion. - **model** (str | None | NotGiven) - Optional - The model to use for completion. - **deployment_id** (str | None | NotGiven) - Optional - The deployment ID to use for completion. - **model_name** (str | None | NotGiven) - Optional - The model name to use for completion. - **model_version** (str | None | NotGiven) - Optional - The model version to use for completion. - **config_id** (str | None | NotGiven) - Optional - The configuration ID to use for completion. - **config_name** (str | None | NotGiven) - Optional - The configuration name to use for completion. ### Request Example ```json { "prompt": "Write a short story about a robot learning to love.", "model": "text-davinci-003" } ``` ### Response #### Success Response (200) - **Completion** - The completion object containing the generated text. #### Response Example ```json { "id": "cmpl-123", "object": "text_completion", "created": 1677652288, "model": "text-davinci-003", "choices": [ { "text": "\n\nOnce upon a time, in a world of circuits and code, lived a robot named Unit 734. Unit 734 was designed for logic and efficiency, but one day, it encountered a glitch in its programming that led to an unexpected emotion: love.", "index": 0, "logprobs": null, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 10, "completion_tokens": 30, "total_tokens": 40 } } ``` ``` -------------------------------- ### Get Orchestration Configs Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.prompt_registry.html Retrieves the latest version of orchestration configurations based on provided filters. ```APIDOC ## GET /v1/prompt-registry/orchestration-configs ### Description Retrieve the latest version of every orchestration config based on the filters. ### Method GET ### Endpoint /v1/prompt-registry/orchestration-configs ### Parameters #### Query Parameters - **scenario** (str) - Required - the scenario name of the orchestration config. - **name** (str) - Required - the name of the orchestration config. - **version** (str) - Required - the version of the orchestration config. - **retrieve** (str) - Optional - both(default), imperative, declarative - **include_spec** (bool) - Optional - false(default), true - **resolve_template_ref** (bool) - Optional - false(default), true ### Response #### Success Response (200) - **configs** (list of OrchestrationConfigGetResponse) - A list of orchestration config entries. #### Response Example { "configs": [ { "id": "123e4567-e89b-12d3-a456-426614174000", "name": "MyConfig", "version": "1.0", "scenario": "MyScenario", "creation_timestamp": "2023-10-27T10:00:00Z", "managed_by": "user", "is_version_head": true, "spec": {} } ] } ``` -------------------------------- ### Get Orchestration Config by ID Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.prompt_registry.html Retrieves a specific version of an orchestration configuration by its unique ID. ```APIDOC ## GET /v1/prompt-registry/orchestration-configs/{config_id} ### Description Retrieve a specific version of the orchestration config by ID. ### Method GET ### Endpoint /v1/prompt-registry/orchestration-configs/{config_id} ### Parameters #### Path Parameters - **config_id** (str) - Required - The ID of the orchestration config to retrieve. #### Query Parameters - **resolve_template_ref** (bool) - Optional - false(default), true ### Response #### Success Response (200) - **id** (str) - UUID of the config. - **name** (str) - Config name. - **version** (str) - Config version. - **scenario** (str) - Scenario name. - **creation_timestamp** (str) - When the config was created. - **managed_by** (str) - Who manages the config. - **is_version_head** (bool) - Whether this is the head version. - **spec** (OrchestrationConfig) - The orchestration config specification (optional). #### Response Example { "id": "123e4567-e89b-12d3-a456-426614174000", "name": "MyConfig", "version": "1.0", "scenario": "MyScenario", "creation_timestamp": "2023-10-27T10:00:00Z", "managed_by": "user", "is_version_head": true, "spec": {} } ``` -------------------------------- ### TextPart.__init__ Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.orchestration.models.html Initializes a TextPart instance with text content and an optional type. ```APIDOC ## TextPart.__init__ ### Description Represents a text segment within a multimodal content block. ### Method `__init__(_text_ , _type ='text'_)` ### Parameters #### Path Parameters - **text** (str) - Required - The string content of the text part. - **type** (str) - Optional - The type identifier, defaulting to "text". ``` -------------------------------- ### Chat Completions with Deployment ID Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/gen_ai_hub.html Example of using Chat Completions, specifying deployment_id instead of model_name. ```python #example where deployment_id is passed instead of model_name parameter from gen_ai_hub.proxy.native.openai import chat messages = [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Does Azure OpenAI support customer managed keys?"}, {"role": "assistant", "content": "Yes, customer managed keys are supported by Azure OpenAI."}, {"role": "user", "content": "Do other Azure Cognitive Services support this too?"}] response = chat.completions.create(deployment_id="dcef02e219ae4916", messages=messages) print(response) ``` -------------------------------- ### Initialize ImageUrl Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.orchestration.models.html Initialize an ImageUrl object with the image URL and an optional detail level. ```python image_url = ImageUrl(url="https://example.com/image.png", detail=ImageDetailLevel.HIGH) ``` -------------------------------- ### Initialize Orchestration Service and LLM Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/document-grounding.html Sets up the OrchestrationService client using the provided API URL and initializes an LLM object with specified parameters. This is a prerequisite for using document grounding features. ```python from gen_ai_hub.orchestration.service import OrchestrationService from gen_ai_hub.orchestration.models.config import OrchestrationConfig from gen_ai_hub.orchestration.models.document_grounding import ( GroundingModule, GroundingType, DataRepositoryType, GroundingFilterSearch, DocumentGrounding, DocumentGroundingFilter ) from gen_ai_hub.orchestration.models.llm import LLM orchestration_service_url = "https://api.ai.<*** cluster-name ***>.aws.ml.hana.ondemand.com/v2/inference/deployments/<*** deployment_id ***>" orchestration_service = OrchestrationService(api_url=orchestration_service_url) llm = LLM( name="gpt-4o-mini", parameters={ 'temperature': 0.0, } ) ``` -------------------------------- ### Dataset Initialization with ArtifactSource (Dictionary) Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.evaluations.html Create a Dataset instance using an ArtifactSource object initialized with an artifact dictionary. ```python >>> Dataset( ... ArtifactSource( ... artifact={ ... "id": "xyfz-rtyu-2456-ojns-yu6s", ... "name": "dataset-artifact", ... "url": "ai://default/eval_dataset" ... }, ... path="rootfolder/data.csv", ... file_type="csv" ... ) ... ) ``` -------------------------------- ### get_proxy_cls Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.proxy.core.html Retrieves the proxy client class for a specified version. If no version is provided, it attempts to get the default version. ```APIDOC ## get_proxy_cls ### Description Get the proxy client class for the given version. ### Method Not applicable (Python method) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * **proxy_version** (str | None, optional) - The proxy version. ### Returns The proxy client class. ### Return Type type[BaseProxyClient] ``` -------------------------------- ### BedrockLLM Initialization with Guardrails Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.proxy.langchain.html Demonstrates how to initialize the BedrockLLM class with specific guardrails enabled. This includes setting the guardrail ID and version. ```APIDOC ## BedrockLLM Initialization with Guardrails ### Description Initializes the BedrockLLM class with optional guardrails configuration. This allows for specifying a guardrail ID and version to control the behavior of the language model. ### Parameters #### `model_id` (str) - Required Id of the model to call, e.g., 'amazon.titan-text-express-v1'. For custom and provisioned models, an ARN value is expected. #### `client` (Any) - Required The Bedrock client object. #### `model_kwargs` (Dict[str, Any]) - Optional Keyword arguments to pass to the model. #### `guardrails` (Optional[Mapping[str, str]]) - Optional An optional dictionary to configure guardrails for Bedrock. This field consists of 'guardrailId' and 'guardrailVersion', which should be strings. ### Request Example ```python from langchain_aws import BedrockLLM llm = BedrockLLM( model_id="", client=, model_kwargs={}, guardrails={ "guardrailId": "", "guardrailVersion": "" } ) ``` ``` -------------------------------- ### Get All Pipelines Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.document_grounding.html Retrieves a list of all available pipelines. Supports limiting the number of results and skipping a specified number of pipelines. ```APIDOC ## GET /pipelines ### Description Get all pipelines. Retrieves a list of all available pipelines. Supports limiting the number of results and skipping a specified number of pipelines. ### Method GET ### Endpoint /pipelines ### Parameters #### Query Parameters - **top** (Optional[int]) - Optional - **skip** (Optional[int]) - Optional - **count** (Optional[bool]) - Optional ### Response #### Success Response (200) - **Get all pipelines** (GetPipelinesResponse) - Get all pipelines ``` -------------------------------- ### AsyncOpenAI Client Initialization Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.proxy.native.openai.html Initializes the AsyncOpenAI client with optional proxy client and API version. ```APIDOC ## AsyncOpenAI Client Initialization ### Description Initializes the AsyncOpenAI client with the provided parameters, allowing for custom proxy clients and API versions. ### Method __init__ ### Parameters * **proxy_client** (Optional[BaseProxyClient]) - Optional - An instance of a Proxy Client. Defaults to None. * **api_version** (Optional[str]) - Optional - API version used for OpenAI API calls. Defaults to DEFAULT_API_VERSION. ### Return type None ``` -------------------------------- ### Get Metric Results Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.evaluations.html Retrieves the metric-level results for an evaluation run. Returns a pandas DataFrame containing the metric results. ```python metrics() Get the metric-level results for the run. Returns: DataFrame containing metric results for the run Return type: pd.DataFrame Raises: **ValueError** -- If error occurs while fetching metric results ``` -------------------------------- ### Get Completion Results Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.evaluations.html Retrieves the completion results for an evaluation run. Returns a pandas DataFrame containing the completion results. ```python completions() Get the completion results for the run. Returns: DataFrame containing completion results for the run Return type: pd.DataFrame Raises: **ValueError** -- If error occurs while fetching completions ``` -------------------------------- ### ImageItem.__init__ Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.orchestration.models.html Initializes an ImageItem instance with a URL and an optional detail level. ```APIDOC ## ImageItem.__init__ ### Description Initializes an ImageItem instance. ### Method `__init__(_url =None_, _detail =None_)` ### Parameters #### Path Parameters - **url** (Optional[str]) - Optional - The image location as a standard or data URL, defaults to None. Standard URL example: 'https://example.com/image.png'. Data URL example: 'data:image/png;base64,...'. - **detail** (Optional[ImageDetailLevel]) - Optional - The image detail level for model processing, defaults to None. ``` -------------------------------- ### Get Orchestration Config History Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.prompt_registry.html Retrieves the history of edits for a specific orchestration configuration identified by scenario, name, and version. ```APIDOC ## GET /v1/prompt-registry/orchestration-configs/history ### Description Retrieve the history of edits to the orchestration config. ### Method GET ### Endpoint /v1/prompt-registry/orchestration-configs/history ### Parameters #### Query Parameters - **scenario** (str) - Required - The scenario name of the orchestration config. - **name** (str) - Required - The name of the orchestration config. - **version** (str) - Required - The version ID of the orchestration config. - **include_spec** (bool) - Optional - false(default), true - **resolve_template_ref** (bool) - Optional - false(default), true ### Response #### Success Response (200) - **configs** (list of OrchestrationConfigGetResponse) - A list of orchestration config history entries. #### Response Example { "configs": [ { "id": "123e4567-e89b-12d3-a456-426614174000", "name": "MyConfig", "version": "1.0", "scenario": "MyScenario", "creation_timestamp": "2023-10-27T10:00:00Z", "managed_by": "user", "is_version_head": true, "spec": {} } ] } ``` -------------------------------- ### _BedrockEmbeddings Initialization Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.proxy.langchain.html Drop-in replacement for LangChain BedrockEmbeddings, extending AICoreBedrockBaseModel. ```APIDOC ## __init__ _BedrockEmbeddings ### Description Initializes the AICoreBedrockBaseModel with AICore specific parameters. Extends the constructor of the base class with aicore specific parameters. ### Parameters #### Keyword Arguments * **model_id** (str, optional) - the model identifier, defaults to "" * **deployment_id** (str, optional) - the deployment identifier, defaults to "" * **model_name** (str, optional) - the model name, defaults to "" * **config_id** (str, optional) - the configuration identifier, defaults to "" * **config_name** (str, optional) - the configuration name, defaults to "" * **proxy_client** (Optional[BaseProxyClient], optional) - the proxy client to use, defaults to None ``` -------------------------------- ### get_mapped_value_if_exists Source: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_api_doc/gen_ai_hub.evaluations.utils.html Gets the first valid mapped value from the list of keys if it exists in variable mapping, else returns the first key. ```APIDOC ## get_mapped_value_if_exists ### Description Gets the first valid mapped value from the list of keys if it exists in variable mapping, else returns the first key. ### Parameters #### Path Parameters - **key_** (Any) - Required - Description of the key. - **mapping_keys** (Any) - Required - Description of the mapping keys. - **variable_mapping** (dict) - Required - Description of the variable mapping. - **dataset_columns** (Any) - Required - Description of the dataset columns. ### Return type str ```