Building an AI Response Generator with LangChain, LangGraph and OpenAI
Today, we’re going to explore how we can leverage a simple AI Agent to streamline a process by building a Complaint Response Generator using Python, LangChain, LangGraph and OpenAI.
Traditionally, handling complaints involves manual review by customer service representatives, which can be time-consuming and prone to inconsistencies. This is where our AI-powered solution comes in.
Our Solution: An AI-Powered Complaint Response Generator
Our system combines the power of OpenAI’s language models with LangChain’s flexible framework to create a two-step process:
1. Review the customer’s complaint
2. Generate an appropriate response
This approach allows us to maintain a high level of understanding and context-awareness while producing tailored responses. Let’s dive into the technical details of how we built this system.
Technical Deep Dive
Setting Up the Project
We’ll use Poetry for dependency management. If you haven’t used Poetry before, it’s a great tool for managing Python projects. Here’s how to get started:
#Create a new project
poetry new complaint-generator
cd complaint-generator
# Add dependencies
poetry add langchain langchain-openai python-dotenv
echo “OPENAI_API_KEY=your_api_key_here” > .env
echo “OPENAI_MODEL=gpt-4” >> .env
Make sure to add `.env` to your `.gitignore` file to prevent accidentally committing your API key.
Building the Core Logic with LangChain
The heart of our system lies in the `chains.py` file. Here, we define two main components:
1. A review prompt that analyzes the complaint
2. A response prompt that generates an appropriate reply
Here’s a simplified version of our `chains.py`:
import os
from dotenv import load_dotenv
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_openai import ChatOpenAI
load_dotenv()
llm = ChatOpenAI(model=openai_model, api_key=openai_api_key)
review_prompt = ChatPromptTemplate.from_messages([
("system", "You are a company representative reviewing a customer's complaint. "
"Analyze the complaint details and provide a thorough assessment."),
MessagesPlaceholder(variable_name="messages"),
])
response_prompt = ChatPromptTemplate.from_messages([
("system", "You are a company AI assistant tasked with drafting responses to customer complaint. "
"Generate a professional and empathetic response to the customer's complaint."),
MessagesPlaceholder(variable_name="messages"),
])
generate_response_chain = response_prompt | llm
review_complaint_chain = review_prompt | llm
Creating the Processing Graph
In our `main.py`, we set up a processing graph that orchestrates the flow of our complaint handling:
from typing import List, Sequence
from langchain_core.messages import BaseMessage, HumanMessage
from langgraph.graph import END, MessageGraph
from chains import generate_response_chain, review_complaint_chain
def response_node(state: Sequence[BaseMessage]):
return generate_response_chain.invoke({"messages": state})
def review_node(messages: Sequence[BaseMessage]) -> List[BaseMessage]:
res = review_complaint_chain.invoke({"messages": messages})
return [HumanMessage(content=res.content)]
builder = MessageGraph()
builder.add_node("RESPOND", response_node)
builder.add_node("REVIEW", review_node)
builder.set_entry_point("REVIEW")
def should_continue(state: List[BaseMessage]):
if len(state) > 4:
return END
return "RESPOND"
builder.add_conditional_edges("REVIEW", should_continue)
builder.add_edge("RESPOND", "REVIEW")
graph = builder.compile()`
To run the system, we can use a simple script:
if __name__ == “__main__”:
print(“Complaint Response Generator”)
inputs = HumanMessage(content=”””
Customer Complaint:
I don’t recognize a charge of $299.99 for ‘TechGadget’ on my statement dated July 15, 2023.
I’ve never bought this product and I believe this charge is fraudulent.
Please investigate and remove this charge from my account.
“””)
response = graph.invoke(inputs)
print(response)
This will process the complaint and generate an appropriate response.
Potential Improvements and Extensions
While our current system provides a solid foundation, there are several ways we could enhance it:
1. Adding more sophisticated logic for different complaint categories
2. Implementing a user interface for easier interaction
3. Incorporating a feedback loop to continually improve responses
Ethical Considerations and Limitations
As with any AI system, it’s crucial to consider the ethical implications:
1. Human oversight is essential. AI-generated responses should be reviewed by human operators before being sent to customers.
2. Regular auditing of the system’s outputs is necessary to identify and correct any biases.