chapter two

2 Executing prompts programmatically

 

This chapter covers

  • Prompts and prompt engineering
  • Different kinds of prompts and how they're structured
  • Enhancing prompt responses using one, two, or a few-shot learning
  • Examples of using prompts with ChatGPT and the OpenAI API

AI applications interact with LLMs mainly through prompts—structured inputs that guide the model’s behavior. It’s a bit like giving directions to a talented but inexperienced colleague: the clearer and more specific you are, the better the results. To get accurate and relevant outputs, prompts need to be carefully crafted and tailored to the task. In practice, prompt design is one of the biggest factors in how well your application performs.

Prompt engineering—the practice of designing and refining prompts to guide an LLM’s output—is a core skill in building LLM applications. You’ll spend much of your time creating, testing, and iterating on prompts to make sure your system delivers reliable, high-quality results.

In this chapter, you'll begin with the basics of prompt design and gradually move to more sophisticated techniques, such as Chain of Thought. LangChain's suite of prompt engineering tools, including PromptTemplate and FewShotPromptTemplate, will be your key resources as you learn to harness the full power of LLMs in your applications.

2.1 Running prompts programmatically

2.1.1 Setting up a Jupyter Notebook environment for this chapter

2.1.2 Minimal prompt execution

2.2 Running prompts with LangChain

2.3 Prompt templates

2.3.1 Implementing a prompt template with a Python function

2.3.2 Using LangChain’s PromptTemplate

2.4 Types of prompt

2.4.1 Text classification

2.4.2 Sentiment analysis

2.4.3 Text summarization

2.4.4 Composing text

2.4.5 Question answering

2.4.6 Reasoning

2.5 Reasoning in detail

2.5.1 One-shot learning

2.5.2 Two-shot learning

2.5.3 Providing steps

2.5.4 Few-shot learning

2.5.5 Implementing few-shot learning with LangChain

2.5.6 Chain of Thought (CoT)

2.6 Prompt structure

2.7 Summary