Skip to main content
Source Code:
Primary API: create_client() factory function Module: gaia.llm Imports:
  • from gaia.llm import create_client (preferred)
  • from gaia.llm import LLMClient, NotSupportedError

Overview

The LLM client package provides a unified interface for generating text from multiple LLM backends using a provider pattern. Each provider implements the abstract LLMClient interface, with optional methods raising NotSupportedError when unavailable. Key Features:
  • Factory-based client creation with create_client()
  • Three providers: Lemonade (local AMD-optimized), OpenAI, Claude
  • Abstract base class for type safety and extensibility
  • Graceful handling of unsupported features via NotSupportedError
  • Streaming and non-streaming generation
  • Backward-compatible use_claude/use_openai flags
Provider Capabilities:
Methods marked with ✗ raise NotSupportedError when called on that provider.

Requirements

Functional Requirements

  1. Factory Pattern
    • create_client() factory function for client creation
    • Explicit provider selection via provider parameter (“lemonade”, “openai”, “claude”)
    • Backward-compatible use_claude/use_openai flags
    • Auto-detection of provider from flags when provider not specified
    • Default to Lemonade provider when no flags set
  2. Abstract Interface
    • LLMClient ABC defines unified interface
    • provider_name property returns provider name
    • Required methods (all providers must implement):
      • generate() - Text completion
      • chat() - Chat completion with message history
    • Optional methods (raise NotSupportedError if not implemented):
      • embed() - Generate embeddings
      • vision() - Vision/image understanding
      • get_performance_stats() - Performance statistics
      • load_model() - Load a model
      • unload_model() - Unload current model
  3. Provider Implementations
    • LemonadeProvider: Full support for all methods, connects to local Lemonade server
    • OpenAIProvider: generate, chat, embed only
    • ClaudeProvider: generate, chat, vision only
    • All providers support streaming and non-streaming modes
  4. Error Handling
    • NotSupportedError raised for unsupported methods
    • Clear error messages indicating provider and unsupported method
    • Connection errors handled by underlying provider implementations

Non-Functional Requirements

  1. Performance
    • Lazy provider loading via importlib (load only when needed)
    • Minimal overhead from abstraction layer
    • Streaming support across all providers
    • Default temperature of 0.1 for deterministic responses
  2. Reliability
    • Type safety through ABC pattern
    • Graceful handling of unsupported features
    • Clear error messages for provider capabilities
    • Provider-specific connection management
  3. Usability
    • Simple factory function interface
    • Backward compatibility with existing code
    • Consistent API across all providers
    • Clear documentation with examples

API Specification

Package Structure

Package Exports (__init__.py)

VLMClient is also re-exported from gaia.llm — see vlm-client for its API.

Factory Function (factory.py)


Abstract Base Class (base_client.py)


NotSupportedError (exceptions.py)


Provider Implementations

LemonadeProvider (providers/lemonade.py)

Full feature support - implements all methods.

OpenAIProvider (providers/openai_provider.py)

Partial support - generate, chat, embed only.

ClaudeProvider (providers/claude.py)

Partial support - generate, chat, vision only.

Implementation Details

Provider Selection Logic

The factory function auto-detects the provider based on parameters:

Lazy Provider Loading

Providers are loaded dynamically using importlib to avoid importing unnecessary dependencies:

NotSupportedError Pattern

Optional methods raise NotSupportedError by default in the ABC:
Providers override only the methods they support:

Temperature Defaults

All providers default to temperature=0.1 for deterministic responses:

Provider-Specific Implementation

LemonadeProvider wraps the low-level LemonadeClient:
OpenAIProvider uses the OpenAI SDK directly:
ClaudeProvider uses the Anthropic SDK:

Testing Requirements

Unit Tests

File: tests/unit/test_llm_client_factory.py

Integration Tests

File: tests/integration/test_llm_providers.py

Dependencies

Required Packages

Import Dependencies

Factory (factory.py):
Base Client (base_client.py):
LemonadeProvider (providers/lemonade.py):
OpenAIProvider (providers/openai_provider.py):
ClaudeProvider (providers/claude.py):

Usage Examples

Example 1: Basic Usage with Factory

Example 2: Explicit Provider Selection

Example 3: Handling Unsupported Features

Example 4: Chat with Message History

Example 5: Streaming Responses

Example 6: Embeddings (Lemonade and OpenAI only)

Example 7: Vision (Lemonade and Claude only)

Example 8: Model Management (Lemonade only)

Example 9: Remote Lemonade Server

Example 10: System Prompts


Third-Party LLM Integration

GAIA supports third-party LLM service providers through its OpenAI-compatible API interface. Any service implementing the OpenAI API specification can be used with GAIA.

Required API Endpoints

Your LLM service must implement at least one of these OpenAI-compatible endpoints:

Completions Endpoint

Default: POST /v1/completionsUsed for pre-formatted prompts

Chat Completions Endpoint

POST /v1/chat/completionsUsed for structured conversations with message history

Completions Endpoint

Chat Completions Endpoint


Configuration

Linux
Windows (PowerShell)
Windows (CMD)
URL Normalization: LemonadeClient automatically appends /api/v1 if not present:
  • http://localhost:8080http://localhost:8080/api/v1
  • If your service uses /v1 instead, provide the full path: http://localhost:8080/v1

Example Integration


Compatibility Checklist

  • OpenAI-compatible endpoints (/v1/completions or /v1/chat/completions)
  • JSON request/response format matching OpenAI specification
  • HTTP POST method for generation requests
  • Non-streaming responses (complete response as JSON)
  • ⚠️ Streaming responses (Server-Sent Events format)
  • ⚠️ Error handling (proper HTTP status codes: 200, 400, 404, 500)
  • ⚠️ Model listing (GET /v1/models endpoint)
  • ⚠️ Token counting (usage statistics in responses)
The following features are specific to Lemonade provider and raise NotSupportedError with third-party services:
  • get_performance_stats() - Performance statistics
  • load_model() - Model loading
  • unload_model() - Model unloading

Troubleshooting

Problem: ConnectionError: LLM Server Connection ErrorSolutions:
  1. Verify service is running:
  2. Check firewall settings
  3. Ensure correct base URL format
  4. Test with explicit base URL:
Problem: 404 endpoint not foundSolutions:
  1. Check if service uses /v1/completions (OpenAI standard)
  2. Verify API path structure: /v1 vs /api/v1
  3. Consult service documentation for correct endpoint paths
  4. Use chat method explicitly if needed:
Problem: Model errors or “model not loaded”Solutions:
  1. Specify model explicitly:
  2. List available models (if service supports):
  3. Ensure model is loaded in your service before connecting
Problem: Streaming responses not workingSolutions:
  1. Verify service supports Server-Sent Events (SSE)
  2. Check Content-Type headers: text/event-stream
  3. Test non-streaming first:
  4. Enable debug logging:

Documentation Updates Required

docs/sdk/sdks/llm.mdx

Add to LLM Section:

LLMClient Technical Specification