liveBook · Manning

Chapter 9: Creating an AI-powered application

What you will learn in chapter 9 of Build Python Web Apps with Streamlit

This chapter introduces building Streamlit features with LLM prompts and responses, controllable structured interactions, sequential state, and editable tables.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces building Streamlit features with LLM prompts and responses, controllable structured interactions, sequential state, and editable tables.

Key terms: LLM, large language model, prompts, responses, natural language, prompt engineering, Structured Outputs, custom parseable format, LLM interactions, state

9.1 Fact Frenzy: An AI trivia game

This section introduces Fact Frenzy, an AI-powered Streamlit trivia game that generates questions, evaluates answers, and supports varied quizmaster styles.

Key terms: Fact Frenzy, AI-powered Streamlit app, artificial intelligence, quizmaster styles, trivia

9.1.1 Stating the concept and requirements

This section defines Fact Frenzy’s AI trivia-game concept, its required features, and the deliberately limited scope for the hands-on project.

Key terms: Fact Frenzy, trivia game, AI, AI model, free-text response, difficulty level, quizmaster speaking styles, score, out of scope, persistent storage

9.1.2 Visualizing the user experience

This section sketches Fact Frenzy’s two-column interface and shows how its AI Quizmaster personality appears in a standard free-form trivia game.

Key terms: two-column layout, New Game button, Quizmaster, Difficulty, score, AI, Gollum, speaking styles, my precious, question-and-answer trivia game

9.1.3 Brainstorming the implementation

This section defines Fact Frenzy’s linear game loop, where the app owns game flow while an LLM supplies and evaluates trivia interactions.

Key terms: Fact Frenzy, LLM, game flow, logic loop, overall game flow, retrieve a trivia question, evaluate the answer, difficulty level, quizmaster, bells and whistles

9.2 Using AI to generate trivia questions

This section introduces LLMs as the AI foundation of Fact Frenzy, explaining their text capabilities and role as the game’s quizmaster.

Key terms: Large Language Model, LLM, Fact Frenzy, quizmaster, human-like text, contextually appropriate text, OpenAI, GPT, Anthropic, Claude

9.2.1 Why use an LLM in Fact Frenzy?

This section explains why Fact Frenzy uses an LLM to generate trivia, judge open-ended answers, and make the game more entertaining while acknowledging its limitations.

Key terms: LLM, generate questions on the fly, free-text user inputs, open-ended trivia questions, evaluate player responses, multiple-choice questions, comic relief provider, quizmasters, personality, mimic the style

9.2.2 Setting up an OpenAI API key

This section explains how to obtain and safely retain an OpenAI secret API key so the Fact Frenzy app can connect to OpenAI’s LLM service.

Key terms: OpenAI account, Settings, API Keys, secret key, OpenAI API, OpenAI API key, free credits, free tier, usage limits, paid tier

9.2.3 Calling the OpenAI API in Python

This section verifies an OpenAI API connection in Python by creating a client, sending a chat-completions prompt, and reading the generated text.

Key terms: openai, API key, client, prompt, messages, role, content, system, user, chat completions endpoint

9.2.4 Writing an LLM class

This section encapsulates OpenAI communication in an Llm class so the rest of Fact Frenzy can request LLM responses through a maintainable interface.

Key terms: Llm class, external LLM, encapsulation, Llm, __init__, OpenAI client, API key, ask, construct_messages, user_msg

9.2.5 The Game class

This subsection introduces an initial Game backend class that owns game logic and asks an LLM, acting as a quizmaster, to generate a trivia question.

Key terms: Game class, backend logic, frontend, game state, Hub class, Game, Llm, __init__, API key, ask_llm_for_question

9.2.6 Calling the Game class in the app

This subsection wires the Game class into a minimal Streamlit frontend that securely calls the OpenAI API and displays a generated trivia question.

Key terms: Game, ask_llm_for_question, main.py, st.write, st.container, .streamlit, secrets.toml, OpenAI API key, streamlit run main.py

9.3 Using AI to evaluate answers

This section explains why evaluating trivia answers requires machine-readable LLM output rather than free-form text.

Key terms: LLM, evaluating answers, correct answer, parse, LLM response, machine-friendly, display, increment the player's score

9.3.1 Structured Outputs

This section uses OpenAI Structured Outputs and a Pydantic schema to evaluate trivia answers reliably, then exposes a Streamlit rerun bug that requires state management.

Key terms: Structured Outputs, AnswerEvaluation, is_correct, correct_answer, schema, pydantic, BaseModel, data validation, type hints, dataclass

9.4 Moving through game states

This section implements Fact Frenzy as a stateful sequential game that persists a Game instance across Streamlit reruns and advances through question, answer, evaluation, and result states.

Key terms: GET_QUESTION, ASK_QUESTION, EVALUATE_ANSWER, STATE_RESULT, game states, Game class, status attribute, obtain_question, accept_answer, curr_question

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

This section introduces the missing game mechanics that will turn Fact Frenzy from a single AI question-and-answer interaction into a game with scoring and defined start and end points.

Key terms: Fact Frenzy, score, New Game, Game Over

9.5.1 Keeping score

This section adds basic scoring, game-over detection, and a session-state-driven New Game interface to Fact Frenzy.

Key terms: score, evaluate_answer, num_questions_completed, max_questions, is_over, start_new_game, st.session_state.game, Game, st.rerun, new_game_button

9.6 Including multiple questions

This introduction frames the need to turn Fact Frenzy from a one-question demo into a multi-question game and previews an LLM-related challenge.

Key terms: Fact Frenzy, score system, game’s start and end, LLM-related challenge

9.6.1 Response variability, or lack thereof

This section explains why LLM trivia responses vary and how Fact Frenzy can increase question diversity while avoiding repeats within a game.

Key terms: deterministic, probabilistic, variability, LLM, tokens, probability distribution, temperature, top_p, cumulative probability, prompt

9.6.2 Implementing multiple questions

This section turns Fact Frenzy into a multi-question AI trivia game by varying prompts, cycling game state, using a callback-driven Next Question button, and considering LLM costs.

Key terms: Llm, ask, temperature, top_p, llm_args, dictionary unpacking, system prompt, quizmaster, seed, already_asked

9.7 Adding AI personalities

This section introduces adding selectable LLM-driven quizmaster personalities to make Fact Frenzy less mechanical and more engaging.

Key terms: LLM, GPT-5.1, quizmaster, personality, players, character

9.7.1 Adding game settings

This section adds editable quizmaster and difficulty settings to Fact Frenzy and passes their current values into the LLM question prompt.

Key terms: st.data_editor, default_settings, num_rows='fixed', key='settings_editor', reruns, st.column_config, SelectBoxColumn, required, CheckboxColumn, DatetimeColumn

9.8 Summary

This summary recaps how Fact Frenzy combines OpenAI LLM calls, Streamlit state and UI components, response control, and cost-conscious design into an AI-powered app.

Where this is used later

Concepts explained in this chapter

← Chapter 8: Building a CRUD app with Streamlit Chapter 10: RAG and agentic apps with LangGraph and Streamlit →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.