chapter nine

9 Creating an AI-powered application

 

This chapter covers

  • Connecting an app to a Large Language Model (LLM)
  • Engineering LLM prompts to achieve desired results
  • Using Structured Outputs to get LLM responses in a custom, parseable format
  • Managing state in a sequential Streamlit app
  • Using st.data_editor to create editable tables

Creating software is very different from what it was just a few years ago. This shift comes from major developments in AI (artificial intelligence), which, unless you’ve been living under a rock, you’ve probably heard of.

I’m talking, of course, about breakthroughs in LLMs, or large language models, and the exciting possibilities they open up. By processing and generating natural language, LLMs can understand context, answer complex questions, and even write software on their own, all with astonishing fluency. A few well-crafted prompts can now accomplish tasks that once required domain-specific expertise or painstaking programming.

In this chapter, we’ll dive into how to harness the power of LLMs in applications, using AI prompts and responses to implement product features that would have required highly advanced techniques half a decade ago. Along the way, we’ll also discuss how to tune LLM interactions to get the results you’re after, without burning a hole in your pocket.

9.1 Fact Frenzy: An AI trivia game

9.1.1 Stating the concept and requirements

9.1.2 Visualizing the user experience

9.1.3 Brainstorming the implementation

9.2 Using AI to generate trivia questions

9.2.1 Why use an LLM in Fact Frenzy?

9.2.2 Setting up an OpenAI API key

9.2.3 Calling the OpenAI API in Python

9.2.4 Writing an LLM class

9.2.5 The Game class

9.2.6 Calling the Game class in the app

9.3 Using AI to evaluate answers

9.3.1 Structured Outputs

9.4 Moving through game states

9.5 Game mechanics: Keeping score, a New Game button, and Game Over

9.5.1 Keeping score

9.7.1 Adding game settings