Introduction xxvii Part I Foundations of Generative AI 1 Chapter 1 Introduction to Generative AI 3 Evolution of AI Applications 4 Key Eras of AI Development 4 Logic and Rules-Based Systems 4 Early Machine Learning 5 Expert Systems 5 Big Data and Statistical Machine Learning 5 Deep Learning 6 The Rise of Generative AI 8 Transition to GenAI 9 Understanding AI and ml 11 What Machine Learning Can Do 12 Supervised Learning 12 Unsupervised Learning 13 Semi-Supervised Learning 13 Reinforcement Learning 13 Self-Supervised Learning 13 Large Language Models 14 Tokenization 14 Embedding 16 Transformer Layers 17 Prediction 18 What Makes Generative AI Different? 18 Generating Content 18 GenAI Is Necessarily Unpredictable 19 GenAI Is Probabilistic 19 GenAI Requires Prompt Design 19 GenAI Is Multimodal 19 GenAI Shifts UX Expectations 20 GenAI Needs Guardrails 20 Real-World Examples of AI Integration 20 AI-Enhanced Customer Service Bots 20 Generative Writing Tools 21 Image Generation in Creative Tools 22 Summary 24 Chapter 2 Understanding Generative AI Models 25 Key Factors in Choosing a Model 25 Cost and Pricing Structure 26 Licensing Model 26 Performance Metrics 27 Suitability for Your Use Case 28 Technical Features 28 Architecture 29 Parameter Count 30 Training Objective and Data 30 Fine-Tuning 30 Context Window 30 Unique Functionalities 31 Proprietary Models 31 GPT (OpenAI) 32 Claude (Anthropic) 33 DALLĀ·E (OpenAI) 33 Gemini (Google DeepMind) 33 Open and Open-Source Models 34 OLMo (Allen AI) 35 Llama (Meta) 35 Stable Diffusion (Stability AI) 36 Deciding Between Proprietary and Open Models 36 When to Use Which Model 38 Adapting Your Model''s Abilities 39 Fine-Tuning 39 Prompt Engineering 40 Retrieval-Augmented Generation 40 Choosing the Right Adaptation Strategy 42 When to Use Non-Generative Models Alongside GenAI 43 Key Advantages of Non-Generative Approaches 43 Strategic Use Cases for Hybrid Approaches 43 Latency-Critical Applications 44 Cost and Performance Optimization 44 Quality Control and Validation 44 Preprocessing and Filtering 45 Decision-Making and Scoring 45 When to Choose Traditional Approaches Over AI 45 Summary 46 Chapter 3 Getting Started with AI APIs and SDKs 47 Exploring Hosted Models 47 Setting Up a Simple Development Environment 48 OpenAI Developer Platform 48 Getting an OpenAI API Key 49 Anthropic''s Build with Claude 55 Google Gemini Developer API 57 GenAI Integration Patterns 59 Common Architectural Models for Integrating GenAI 59 Backend Service Integration 59 Frontend-Only Integration 61 Plugin-Based Integration 66 Hybrid Integration 66 Model Access Patterns 66 Synchronous vs. Asynchronous 67 Streaming vs. Batch 68 Input Types for GenAI Integration 69 Plain Text Prompts 69 Structured Prompts 69 Multimodal Prompts 70 Response Handling 71 Integrating Responses into the User Interface 71 Logging and Analytics 71 Chaining Responses to Other Services 72 Combining Techniques 72 Summary 73 Chapter 4 AI-Generated Data and Synthetic Users 75 Generating Test Data with GenAI 76 Traditional Test Data Generation 76 Manual Generation 76 Automated Data Generation 76 Data Masking 76 Using GenAI for Test Data Generation 77 Introducing the Sample App 77 Techniques for Generating Synthetic Data 79 Few-Shot Prompting for Schema-Aligned Data 79 Template-Based Generation with Randomized Inputs 81 Structured Output Formats 83 Simulating User Behavior and Interaction Flows 86 Simulating Chat-Based Interactions 86 Simulating Navigational Flows and Multistep Interactions 87 Simulating Edge Case and Adversarial Behavior 88 Best Practices and Limitations of Behavior Simulation 89 Summary 90 Chapter 5 Prompt Engineering 91 Why Prompt Design Matters in GenAI Applications 92 Prompt Quality Affects Output Quality 92 Prompting Is Cheaper and Faster than Fine-Tuning 93 Prompts Shape the Voice and Tone of AI 93 Better Prompts Reduce Hallucinations 93 Prompts Embed Business Logic 94 Prompt Design Supports Edge Case Handling 94 Good Prompts Improve Performance and Reduce Cost 94 Prompt Types 95 Zero-Shot Prompting 95 Few-Shot Prompting 96 Chain-of-Thought Prompting 96 Prompting Best Practices 97 Guiding the LLM with System Messages 98 Prompt Templates for Repeatable Interactions 98 Adjusting Generation Parameters 101 Max Tokens 101 Temperature 102 Top P 103 Top K 104 Stop Sequences 104 Deciding How to Set Inference Parameters 104 Tooling for Prompt Development 105 In-Browser Prompt Playgrounds 105 Anthropic Workbench 105 OpenAI Playground 112 Google AI Studio 115 Prompt Management 116 Summary 117 Part II Designing for a Better AI Experience 119 Chapter 6 Human-AI Interaction and UX Design 121 Managing User Expectations 122 Clarify the AI''s Capabilities Up Front 123 Set Expectations Around Potential Failure 124 Communicate When Outputs are Probabilistic 124 Provide Cues that Suggest When the AI is "Thinking" 124 Use Progressive Disclosure to Build Trust 125 Avoid Overpromising AI Abilities 125 Designing Interfaces for AI-Powered Features 126 Understand the Users and Context 126 Ensure Clarity of AI-Generated vs. User-Generated Content 126 Provide Opportunities for Correcting or Refining AI Outputs 127 Use Visual or Interaction Cues to Indicate When the AI Is Active or Idle 127 Offer Undo or Step-Back Controls to Reduce Risk and Build Confidence 127 Design for Uncertainty and Failure 128 Balancing Automation with Human Control 128 Improving Over Time 129 Capturing and Using User Feedback 129 Balancing Explicit Ratings and Behavioral Signals 130 Learning Without Surprising Users 130 Monitoring for Drift and Relevance 130 Accessibility and Inclusion in AI UX 131 Accessibility Standards for AI Applications 131 Best Practices for Accessible AI UX 132 GenAI as an Accessibility Aid 134 Testing GenAI Accessibility 134 Using GenAI to Test GenAI Outputs 137 Human-Centered AI in the Real World 138 Summary 139 Chapter 7 Optimizing AI for Performance and Cost 141 From Prototype to Production 141 The Hidden Cost of GenAI Features 142 Why Optimization Matters 142 The Trade-Off Triangle 143 Minimize Latency and Reduce Redundant API Calls 144 Reduce Prompt Size 144 Reduce the Size of the Model''s Response 145 Use Caching to Avoid Redundant Calls 146 Cache Exact Prompt-Response Pairs 146 Prompt Fingerprint Caching 148 Reuse Similar Responses with Embedding Search 150 Parallelize Requests 153 Stream Responses 155 Precompute for Known Flows 157 Lightweight Fine-Tuning 158 Profile and Monitor Performance 158 Logging to Identify Latency Hotspots 159 Observability Tools for GenAI Systems 159 Handle Rate Limits Gracefully 160 Understanding Usage Tiers 162 Throttle and Buffer Requests 162 Design for Fallback and Graceful Degradation 162 Summary 163 Part III Integrating AI into Applications 165 Chapter 8 Building AI-Powered Chatbots and Assistants 167 Start with a Simple Chatbot 168 Principles of Conversational Interface Design 174 Managing Turn-Taking, Flow, and Feedback in Dialogue 175 Show Feedback and Errors 176 Temporarily Disable the Input to Prevent Accidental Repeat Submissions 178 Use Backchannel Cues and Confirmations 178 Guide the Next Turn 180 Keep the User Oriented 181 Handling Memory, Context, and User Personalization 183 Tracking Conversation History 183 Adding Basic Personalization 186 Steering AI Toward Specific Tasks or Domains 190 Using System Prompts to Constrain Behavior 190 Welcoming the User 192 When to Use RAG for External Knowledge 194 Adding Auto-Scroll and Streaming Responses 195 Designing for Fallback, Clarification, and Edge Cases 200 Clarify Ambiguous Questions 200 Fall Back When the Answer Isn''t Known 202 Handle Out-of-Scope Requests Gracefully 202 Best Practices for Customer Service Chatbots 203 Summary 204 Chapter 9 Generating and Enhancing Content with AI 205 Building SPOT: Fast, On-Brand, and Grounded 205 Overview of SPOT 206 Getting Set Up 208 Where to Put This in a Real Application 208 AI-Assisted Writing and Summarization 209 Going from Brief to Draft 209 Rewriting for Tone, Audience, and Locale 211 Summarization with Source Citations 212 Repurposing Long-Form Content 212 Choosing the Right Summarization Mode 213 Keep It On-Brand with the Style Pack 214 Prompt-Time Injection 214 Po.
A Developer's Guide to Integrating Generative AI into Applications