liveBook · Manning

ChainOfThought module

As explained in Building LLM Applications with DSPy

A DSPy module that elicits intermediate reasoning before producing an answer.

ChainOfThought extends a task signature with a rationale or reasoning output, then uses a Predict module to make one language-model call. Placing that field before the task’s other outputs requests reasoning first; the generated rationale may affect later outputs. section 7.3.1

When ChainOfThought is used with an optimizer:

  • The module remains ChainOfThought; optimization can add demonstrations or improve instructions without changing its input or output fields. section 5.2.3
  • Training examples need not supply rationales. Demonstrations can mark the missing reasoning output as “Not supplied for this particular example.” section 5.2.3
prog = ChainOfThought(
   "question -> answer", 
   rationale_field=dspy.OutputField(desc="Think hard and slowly"), 
   rationale_field_type=dspy.OutputField)
prog(question="How would you describe the work of Sartre?")

This example customizes the rationale field’s prompt description and applies ChainOfThought to a question-and-answer task. section 7.3.1

How Building LLM Applications with DSPy covers it

How deep it goes
Traces the single-call implementation through signature extension, rationale-field customization, and prediction delegation.
Examples
Uses question-to-answer prompting, Sartre, intent classification, and demonstrations with missing rationale values.
What you can do after
Apply it via DSPy by extending a task signature with a rationale field and invoking the resulting module.
How it connects
Connects ChainOfThought to DSPy signatures, Predict modules, output fields, and few-shot optimizers.

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.