chapter two

2 Basic prompting and DSPy

 

This chapter covers

  • Common prompt components
  • Our first DSPy application
  • An overview of DSPy’s main concepts

In this chapter, we look more closely at the prompts sent to language models (LMs) when working with an LM-based application. When interacting with an LM through voice or a GUI hosted by the LM provider, we usually use a short, simple prompt and, if necessary, rephrase it a few times until we get a suitable response. But when software makes the LM calls, we can’t repeatedly reword the prompt until we get a good response; the calls have to be reliable. As indicated in chapter 1, this tends to result in longer, more carefully created prompts. These are the situations where either prompt engineering (manually tweaking the prompt through trial and error) or automation through prompt programming is often necessary.

In these situations, prompts tend to include a number of standard sections (and often some less common sections) to help ensure that LM responses are reliable. Examples include indicating high stakes for the task, asking the LM to adopt a persona, providing examples (aka demonstrations), and so on. The ideal sections vary from one task to another and from one LM to another, but they are fairly standard. New sections are also added to our repertoires occasionally as new prompting techniques are discovered. Shortly, we’ll look at sections that are, at least currently, reasonably standard.

2.1 Prompts

2.1.1 Classification prompt example

2.1.2 The components of an effective prompt

2.2 A full DSPy application

2.3 The main concepts in DSPy

2.4 The language model

2.4.1 Storing API keys in .env files

2.4.2 Calling LMs directly

2.4.3 Using LiteLLM to access LMs

2.4.4 LM caching

2.4.5 Setting LM parameters

2.4.6 Switching between LMs

2.5 Signatures

2.5.1 Example asking for a confidence score

2.5.2 Summarization example

2.5.3 Translation example

2.5.4 Entailment example

2.5.5 Style transfer example

2.6 Modules

2.7 Predictions