liveBook · Manning

Baseline

As explained in Building LLM Applications with DSPy

A reference performance result against which other results are compared.

A baseline for prompt optimization is the starting point that added instructions can strengthen. Prefer generally useful additions; extensive manual tweaks can drift into prompt engineering. section 7.4.1

Adding a closing instruction

A DSPy module can append “Answer very carefully” to a signature before prediction. An optimizer may rewrite the instruction, so manual additions should be useful beyond a single prompt. section 7.4.1

Listing 7.11 Custom module to add a closing section
from dspy.primitives.module import Module
from dspy.signatures.signature import Signature, ensure_signature
from typing import Any

class Closer_Example(Module):
    def __init__(self, signature: str, **config: dict[str, Any]):
        super().__init__()
        signature = ensure_signature(signature)
        signature.instructions += " Answer very carefully."
        self.predict = dspy.Predict(signature, **config)  #1

    def forward(self, **kwargs):
        return self.predict(**kwargs)

    async def aforward(self, **kwargs):
        return await self.predict.acall(**kwargs)

prog = Closer_Example("question -> answer")
prog(question="What is the capital of Spain?")
print(lm.inspect_history(n=1))

Shows a custom module that adds a closing instruction to a signature and delegates prediction to DSPy. section 7.4.1

How Building LLM Applications with DSPy covers it

How deep it goes
Shows how to append prompt text and cautions against manual over-tuning, without detailing an optimization procedure.
Examples
Uses compliments, gratitude, threats, and a DSPy module that appends “Answer very carefully” to a signature.
What you can do after
Build it from scratch by appending a closing instruction to a signature before optimization.
How it connects
Connects prompt optimization and prompt engineering through the choice between optimizer-selected phrases and manual prompt tweaks.

Also explained in

Compare how 16 books explain baseline →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building LLM Applications with DSPy. The section links point to the parts of the book it draws on.