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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# Chat-as-a-Service Integration Guide

## Overview

The Multi-Language Chat Agent can be used as a service by external applications. This guide explains how to integrate the chat service into your application, manage sessions, and handle different use cases.

## Architecture

```

┌─────────────────┐    HTTP/REST API    ┌─────────────────┐

│   Your App      │◄──────────────────►│   Chat Service  │

│                 │                     │   (This App)    │

└─────────────────┘                     └─────────────────┘



                                    ┌─────────────────┐

                                    │   Groq API      │

                                    │   Redis Cache   │

                                    │   PostgreSQL    │

                                    └─────────────────┘

```

## Session Management

### How Sessions Work

1. **Session Creation**: Each user gets a unique session per programming language
2. **Session Persistence**: Sessions are stored in PostgreSQL with Redis caching
3. **Session Isolation**: Each session maintains its own conversation history
4. **Session Expiry**: Sessions automatically expire after inactivity (configurable)

### Session Lifecycle

```python

# 1. Create Session

POST /api/v1/chat/sessions

{

    "language": "python",

    "metadata": {"user_type": "student", "course": "CS101"}

}

# Returns: {"session_id": "uuid", "user_id": "your-user", ...}



# 2. Send Messages

POST /api/v1/chat/sessions/{session_id}/message

{

    "content": "What is a Python list?",

    "language": "python"  # optional override

}

# Returns: {"response": "A Python list is...", ...}



# 3. Manage Session

GET /api/v1/chat/sessions/{session_id}           # Get session info

PUT /api/v1/chat/sessions/{session_id}/language  # Switch language

DELETE /api/v1/chat/sessions/{session_id}        # Delete session

```

## Integration Patterns

### 1. Single User, Multiple Languages

```python

from examples.chat_service_client import ChatServiceClient



client = ChatServiceClient("http://localhost:5000", "MyApp")



# Create sessions for different languages

python_session = client.create_session("user123", "python")

js_session = client.create_session("user123", "javascript")



# Send language-specific questions

python_response = client.send_message(python_session['session_id'], "How do I create a list?")

js_response = client.send_message(js_session['session_id'], "How do I create an array?")

```

### 2. Multiple Users, Shared Service

```python

from examples.chat_service_client import MultiUserChatManager



manager = MultiUserChatManager("http://localhost:5000", "LearningPlatform")



# Start chats for multiple users

manager.start_chat_for_user("student1", "python")

manager.start_chat_for_user("student2", "javascript")



# Send messages for specific users

response1 = manager.send_user_message("student1", "What are Python functions?")

response2 = manager.send_user_message("student2", "What are JS functions?")

```

### 3. Anonymous/Guest Users

```python

from examples.integration_examples import WebsiteChatbot



chatbot = WebsiteChatbot("http://localhost:5000")



# Handle anonymous users with browser ID

browser_id = "browser_12345"  # From cookies/localStorage

chat_data = chatbot.start_anonymous_chat(browser_id, "python")



# Continue conversation

response = chatbot.continue_anonymous_chat(browser_id, "What is Python?")

```

## Authentication & Security

### User Identification

The service uses the `X-User-ID` header to identify users:

```python

headers = {

    "X-User-ID": "your-app-user-123",

    "Content-Type": "application/json"

}

```

### Session Ownership

- Users can only access their own sessions
- Session ownership is validated on every request
- Cross-user access returns 403 Forbidden

### Rate Limiting

Default rate limits (configurable):
- Session creation: 10 per minute
- Message sending: 30 per minute
- Other endpoints: 20 per minute

## Error Handling

### Common Error Responses

```python

# Session not found

{

    "error": "Session not found",

    "code": 404

}



# Session expired

{

    "error": "Session has expired", 

    "code": 410

}



# Rate limit exceeded

{

    "error": "Rate limit exceeded",

    "code": 429,

    "retry_after": 60

}



# Invalid language

{

    "error": "Unsupported language: xyz. Supported: python, javascript, java...",

    "code": 400

}

```

