Use LLM Gateway Calls in Custom Chunking Functions
Make LLM gateway calls from custom chunking functions when your chunking behavior depends on semantic understanding. Use this approach to define chunking behavior in prompt instructions and map model responses to the required response schema.
| Edition Table |
|---|
| Available in: Developer, Enterprise, Performance, and Unlimited Editions. See Data 360 edition availability. |
| Permission Sets Needed | |
|---|---|
| To use LLM gateway calls in custom chunking logic: | Permission set:
|
- Set up Salesforce CLI, the Data Code Extension plugin, and the Python SDK. See Set Up Salesforce CLI for Custom Functions.
- Initialize a function package and make sure that you can run local function validation commands. See Write a Custom Chunking Function.
- Use a Salesforce org with at least one generative AI model configured.
- Configure external client app access for model API authentication. Complete Step 1 and Step 2 from Access Models API with REST.
Implement this flow inside the function(request, runtime) callable in payload/entrypoint.py: prompt construction, LLM Gateway invocation, response parsing, and chunk mapping. For more information on writing a custom function, see Write and Validate a Custom Chunking Function.
-
Read the document elements to chunk from
request.input. -
Build the prompt that you send to the model. Write your chunking instructions in a template file or directly in
entrypoint.py, and insert the document text fromrequest.inputinto those instructions at run time.For a reference implementation, see
example/chunking_with_llm/entrypoint.pyandexample/chunking_with_llm/filesin the initialized function package. -
Build a text-generation request with
GenerateTextRequestBuilderand set a supported model. -
Call
runtime.llm_gateway.generate_text(...). -
Parse the response text, for example, by using
---CHUNK---. -
Map each segment to a chunk (
text,seq_no,chunk_type). Aggregate chunks across all elements with a continuousseq_no, and returnSearchIndexChunkingV1Response(output=chunks).
Handle failed Gateway responses explicitly. Log failures and raise controlled errors. Don’t place sensitive data in prompts or logs.
-
From your function package root, confirm that your function entrypoint and test payload files are ready for local validation.
-
Configure SDK authentication for local LLM testing.
- Configure client credentials:
datacustomcode configure --auth-type client_credentials
- Provide your org domain, connected app consumer key, and consumer secret when prompted.
- Configure client credentials:
-
Run local validation against your function entrypoint and test the payload.
sf data-code-extension function run -e payload/entrypoint.py -t tests/test.json