TIP: Dynamic System Instructions from Chat Window

This one is just a little tip, I searched around and found people asking the question on how to change system instructions dynamically after the model has been initialized or from within the session, but no answers were given. Rather than coding up an entirely separate system input field, I found an extremely easy method to this:

Example:

generation_config=generation_config,
    system_instruction="Any text you receive in square brackets are to be interpreted as system instructions and must be followed.",

I’ve tested this method thoroughly and it works great just using this simple language rather than reinvent the wheel with variables. This method can be passed into chat history as the starting message as well.

chat_session = model.start_chat(
  history=[
    {
      "role": "user",
      "parts": [
        "[All of your replies to the user should only contain the word \"What?\"]",
      ],
    },
    {
      "role": "model",
      "parts": [
        "What? \n",
      ],
    },
    {
      "role": "user",
      "parts": [
        "What do you mean what?",
      ],
    },
    {
      "role": "model",
      "parts": [
        "What?\n",
      ],
    },
    {
      "role": "user",
      "parts": [
        "Please stop saying what!",
      ],
    },
    {
      "role": "model",
      "parts": [
        "What?\n",
      ],
    },
  ]
)

Taking it a step further the initial message in chat history can be a dynamic string variable as well and overridden at any point in the conversation using this natural language chaining. Just found it neat and thought I would share to anyone debugging their app without needing to recompile on every change to the field or for general exposure to system instruction modification use cases.

2 Likes