Advanced Usage

Advanced Usage #

Advanced techniques and patterns for working with LLMITE.

Custom Models #

Training Your Own Model #

LLMITE allows you to train custom models using your own data:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import llmite

client = llmite.Client(api_key="your-api-key")

# Upload training data
training_job = client.models.train(
    name="my-custom-model",
    training_data="path/to/training.jsonl",
    validation_data="path/to/validation.jsonl",
    parameters={
        "learning_rate": 0.001,
        "batch_size": 32,
        "epochs": 10
    }
)

# Monitor training progress  
status = client.models.get_training_status(training_job.id)

Fine-tuning Existing Models #

1
2
3
4
5
6
7
8
9
# Fine-tune a pre-trained model
fine_tune_job = client.models.fine_tune(
    base_model="llmite-base-v1",
    training_data="path/to/fine_tune_data.jsonl",
    parameters={
        "learning_rate": 0.0001,
        "epochs": 3
    }
)

Batch Processing #

Large Scale Processing #

For processing large amounts of data efficiently:

 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
import asyncio
import aiohttp

async def process_batch(session, batch):
    async with session.post(
        "https://api.llmite.com/v1/models/batch-process",
        headers={"Authorization": f"Bearer {api_key}"},
        json={"inputs": batch}
    ) as response:
        return await response.json()

async def process_large_dataset(data, batch_size=100):
    async with aiohttp.ClientSession() as session:
        tasks = []
        for i in range(0, len(data), batch_size):
            batch = data[i:i + batch_size]
            task = process_batch(session, batch)
            tasks.append(task)
        
        results = await asyncio.gather(*tasks)
        return results

# Usage
data = ["text1", "text2", ...]  # Your large dataset
results = asyncio.run(process_large_dataset(data))

Webhooks #

Setting Up Webhooks #

Configure webhooks to receive real-time notifications:

 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
# Register a webhook
webhook = client.webhooks.create(
    url="https://your-app.com/webhook",
    events=["model.completed", "training.finished"],
    secret="your-webhook-secret"
)

# Webhook handler (Flask example)
from flask import Flask, request
import hmac
import hashlib

app = Flask(__name__)

@app.route('/webhook', methods=['POST'])
def handle_webhook():
    # Verify webhook signature
    signature = request.headers.get('X-LLMITE-Signature')
    payload = request.get_data()
    
    expected_signature = hmac.new(
        webhook_secret.encode(),
        payload,
        hashlib.sha256
    ).hexdigest()
    
    if not hmac.compare_digest(signature, expected_signature):
        return "Invalid signature", 401
    
    # Process webhook event
    event = request.json
    if event['type'] == 'model.completed':
        handle_model_completion(event['data'])
    
    return "OK", 200

Caching and Optimization #

Implementing Response Caching #

 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
import hashlib
import json
from functools import lru_cache

class LLMITEClient:
    def __init__(self, api_key):
        self.api_key = api_key
        self.session = requests.Session()
    
    def _cache_key(self, endpoint, data):
        content = json.dumps(data, sort_keys=True)
        return hashlib.md5(f"{endpoint}:{content}".encode()).hexdigest()
    
    @lru_cache(maxsize=1000)
    def cached_request(self, endpoint, data_hash, data):
        response = self.session.post(
            f"https://api.llmite.com/v1/{endpoint}",
            headers={"Authorization": f"Bearer {self.api_key}"},
            json=data
        )
        return response.json()
    
    def process_text(self, text, parameters=None):
        data = {"input": text, "parameters": parameters or {}}
        cache_key = self._cache_key("models/process", data)
        return self.cached_request("models/process", cache_key, data)

Connection Pooling #

 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
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

class OptimizedLLMITEClient:
    def __init__(self, api_key):
        self.api_key = api_key
        self.session = self._create_session()
    
    def _create_session(self):
        session = requests.Session()
        
        # Configure retry strategy
        retry_strategy = Retry(
            total=3,
            status_forcelist=[429, 500, 502, 503, 504],
            method_whitelist=["HEAD", "GET", "POST"],
            backoff_factor=1
        )
        
        # Configure connection pooling
        adapter = HTTPAdapter(
            pool_connections=10,
            pool_maxsize=20,
            max_retries=retry_strategy
        )
        
        session.mount("http://", adapter)
        session.mount("https://", adapter)
        
        return session

Monitoring and Analytics #

Request Metrics #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import time
import logging
from contextlib import contextmanager

@contextmanager
def track_request(operation):
    start_time = time.time()
    try:
        yield
        duration = time.time() - start_time
        logging.info(f"{operation} completed in {duration:.2f}s")
    except Exception as e:
        duration = time.time() - start_time
        logging.error(f"{operation} failed after {duration:.2f}s: {e}")
        raise

# Usage
with track_request("text_processing"):
    result = client.process_text("Hello world")

Custom Metrics Dashboard #

 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
from dataclasses import dataclass
from typing import Dict, List
import json

@dataclass
class RequestMetrics:
    endpoint: str
    duration: float
    status_code: int
    tokens_used: int
    timestamp: float

class MetricsCollector:
    def __init__(self):
        self.metrics: List[RequestMetrics] = []
    
    def record_request(self, endpoint, duration, status_code, tokens_used):
        self.metrics.append(RequestMetrics(
            endpoint=endpoint,
            duration=duration,
            status_code=status_code,
            tokens_used=tokens_used,
            timestamp=time.time()
        ))
    
    def get_stats(self) -> Dict:
        if not self.metrics:
            return {}
        
        return {
            "total_requests": len(self.metrics),
            "avg_duration": sum(m.duration for m in self.metrics) / len(self.metrics),
            "total_tokens": sum(m.tokens_used for m in self.metrics),
            "success_rate": len([m for m in self.metrics if m.status_code == 200]) / len(self.metrics)
        }

Security Best Practices #

API Key Rotation #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class SecureLLMITEClient:
    def __init__(self, primary_key, backup_key=None):
        self.primary_key = primary_key
        self.backup_key = backup_key
        self.current_key = primary_key
    
    def _make_request(self, endpoint, data):
        try:
            return self._request_with_key(endpoint, data, self.current_key)
        except requests.exceptions.HTTPError as e:
            if e.response.status_code == 401 and self.backup_key:
                # Primary key might be expired, try backup
                return self._request_with_key(endpoint, data, self.backup_key)
            raise
    
    def _request_with_key(self, endpoint, data, api_key):
        response = requests.post(
            f"https://api.llmite.com/v1/{endpoint}",
            headers={"Authorization": f"Bearer {api_key}"},
            json=data
        )
        response.raise_for_status()
        return response.json()