DEV Community

James Li
James Li

Posted on

Building an Intelligent Customer Service Agent System from Scratch

System Architecture Overview

Image description

1. Multi-turn Dialogue Management Design

Multi-turn dialogue management is the core of an intelligent customer service system. Good dialogue management enables the system to "remember" context and provide coherent conversation experience.

from typing import Dict, List, Optional
from dataclasses import dataclass
from datetime import datetime

@dataclass
class DialogueContext:
    session_id: str
    user_id: str
    start_time: datetime
    last_update: datetime
    conversation_history: List[Dict]
    current_intent: Optional[str] = None
    entities: Dict = None
    sentiment: float = 0.0

class DialogueManager:
    def __init__(self, llm_service, knowledge_base):
        self.llm = llm_service
        self.kb = knowledge_base
        self.sessions: Dict[str, DialogueContext] = {}

    async def handle_message(self, session_id: str, message: str) -> str:
        """Handle user message"""
        # Get or create session context
        context = self._get_or_create_session(session_id)

        # Update conversation history
        context.conversation_history.append({
            "role": "user",
            "content": message,
            "timestamp": datetime.now()
        })

        # Intent recognition
        intent = await self._identify_intent(message, context)
        context.current_intent = intent

        # Entity extraction
        entities = await self._extract_entities(message, context)
        context.entities.update(entities)

        # Sentiment analysis
        sentiment = await self._analyze_sentiment(message)
        context.sentiment = sentiment

        # Generate response
        response = await self._generate_response(context)

        # Update conversation history
        context.conversation_history.append({
            "role": "assistant",
            "content": response,
            "timestamp": datetime.now()
        })

        return response

    async def _identify_intent(self, message: str, context: DialogueContext) -> str:
        """Intent recognition"""
        prompt = f"""
        Conversation History: {context.conversation_history[-3:]}
        Current User Message: {message}

        Please identify user intent from the following options:
        - inquiry_product: Product inquiry
        - technical_support: Technical support
        - complaint: Complaint
        - general_chat: General chat
        - other: Other

        Return intent identifier only.
        """
        return await self.llm.generate(prompt)
Enter fullscreen mode Exit fullscreen mode

💡 Best Practices

  1. Keep only the most recent 3-5 rounds of dialogue history to provide sufficient context while avoiding long prompts
  2. Cache entity extraction results to improve system response time
  3. Use sentiment analysis results to dynamically adjust response strategies
  4. Regularly clean up expired sessions to optimize memory usage

⚠️ Common Pitfalls

  1. Over-reliance on historical context may cause conversation drift
  2. Overly strict entity extraction rules may miss important information
  3. Sentiment analysis should not overly influence system professionalism
  4. Session state management needs to consider concurrency safety

2. Knowledge Base Integration

Knowledge base is the "brain" of an intelligent customer service system. Efficient knowledge retrieval and management directly affects response quality. Here we implement a vector database-based knowledge system.

from typing import List, Tuple
import faiss
import numpy as np

class KnowledgeBase:
    def __init__(self, embedding_model):
        self.embedding_model = embedding_model
        self.index = faiss.IndexFlatL2(384)  # vector dimension
        self.documents = []

    async def add_document(self, document: str):
        """Add document to knowledge base"""
        # Document chunking
        chunks = self._split_document(document)

        # Generate vector embeddings
        embeddings = await self._generate_embeddings(chunks)

        # Add to index
        self.index.add(embeddings)
        self.documents.extend(chunks)

    async def search(self, query: str, top_k: int = 3) -> List[Tuple[str, float]]:
        """Search related documents"""
        # Generate query vector
        query_embedding = await self._generate_embeddings([query])

        # Perform vector search
        distances, indices = self.index.search(query_embedding, top_k)

        # Return results
        results = [
            (self.documents[idx], float(distance))
            for idx, distance in zip(indices[0], distances[0])
        ]

        return results

    def _split_document(self, document: str) -> List[str]:
        """Document chunking strategy"""
        # Implement document chunking logic
        chunks = []
        # ... chunking logic ...
        return chunks
Enter fullscreen mode Exit fullscreen mode

💡 Optimization Tips

  1. Consider semantic integrity when chunking documents, avoid mechanical word count splitting
  2. Use algorithms like IVF or HNSW to improve retrieval efficiency
  3. Implement periodic index rebuilding mechanism to optimize vector distribution
  4. Consider introducing document version control to support knowledge updates and rollbacks

🔧 Performance Tuning

  1. Generate vector embeddings in batch to reduce model calls
  2. Use async operations for I/O intensive tasks
  3. Implement smart caching strategy for hot knowledge access
  4. Regular cleanup of expired cache and documents

⚠️ Important Notes

  1. Vector dimensions must match model output
  2. Consider sharded storage for large-scale knowledge bases
  3. Regular knowledge base data backup
  4. Monitor index quality and retrieval performance

