repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | --- |
| #2 | title: LangGraph |
| #3 | --- |
| #4 | |
| #5 | 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. |
| #6 | |
| #7 | ## Overview |
| #8 | |
| #9 | In this guide, we'll create a Customer Support AI Agent that: |
| #10 | 1. Uses LangGraph to manage conversation flow |
| #11 | 2. Leverages Mem0 to store and retrieve relevant information from past interactions |
| #12 | 3. Provides personalized responses based on user history |
| #13 | |
| #14 | ## Setup and Configuration |
| #15 | |
| #16 | Install necessary libraries: |
| #17 | |
| #18 | ```bash |
| #19 | pip install langgraph langchain-openai mem0ai python-dotenv |
| #20 | ``` |
| #21 | |
| #22 | |
| #23 | Import required modules and set up configurations: |
| #24 | |
| #25 | <Note>Remember to get the Mem0 API key from [Mem0 Platform](https://app.mem0.ai).</Note> |
| #26 | |
| #27 | ```python |
| #28 | from typing import Annotated, TypedDict, List |
| #29 | from langgraph.graph import StateGraph, START |
| #30 | from langgraph.graph.message import add_messages |
| #31 | from langchain_openai import ChatOpenAI |
| #32 | from mem0 import MemoryClient |
| #33 | from langchain_core.messages import SystemMessage, HumanMessage, AIMessage |
| #34 | from dotenv import load_dotenv |
| #35 | |
| #36 | load_dotenv() |
| #37 | |
| #38 | # Configuration |
| #39 | # OPENAI_API_KEY = 'sk-xxx' # Replace with your actual OpenAI API key |
| #40 | # MEM0_API_KEY = 'your-mem0-key' # Replace with your actual Mem0 API key |
| #41 | |
| #42 | # Initialize LangChain and Mem0 |
| #43 | llm = ChatOpenAI(model="gpt-4") |
| #44 | mem0 = MemoryClient() |
| #45 | ``` |
| #46 | |
| #47 | ## Define State and Graph |
| #48 | |
| #49 | Set up the conversation state and LangGraph structure: |
| #50 | |
| #51 | ```python |
| #52 | class State(TypedDict): |
| #53 | messages: Annotated[List[HumanMessage | AIMessage], add_messages] |
| #54 | mem0_user_id: str |
| #55 | |
| #56 | graph = StateGraph(State) |
| #57 | ``` |
| #58 | |
| #59 | ## Create Chatbot Function |
| #60 | |
| #61 | Define the core logic for the Customer Support AI Agent: |
| #62 | |
| #63 | ```python |
| #64 | def chatbot(state: State): |
| #65 | messages = state["messages"] |
| #66 | user_id = state["mem0_user_id"] |
| #67 | |
| #68 | try: |
| #69 | # Retrieve relevant memories |
| #70 | memories = mem0.search(messages[-1].content, user_id=user_id) |
| #71 | |
| #72 | # Handle dict response format |
| #73 | memory_list = memories['results'] |
| #74 | |
| #75 | context = "Relevant information from previous conversations:\n" |
| #76 | for memory in memory_list: |
| #77 | context += f"- {memory['memory']}\n" |
| #78 | |
| #79 | 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. |
| #80 | {context}""") |
| #81 | |
| #82 | full_messages = [system_message] + messages |
| #83 | response = llm.invoke(full_messages) |
| #84 | |
| #85 | # Store the interaction in Mem0 |
| #86 | try: |
| #87 | interaction = [ |
| #88 | { |
| #89 | "role": "user", |
| #90 | "content": messages[-1].content |
| #91 | }, |
| #92 | { |
| #93 | "role": "assistant", |
| #94 | "content": response.content |
| #95 | } |
| #96 | ] |
| #97 | result = mem0.add(interaction, user_id=user_id) |
| #98 | print(f"Memory saved: {len(result.get('results', []))} memories added") |
| #99 | except Exception as e: |
| #100 | print(f"Error saving memory: {e}") |
| #101 | |
| #102 | return {"messages": [response]} |
| #103 | |
| #104 | except Exception as e: |
| #105 | print(f"Error in chatbot: {e}") |
| #106 | # Fallback response without memory context |
| #107 | response = llm.invoke(messages) |
| #108 | return {"messages": [response]} |
| #109 | ``` |
| #110 | |
| #111 | ## Set Up Graph Structure |
| #112 | |
| #113 | Configure the LangGraph with appropriate nodes and edges: |
| #114 | |
| #115 | ```python |
| #116 | graph.add_node("chatbot", chatbot) |
| #117 | graph.add_edge(START, "chatbot") |
| #118 | graph.add_edge("chatbot", "chatbot") |
| #119 | |
| #120 | compiled_graph = graph.compile() |
| #121 | ``` |
| #122 | |
| #123 | ## Create Conversation Runner |
| #124 | |
| #125 | Implement a function to manage the conversation flow: |
| #126 | |
| #127 | ```python |
| #128 | def run_conversation(user_input: str, mem0_user_id: str): |
| #129 | config = {"configurable": {"thread_id": mem0_user_id}} |
| #130 | state = {"messages": [HumanMessage(content=user_input)], "mem0_user_id": mem0_user_id} |
| #131 | |
| #132 | for event in compiled_graph.stream(state, config): |
| #133 | for value in event.values(): |
| #134 | if value.get("messages"): |
| #135 | print("Customer Support:", value["messages"][-1].content) |
| #136 | return |
| #137 | ``` |
| #138 | |
| #139 | ## Main Interaction Loop |
| #140 | |
| #141 | Set up the main program loop for user interaction: |
| #142 | |
| #143 | ```python |
| #144 | if __name__ == "__main__": |
| #145 | print("Welcome to Customer Support! How can I assist you today?") |
| #146 | mem0_user_id = "alice" # You can generate or retrieve this based on your user management system |
| #147 | while True: |
| #148 | user_input = input("You: ") |
| #149 | if user_input.lower() in ['quit', 'exit', 'bye']: |
| #150 | print("Customer Support: Thank you for contacting us. Have a great day!") |
| #151 | break |
| #152 | run_conversation(user_input, mem0_user_id) |
| #153 | ``` |
| #154 | |
| #155 | ## Key Features |
| #156 | |
| #157 | 1. **Memory Integration**: Uses Mem0 to store and retrieve relevant information from past interactions. |
| #158 | 2. **Personalization**: Provides context-aware responses based on user history. |
| #159 | 3. **Flexible Architecture**: LangGraph structure allows for easy expansion of the conversation flow. |
| #160 | 4. **Continuous Learning**: Each interaction is stored, improving future responses. |
| #161 | |
| #162 | ## Conclusion |
| #163 | |
| #164 | By integrating LangGraph with Mem0, you can build a personalized Customer Support AI Agent that can maintain context across interactions and provide personalized assistance. |
| #165 | |
| #166 | <CardGroup cols={2}> |
| #167 | <Card title="LangChain Integration" icon="link" href="/integrations/langchain"> |
| #168 | Build conversational agents with LangChain and Mem0 |
| #169 | </Card> |
| #170 | <Card title="CrewAI Integration" icon="users" href="/integrations/crewai"> |
| #171 | Create multi-agent systems with CrewAI |
| #172 | </Card> |
| #173 | </CardGroup> |
| #174 | |
| #175 |