WebashalarForML's picture
Upload 178 files
330b6e4 verified

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

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:

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

See the example usage:

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