### Error Handling Best Practices

```python

import requests



def safe_api_call(url, headers, data):

    try:

        response = requests.post(url, headers=headers, json=data, timeout=30)

        

        if response.status_code == 429:

            # Rate limited - wait and retry

            retry_after = int(response.headers.get('Retry-After', 60))

            time.sleep(retry_after)

            return safe_api_call(url, headers, data)

        

        elif response.status_code == 410:

            # Session expired - create new session

            return create_new_session_and_retry(data)

        

        elif response.status_code >= 400:

            error_data = response.json()

            raise Exception(f"API Error: {error_data.get('error', 'Unknown error')}")

        

        return response.json()

        

    except requests.exceptions.Timeout:

        raise Exception("Request timeout - service may be overloaded")

    except requests.exceptions.ConnectionError:

        raise Exception("Cannot connect to chat service")

```

## Use Case Examples

### 1. Learning Management System (LMS)

```python

class LMSIntegration:

    def __init__(self):

        self.chat_manager = MultiUserChatManager("http://chat-service:5000", "LMS")

    

    def enroll_student(self, student_id, course_id):

        # Map course to programming language

        language_map = {

            "python-101": "python",

            "js-fundamentals": "javascript",

            "java-oop": "java"

        }

        

        language = language_map.get(course_id, "python")

        user_id = f"{student_id}_{course_id}"

        

        # Create session with course context

        session_id = self.chat_manager.start_chat_for_user(

            user_id, 

            language,

            {"student_id": student_id, "course_id": course_id}

        )

        

        return session_id

    

    def student_ask_question(self, student_id, course_id, question):

        user_id = f"{student_id}_{course_id}"

        response = self.chat_manager.send_user_message(user_id, question)

        return response['response']

```

### 2. Code Editor Plugin

```python

class CodeEditorPlugin:

    def __init__(self):

        self.client = ChatServiceClient("http://chat-service:5000", "CodeEditor")

        self.user_sessions = {}

    

    def explain_code(self, user_id, language, code_snippet, question):

        # Get or create session for this language

        session_id = self.get_session_for_language(user_id, language)

        

        # Format question with code context

        formatted_question = f"""

I have this {language} code:

```{language}

{code_snippet}

```
{question}
"""
        

        response = self.client.send_message(session_id, formatted_question)

        return response['response']

    

    def get_session_for_language(self, user_id, language):

        key = f"{user_id}_{language}"

        if key not in self.user_sessions:

            session = self.client.create_session(key, language)

            self.user_sessions[key] = session['session_id']

        return self.user_sessions[key]

```


### 3. Mobile App with Offline Support

```python

class MobileAppIntegration:

    def __init__(self):

        self.chat_manager = MultiUserChatManager("http://chat-service:5000", "MobileApp")

        self.offline_queue = {}

    

    def send_message_with_offline(self, user_id, message):

        try:

            # Try to send immediately

            response = self.chat_manager.send_user_message(user_id, message)

            return {"status": "sent", "response": response['response']}

        

        except Exception:

            # Queue for later if offline

            if user_id not in self.offline_queue:

                self.offline_queue[user_id] = []

            

            self.offline_queue[user_id].append(message)

            return {"status": "queued", "message": "Will send when online"}

    

    def sync_offline_messages(self, user_id):

        if user_id not in self.offline_queue:

            return {"synced": 0}

        

        messages = self.offline_queue[user_id]

        synced = 0

        

        for message in messages:

            try:

                self.chat_manager.send_user_message(user_id, message)

                synced += 1

            except Exception:

                break

        

        # Remove synced messages

        self.offline_queue[user_id] = messages[synced:]

        if not self.offline_queue[user_id]:

            del self.offline_queue[user_id]

        

        return {"synced": synced, "remaining": len(messages) - synced}

```

## Deployment Considerations

### Scaling the Service

