You have seen a way to access generative AI hub using SAP AI Launchpad. Using generative-AI-hub-SDK you can leverage the power of LLMs available in generative AI for creating customized solutions combining the power of LLMs with and buisness context using SAP data.
With this SDK, you can interact with these models and create natural language completions, chat responses, and embeddings.
Note
It's recommended that you use python kernel 3.11.9 and above to execute all codes in this learning journey. You may refer to python guide here. In case of any errors, ensure that you have properly deployed, configured, and provisioned generative AI hub in SAP BTP.
Here are some key points:
Installation: You can install the SDK using the following pip command:
12
pip install "generative-ai-hub-sdk[all]"
Note
Here [all]
implies installing all extra packages including langchain, which is not available by default.
.Configuration: Ensure that you have access to generative AI hub and deployed models. Refer to the topic generative AI hub in lesson 1 in unit 1. The SDK reuses configuration settings from the AI-core-SDK. These include client ID, client secret, authentication URL, base URL, and resource group. You can set these values as environment variables or via a config file.
To utilize LLMs, you must configure the proxy modules.
We suggest setting these values as environment variables for AI core credentials via a configuration file. The default path for this file is ~/.aicore/config.json for Mac.
You can use the following steps for Mac:
- Open Notepad and replace the values in below json with your AI core Service keys that you downloaded from BTP and press Ctrl + S (Command + O for mac) to save file. A pop up will appear on the screen where navigate to ~/.aicore/ and location and save the file as config.json
In case you are using mac, you might not be able to create .aicore folder directly. In that case, create the folder using the command
- Now use the following command to open config.json file in nano.
1
nano ~/.aicore/config.json
Now paste the following json script on config.json file.
123456
"AICORE_AUTH_URL": "https://* * * .authentication.sap.hana.ondemand.com",
"AICORE_CLIENT_ID": "* * * ",
"AICORE_CLIENT_SECRET": "* * * ",
"AICORE_RESOURCE_GROUP": "* * * ",
"AICORE_BASE_URL": "https://api.ai.* * *.cfapps.sap.hana.ondemand.com/v2"
}
For windows, use the following steps:
- Create .aicore folder in C:\Users\<current user>
- Create a config.json file with the below contents
1234567
{
"AICORE_AUTH_URL":
"AICORE_CLIENT_ID": ,
"AICORE_CLIENT_SECRET": ,
"AICORE_RESOURCE_GROUP":
"AICORE_BASE_URL":
}
You can get these values from AI Core service key in BTP.
Alternatively, you can refer to the https://github.com/SAP-samples/ai-core-samples/blob/main/10_Learning_Journeys/README.md file for detailed configuration steps to execute the notebook for this learning journey in your system.
Usage Examples:
OpenAI-like API Completion:
123456789
from gen_ai_hub.proxy.native.openai import completions
response = completions.create(
model_name="tiiuae--falcon-40b-instruct",
prompt="The Answer to the Ultimate Question of Life, the Universe, and Everything is",
max_tokens=7,
temperature=0
)
print(response)
This Python code generates a completion for the given prompt using the "tiiuae--falcon-40b-instruct" model from the OpenAI library. It asks for a short response about the answer to the ultimate question of life and sets parameters for response length and randomness. Finally, it prints the generated completion.
Chat Completion:
1234567891011
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?"}
]
kwargs = dict(model_name='gpt-35-turbo', messages=messages)
response = chat.completions.create(**kwargs)
print(response)
This code interacts with the OpenAI API to simulate a chat conversation. By loading predefined messages into the "messages" list and specifying model details in "kwargs", it sends these inputs to the API to generate a response. This allows seamless integration for querying and getting automated replies within your application using any model, for example, "gpt-35-turbo" model from OpenAI in this code.
Embeddings:
1234567
from gen_ai_hub.proxy.native.openai import embeddings
response = embeddings.create(
input="Every decoding is another encoding.",
model_name="text-embedding-ada-002",
encoding_format='base64'
)
This code imports the "embeddings" module from "gen_ai_hub.proxy.native.openai" and generates an encoded representation of the text "Every decoding is another encoding." by calling the "create" function. It specifies the model "text-embedding-ada-002" and uses "base64" for the encoding format. This helps convert text into a machine-readable format for advanced text processing.