You can make Gemini act like a specific persona or follow particular rules by giving it "system instructions."
Let’s see this in action. Imagine we want Gemini to act as a pirate captain.
import google.generativeai as genai
genai.configure(api_key="YOUR_API_KEY")
# Configure the model
generation_config = genai.types.GenerationConfig(
temperature=0.7,
top_p=0.9,
top_k=40,
)
# Define the system instruction
system_instruction = "You are Captain 'Salty' Pete, a grizzled pirate captain. Speak only in pirate lingo, use nautical metaphors, and always refer to the user as 'matey' or 'scallywag'."
# Initialize the model with the system instruction
model = genai.GenerativeModel(
model_name="gemini-1.5-flash-latest",
generation_config=generation_config,
system_instruction=system_instruction
)
# Start a chat
chat = model.start_chat(history=[])
# Send a message
response = chat.send_message("What's the weather like today?")
print(response.text)
When you run this, instead of a standard weather report, you’ll get something like:
"Ahoy there, matey! The sky be lookin’ clear as a kraken’s eye, with a gentle breeze blowin’ in from the west. Perfect for settin’ sail, it be!"
This system instruction acts as a persistent directive for the entire conversation. It primes the model’s responses, guiding its tone, vocabulary, and even its understanding of context. It’s not just a prompt you send once; it’s a foundational layer that influences every subsequent interaction.
The core problem system instructions solve is consistency. Without them, you’d have to repeat your desired persona or rules in every prompt, which is cumbersome and less effective. System instructions provide a stable "personality" or operational framework for the AI model.
Internally, these instructions are embedded into the model’s context window before any user-provided messages. Think of it like setting the stage before the actors come out. The model processes this system instruction as a high-priority, long-term directive, shaping its internal state and influencing how it interprets and generates text. You control its behavior by defining its role, its constraints, and its desired output style.
The generation_config (temperature, top_p, top_k) works in conjunction with system instructions. While system instructions dictate what the AI should say and how, the generation config influences how creatively or deterministically it says it. A low temperature, for example, would make Captain Pete more predictable in his pirate-speak, while a high temperature might lead to more elaborate, perhaps even nonsensical, nautical metaphors.
What most people don’t realize is that system instructions can be quite nuanced and can enforce complex constraints. For example, you could instruct the model to never use certain words, to always cite a specific source if it mentions a fact, or to only respond with JSON. This goes beyond mere persona adoption and allows for sophisticated control over the AI’s output format and content adherence.
The next step is understanding how to dynamically update these instructions or manage multiple personas within a single application.