Harish Kumar

Software Engineer | Go | Python | AI/ML

Langchain Prompt Messages and Prompt Templates

Prompt Messages

LangChain has three types of very important messages.

  • System Message
  • Human Message
  • AI Message

The prompt to the LLM is a set of instructions which includes the SystemMessage and HumanMessage.

System Message is a behavior or role for the LLM.

Human Message are the queries that humans ask and which are appended to the prompt.

AI Message is the response to the prompt from the model.

We can use a chat model as gemini-pro from Google and prompt messages to it from Google colab. Example below;

The Google colab notebook is hosted in Github.

systemMessage = "You are a Math Tutor"
humanMessage = "Explain the concept of Trigonometry"
mesaages = [
    SystemMessage(content=systemMessage),
    HumanMessage(content=humanMessage)
]

Prompt Templates

While Prompt Messages are very much helpful, there is a option of using Prompt Templates which are very much flexible. We can use the Prompt Templates for System and Human Messages along with Prompting techniques such as zero shot, one shot and few shot templates.

systemMessage = "You are a {subject} Tutor"
humanMessage = "Explain the concept of {concept}"

prompt_template = ChatPromptTemplate.from_messages(
    [("system", systemMessage),
     ("human", humanMessage),
     ])

Langchain Key libraries.

Langchain is evolving and as of now, below are the core modules and key libraries.

  • langchain-core: Base abstractions and LangChain Expression Language. It has messages, prompts and tools.
  • langchain-community: Third party integrations with LLM providers
  • langchain: It contains the chains, agents, and retrieval strategies, and tools, formatters and output parsers.