Smolagents example connecting to Sap Generative AI Hub via LiteLLM
How Smolagents work
Installation
%pip install “smolagents[toolkit] litellm”
Credentials for SAP Gen AI Hub
Get the service key from your SAP BTP tenant with AI subscription.
Add the following variables from the service key in a file called “.env” and put it in the same folder where you run the notebook:
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/
Run the Smolagents with LiteLLM and SAP LLMs
[ ]:
from dotenv import load_dotenv
from smolagents import CodeAgent, LiteLLMModel, tool
Load your credentials as environment variables that Litellm can use automatically.
[ ]:
load_dotenv()
Define the agent tool
[ ]:
@tool
def get_weather(city: str) -> str:
"""Retrieves the current weather report for a specified city.
Args:
city (str): The name of the city to retrieve weather information for.
Examples: "New York", "London", "Tokyo".
"""
city_normalized = city.lower().replace(" ", "")
mock_weather_db = {
"newyork": "The weather in New York is sunny with a temperature of 25°C.",
"london": "It's cloudy in London with a temperature of 15°C.",
"tokyo": "Tokyo is experiencing light rain and a temperature of 18°C.",
}
if city_normalized in mock_weather_db:
return mock_weather_db[city_normalized]
else:
return f"The weather in {city} is sunny with a temperature of 20°C."
Define the model
[ ]:
model = LiteLLMModel(model_id="sap/gpt-5")
Define the agent
[ ]:
agent = CodeAgent(tools=[get_weather], model=model)
Run the agent
[ ]:
response = agent.run("What is the weather like in London?")
print(response)