Prompt Engineering for LLMs

Book description

Large language models (LLMs) are revolutionizing the world, promising to automate tasks and solve complex problems. A new generation of software applications are using these models as building blocks to unlock new potential in almost every domain, but reliably accessing these capabilities requires new skills. This book will teach you the art and science of prompt engineering-the key to unlocking the true potential of LLMs.

Industry experts John Berryman and Albert Ziegler share how to communicate effectively with AI, transforming your ideas into a language model-friendly format. By learning both the philosophical foundation and practical techniques, you'll be equipped with the knowledge and confidence to build the next generation of LLM-powered applications.

  • Understand LLM architecture and learn how to best interact with it
  • Design a complete prompt-crafting strategy for an application
  • Gather, triage, and present context elements to make an efficient prompt
  • Master specific prompt-crafting techniques like few-shot learning, chain-of-thought prompting, and RAG
  • Publisher resources

    View/Submit Errata

    Table of contents

    1. Preface
      1. Who Is This Book For?
      2. What You Will Learn
      3. Conventions Used in This Book
      4. O’Reilly Online Learning
      5. How to Contact Us
      6. Acknowledgments
        1. From John
        2. From Albert
    2. I. Foundations
    3. 1. Introduction to Prompt Engineering
      1. LLMs Are Magic
      2. Language Models: How Did We Get Here?
        1. Early Language Models
        2. GPT Enters the Scene
      3. Prompt Engineering
      4. Conclusion
    4. 2. Understanding LLMs
      1. What Are LLMs?
        1. Completing a Document
        2. Human Thought Versus LLM Processing
        3. Hallucinations
      2. How LLMs See the World
        1. Difference 1: LLMs Use Deterministic Tokenizers
        2. Difference 2: LLMs Can’t Slow Down and Examine Letters
        3. Difference 3: LLMs See Text Differently
        4. Counting Tokens
      3. One Token at a Time
        1. Auto-Regressive Models
        2. Patterns and Repetitions
      4. Temperature and Probabilities
      5. The Transformer Architecture
      6. Conclusion
    5. 3. Moving to Chat
      1. Reinforcement Learning from Human Feedback
        1. The Process of Building an RLHF Model
        2. Keeping LLMs Honest
        3. Avoiding Idiosyncratic Behavior
        4. RLHF Packs a Lot of Bang for the Buck
        5. Beware of the Alignment Tax
      2. Moving from Instruct to Chat
        1. Instruct Models
        2. Chat Models
      3. The Changing API
        1. Chat Completion API
        2. Comparing Chat with Completion
        3. Moving Beyond Chat to Tools
      4. Prompt Engineering as Playwriting
      5. Conclusion
    6. 4. Designing LLM Applications
      1. The Anatomy of the Loop
        1. The User’s Problem
        2. Converting the User’s Problem to the Model Domain
        3. Using the LLM to Complete the Prompt
        4. Transforming Back to User Domain
      2. Zooming In to the Feedforward Pass
        1. Building the Basic Feedforward Pass
        2. Exploring the Complexity of the Loop
      3. Evaluating LLM Application Quality
        1. Offline Evaluation
        2. Online Evaluation
      4. Conclusion
    7. II. Core Techniques
    8. 5. Prompt Content
      1. Sources of Content
      2. Static Content
        1. Clarifying Your Question
        2. Few-Shot Prompting
      3. Dynamic Content
        1. Finding Dynamic Context
        2. Retrieval-Augmented Generation
        3. Summarization
      4. Conclusion
    9. 6. Assembling the Prompt
      1. Anatomy of the Ideal Prompt
      2. What Kind of Document?
        1. The Advice Conversation
        2. The Analytic Report
        3. The Structured Document
      3. Formatting Snippets
        1. More on Inertness
        2. Formatting Few-Shot Examples
      4. Elastic Snippets
      5. Relationships Among Prompt Elements
        1. Position
        2. Importance
        3. Dependency
      6. Putting It All Together
      7. Conclusion
    10. 7. Taming the Model
      1. Anatomy of the Ideal Completion
        1. The Preamble
        2. Recognizable Start and End
        3. Postscript
      2. Beyond the Text: Logprobs
        1. How Good Is the Completion?
        2. LLMs for Classification
        3. Critical Points in the Prompt
      3. Choosing the Model
      4. Conclusion
    11. III. An Expert of the Craft
    12. 8. Conversational Agency
      1. Tool Usage
        1. LLMs Trained for Tool Usage
        2. Guidelines for Tool Definitions
      2. Reasoning
        1. Chain of Thought
        2. ReAct: Iterative Reasoning and Action
        3. Beyond ReAct
      3. Context for Task-Based Interactions
        1. Sources for Context
        2. Selecting and Organizing Context
      4. Building a Conversational Agent
        1. Managing Conversations
        2. User Experience
      5. Conclusion
    13. 9. LLM Workflows
      1. Would a Conversational Agent Suffice?
      2. Basic LLM Workflows
        1. Tasks
        2. Assembling the Workflow
        3. Example Workflow: Shopify Plug-in Marketing
      3. Advanced LLM Workflows
        1. Allowing an LLM Agent to Drive the Workflow
        2. Stateful Task Agents
        3. Roles and Delegation
      4. Conclusion
    14. 10. Evaluating LLM Applications
      1. What Are We Even Testing?
      2. Offline Evaluation
        1. Example Suites
        2. Finding Samples
        3. Evaluating Solutions
        4. SOMA Assessment
      3. Online Evaluation
        1. A/B Testing
        2. Metrics
      4. Conclusion
    15. 11. Looking Ahead
      1. Multimodality
        1. User Experience and User Interface
        2. Intelligence
      2. Conclusion
    16. Index
    17. About the Authors

    Product information

    • Title: Prompt Engineering for LLMs
    • Author(s): John Berryman, Albert Ziegler
    • Release date: November 2024
    • Publisher(s): O'Reilly Media, Inc.
    • ISBN: 9781098156152