Arbor provides the ArborGRPO compiler to optimize DSPy programs using Reinforcement Learning (specifically Group Relative Policy Optimization).
To use it, you must:
- Initialize the Arbor server using
arbor.init(). - Define a DSPy program.
- Connect the program to Arbor using
ArborProvider and a dspy.LM configured with the openai/arbor:{model_name} prefix and the base_url provided by arbor.init(). - Define a reward function that takes
(example, pred, trace=None) and returns a float. - Configure
ArborGRPO with your metric, training arguments (train_kwargs), and rollout settings. - Call
compiler.compile(student, trainset, valset) to run the optimization.
import dspy
import arbor
from arbor import ArborGRPO, ArborProvider
# 1. Start Arbor server
arbor_server_info = arbor.init()
# 2. Define DSPy program
translate_program = dspy.Predict("english -> french")
# 3. Connect DSPy to Arbor
provider = ArborProvider()
model_name = "Qwen/Qwen2.5-1.5B-Instruct"
هاlm = dspy.LM(
model=f"openai/arbor:{model_name}",
provider=provider,
api_base=arbor_server_info["base_url"],
api_key="arbor",
temperature=1.0,
top_p=1.0,
top_k=-1,
repetition_penalty=1.0,
max_tokens=2048,
)
translate_program.set_lm(lm)
# 4. Define reward function
def my_reward(example, pred, trace=None) -> float:
return float(len(set(pred.french)))
# 5. Configure compiler
compiler = ArborGRPO(
metric=my_reward,
num_dspy_examples_per_grpo_step=6,
num_rollouts_per_grpo_step=24,
train_kwargs={"learning_rate": 1e-6, "num_training_gpus": 3},
# ... other args
)
# 6. Run optimization
optimized_translate = compiler.compile(
student=translate_program,
trainset=trainset,
valset=valset,
)