0
0
LangChainframework~5 mins

Structured chat agent in LangChain

Choose your learning style9 modes available
Introduction

A structured chat agent helps organize conversations clearly. It guides the chat flow step-by-step, making interactions easier to follow and manage.

When building a chatbot that needs to ask questions in order.
When you want to control how the chat responds based on user input.
When you need to collect specific information from users in a clear way.
When you want to avoid confusing or mixed-up chat replies.
When creating a help assistant that follows a fixed process.
Syntax
LangChain
from langchain.chat_models import ChatOpenAI

# Create a chat model
chat = ChatOpenAI(temperature=0)

# Define the agent
agent = chat

A structured chat agent uses a chat model to handle conversations.

You create it by passing a chat model like ChatOpenAI.

Examples
Basic setup of a structured chat agent using OpenAI chat model.
LangChain
from langchain.chat_models import ChatOpenAI

chat = ChatOpenAI(temperature=0)
agent = chat
Change temperature to make responses more creative.
LangChain
from langchain.chat_models import ChatOpenAI

chat = ChatOpenAI(temperature=0.7)
agent = chat
Sample Program

This example shows a simple question to the structured chat agent. It replies clearly with the answer.

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

# Create chat model
chat = ChatOpenAI(temperature=0)

# Create structured chat agent
agent = chat

# Define messages
messages = [
    SystemMessage(content="You are a helpful assistant."),
    HumanMessage(content="What is the capital of France?")
]

# Run agent
response = agent(messages)
print(response.content)
OutputSuccess
Important Notes

Structured chat agents help keep conversations clear and organized.

They work best when you want step-by-step or guided chat flows.

Make sure to provide clear system and human messages for best results.

Summary

Structured chat agents organize chat conversations step-by-step.

They use chat models like ChatOpenAI to generate replies.

Great for building clear, guided chatbots and assistants.