Skip to main content
After reading this guide, you will know:
  • what DSPy and Ruby on DSPy are, and why the gem runs DSPy itself;
  • what a few lines of dspy-rb look like;
  • what a signature, a predictor and a prediction are.

DSPy and Ruby on DSPy

DSPy is a Python library for programming language models: you declare what a task takes in and gives back, and DSPy writes the prompt, calls the model and reads the reply back as typed values. Ruby on DSPy runs DSPy from Ruby. The gem installs DSPy and runs it in a Python process of its own, so you write only Ruby. Because every call runs in DSPy itself, the prompts are exactly the ones DSPy sends from Python, and DSPy’s docs explain the ideas behind them. The gem is dspy-rb, and its module is DSPyRb. It’s not DSPy.rb, a separate project that rewrites DSPy in Ruby.

A first look

Say your app receives support tickets, and you want a model to label each one with a category and whether it’s urgent. With Ruby on DSPy, this is all the code:
It prints the model’s labels:
Output
DSPyRb::LM[...] names the language model DSPy calls, as provider/model. config.lm makes it the model every call uses. A signature declares a task by its inputs and outputs. TriageTicket is a signature, written as a class. It has three parts:
  • instructions tells the model the task, in a sentence.
  • input declares what goes in: here, a ticket’s subject and body.
  • output declares what comes back: here, a category and an urgent flag.
Each output has a type, which tells DSPy what to ask the model for and how to read its reply. So category comes back as one of the names in CATEGORIES, and urgent as true or false. A reply that doesn’t fit raises an error. A signature can also be a string of names alone, such as "question -> answer", when a task needs no types. Names before the arrow are inputs; names after it are outputs. DSPyRb::Predict[signature] makes a predictor, which runs the signature: its call takes the inputs as keywords and asks the model. call returns a prediction, with a reader for each output. There’s no prompt in the code. DSPy writes it from the signature.

Next

Installation

Add the gem, install DSPy and make a first call.