1. **Horizontal Scaling**: Run multiple instances behind a load balancer
2. **Database Scaling**: Use PostgreSQL read replicas for heavy read workloads
3. **Redis Clustering**: Use Redis cluster for high availability caching
4. **API Gateway**: Use an API gateway for rate limiting and authentication

### Configuration for Production

```bash

# Environment variables for production

GROQ_API_KEY=your-production-api-key

DATABASE_URL=postgresql://user:pass@db-cluster:5432/chatdb

REDIS_URL=redis://redis-cluster:6379/0



# Rate limiting

RATE_LIMIT_ENABLED=true

RATE_LIMIT_STORAGE=redis



# Session management

SESSION_TIMEOUT=7200  # 2 hours

CLEANUP_INTERVAL=300  # 5 minutes



# Security

SECRET_KEY=your-production-secret-key

CORS_ORIGINS=https://yourdomain.com,https://app.yourdomain.com

```

### Monitoring & Observability

```python

# Health check endpoint

GET /api/v1/chat/health



# Response

{

    "status": "healthy",

    "services": {

        "database": "connected",

        "redis": "connected",

        "groq_api": "available"

    },

    "timestamp": "2023-01-01T00:00:00Z"

}

```

### Docker Deployment

```yaml

# docker-compose.yml

version: '3.8'

services:

  chat-service:

    build: .

    ports:

      - "5000:5000"

    environment:

      - DATABASE_URL=postgresql://postgres:password@db:5432/chatdb

      - REDIS_URL=redis://redis:6379/0

      - GROQ_API_KEY=${GROQ_API_KEY}

    depends_on:

      - db

      - redis

  

  db:

    image: postgres:13

    environment:

      - POSTGRES_DB=chatdb

      - POSTGRES_USER=postgres

      - POSTGRES_PASSWORD=password

    volumes:

      - postgres_data:/var/lib/postgresql/data

  

  redis:

    image: redis:6-alpine

    volumes:

      - redis_data:/data



volumes:

  postgres_data:

  redis_data:

```

## Best Practices

### 1. Session Management
- Create sessions per user per language context
- Clean up expired sessions regularly
- Use meaningful metadata for tracking

### 2. Error Handling
- Implement retry logic for transient failures
- Handle rate limiting gracefully
- Provide fallback responses when service is unavailable

### 3. Performance
- Cache session IDs in your application
- Batch operations when possible
- Use connection pooling for HTTP requests

### 4. Security
- Validate user IDs before making requests
- Use HTTPS in production
- Implement proper authentication in your app

### 5. Monitoring
- Monitor API response times
- Track error rates and types
- Set up alerts for service health

## Testing Your Integration

```python

# Test script for your integration

def test_chat_integration():

    client = ChatServiceClient("http://localhost:5000", "TestApp")

    

    # Test health

    health = client.health_check()

    assert health['status'] == 'healthy'

    

    # Test session creation

    session = client.create_session("test-user", "python")

    assert 'session_id' in session

    

    # Test message sending

    response = client.send_message(session['session_id'], "What is Python?")

    assert 'response' in response

    assert len(response['response']) > 0

    

    # Test language switching

    switch_result = client.switch_language(session['session_id'], "javascript")

    assert switch_result['new_language'] == 'javascript'

    

    # Cleanup

    client.delete_session(session['session_id'])

    

    print("✅ All integration tests passed!")



if __name__ == "__main__":

    test_chat_integration()

```

## Support & Troubleshooting

### Common Issues

1. **Connection Refused**: Check if the service is running on the correct port
2. **Session Not Found**: Session may have expired, create a new one
3. **Rate Limited**: Implement exponential backoff retry logic
4. **Invalid Language**: Check supported languages via `/api/v1/chat/languages`

### Getting Help

- Check the API documentation at `/api/v1/chat/` (when service is running)
- Review logs for detailed error messages
- Use the health check endpoint to verify service status

---

This guide provides everything you need to integrate the chat service into your application. The service is designed to be stateless and scalable, making it suitable for production use across different types of applications.