liveBook · Manning

Language model

As explained in Build Python Web Apps with Streamlit

An LLM is a large language model that generates or transforms text from input sequences.

Suppose a trivia game needs fresh questions and must judge answers typed in a player’s own words. A large language model (LLM) can handle both jobs: it processes text supplied to it and generates a text response. The same model can give the quizmaster a playful voice. LLMs are versatile, but they can make mistakes, especially when an answer is ambiguous. section 9.2 section 9.2.1

To use an LLM in a Python app, send it a prompt through a provider’s API. A chat request can contain messages labeled system, user, and assistant; the model responds with the next message. A system message can supply instructions, while user messages carry requests. Keeping the API call in a small class separates provider details from the rest of the app. section 9.2.4 section 9.8

Listing 9.1 Creating the Llm class in llm.py
from openai import OpenAI

class Llm:
  def __init__(self, api_key):
    self.client = OpenAI(api_key=api_key)

  @staticmethod
  def construct_messages(user_msg, sys_msg=None):
    messages = []
    if sys_msg:
      messages.append({"role": "system", "content": sys_msg})
    messages.append({"role": "user", "content": user_msg})
    return messages

  def ask(self, user_msg, sys_msg=None):
    messages = self.construct_messages(user_msg, sys_msg)
    completion = self.client.chat.completions.create(
      model="gpt-5.1",
      messages=messages
    )
    return completion.choices[0].message.content

The Llm class builds the message list, calls OpenAI’s chat completion API, and returns the response text. Supply the API key securely rather than putting it in shared code. section 9.2.4 section 1.3.6

Design the interaction around the result the app needs:

  • Use prompts to specify the task, such as generating a trivia question or evaluating a free-text answer. Structured Outputs can require a response that follows a parseable schema. section 9.1.1 section 9.8
  • Adjust temperature or top_p to affect creativity and predictability. Watch costs by reducing input and output tokens, calls, or model expense. section 9.8
  • Provide relevant private information when the model’s pre-trained knowledge is insufficient. Retrieval-augmented generation (RAG) selects useful context instead of sending an entire knowledge base with every question. section 10.5.1

From one response to a useful chatbot

A one-node LangGraph workflow passes a list of messages to an LLM and appends its reply. Streamlit chat elements can display the exchange. For customer support, a useful next step is retrieving company information and enabling actions such as tracking an order; a pre-trained model call alone does not supply that business context. section 10.2.3 section 10.0

How Build Python Web Apps with Streamlit covers it

How deep it goes
Defines text processing and generation, then works through API calls and application patterns without explaining model-training internals.
Examples
Uses a trivia game, a short Streamlit chatbot, and a one-node LangGraph chatbot.
What it assumes
Assumes Python app development and an API account and key; introduces the LLM’s role before showing calls.
What you can do after
Build it from scratch as a Python app feature that calls an LLM, handles its response, and can add retrieved context.
How it connects
Connects prompts and chat completions to Structured Outputs, Streamlit, LangGraph, and retrieval-augmented generation.

Also explained in

Compare how 67 books explain language model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.