Coding

Understanding GPT Chat API Roles and Conversation Format

Learn how to structure input messages for GPT-3.5 and GPT-4 chat models using the roles system, user, and assistant. This guide explains how to format conversations, include conversation history, and interpret API responses with examples in Python.

7 steps English

Prompt template

Run these steps in order.

01
Understand that GPT chat models require input as a series of messages, each with a role ('system', 'user', or 'assistant') and content.
02
The typical conversation starts with a 'system' message to set the assistant's behavior, followed by alternating 'user' and 'assistant' messages.
03
User messages represent instructions or queries from the end user or developer.
04
Assistant messages represent prior responses from the model or example outputs provided by the developer.
05
Including previous conversation messages is essential because the model does not retain memory between calls; relevant context must be included each time.
06
Be aware that conversation length is limited by the model's token limit; long histories may need trimming.
07
Review API responses, which include the assistant's reply under choices[0].message.content, and a finish_reason indicating if the response is complete or cut off.

Prompt library

Use these prompts directly inside ChatGPT.

Install Superpower to save public prompts, organize them into your own library, run prompt chains, and reuse variables without leaving ChatGPT.