Prompting with LangChain
LangChain standardise messages for various integrations by using three types of messages:
SystemMessage
: Provide context and guidance for the conversationHumanMessages
: Represent prompts or questions from userAIMessages
: Represent response from language models
Messages are commonly stored in a list then pass to the Large Language Model (LLM).
Using Messages Abstraction
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
messages = [SystemMessage("You are a friendly assistant"),
HumanMessage("What is LangChain?")]
In the example above, we use SystemMessage
to set the LLM tone and HumanMessage
as a prompt to the LLM.
Using LLM Integrations in LangChain
LangChain also tries to standardise the way we instantiate LLMs which typically follow:
from langchain_{company_name} import Chat{company_name}
llm = Chat{company_name}(..."pass in model + api_key if not in environment"...)
OR
from langchain_community.{provider_name} import Chat{model_name}
llm = Chat{model_name}(..."pass in model + api_key if not in environment"...)
It also implements these standard methods:
.invoke()
: takes a list / coerce messages into a list to be used as input to LLM.stream()
: stream the output as LLM are generating the content.batch()
: perform multiple requests to the LLM at the same time.bind_tools()
: bind tools to your model for tool calling.with_structured_output()
: set rules to get an expected output
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
messages = [SystemMessage("You are a friendly assistant"),
HumanMessage("What is LangChain?")]
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model = 'gpt-4o-mini', temperature = 0)
response = llm.invoke(messages)
print(response)
With the example above, we have learnt how to connect with different providers and perform basic prompting like how we can do in their respective native APIs.
Hands on: Prompt Adventure
Use the LangChain messages abstraction and LLM integration to ask a question related to culture in your country:
- Your system message should state that the LLM is an expert in culture
- Your human message should ask about a local festival in your country
Solution
from langchain_core.messages import SystemMessage, HumanMessage
from langchain_openai import ChatOpenAI
messages = [
SystemMessage("You are a Singaporean which is expert in all cultures and heritage here.")
HumanMessage("Can you explain more on Chingay?")
]
llm = ChatOpenAI(model = 'gpt-4o-mini', temperature = 0)
response = llm.invoke(messages)
print(response)
The message you would get back after invoking is an AIMessage
. With this knowledge, create an interactive chatbot by extending your code above:
Extension
from langchain_core.messages import SystemMessage, HumanMessage
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model = 'gpt-4o-mini', temperature = 0)
messages = [
SystemMessage("You are a Singaporean which is expert in all cultures and heritage here."),
]
while True:
question = str(input("Enter your question here: (Enter q / Q to quit) "))
if question.lower() == 'q':
print("Thanks for Prompting!")
break
messages.append(HumanMessage(question))
response = llm.invoke(messages)
messages.append(response)
print(response.content)