File size: 7,398 Bytes
4343907
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"""
SAAP colossus Server Integration
OpenAI-Compatible API Client for mistral-small3.2:24b-instruct-2506
"""
import requests
import json
import asyncio
import aiohttp
from typing import Dict, List, Optional
from dataclasses import dataclass
import time

@dataclass
class ColossusConfig:
    """colossus Server Configuration"""
    base_url: str = "https://ai.adrian-schupp.de"
    api_key: str = "sk-dBoxml3krytIRLdjr35Lnw"
    model: str = "mistral-small3.2:24b-instruct-2506"
    timeout: int = 90  # Increased from 30 to 90 seconds for larger models
    max_tokens: int = 1000
    temperature: float = 0.7

class ColossusClient:
    """
    OpenAI-Compatible API Client for colossus Server
    Handles communication with mistral-small model for SAAP Agents
    """
    
    def __init__(self, config: ColossusConfig = None):
        self.config = config or ColossusConfig()
        self.session = None
        
    async def __aenter__(self):
        self.session = aiohttp.ClientSession(
            timeout=aiohttp.ClientTimeout(total=self.config.timeout),
            headers={
                "Authorization": f"Bearer {self.config.api_key}",
                "Content-Type": "application/json"
            }
        )
        return self
    
    async def __aexit__(self, exc_type, exc_val, exc_tb):
        if self.session:
            await self.session.close()
    
    async def chat_completion(
        self,
        messages: List[Dict[str, str]],
        agent_id: str = "default",
        temperature: Optional[float] = None,
        max_tokens: Optional[int] = None
    ) -> Dict:
        """
        Send chat completion request to colossus
        
        Args:
            messages: List of message objects [{"role": "user", "content": "..."}]
            agent_id: SAAP Agent identifier for logging
            temperature: Model temperature override
            max_tokens: Max tokens override
            
        Returns:
            API response with generated text
        """
        
        start_time = time.time()
        
        payload = {
            "model": self.config.model,
            "messages": messages,
            "temperature": temperature or self.config.temperature,
            "max_tokens": max_tokens or self.config.max_tokens,
            "stream": False
        }
        
        try:
            async with self.session.post(
                f"{self.config.base_url}/v1/chat/completions",
                json=payload
            ) as response:
                
                response_time = time.time() - start_time
                
                if response.status == 200:
                    data = await response.json()
                    
                    # SAAP Performance Monitoring
                    print(f"βœ… colossus Response [{agent_id}]: {response_time:.2f}s")
                    
                    return {
                        "success": True,
                        "response": data,
                        "response_time": response_time,
                        "agent_id": agent_id,
                        "model": self.config.model
                    }
                else:
                    error_text = await response.text()
                    print(f"❌ colossus Error [{agent_id}]: {response.status} - {error_text}")
                    
                    return {
                        "success": False,
                        "error": f"HTTP {response.status}: {error_text}",
                        "response_time": response_time,
                        "agent_id": agent_id
                    }
                    
        except asyncio.TimeoutError:
            return {
                "success": False,
                "error": "Request timeout",
                "response_time": self.config.timeout,
                "agent_id": agent_id
            }
        except Exception as e:
            return {
                "success": False,
                "error": str(e),
                "response_time": time.time() - start_time,
                "agent_id": agent_id
            }
    
    def sync_chat_completion(
        self,
        messages: List[Dict[str, str]],
        agent_id: str = "default"
    ) -> Dict:
        """
        Synchronous version for compatibility
        """
        headers = {
            "Authorization": f"Bearer {self.config.api_key}",
            "Content-Type": "application/json"
        }
        
        payload = {
            "model": self.config.model,
            "messages": messages,
            "temperature": self.config.temperature,
            "max_tokens": self.config.max_tokens
        }
        
        start_time = time.time()
        
        try:
            response = requests.post(
                f"{self.config.base_url}/v1/chat/completions",
                headers=headers,
                json=payload,
                timeout=self.config.timeout
            )
            
            response_time = time.time() - start_time
            
            if response.status_code == 200:
                print(f"βœ… colossus Response [{agent_id}]: {response_time:.2f}s")
                return {
                    "success": True,
                    "response": response.json(),
                    "response_time": response_time,
                    "agent_id": agent_id
                }
            else:
                print(f"❌ colossus Error [{agent_id}]: {response.status_code}")
                return {
                    "success": False,
                    "error": f"HTTP {response.status_code}: {response.text}",
                    "response_time": response_time,
                    "agent_id": agent_id
                }
                
        except Exception as e:
            return {
                "success": False,
                "error": str(e),
                "response_time": time.time() - start_time,
                "agent_id": agent_id
            }

# Performance Test Function
async def test_colossus_performance():
    """
    Test colossus Server Performance 
    Target: < 2s Response Time
    """
    print("πŸš€ SAAP colossus Performance Test Starting...")
    
    test_messages = [
        {"role": "system", "content": "You are Jane Alesi, lead AI architect for SAAP platform."},
        {"role": "user", "content": "Hello Jane, please introduce yourself and explain your role in coordinating other AI agents."}
    ]
    
    async with ColossusClient() as client:
        # Single Request Test
        result = await client.chat_completion(test_messages, agent_id="jane_alesi")
        
        if result["success"]:
            response_text = result["response"]["choices"][0]["message"]["content"]
            response_time = result["response_time"]
            
            print(f"\nπŸ“Š PERFORMANCE RESULTS:")
            print(f"⏱️  Response Time: {response_time:.2f}s")
            print(f"🎯 Target Met: {'βœ… YES' if response_time < 2.0 else '❌ NO'}")
            print(f"πŸ€– Model: {result['model']}")
            print(f"\nπŸ’¬ Response Preview:")
            print(f"{response_text[:200]}...")
            
            return result
        else:
            print(f"❌ Test Failed: {result['error']}")
            return result

if __name__ == "__main__":
    # Run Performance Test
    result = asyncio.run(test_colossus_performance())