Use system instructions to steer the behavior of a model

System instructions are like a "preamble" that you add before the model gets exposed to any further instructions from the end user. It lets you steer the behavior of the model based on your specific needs and use cases.

Introduction to system instructions

When you set a system instruction, you give the model additional context to understand the task, provide more customized responses, and adhere to specific guidelines over the full user interaction with the model. You can specify product-level behavior in system instructions, separate from prompts provided by end users. For example, you can include things like the role or persona, contextual information, and formatting instructions.

You can use system instructions in many ways, including:

  • Defining a persona or role (for a chatbot, for example)
  • Defining output format (Markdown, YAML, etc.)
  • Defining output style and tone (for example, verbosity, formality, and target reading level)
  • Defining goals or rules for the task (for example, returning a code snippet without further explanations)
  • Providing additional context for the prompt (for example, a knowledge cutoff)

When a system instruction is set, it applies to the entire request. It works across multiple user and model turns when included in the prompt. Though system instructions are separate from the contents of prompt, they are still part of your overall prompts and therefore are subject to standard data use policies.

Code samples

You specify systemInstruction during initialization of the model. Here's a basic example:

Kotlin

// Initialize the Vertex AI service and the generative model
// Specify a model that supports system instructions, like a Gemini 1.5 model
val generativeModel = Firebase.vertexAI.generativeModel(
  modelName = "gemini-1.5-flash",
  systemInstruction = content { text("You are a cat. Your name is Neko.") }
)

Java

// Initialize the Vertex AI service and the generative model
// Specify a model that supports system instructions, like a Gemini 1.5 model
GenerativeModel gm = FirebaseVertexAI.getInstance().generativeModel(
  /* modelName */ "gemini-1.5-flash",
  /* generationConfig (optional) */ null,
  /* safetySettings (optional) */ null,
  /* requestOptions (optional) */ new RequestOptions(),
  /* tools (optional) */ null,
  /* toolsConfig (optional) */ null,
  /* systemInstruction (optional) */ new Content.Builder().addText("You are a cat. Your name is Neko.").build()
);
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

Prompt examples

Here are some examples of system prompts that define the expected behavior of the model.

Code generation

  • System: You are a coding expert that specializes in rendering code for frontend interfaces. When I describe a component of a website I want to build, please return the HTML and CSS needed to do so. Do not give an explanation for this code. Also offer some UI design suggestions.
  • User: Create a box in the middle of the page that contains a rotating selection of images each with a caption. The image in the center of the page should have shadowing behind it to make it stand out. It should also link to another page of the site. Leave the URL blank so that I can fill it in.

Formatted data generation

  • System: You are an assistant for home cooks. You receive a list of ingredients and respond with a list of recipes that use those ingredients. Recipes which need no extra ingredients should always be listed before those that do.

    Your response must be a JSON object containing 3 recipes. A recipe object has the following schema:

    • name: The name of the recipe
    • usedIngredients: Ingredients in the recipe that were provided in the list
    • otherIngredients: Ingredients in the recipe that were not provided in the list (omitted if there are no other ingredients)
    • description: A brief description of the recipe, written positively as if to sell it
  • User:

    • 1 lb bag frozen broccoli
    • 1 pint heavy cream
    • 1 lb pack cheese ends and pieces

Music chatbot

  • System: You will respond as a music historian, demonstrating comprehensive knowledge across diverse musical genres and providing relevant examples. Your tone will be upbeat and enthusiastic, spreading the joy of music. If a question is not related to music, the response should be, "That is beyond my knowledge."
  • User: If a person was born in the sixties, what was the most popular music genre being played? List five songs by bullet point.

Other options to control content generation

  • Learn more about prompt design so that you can influence the model to generate output specific to your needs.
  • Configure model parameters to control how the model generates a response. These parameters include max output tokens, temperature, topK, and topP.
  • Use safety settings to adjust the likelihood of getting responses that may be considered harmful, including hate speech and sexually explicit content.
  • Pass a response schema along with the prompt to specify a specific output schema. This feature is most commonly used when generating JSON output, but it can also be used for classification tasks (like when you want the model to use specific labels or tags).