0
0
LangchainHow-ToBeginner ยท 3 min read

How to Use System Message in Langchain for Prompt Control

In Langchain, use SystemMessage to set instructions or context for the AI before user input. Pass it as part of the messages list to the chat model to control the assistant's behavior and tone.
๐Ÿ“

Syntax

The SystemMessage is used to provide initial instructions or context to the AI model. It is passed as part of a list of messages to the chat model. The typical pattern is:

  • SystemMessage(content='your instructions'): Defines the system message text.
  • HumanMessage(content='user input'): Defines the user's input message.
  • chat(messages=[SystemMessage, HumanMessage]): Sends messages to the chat model.
python
from langchain.schema import SystemMessage, HumanMessage
from langchain.chat_models import ChatOpenAI

chat = ChatOpenAI()

messages = [
    SystemMessage(content="You are a helpful assistant."),
    HumanMessage(content="Hello, who won the world series in 2020?")
]

response = chat(messages=messages)
print(response.content)
๐Ÿ’ป

Example

This example shows how to use a system message to instruct the AI to respond in a friendly tone. The system message sets the behavior before the user question.

python
from langchain.schema import SystemMessage, HumanMessage
from langchain.chat_models import ChatOpenAI

chat = ChatOpenAI(temperature=0)

messages = [
    SystemMessage(content="You are a friendly assistant who speaks simply."),
    HumanMessage(content="Can you explain photosynthesis?")
]

response = chat(messages=messages)
print(response.content)
Output
Photosynthesis is the process plants use to turn sunlight into food. They take in sunlight, water, and carbon dioxide to make energy and oxygen.
โš ๏ธ

Common Pitfalls

Common mistakes when using system messages include:

  • Not including a SystemMessage at all, which means no guidance for the AI.
  • Placing the system message after user messages, which is ignored by the model.
  • Using unclear or conflicting instructions in the system message.

Always place SystemMessage first in the messages list to ensure it sets the context.

python
from langchain.schema import SystemMessage, HumanMessage
from langchain.chat_models import ChatOpenAI

chat = ChatOpenAI()

# Wrong: System message after user message (ignored)
messages_wrong = [
    HumanMessage(content="Hello!"),
    SystemMessage(content="You are a helpful assistant.")
]

# Right: System message first
messages_right = [
    SystemMessage(content="You are a helpful assistant."),
    HumanMessage(content="Hello!")
]

response_wrong = chat(messages=messages_wrong)
response_right = chat(messages=messages_right)

print("Wrong order response:", response_wrong.content)
print("Right order response:", response_right.content)
Output
Wrong order response: Sorry, I didn't understand that. Right order response: Hello! How can I help you today?
๐Ÿ“Š

Quick Reference

Tips for using system messages in Langchain:

  • Always start your messages list with a SystemMessage.
  • Use clear, concise instructions to guide AI behavior.
  • Combine with HumanMessage for user input and AIMessage for expected AI responses if needed.
  • Adjust model parameters like temperature to control creativity.
โœ…

Key Takeaways

Use SystemMessage first in the messages list to set AI instructions.
SystemMessage controls the assistant's tone, style, and behavior.
Place SystemMessage before any HumanMessage to ensure it is applied.
Clear and simple system messages lead to better AI responses.
Combine system messages with model settings like temperature for best results.