Optimize Prompts for Tool Calling Using a Custom LLM-as-a-Judge Metric in SAP AI Core
- How to create a custom LLM-as-a-judge evaluation metric (
tool-call-accuracy) directly through the AI Core API, including its rating rubric, evaluation steps, and few-shot example. - How to configure a prompt-optimization run that uses a custom metric instead of a built-in one.
- How to normalize a raw BFCL v3 dataset into the optimizer’s “golden record” format and upload it as a dataset artifact.
- How to register a base prompt, trigger and monitor an optimization execution, and retrieve the optimized prompt.
- How to compare base vs optimized prompts through live inference, and how the choice of metric (custom vs
JSON_Match) shapes the optimizer’s output.
Prerequisites
- You have an SAP AI Core service instance and a service key (with
clientid,clientsecret,url, andserviceurls.AI_API_URL). - You have the
generative-ai-hub-sdk(gen_ai_hub) andai-api-client-sdkinstalled, plusrequests,python-dotenv, andpydantic. - The models
gpt-4o:2024-08-06andgemini-2.5-pro:001are available in your AI Core tenant (check Generative AI Hub → Models). - You have a running orchestration deployment in your tenant (required only for the live comparison in Step 9).
- You have the BFCL v3 dataset file
BFCL_v3_parallel_multiple_10tools.jsonin your working directory. This is a subset of the Berkeley Function-Calling Leaderboard v3 benchmark. - (Optional) You have Bruno installed if you want to follow the REST API option blocks instead of the Python SDK.
Large language models are often asked to read a user’s question and decide which tools to call and with what arguments (“tool calling”). A vague system prompt like "You are a helpful assistant." usually makes the model reply in prose instead of structured JSON, which breaks any downstream code expecting machine-readable tool calls.
Prompt Optimization in SAP AI Core automates the trial-and-error of prompt engineering. You give it a starting prompt, a dataset of questions paired with correct tool calls (“golden answers”), and a metric that scores how good a candidate prompt’s output is. The optimizer then iteratively rewrites the prompt, tests it, and keeps the best-scoring version.
This tutorial goes one step further than simply using a pre-existing metric: you will create the custom evaluation metric itself from code, run the full pipeline, and finish by comparing a custom-metric-optimized prompt against a JSON_Match-optimized one to see how the metric choice affects the result.