3. Emotion Recognition and Processing

Accurate emotion recognition and appropriate emotional handling are key differentiating capabilities of an intelligent customer service system. Here we implement a comprehensive emotion management system.

class EmotionHandler:
    def __init__(self, llm_service):
        self.llm = llm_service
        self.emotion_thresholds = {
            "anger": 0.7,
            "frustration": 0.6,
            "satisfaction": 0.8
        }

    async def analyze_emotion(self, message: str) -> Dict[str, float]:
        """Analyze user emotion"""
        prompt = f"""
        User message: {message}

        Please analyze user emotion and return probability values (0-1) for:
        - anger
        - frustration
        - satisfaction
        """

        emotion_scores = await self.llm.generate(prompt)
        return emotion_scores

    async def generate_emotional_response(
        self, 
        message: str,
        emotion_scores: Dict[str, float],
        base_response: str
    ) -> str:
        """Generate emotion-adaptive response"""
        if emotion_scores["anger"] > self.emotion_thresholds["anger"]:
            return await self._handle_angry_customer(base_response)
        elif emotion_scores["frustration"] > self.emotion_thresholds["frustration"]:
            return await self._handle_frustrated_customer(base_response)
        else:
            return base_response

    async def _handle_angry_customer(self, base_response: str) -> str:
        """Handle angry emotion"""
        prompt = f"""
        Original response: {base_response}

        User is currently angry, please adjust response tone to:
        1. Show understanding and apology
        2. Provide clear solutions
        3. Maintain sincere and calm tone
        """

        return await self.llm.generate(prompt)
Enter fullscreen mode Exit fullscreen mode

💡 Best Practices

  1. Emotion analysis should consider context, not just isolated messages
  2. Establish quick response mechanisms for high-risk emotions (like anger)
  3. Set emotion escalation thresholds for timely human service transfer
  4. Save emotion analysis logs for system optimization

🎯 Optimization Directions

  1. Introduce multimodal emotion recognition (text + voice + expression)
  2. Establish personalized emotion baselines for improved accuracy
  3. Optimize dynamic adjustment of response strategies
  4. Add emotion prediction capabilities for early intervention

⚠️ Common Issues

  1. Over-reliance on single emotion labels
  2. Ignoring cultural differences in emotional expression
  3. Mechanical emotional response templates
  4. Failure to identify emotion escalation signals

4. Performance Optimization Practices

The performance of an intelligent customer service system directly affects user experience. Here we implement system optimization from multiple dimensions.

class PerformanceOptimizer:
    def __init__(self):
        self.response_cache = LRUCache(maxsize=1000)
        self.embedding_cache = LRUCache(maxsize=5000)
        self.batch_processor = BatchProcessor()

    async def optimize_response_generation(
        self,
        context: DialogueContext,
        knowledge_base: KnowledgeBase
    ) -> str:
        """Optimize response generation process"""
        # 1. Cache lookup
        cache_key = self._generate_cache_key(context)
        if cached_response := self.response_cache.get(cache_key):
            return cached_response

        # 2. Batch processing
        if self.batch_processor.should_batch():
            return await self.batch_processor.add_task(
                context, knowledge_base
            )

        # 3. Parallel processing
        results = await asyncio.gather(
            self._fetch_knowledge(context, knowledge_base),
            self._analyze_emotion(context),
            self._prepare_response_template(context)
        )

        # 4. Generate final response
        response = await self._generate_final_response(results)

        # 5. Update cache
        self.response_cache.set(cache_key, response)

        return response
Enter fullscreen mode Exit fullscreen mode

💡 Performance Optimization Key Points

  1. Use multi-level caching strategy to reduce repeated calculations
  2. Implement smart preloading to prepare responses for high-probability requests
  3. Use async programming and coroutines to improve concurrent processing
  4. Establish complete monitoring and alerting system

🔍 Monitoring Metrics

  1. Average response time (P95, P99)
  2. CPU and memory usage
  3. Concurrent request count
  4. Error rate and exception distribution
  5. Cache hit rate
  6. Token usage

Performance Enhancement Tips

  1. Use connection pools to reuse database connections
  2. Implement request batching
  3. Adopt progressive loading strategy
  4. Optimize data serialization methods
  5. Implement intelligent load balancing

Practical Experience Summary

  1. System Design Principles

    • Modular design for easy expansion
    • Focus on performance and scalability
    • Emphasize monitoring and operations
    • Continuous optimization and iteration
  2. Common Challenges and Solutions

    • Multi-turn dialogue context management
    • Real-time knowledge base updates
    • High concurrency handling
    • Emotion recognition accuracy
  3. Performance Optimization Techniques

    • Appropriate use of caching
    • Batch request processing
    • Async parallel processing
    • Dynamic resource scaling

Top comments (0)