| --- |
| title: LangGraph |
| description: "Build customer support agents using LangGraph for conversation flow and Mem0 for personalized responses." |
| --- |
|
|
| Build a personalized Customer Support AI Agent using LangGraph for conversation flow and Mem0 for memory retention. This integration enables context-aware and efficient support experiences. |
|
|
| |
|
|
| In this guide, we'll create a Customer Support AI Agent that: |
| 1. Uses LangGraph to manage conversation flow |
| 2. Leverages Mem0 to store and retrieve relevant information from past interactions |
| 3. Provides personalized responses based on user history |
|
|
| |
|
|
| Install necessary libraries: |
|
|
| ```bash |
| pip install langgraph langchain-openai mem0ai python-dotenv |
| ``` |
|
|
|
|
| Import required modules and set up configurations: |
|
|
| <Note>Remember to get the Mem0 API key from [Mem0 Platform](https://app.mem0.ai).</Note> |
|
|
| ```python |
| from typing import Annotated, TypedDict, List |
| from langgraph.graph import StateGraph, START |
| from langgraph.graph.message import add_messages |
| from langchain_openai import ChatOpenAI |
| from mem0 import MemoryClient |
| from langchain_core.messages import SystemMessage, HumanMessage, AIMessage |
| from dotenv import load_dotenv |
|
|
| load_dotenv() |
|
|
| |
| |
| |
|
|
| |
| llm = ChatOpenAI(model="gpt-4") |
| mem0 = MemoryClient() |
| ``` |
|
|
| |
|
|
| Set up the conversation state and LangGraph structure: |
|
|
| ```python |
| class State(TypedDict): |
| messages: Annotated[List[HumanMessage | AIMessage], add_messages] |
| mem0_user_id: str |
|
|
| graph = StateGraph(State) |
| ``` |
|
|
| |
|
|
| Define the core logic for the Customer Support AI Agent: |
|
|
| ```python |
| def chatbot(state: State): |
| messages = state["messages"] |
| user_id = state["mem0_user_id"] |
|
|
| try: |
| |
| memories = mem0.search(messages[-1].content, user_id=user_id) |
| |
| |
| memory_list = memories['results'] |
|
|
| context = "Relevant information from previous conversations:\n" |
| for memory in memory_list: |
| context += f"- {memory['memory']}\n" |
|
|
| system_message = SystemMessage(content=f"""You are a helpful customer support assistant. Use the provided context to personalize your responses and remember user preferences and past interactions. |
| {context}""") |
|
|
| full_messages = [system_message] + messages |
| response = llm.invoke(full_messages) |
|
|
| |
| try: |
| interaction = [ |
| { |
| "role": "user", |
| "content": messages[-1].content |
| }, |
| { |
| "role": "assistant", |
| "content": response.content |
| } |
| ] |
| result = mem0.add(interaction, user_id=user_id) |
| print(f"Memory saved: {len(result.get('results', []))} memories added") |
| except Exception as e: |
| print(f"Error saving memory: {e}") |
| |
| return {"messages": [response]} |
| |
| except Exception as e: |
| print(f"Error in chatbot: {e}") |
| |
| response = llm.invoke(messages) |
| return {"messages": [response]} |
| ``` |
|
|
| |
|
|
| Configure the LangGraph with appropriate nodes and edges: |
|
|
| ```python |
| graph.add_node("chatbot", chatbot) |
| graph.add_edge(START, "chatbot") |
| graph.add_edge("chatbot", "chatbot") |
|
|
| compiled_graph = graph.compile() |
| ``` |
|
|
| |
|
|
| Implement a function to manage the conversation flow: |
|
|
| ```python |
| def run_conversation(user_input: str, mem0_user_id: str): |
| config = {"configurable": {"thread_id": mem0_user_id}} |
| state = {"messages": [HumanMessage(content=user_input)], "mem0_user_id": mem0_user_id} |
|
|
| for event in compiled_graph.stream(state, config): |
| for value in event.values(): |
| if value.get("messages"): |
| print("Customer Support:", value["messages"][-1].content) |
| return |
| ``` |
|
|
| |
|
|
| Set up the main program loop for user interaction: |
|
|
| ```python |
| if __name__ == "__main__": |
| print("Welcome to Customer Support! How can I assist you today?") |
| mem0_user_id = "alice" |
| while True: |
| user_input = input("You: ") |
| if user_input.lower() in ['quit', 'exit', 'bye']: |
| print("Customer Support: Thank you for contacting us. Have a great day!") |
| break |
| run_conversation(user_input, mem0_user_id) |
| ``` |
|
|
| |
|
|
| 1. **Memory Integration**: Uses Mem0 to store and retrieve relevant information from past interactions. |
| 2. **Personalization**: Provides context-aware responses based on user history. |
| 3. **Flexible Architecture**: LangGraph structure allows for easy expansion of the conversation flow. |
| 4. **Continuous Learning**: Each interaction is stored, improving future responses. |
|
|
| |
|
|
| By integrating LangGraph with Mem0, you can build a personalized Customer Support AI Agent that can maintain context across interactions and provide personalized assistance. |
|
|
| <CardGroup cols={2}> |
| <Card title="LangChain Integration" icon="link" href="/integrations/langchain"> |
| Build conversational agents with LangChain and Mem0 |
| </Card> |
| <Card title="CrewAI Integration" icon="users" href="/integrations/crewai"> |
| Create multi-agent systems with CrewAI |
| </Card> |
| </CardGroup> |
|
|
|
|