Coding

Understanding GPT Chat API Roles and Conversation Format

Learn how to use the GPT chat API by formatting input as a series of messages with roles ('system', 'user', 'assistant'). Understand how to structure conversations, provide context, and interpret responses, including handling token limits and finish reasons.

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
Start conversations with a 'system' message to set the assistant's behavior, followed by alternating 'user' and 'assistant' messages.
03
Use 'user' messages to provide instructions or queries from end users or developers.
04
Use 'assistant' messages to include prior model responses or developer-provided example outputs.
05
Include previous conversation messages in each API call since models do not retain memory between calls; provide all relevant context each time.
06
Be aware of the model's token limit; shorten conversation history if necessary to fit within token limits.
07
Interpret API responses by extracting the assistant's reply from choices[0].message.content and check the finish_reason to determine if the output is complete or truncated.

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.