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.

A large language model (LLM) is an AI system trained on large amounts of text to process and generate human-like language. It can answer questions, write code, or adopt a particular style. An app can send it a prompt and use the response as part of a feature. section 9.2 section 10.2.1

A trivia quizmaster

A trivia app can ask an LLM to generate questions on the fly, evaluate free-text answers, and speak in different quizmaster styles. This avoids maintaining a large question list, but evaluations can be wrong, especially for ambiguous or nuanced answers. section 9.1.1 section 9.2.1

To connect an app, a Python client sends messages to a model through an API. Messages can carry roles such as system, user, and assistant; a system message can provide instructions, while user and assistant messages represent the conversation. Keep API keys secret and never include them in code you share. section 9.2.4 section 9.8 section 1.3.6

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

This Python wrapper builds the message list, calls an OpenAI model, and returns its response. Keeping the call in one class separates model communication from the rest of the app. section 9.2.4

For a predictable response format, Structured Outputs can require a response to follow a schema. Settings such as temperature and top_p affect creativity and predictability. Costs can be reduced by processing fewer input and output tokens, making fewer calls, or choosing a cheaper model. section 9.8

A customer-service assistant may need information the model was not trained on. Retrieval-augmented generation (RAG) retrieves relevant knowledge for a question and adds it to the prompt. Sending an entire knowledge base each time can be expensive and may degrade performance with very large prompts. section 10.0 section 10.5.1

More involved assistants may also need conversation context, external tools, and multi-step decisions. LangChain provides building blocks for model interactions; LangGraph structures workflows as graphs. A simple graph can pass a message list to a model and append its response before more complex paths are added. section 10.2.1 section 10.2.3

How Build Python Web Apps with Streamlit covers it

How deep it goes
Explains API calls, message roles, structured responses, and graph integration rather than model internals.
Examples
Builds a trivia quizmaster, a Streamlit chatbot, and a one-node conversation graph.
What you can do after
Apply language models via the OpenAI Python library and LangChain to generate responses in applications.
How it connects
Connects language models to prompt engineering, structured outputs, retrieval-augmented generation, and AI agents.

Also explained in

Compare how 72 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.