File size: 3,629 Bytes
bb0c63f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Basic usage examples for the Enterprise AI Gateway
"""

import requests
import json
import time

# Configuration
BASE_URL = "http://localhost:8000"
API_KEY = "your-api-key-here"  # Replace with your actual API key

def example_health_check():
    """Example of health check endpoint usage"""
    print("=== Health Check Example ===")
    
    response = requests.get(f"{BASE_URL}/health")
    
    if response.status_code == 200:
        data = response.json()
        print(f"Status: {data['status']}")
        print(f"Provider: {data['provider']}")
        print(f"Timestamp: {data['timestamp']}")
    else:
        print(f"Health check failed with status code: {response.status_code}")

def example_single_query():
    """Example of single query to the LLM"""
    print("\n=== Single Query Example ===")
    
    headers = {
        "Content-Type": "application/json",
        "X-API-Key": API_KEY
    }
    
    payload = {
        "prompt": "Explain the benefits of using a secure LLM gateway in enterprise applications.",
        "max_tokens": 256,
        "temperature": 0.7
    }
    
    response = requests.post(f"{BASE_URL}/query", headers=headers, json=payload)
    
    if response.status_code == 200:
        data = response.json()
        print(f"Response: {data['response']}")
        print(f"Provider: {data['provider']}")
        print(f"Latency: {data['latency_ms']} ms")
        print(f"Status: {data['status']}")
    else:
        print(f"Query failed with status code: {response.status_code}")
        print(f"Response: {response.text}")

def example_batch_queries():
    """Example of batch queries to the LLM"""
    print("\n=== Batch Queries Example ===")
    
    queries = [
        "What is artificial intelligence?",
        "Explain machine learning in simple terms",
        "What are the benefits of cloud computing?",
        "How does blockchain technology work?",
        "What is the difference between HTTP and HTTPS?"
    ]
    
    headers = {
        "Content-Type": "application/json",
        "X-API-Key": API_KEY
    }
    
    results = []
    
    for i, query in enumerate(queries, 1):
        print(f"\nProcessing query {i}/{len(queries)}: {query[:50]}...")
        
        payload = {
            "prompt": query,
            "max_tokens": 128,
            "temperature": 0.7
        }
        
        response = requests.post(f"{BASE_URL}/query", headers=headers, json=payload)
        
        if response.status_code == 200:
            data = response.json()
            results.append({
                "query": query,
                "response": data["response"],
                "provider": data["provider"],
                "latency_ms": data["latency_ms"]
            })
            print(f"  ✓ Success - {data['provider']} ({data['latency_ms']} ms)")
        else:
            print(f"  ✗ Failed - Status {response.status_code}")
        
        # Small delay to avoid rate limiting
        time.sleep(0.5)
    
    # Print summary
    print(f"\n=== Batch Results Summary ===")
    total_latency = sum(result["latency_ms"] for result in results if "latency_ms" in result)
    avg_latency = total_latency / len(results) if results else 0
    
    print(f"Successful queries: {len(results)}/{len(queries)}")
    print(f"Average latency: {avg_latency:.2f} ms")
    print(f"Providers used: {set(result['provider'] for result in results if 'provider' in result)}")

if __name__ == "__main__":
    # Note: Make sure the Enterprise AI Gateway is running before executing these examples
    example_health_check()
    example_single_query()
    example_batch_queries()