Setting System Prompts & Personas
Have you ever wanted your AI to act like a specific character, a strict professional, or an expert in a certain field? In Ollama, you can completely change how a model behaves by passing a System Instruction (also known as a System Prompt).
It is a set of "invisible rules" given to the AI before it even looks at your actual question. It defines the AI's personality, boundaries, and formatting rules.
๐จ๐ฌ Example 1: The Funny Science Teacher
Let's say we want to ask the model a basic science question, but we want the answer to be highly engaging and humorous for young students. We can achieve this by passing our rules into the system parameter.
import ollama
# 1. Define the AI's personality and rules
system_instruction = "You are a funny science teacher. Generate the answers in a funny way so that students can understand them easily."
# 2. Pass the instruction using the 'system' parameter
response = ollama.generate(
model="llama3.2:latest",
prompt="Why is the ocean blue?",
system=system_instruction
)
print(response['response'])
๐ Expected Output
๐ป Example 2: The Strict Code Reviewer
System prompts aren't just for fun. You can use them to strictly control the output format. What if you only want code, with absolutely no conversational filler (like "Here is your code...")?
import ollama
# Force the AI to be a strict coding machine
strict_coder_prompt = "You are a Senior Python Developer. ONLY output raw Python code. Do not include any explanations, markdown formatting, or conversational text."
response = ollama.generate(
model="llama3.2:latest",
prompt="Write a function to add two numbers.",
system=strict_coder_prompt
)
print(response['response'])
๐ Expected Output
Notice: Because of the system instruction, the AI skipped the polite "Sure, I can help with that!" and gave us exactly what we needed for an automated pipeline.
๐ Conclusion
Using the system parameter in ollama.generate() is a lightweight and powerful way to override a model's default behavior. Whether you need a sarcastic chatbot, a strict JSON generator, or a language translator, system prompts are your best tool!