AI Hints
Home›LangChain Tutorial›Models and Messages
LangChain · Lesson 5 of 120

Messages

System, human, AI.

⏱ 2 min read▶ Video at 4:12🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

Chat models don't just take one string. They take a list of messages, each with a role. The role tells the model who said what.

Messages
From the video: Messages
  • A list of messages
  • Each with a role
  • Who said what

Three roles

There are three main roles. A system message sets the rules and personality, like, you are a friendly bookshop assistant. A human message is what the user says. And an AI message is what the model said before. Later, we'll meet a fourth, the tool message.

RoleClassPurpose
systemSystemMessagerules and personality
humanHumanMessagewhat the user says
aiAIMessagewhat the model said
toolToolMessagea tool's result (later)

A system message

Here we send two messages. The system message tells the model to answer like a pirate, in under fifteen words. The human message asks a simple question. Run it. The answer follows the rules from the system message. That's how you give an assistant its personality.

roles.pypython
from langchain.chat_models import init_chat_model
from langchain.messages import SystemMessage, HumanMessage

model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)

messages = [
    SystemMessage("You are a pirate. Answer in under 15 words."),
    HumanMessage("What is a library?"),
]
print(model.invoke(messages).text)
Output
Ahoy! A treasure chest of stolen scrolls and silent maps, ye scallywag!

Summary

Messages carry roles. System sets the rules, human is the user, and AI is the model. Next, using a list of messages to hold a whole conversation.

Key takeaways
  • SystemMessage: rules
  • HumanMessage: user
  • AIMessage: model
Next up: Conversations — a list of messages