Unpacking Chat Models with LangChain
Data Mastery Series — Episode 29: LangChain Website (Part 4)
Unpacking Chat Models with LangChain
Data Mastery Series — Episode 29: LangChain Website (Part 4)

Connect with me and follow our journey: Linkedin, Facebook
Hey everyone! Welcome back to the Data Mastery Series! We’re diving deeper into LangChain and exploring more about prompts. If you’re new here, feel free to catch up on our previous episodes:
- Part 1: LangChain Model I/O Basics
- Part 2: Unpacking Prompt Templates with LangChain
- Part 3: Exploring Few-Shot Prompts with LangChain
Note: As we dive into LangChain, I’ll be sharing insights and key notes from my own study of the LangChain documentation. Let’s jump in and explore some fascinating features in today’s episode!
Source: https://python.langchain.com/v0.1/docs/modules/model%5Fio/chat/
Today, we’re diving into Chat Models. Ever wondered how chatbots understand and respond to your questions? Let’s break it down in a way that’s easy for everyone to grasp.
Messages: The Building Blocks of Conversation
Chat models, such as OpenAI’s, handle conversations by processing Messages. In LangChain, there are three main message types:
- SystemMessage: This sets the scene. It gives the model instructions or context, like assigning it a personality or a specific role.
- HumanMessage: This is your input — the question or instruction you give to the model.
- AIMessage: This is the model’s response back to you.
Example - Messages
chat = ChatOpenAI(model="gpt-4", api_key=OPENAI_API_KEY)
messages = [
SystemMessage(content="You're a helpful assistant"),
HumanMessage(content="What is LangChain Expression Language (LCEL)?, in concise version, explain for non-technical person"),
]
(Note: This explanation focuses on OpenAI models.)
LangChain Expression Language (LCEL)
LangChain provides several ways to interact with these chat models:
- invoke: The standard Q&A. You send a message and get a response.
- stream: Get the response word by word, in real-time, like watching someone type. Great for long responses or showing progress.
- batch: Send multiple questions at once for efficiency.
There are also asynchronous versions of these ( ainvoke, astream, astream_log). These are like sending a text and continuing with your day while you wait for a reply. Your application doesn’t freeze while waiting.
1. Synchronous Methods: Waiting for a Reply
When using synchronous methods, your program waits for the model’s response before continuing. There are three main types:
A. **invoke**: Standard Q&A
Use invoke for a straightforward question-and-answer interaction. It’s perfect for simple tasks where you send a question and wait for a single response.
Example - invoke (Synchronous Methods 1)
Question: What is LangChain Expression Language (LCEL)?, in concise version, explain for non-technical person"
result = chat.invoke(messages)
print(result.content)
Output
'''
LangChain Expression Language (LCEL) is a tool used in the field of computer programming. It's designed to help programmers create or "express" certain actions or functions in a more simplified and efficient way. It's like a special language used by computers to process commands more easily. Think of it as a shortcut language that helps programmers communicate with computers more effectively.
'''
B. **stream**: Real-time Responses
stream is useful for longer responses, as it allows you to see the answer word-by-word, like watching someone type. Great for displaying progress!
Example - stream (Synchronous Methods 2)
Question: What is LangChain Expression Language (LCEL)?, in concise version, explain for non-technical person"
result = chat.invoke(messages)
print(result.content)
Output (printed word-by-word):
'''
LangChain Expression Language (LCEL) is essentially a tool or language designed to help you interact with the LangChain system. It's like a set of instructions or commands that you use to tell the system what to do. For example, if you want to translate text or work with languages in LangChain, you would use LCEL to tell the system exactly what you want it to do. It's designed to be simple and easy to use, even if you're not a technical person.
'''
C. **batch**: Multiple Questions at Once
Use batch to send several questions at once. This can make interactions more efficient when you have multiple queries.
Example - batch (Synchronous Methods 3)
Question: What is LangChain Expression Language (LCEL)?, in concise version, explain for non-technical person"
chat.batch([messages])
Output
'''
[AIMessage(content="LangChain Expression Language (LCEL) is a language used in computer programming, specifically designed for LangChain, a platform that aims to make language translation more efficient using blockchain technology. This language allows developers to write programs that can manipulate and translate different languages efficiently. For a non-technical person, you can think of it as a kind of 'grammar rules' that a computer can understand to help it translate languages more accurately.", additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 85, 'prompt_tokens': 37, 'total_tokens': 122, 'completion_tokens_details': {'audio_tokens': None, 'reasoning_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}}, 'model_name': 'gpt-4-0613', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-5dfe99db-1c72-46de-962b-a61f914e0e77-0', usage_metadata={'input_tokens': 37, 'output_tokens': 85, 'total_tokens': 122, 'input_token_details': {'cache_read': 0}, 'output_token_details': {'reasoning': 0}})]
'''
2. Asynchronous Methods (Multitasking)
Asynchronous methods let your program continue working on other tasks while waiting for the model’s response.
A. **ainvoke**: Non-Blocking Q&A
Example - ainvoke (Asynchronous Methods 1)
Question: What is LangChain Expression Language (LCEL)?, in concise version, explain for non-technical person"
result = await chat.ainvoke(messages)
print(result.content)
Output
'''
LangChain Expression Language (LCEL) is a simplified computer programming language that is used to create or define specific operations or tasks within the LangChain system. It's like giving instructions to the system on what to do, in a simplified way that is easier to understand and use, even for people who are not necessarily tech-savvy.
'''
B. **astream**: Asynchronous Streaming
Example - astream (Asynchronous Methods 2)
Question: What is LangChain Expression Language (LCEL)?, in concise version, explain for non-technical person"
async for chunk in chat.astream(messages):
print(chunk.content, end="", flush=True)
Output
'''
LangChain Expression Language (LCEL) is a tool that helps in automating tasks in LangChain, a language translation platform. Think of it like a set of simple instructions that tells the platform what to do with the text you want translated. It's designed to be easy to use, even for people without a technical background.
'''
C. **astream_log**: Streaming with Detailed Logs
Example - astream_log (Asynchronous Methods 3)
Question: What is LangChain Expression Language (LCEL)?, in concise version, explain for non-technical person"
async for chunk in chat.astream_log(messages):
print(chunk)
Output
'''
RunLogPatch({'op': 'replace',
'path': '',
'value': {'final_output': None,
'id': '99aecc01-393c-470d-b7ab-b04cf3d54f95',
'logs': {},
'name': 'ChatOpenAI',
'streamed_output': [],
'type': 'llm'}})
RunLogPatch({'op': 'add',
'path': '/streamed_output/-',
'value': AIMessageChunk(content='', additional_kwargs={}, response_metadata={}, id='run-99aecc01-393c-470d-b7ab-b04cf3d54f95')},
{'op': 'replace',
'path': '/final_output',
'value': AIMessageChunk(content='', additional_kwargs={}, response_metadata={}, id='run-99aecc01-393c-470d-b7ab-b04cf3d54f95')})
...
RunLogPatch({'op': 'add',
'path': '/streamed_output/-',
'value': AIMessageChunk(content='.', additional_kwargs={}, response_metadata={}, id='run-99aecc01-393c-470d-b7ab-b04cf3d54f95')},
{'op': 'replace',
'path': '/final_output',
'value': AIMessageChunk(content="LangChain Expression Language (LCEL) is a tool used in a program known as LangChain. It allows users to create rules and instructions for the program in a simplified manner. Imagine it like a translator, helping to convert your instructions into a language the program can understand easily. It's designed to be user-friendly so you don't need any advanced technical knowledge to use it.", additional_kwargs={}, response_metadata={}, id='run-99aecc01-393c-470d-b7ab-b04cf3d54f95')})
RunLogPatch({'op': 'add',
'path': '/streamed_output/-',
'value': AIMessageChunk(content='', additional_kwargs={}, response_metadata={'finish_reason': 'stop', 'model_name': 'gpt-4-0613'}, id='run-99aecc01-393c-470d-b7ab-b04cf3d54f95')},
{'op': 'replace',
'path': '/final_output',
'value': AIMessageChunk(content="LangChain Expression Language (LCEL) is a tool used in a program known as LangChain. It allows users to create rules and instructions for the program in a simplified manner. Imagine it like a translator, helping to convert your instructions into a language the program can understand easily. It's designed to be user-friendly so you don't need any advanced technical knowledge to use it.", additional_kwargs={}, response_metadata={'finish_reason': 'stop', 'model_name': 'gpt-4-0613'}, id='run-99aecc01-393c-470d-b7ab-b04cf3d54f95')})
'''
LangSmith: Keeping Track of Everything
LangSmith is an optional tool that logs details about your model calls, including inputs, outputs, and timings. It’s helpful for understanding and improving your application’s performance.
Legacy Methods: Older Approaches
LangChain also has some legacy methods, such as __call__ and generate. These provide additional ways to interact with chat models and handle multiple messages in one go.
**__call__**: Direct Function Call
Using __call__, you can interact with the model in a shorthand way, like calling a function.
Example - [Legacy] call
chat([HumanMessage(content="Translate this sentence from English to French: I love programming.")])
Output
'''
AIMessage(content="J'aime la programmation.", additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 7, 'prompt_tokens': 19, 'total_tokens': 26, 'completion_tokens_details': {'audio_tokens': None, 'reasoning_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}}, 'model_name': 'gpt-4-0613', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-2c5e0ec7-3093-4a4e-b163-b3d81848eab9-0', usage_metadata={'input_tokens': 19, 'output_tokens': 7, 'total_tokens': 26, 'input_token_details': {'cache_read': 0}, 'output_token_details': {'reasoning': 0}})
'''
For multiple messages as input.
Example - [Legacy] call
messages = [
SystemMessage(
content="You are a helpful assistant that translates English to French."
),
HumanMessage(content="I love programming."),
]
chat(messages)
Output
'''
AIMessage(content="J'aime la programmation.", additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 7, 'prompt_tokens': 26, 'total_tokens': 33, 'completion_tokens_details': {'audio_tokens': None, 'reasoning_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}}, 'model_name': 'gpt-4-0613', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-4bd6b9f7-a316-4c83-ac30-65e43cd57f30-0', usage_metadata={'input_tokens': 26, 'output_tokens': 7, 'total_tokens': 33, 'input_token_details': {'cache_read': 0}, 'output_token_details': {'reasoning': 0}})
'''
**generate**: Batched Responses with Extra Info
generate is similar to batch but provides more detailed information about each response.
Example - [Legacy] call
batch_messages = [
[
SystemMessage(
content="You are a helpful assistant that translates English to French."
),
HumanMessage(content="I love programming."),
],
[
SystemMessage(
content="You are a helpful assistant that translates English to French."
),
HumanMessage(content="I love artificial intelligence."),
],
]
result = chat.generate(batch_messages)
result
Output (Rearrange output for easy understanding)
'''
LLMResult(generations=[
[ChatGeneration(text="J'aime la programmation.", generation_info={'finish_reason': 'stop', 'logprobs': None}, message=AIMessage(content="J'aime la programmation.", additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 7, 'prompt_tokens': 26, 'total_tokens': 33, 'completion_tokens_details': {'audio_tokens': None, 'reasoning_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}}, 'model_name': 'gpt-4-0613', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-f80127d1-931e-4f58-83e8-4aee368a8626-0', usage_metadata={'input_tokens': 26, 'output_tokens': 7, 'total_tokens': 33, 'input_token_details': {'cache_read': 0}, 'output_token_details': {'reasoning': 0}}))],
[ChatGeneration(text="J'aime l'intelligence artificielle.", generation_info={'finish_reason': 'stop', 'logprobs': None}, message=AIMessage(content="J'aime l'intelligence artificielle.", additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 11, 'prompt_tokens': 27, 'total_tokens': 38, 'completion_tokens_details': {'audio_tokens': None, 'reasoning_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}}, 'model_name': 'gpt-4-0613', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-fce15dd3-5e55-45f3-b83c-6cdf751e1320-0', usage_metadata={'input_tokens': 27, 'output_tokens': 11, 'total_tokens': 38, 'input_token_details': {'cache_read': 0}, 'output_token_details': {'reasoning': 0}}))]], llm_output={'token_usage': {'completion_tokens': 18, 'prompt_tokens': 53, 'total_tokens': 71, 'completion_tokens_details': {'audio_tokens': None, 'reasoning_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}}, 'model_name': 'gpt-4'}, run=[RunInfo(run_id=UUID('f80127d1-931e-4f58-83e8-4aee368a8626')), RunInfo(run_id=UUID('fce15dd3-5e55-45f3-b83c-6cdf751e1320'))], type='LLMResult')
'''
That’s a quick look at Chat Models in LangChain! By understanding these methods, you can make your AI interactions more efficient and responsive. In the next episode, we’ll dive into more advanced ways to use LangChain, so stay tuned!
Data Science Explore the world of data science with Donato_Story
Dashboard Discover the power of data visualization with Donato_Story
Donato_Journey Join me on my journey (Thai version)
Course_Review Discover the training courses with Donato_Story (Thai version)
I really value your thoughts and feedback. Please share any comments or questions you have below, or reach out to me on:
Medium: medium.com/donato-story
Facebook: web.facebook.com/DonatoStory
Linkedin: linkedin.com/in/nattapong-thanngam
Originally published on Medium
Related
Chat with Document: A Closer Look at Splitting, Embeddings, and RAG
Data Mastery Series — Episode 21: The Chat with Document and Langchain Series (Part 2)
Chat with Document: Basics and Demonstrations
Data Mastery Series — Episode 20: The Chat with Document and Langchain Series (Part 1)
Continue Exploring Chat Models with LangChain
Data Mastery Series — Episode 31: LangChain Website (Part 6)
Continue Exploring Retrievers with LangChain
Data Mastery Series — Episode 39: LangChain Website (Part 14 )