customer-support-agent.mdx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. ---
  2. title: Customer Support AI Agent
  3. ---
  4. You can create a personalized Customer Support AI Agent using Mem0. This guide will walk you through the necessary steps and provide the complete code to get you started.
  5. ## Overview
  6. The Customer Support AI Agent leverages Mem0 to retain information across interactions, enabling a personalized and efficient support experience.
  7. ## Setup
  8. Install the necessary packages using pip:
  9. ```bash
  10. pip install openai mem0ai
  11. ```
  12. ## Full Code Example
  13. Below is the simplified code to create and interact with a Customer Support AI Agent using Mem0:
  14. ```python
  15. from openai import OpenAI
  16. from mem0 import Memory
  17. class CustomerSupportAIAgent:
  18. def __init__(self):
  19. """
  20. Initialize the CustomerSupportAIAgent with memory configuration and OpenAI client.
  21. """
  22. config = {
  23. "vector_store": {
  24. "provider": "qdrant",
  25. "config": {
  26. "host": "localhost",
  27. "port": 6333,
  28. }
  29. },
  30. }
  31. self.memory = Memory.from_config(config)
  32. self.client = OpenAI()
  33. self.app_id = "customer-support"
  34. def handle_query(self, query, user_id=None):
  35. """
  36. Handle a customer query and store the relevant information in memory.
  37. :param query: The customer query to handle.
  38. :param user_id: Optional user ID to associate with the memory.
  39. """
  40. # Start a streaming chat completion request to the AI
  41. stream = self.client.chat.completions.create(
  42. model="gpt-4",
  43. stream=True,
  44. messages=[
  45. {"role": "system", "content": "You are a customer support AI agent."},
  46. {"role": "user", "content": query}
  47. ]
  48. )
  49. # Store the query in memory
  50. self.memory.add(query, user_id=user_id, metadata={"app_id": self.app_id})
  51. # Print the response from the AI in real-time
  52. for chunk in stream:
  53. if chunk.choices[0].delta.content is not None:
  54. print(chunk.choices[0].delta.content, end="")
  55. def get_memories(self, user_id=None):
  56. """
  57. Retrieve all memories associated with the given customer ID.
  58. :param user_id: Optional user ID to filter memories.
  59. :return: List of memories.
  60. """
  61. return self.memory.get_all(user_id=user_id)
  62. # Instantiate the CustomerSupportAIAgent
  63. support_agent = CustomerSupportAIAgent()
  64. # Define a customer ID
  65. customer_id = "jane_doe"
  66. # Handle a customer query
  67. support_agent.handle_query("I need help with my recent order. It hasn't arrived yet.", user_id=customer_id)
  68. ```
  69. ### Fetching Memories
  70. You can fetch all the memories at any point in time using the following code:
  71. ```python
  72. memories = support_agent.get_memories(user_id=customer_id)
  73. for m in memories:
  74. print(m['text'])
  75. ```
  76. ### Key Points
  77. - **Initialization**: The CustomerSupportAIAgent class is initialized with the necessary memory configuration and OpenAI client setup.
  78. - **Handling Queries**: The handle_query method sends a query to the AI and stores the relevant information in memory.
  79. - **Retrieving Memories**: The get_memories method fetches all stored memories associated with a customer.
  80. ### Conclusion
  81. As the conversation progresses, Mem0's memory automatically updates based on the interactions, providing a continuously improving personalized support experience.