Overview
The@sglang.function decorator converts a Python function into an SGLang prompt program that can be executed with language models.
Syntax
Parameters
int
Number of tokens to reserve for API specifications. Used for advanced features.
Function Requirements
- The first parameter must be named
s(the SGLang state object) - Additional parameters define the program’s inputs
- The function body contains prompt logic using SGLang primitives
Usage
Basic Definition
Running the Program
Batch Execution
Methods
.run()
Executes the program with a single input.
Parameters:
- All function arguments as keyword arguments
- Sampling parameters:
max_new_tokens,temperature,top_p,top_k, etc. backend: Backend to use (defaults to global backend)stream: Whether to stream results
.run_batch()
Executes the program with multiple inputs in parallel.
Parameters:
batch_kwargs: List of dictionaries, each containing function arguments- Sampling parameters (applied to all executions)
num_threads: Number of parallel threadsprogress_bar: Show progress bar
.bind()
Partially binds arguments to create a new function.
.trace()
Traces the program execution for debugging.
.cache()
Caches the program’s prefix for faster repeated execution.
Example: Multi-turn Conversation
See Also
- gen() - Generate text from the model
- select() - Choose from predefined options
- Role markers - Structure conversations
