9 Creating an AI-powered application

 

This chapter covers

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

Creating software is significantly different from what it was just a few short years ago. The difference stems from major developments in the field of 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 tremendously 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. Tasks that once required domain-specific expertise or painstaking programming can now be achieved with just a few well-crafted "prompts".

In this chapter, we'll dive into how you can harness the power of LLMs in your applications, relying on 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 your LLM interactions to get the results you want, and how to do so 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 our 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.6 Including multiple questions

9.6.1 Response variability, or lack thereof

9.6.2 Implementing multiple questions

9.7 Adding AI personalities

9.7.1 Adding game settings

9.8 Summary