chapter two

2 Generating Trustworthy Responses with Prompt Engineering

 

This chapter covers

  • Tailoring the settings of LLMs for maximum reliability
  • The foundations of prompt engineering for reliable LLMs
  • Prompt engineering techniques to reduce hallucinations.

Prompting has become an important technique for effectively utilizing the capabilities of large language models (LLMs). Carefully designed prompts provide the context, instructions, and examples needed to guide LLM text generation for a variety of applications. Prompt engineering involves the iterative process of constructing, analyzing, and refining prompts to produce high-quality outputs from models like GPT, Claude and Gemini.

Prompt engineering is how you control LLM behavior. The right prompts guide models toward accurate outputs; the wrong ones cause hallucinations. This chapter covers the settings and techniques that separate unreliable demos from production systems. We'll build an e-commerce chatbot as our running example. This chapter also compiles prompt engineering research, techniques, and tools for building robust applications.

2.1 Tailoring LLM Settings for Maximum Reliability

2.1.1 Optimizing temperature for predictable outputs

2.2 Limit output and turns for reduction of hallucination risk

2.3 Applying frequency and presence penalties for balanced content

2.4 Minimizing intrinsic randomness for stable performance

2.5 Foundations of prompt engineering for reliable LLMs

2.5.1 Designing components of a prompt for reliability

2.5.2 Crafting basic prompts with a focus on dependability

2.6 Prompt engineering techniques for preventing hallucinations

2.6.1 Zero-shot prompting

2.6.2 Few-shot prompting for contextual stability

2.6.3 Chain-of-thought prompting for transparent reasoning

2.6.4 Automatic chain of thought (Auto-CoT) for scalable reasoning

2.6.5 Self-consistency for cross-verification

2.6.6 Tree-of-thought (ToT) prompting for structured decision making

2.7 Project: Creating a reliable weather assistant with OpenAI's function calls

2.7.1 Building a weather assistant

2.7.2 Getting started and defining the weather function

2.7.3 Integrating the chatbot

2.8 Summary

2.9 References