File size: 3,800 Bytes
330b6e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
# Chat Agent Services

This directory contains service modules for the multi-language chat agent.

## Groq LangChain Integration Service

The `groq_client.py` module provides integration with Groq's LangChain API for generating chat responses with programming language context and chat history support.

### Features

- **API Authentication**: Secure API key management and authentication
- **Language Context**: Support for multiple programming languages (Python, JavaScript, Java, C++)
- **Chat History**: Maintains conversation context for better responses
- **Streaming Responses**: Real-time response generation for better user experience
- **Error Handling**: Comprehensive error handling for API failures, rate limits, and network issues
- **Retry Logic**: Automatic retry with exponential backoff for transient failures

### Usage

```python

from chat_agent.services import GroqClient, ChatMessage, create_language_context



# Initialize client (requires GROQ_API_KEY environment variable)

client = GroqClient()



# Create language context

python_context = create_language_context("python")



# Build chat history

chat_history = [

    ChatMessage(role="user", content="What is Python?"),

    ChatMessage(role="assistant", content="Python is a programming language...")

]



# Generate response

response = client.generate_response(

    prompt="How do I create a list?",

    chat_history=chat_history,

    language_context=python_context

)



# Or use streaming for real-time responses

for chunk in client.stream_response(prompt, chat_history, python_context):

    print(chunk, end='', flush=True)

```

### Configuration

Set the following environment variables:

- `GROQ_API_KEY`: Your Groq API key (required)
- `GROQ_MODEL`: Model name (default: mixtral-8x7b-32768)
- `MAX_TOKENS`: Maximum response tokens (default: 2048)
- `TEMPERATURE`: Response creativity (default: 0.7)
- `CONTEXT_WINDOW_SIZE`: Number of recent messages to include (default: 10)

### Supported Languages

- Python
- JavaScript
- Java
- C++
- C#
- Go
- Rust
- TypeScript

### Error Handling

The client handles various error scenarios:

- **Rate Limiting**: Automatic backoff and user-friendly messages
- **Authentication Errors**: Clear error messages for API key issues
- **Network Errors**: Graceful handling of connection problems
- **Quota Exceeded**: Appropriate fallback responses

### Testing

Run the unit tests:

```bash

python -m pytest tests/unit/test_groq_client.py -v

```

See the example usage:

```bash

python examples/groq_client_example.py

```

### Requirements

The following requirements are satisfied by task 3:

- **3.1**: Groq LangChain API integration for LLM responses
- **3.2**: Secure API authentication and configuration management
- **3.3**: Comprehensive error handling for API failures and rate limits
- **3.5**: Appropriate backoff strategies for rate limiting

### Architecture

The GroqClient follows these design principles:

1. **Separation of Concerns**: Clear separation between API communication, error handling, and message processing
2. **Configurability**: Environment-based configuration for different deployment scenarios
3. **Extensibility**: Easy to add new programming languages and prompt templates
4. **Reliability**: Robust error handling and retry mechanisms
5. **Performance**: Streaming responses for better user experience

### Next Steps

This service integrates with:

- **Language Context Manager** (Task 4): For managing programming language contexts
- **Session Manager** (Task 5): For user session management
- **Chat History Manager** (Task 6): For persistent chat history
- **Chat Agent Service** (Task 7): For orchestrating the complete chat workflow