customer-support-agent.mdx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. # Set the OpenAI API key
  18. os.environ['OPENAI_API_KEY'] = 'sk-xxx'
  19. class CustomerSupportAIAgent:
  20. def __init__(self):
  21. """
  22. Initialize the CustomerSupportAIAgent with memory configuration and OpenAI client.
  23. """
  24. config = {
  25. "vector_store": {
  26. "provider": "qdrant",
  27. "config": {
  28. "host": "localhost",
  29. "port": 6333,
  30. }
  31. },
  32. }
  33. self.memory = Memory.from_config(config)
  34. self.client = OpenAI()
  35. self.app_id = "customer-support"
  36. def handle_query(self, query, user_id=None):
  37. """
  38. Handle a customer query and store the relevant information in memory.
  39. :param query: The customer query to handle.
  40. :param user_id: Optional user ID to associate with the memory.
  41. """
  42. # Start a streaming chat completion request to the AI
  43. stream = self.client.chat.completions.create(
  44. model="gpt-4",
  45. stream=True,
  46. messages=[
  47. {"role": "system", "content": "You are a customer support AI agent."},
  48. {"role": "user", "content": query}
  49. ]
  50. )
  51. # Store the query in memory
  52. self.memory.add(query, user_id=user_id, metadata={"app_id": self.app_id})
  53. # Print the response from the AI in real-time
  54. for chunk in stream:
  55. if chunk.choices[0].delta.content is not None:
  56. print(chunk.choices[0].delta.content, end="")
  57. def get_memories(self, user_id=None):
  58. """
  59. Retrieve all memories associated with the given customer ID.
  60. :param user_id: Optional user ID to filter memories.
  61. :return: List of memories.
  62. """
  63. return self.memory.get_all(user_id=user_id)
  64. # Instantiate the CustomerSupportAIAgent
  65. support_agent = CustomerSupportAIAgent()
  66. # Define a customer ID
  67. customer_id = "jane_doe"
  68. # Handle a customer query
  69. support_agent.handle_query("I need help with my recent order. It hasn't arrived yet.", user_id=customer_id)
  70. ```
  71. ### Fetching Memories
  72. You can fetch all the memories at any point in time using the following code:
  73. ```python
  74. memories = support_agent.get_memories(user_id=customer_id)
  75. for m in memories:
  76. print(m['text'])
  77. ```
  78. ### Key Points
  79. - **Initialization**: The CustomerSupportAIAgent class is initialized with the necessary memory configuration and OpenAI client setup.
  80. - **Handling Queries**: The handle_query method sends a query to the AI and stores the relevant information in memory.
  81. - **Retrieving Memories**: The get_memories method fetches all stored memories associated with a customer.
  82. ### Conclusion
  83. As the conversation progresses, Mem0's memory automatically updates based on the interactions, providing a continuously improving personalized support experience.