Welcome! Today, we’ll explore a key concept for building effective AI agents: system prompts. If you’ve noticed how AI assistants can sound friendly in one case and formal in another, it’s often due to system prompts.
By the end of this lesson, you’ll know what system prompts are, how they shape AI behavior, and how to use them to control your AI’s responses. This is essential for building AI agents that adapt to different user needs and situations.
Let’s start with the basics. In chat-based AI models, a system prompt is a special instruction given to the AI before any user input. Think of it as setting the AI’s “role” before the conversation.
For example, to make your AI friendly:
"You are a helpful and friendly assistant. Answer clearly and politely."
For a formal tone:
"You are a formal and professional assistant. Provide concise and accurate responses."
The system prompt is different from the user prompt. The user prompt is the actual question; the system prompt is a behind-the-scenes instruction guiding the AI’s style and behavior.
Why are system prompts important? Imagine you’re an actor. The director says, “Be cheerful and supportive,” or “Be serious and precise.” These instructions shape your performance.
Similarly, the system prompt tells the AI how to behave. It can affect tone (friendly, formal, etc.), level of detail, language style (simple, technical, etc.), and the assistant’s persona.
For example, if you ask, “How do I bake a chocolate cake?”:
- With a friendly prompt:
Sure! Baking a chocolate cake is fun and easy. Here’s a simple recipe you’ll love... - With a formal prompt:
To bake a chocolate cake, follow these steps: ...
This flexibility is crucial in customer support, education, or professional services, where the right tone matters.
Let’s see how to use system prompts in practice. With the OpenAI API, you send a list of messages, each with a role: system, user, or assistant.
Here’s a basic example:
The first message is the system prompt; the second is the user’s question.
To make this flexible, use a function to build messages with different system prompts:
This lets you choose the assistant’s style by changing the style parameter. For example, build_messages("How do I bake a chocolate cake?", style="formal") creates a formal prompt.
Let’s see how different system prompts affect responses. Here’s a complete example using the OpenAI API:
This code asks the same question with two different system prompts. The AI’s answers will match the style set by the prompt. This is useful when your app needs to switch between a casual chatbot and a professional assistant.
In this lesson, you learned what system prompts are and how they shape the behavior and style of AI agents. You saw how to build message lists with system prompts and how changing the prompt changes the AI’s replies.
Now, you’ll move on to hands-on practice. You’ll create your own system prompts and see how they influence the AI’s responses, helping you design AI agents that fit your needs.
