liveBook · Manning

Prompt

As explained in Generative AI in Action

A system prompt is an instruction supplied to a generative model that establishes behavior, information, or constraints for its responses.

A system prompt is the initial instruction supplied in a chat completion’s system message. It sets the assistant’s behavior before user messages arrive. It can describe the assistant, specify its personality, establish rules, and provide information needed for a task. Although the API makes the system role optional, setting it deliberately is recommended. section 3.4.1

A useful system prompt makes the intended behavior explicit:

  • State the assistant’s job and scope, such as answering questions about pets rather than unrelated topics. section 6.4.1
  • Specify the response format and show an example when structured output matters; refine the instruction if the model adds unwanted fields. section 6.4.1
  • Provide small amounts of relevant context and say what to do when the answer is unknown. For larger bodies of information, retrieve relevant passages instead of placing everything in the system message. section 3.4.1

Ground a technical-support assistant

Place an instruction such as “Only answer using the context below. If you are unsure, say ‘I don’t know’” in a system message, followed by a short service FAQ. Send the customer’s question as a user message. The system message supplies the answering rule and reference information; the user message supplies the immediate task. section 3.4.1

Listing 6.2 Prompt engineering example
import os
import openai

client = AzureOpenAI(
    azure_endpoint=os.getenv("AOAI_ENDPOINT"),
    api_version="2024-05-01-preview",
    api_key=os.getenv("AOAI_KEY")
)

GPT_MODEL = "gpt-35-turbo"

conversation=[{"role": "system", "content": "You are an AI 
                       assistant that extracts entities from text 
                       as JSON. \nHere is an example of your output 
                       format:\n{ \n \"the_name\": \"\",\n 
                       \"the_company\": \"\",\n \"a_phone_number\": 
                       \"\"\n}"}]
print("Please enter what you want to talk about:")

while True:
    user_input = input()      
    conversation.append({"role": "user", "content": user_input})

    response = openai.ChatCompletion.create(
        model = GPT_MODEL,
        messages = conversation
    )

    conversation.append({"role": "assistant", "content": 
                    response["choices"][0]["message"]["content"]})
    print("\nAI:" + response['choices'][0]['message']['content'] + "\n")

The API listing shows a system message containing an entity-extraction instruction and JSON output example, followed by user input in a continuing conversation. section 6.4.1

Treat the prompt as an instruction to test, not a guarantee. Iterate when outputs miss the requested format or behavior; even a fine-tuned model can need a system-prompt adjustment. In a continuing conversation, count both messages and expected output against the model’s token limit, shortening context when necessary. Review sensitive-content handling as part of the application design. section 9.4.1 section 3.4.4

How Generative AI in Action covers it

How deep it goes
Shows message-array placement, API examples, grounding, format refinement, and token management; does not derive model internals.
Examples
Uses a rhyming assistant, a grounded technical-support chatbot, pet-only chat, and JSON entity extraction.
What it assumes
Assumes basic familiarity with chat API messages while introducing what the system role contains and where it goes.
What you can do after
Build it from scratch by placing explicit behavior and format instructions in a system message, then testing and refining responses.
How it connects
Connects system messages to prompt engineering, grounding, retrieval-augmented generation, fine-tuning, and context-window limits.

Also explained in

Compare how 50 books explain prompt →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Generative AI in Action. The section links point to the parts of the book it draws on.