File size: 16,553 Bytes
3b8a72f
 
 
4c40bac
3b8a72f
4c40bac
 
 
 
 
 
 
 
 
 
 
2a464ca
 
 
 
 
 
 
eff2c28
 
 
 
 
 
 
4c40bac
2a464ca
 
4c40bac
2a464ca
 
4c40bac
 
 
2a464ca
 
 
 
 
 
 
 
4c40bac
2a464ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c40bac
2a464ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eff2c28
2a464ca
 
 
 
 
 
eff2c28
 
 
 
2a464ca
 
 
 
 
 
 
 
 
 
b53417f
 
 
 
3b8a72f
c4c3b2e
 
 
 
 
 
c25123d
 
c4c3b2e
 
 
 
748f7ac
c4c3b2e
 
 
 
 
 
 
c25123d
 
 
c4c3b2e
3b8a72f
 
 
 
748f7ac
 
3b8a72f
 
 
 
748f7ac
3b8a72f
 
 
 
 
 
 
 
 
ac390b8
3b8a72f
 
 
 
 
 
 
 
 
ac390b8
3b8a72f
 
 
ac390b8
3b8a72f
 
 
 
 
 
 
2a464ca
3b8a72f
 
 
748f7ac
 
3b8a72f
d620370
 
 
 
 
 
 
b53417f
 
d620370
3b8a72f
9188527
2a464ca
3b8a72f
d620370
9188527
 
 
 
 
 
 
 
d620370
 
2a464ca
d620370
 
 
 
8f6e640
d620370
8f6e640
d620370
8f6e640
d620370
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98263a0
 
748f7ac
b53417f
98263a0
b53417f
98263a0
d620370
 
 
 
 
 
 
 
 
 
2a464ca
d620370
3b8a72f
 
2a464ca
3b8a72f
2a464ca
3b8a72f
 
9188527
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b8a72f
 
 
 
 
 
 
9188527
3b8a72f
9188527
 
 
 
 
 
748f7ac
9188527
 
 
 
 
748f7ac
9188527
3b8a72f
 
748f7ac
3b8a72f
 
9188527
 
3b8a72f
9188527
 
 
 
 
 
 
 
 
3b8a72f
9188527
 
 
 
 
 
 
 
 
3b8a72f
9188527
3b8a72f
 
9188527
 
3b8a72f
 
 
 
 
4c40bac
 
 
 
 
 
2a464ca
 
c4c3b2e
 
c25123d
c4c3b2e
 
 
 
 
 
 
 
c25123d
 
 
c4c3b2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c25123d
 
 
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import asyncio
import aiohttp
from typing import List, Dict, Any, Optional, Set
from datetime import datetime
import random

from .openrouter_client import OpenRouterClient
from . import config
from .utils import clean_model_name


class ModelTester:
    def __init__(self):
        self.client = OpenRouterClient()
        self.max_concurrency = config.get_max_concurrency()
        self.test_prompt = config.get_test_prompt()
        
        self._all_models: List[str] = []
        self._free_models: List[str] = []
        self._available_models: List[str] = []
        self._available_free_models: List[str] = []
        self._scan_in_progress = False
        self._last_scan_time: Optional[datetime] = None
        self.scan_result: Dict[str, Any] = {
            "available_models": [],
            "available_free_models": [],
            "total_available": 0,
            "free_available": 0,
            "timestamp": None
        }

    def refresh_model_list(self):
        """Get latest model list from API"""
        models = self.client.get_models()
        all_ids = []
        free_ids = []

        for model in models:
            model_id = model.get("id", "")
            if model_id:
                all_ids.append(model_id)
                if ":free" in model_id:
                    free_ids.append(model_id)

        self._all_models = all_ids
        self._free_models = free_ids
        return len(self._all_models), len(self._free_models)

    async def test_single_model_async(
        self,
        session: aiohttp.ClientSession,
        model_id: str,
        api_key: str
    ) -> tuple[str, bool]:
        url = "https://openrouter.ai/api/v1/chat/completions"
        payload = {
            "model": model_id,
            "messages": [{"role": "user", "content": self.test_prompt}],
            "max_tokens": 10
        }
        headers = {
            "Authorization": f"Bearer {api_key}",
            "Content-Type": "application/json"
        }

        try:
            timeout = aiohttp.ClientTimeout(total=config.get_request_timeout())
            async with session.post(url, json=payload, headers=headers, timeout=timeout) as response:
                is_success = response.status == 200
                is_free = ":free" in model_id
                return model_id, is_success
        except Exception:
            return model_id, False

    async def scan_all_models_async(self):
        """Async scan all models concurrently"""
        if self._scan_in_progress:
            return {"error": "Scan already in progress"}
        
        self._scan_in_progress = True
        print(f"[{datetime.now()}] Starting model scan...")
        
        all_count, free_count = self.refresh_model_list()
        print(f"Total models: {all_count}, Free models: {free_count}")
        
        api_keys = config.get_api_keys()
        api_key = random.choice(api_keys)
        
        available: Set[str] = set()
        available_free: Set[str] = set()
        
        async with aiohttp.ClientSession() as session:
            semaphore = asyncio.Semaphore(self.max_concurrency)
            
            async def test_with_semaphore(model_id: str):
                async with semaphore:
                    return await self.test_single_model_async(session, model_id, api_key)
            
            tasks = [test_with_semaphore(m) for m in self._all_models]
            results = await asyncio.gather(*tasks, return_exceptions=True)
            
            for result in results:
                if isinstance(result, tuple):
                    model_id, success = result
                    cleaned = clean_model_name(model_id)
                    if success:
                        available.add(cleaned)
                        if ":free" in model_id:
                            available_free.add(cleaned)
        
        self._available_models = sorted(list(available))
        self._available_free_models = sorted(list(available_free))
        self._last_scan_time = datetime.now()
        self._scan_in_progress = False
        
        self.scan_result = {
            "available_models": self._available_models,
            "available_free_models": self._available_free_models,
            "total_available": len(self._available_models),
            "free_available": len(self._available_free_models),
            "timestamp": self._last_scan_time.isoformat() if self._last_scan_time else None
        }
        
        print(f"Scan complete: {len(self._available_free_models)} free, {len(self._available_models)} total available")
        
        return self.scan_result

    def scan_all_models(self):
        """Sync wrapper for scan"""
        return asyncio.run(self.scan_all_models_async())

    def get_available_models(self, free_only: bool = False) -> List[str]:
        """Get available models list"""
        if free_only:
            return self._available_free_models
        return self._available_models
    
    def get_all_free_models(self) -> List[str]:
        """Get all free models from API list (not tested)"""
        return self._free_models

    async def try_model_direct_stream(
        self,
        session: aiohttp.ClientSession,
        model_id: str,
        api_key: str,
        messages: List[Dict[str, str]]
    ):
        """发送流式请求到OpenRouter,返回流式迭代器"""
        url = "https://openrouter.ai/api/v1/chat/completions"
        payload = {
            "model": model_id,
            "messages": messages,

            "stream": True
        }
        headers = {
            "Authorization": f"Bearer {api_key}",
            "Content-Type": "application/json"
        }

        async with session.post(url, json=payload, headers=headers, timeout=aiohttp.ClientTimeout(total=120)) as response:
            async for line in response.content:
                yield line

    async def try_model_direct(
        self, 
        session: aiohttp.ClientSession, 
        model_id: str,
        api_key: str,
        prompt: str = None
    ) -> Optional[Dict[str, Any]]:
        url = "https://openrouter.ai/api/v1/chat/completions"
        payload = {
            "model": model_id,
            "messages": [{"role": "user", "content": prompt or self.test_prompt}]
        }
        headers = {
            "Authorization": f"Bearer {api_key}",
            "Content-Type": "application/json"
        }

        try:
            timeout = aiohttp.ClientTimeout(total=config.get_request_timeout())
            async with session.post(url, json=payload, headers=headers, timeout=timeout) as response:
                body = await response.text()
                if response.status == 200:
                    data = await response.json()
                    return {
                        "success": True,
                        "model": model_id,
                        "response": data,
                        "method": "direct"
                    }
                else:
                    print(f"[try_model_direct] ERROR {model_id}: HTTP {response.status}, body: {body[:200]}")
                    return {
                        "success": False,
                        "model": model_id,
                        "error": f"HTTP {response.status}: {body[:100]}",
                        "method": "direct"
                    }
        except asyncio.TimeoutError:
            return {"success": False, "model": model_id, "error": "timeout", "method": "direct"}
        except Exception as e:
            return {"success": False, "model": model_id, "error": str(e), "method": "direct"}

    async def try_best_available_model(
        self,
        session: aiohttp.ClientSession,
        keyword: str,
        api_key: str,
        prompt: str = None
    ) -> Optional[Dict[str, Any]]:
        # 第一步:从API获取最新的free模型列表
        print(f"[try_best] Keyword: {keyword}, Refreshing model list...")
        try:
            self.refresh_model_list()
        except Exception as e:
            print(f"[try_best] refresh_model_list failed: {e}")
        
        # 使用所有free模型,而不是已测试的
        available_free = self.get_all_free_models()
        print(f"[try_best] Found {len(available_free)} free models")
        
        # 第二步:用关键词匹配模型(避免匹配到不完整的ID)
        candidates = []
        
        if keyword and available_free:
            # 只匹配模型名部分,不匹配作者前缀
            matched = []
            for m in available_free:
                model_name = m.replace(":free", "").split("/")[-1]
                if keyword.lower() in model_name.lower():
                    matched.append(m)
            
            print(f"[try_best] Keyword '{keyword}' matched: {matched[:5]}")
            if matched:
                candidates.extend([(m, "matched") for m in matched[:10]])
        
        # 如果关键词没匹配或没提供关键词,随机取free模型
        if not candidates and available_free:
            candidates = [(m, "random") for m in available_free[:15]]
            print(f"[try_best] Using random models: {candidates[:3]}")
        
        # 如果列表为空,从API直接获取并测试
        if not candidates:
            print("[try_best] No candidates, fetching from API directly...")
            try:
                all_models = self.client.get_models()
                all_free = [m.get("id", "") for m in all_models if ":free" in m.get("id", "")]
                print(f"[try_best] API returned {len(all_free)} free models")
                if all_free:
                    candidates = [(m, "api") for m in all_free[:20]]
            except Exception as e:
                print(f"[try_best] API fetch failed: {e}")
        
        # 第三步:并发发送请求测试这些模型
        if not candidates:
            return {
                "success": False,
                "model": None,
                "error": "No candidates available",
                "method": "list_empty"
            }
        
        print(f"[try_best] Testing {len(candidates)} candidates...")
        semaphore = asyncio.Semaphore(5)
        
        async def try_one(model_id, match_type):
            async with semaphore:
                # 模型ID已经是完整格式(包含:free),不需要再添加
                print(f"[try_best] Testing model: {model_id}")
                result = await self.try_model_direct(session, model_id, api_key, prompt)
                if result:
                    print(f"[try_best] Result for {model_id}: success={result.get('success')}, error={result.get('error', 'none')}")
                else:
                    print(f"[try_best] Result for {model_id}: None")
                return result
        
        tasks = [try_one(m, t) for m, t in candidates]
        results = await asyncio.gather(*tasks, return_exceptions=True)
        
        for i, result in enumerate(results):
            if isinstance(result, dict) and result.get("success"):
                result["method"] = f"list_{candidates[i][1]}"
                print(f"[try_best] SUCCESS with {candidates[i][0]}")
                return result
        
        print(f"[try_best] All candidates failed")
        return {
            "success": False,
            "model": candidates[0][0] if candidates else None,
            "error": "No available model found",
            "method": "list_fallback"
        }

    def find_model_in_list(self, keyword: str) -> Optional[str]:
        """Find full model ID from keyword"""
        available_free = self.get_all_free_models()
        
        # 先精确匹配
        for model in available_free:
            model_name = model.replace(":free", "").split("/")[-1]
            if model_name.lower() == keyword.lower():
                return model
        
        # 然后模糊匹配
        for model in available_free:
            if keyword.lower() in model.lower():
                return model
        
        return None

    async def chat_completion(self, prompt: str, model_hint: Optional[str] = None) -> Dict[str, Any]:
        api_keys = config.get_api_keys()
        api_key = random.choice(api_keys)

        async with aiohttp.ClientSession() as session:
            tasks = []

            # 方案1:用户指定模型,需要先找到完整的模型ID
            if model_hint:
                # 尝试在模型列表中找到匹配的完整模型ID
                full_model_id = self.find_model_in_list(model_hint)
                
                if full_model_id:
                    # 找到完整ID,直接使用
                    tasks.append(asyncio.create_task(
                        self.try_model_direct(session, full_model_id, api_key, prompt)
                    ))
                else:
                    # 没找到,尝试用原始输入(可能是完整ID)
                    full_model = f"{model_hint}:free" if ":free" not in model_hint else model_hint
                    tasks.append(asyncio.create_task(
                        self.try_model_direct(session, full_model, api_key, prompt)
                    ))

            tasks.append(asyncio.create_task(
                self.try_best_available_model(session, model_hint or "", api_key, prompt)
            ))

            # 等待所有任务完成
            results = await asyncio.gather(*tasks, return_exceptions=True)

            # 先检查方案1
            result1 = results[0]
            if isinstance(result1, dict) and result1.get("success"):
                return {
                    "success": True,
                    "response": result1.get("response"),
                    "method": result1.get("method"),
                    "model": result1.get("model")
                }

            # 方案1失败,检查方案2
            result2 = results[1]
            if isinstance(result2, dict) and result2.get("success"):
                return {
                    "success": True,
                    "response": result2.get("response"),
                    "method": result2.get("method"),
                    "model": result2.get("model")
                }

            # 都失败了,返回方案2的错误(更详细)
            return {
                "success": False,
                "error": result2.get("error", "Unknown error") if isinstance(result2, dict) else "Request failed",
                "method": result2.get("method", "both_failed") if isinstance(result2, dict) else "both_failed"
            }

    def chat_completion_sync(self, prompt: str, model_hint: Optional[str] = None) -> Dict[str, Any]:
        return asyncio.run(self.chat_completion(prompt, model_hint))

    def test_single_model(self, model_id: str) -> tuple[str, bool]:
        is_available = self.client.test_model(model_id, self.test_prompt)
        cleaned_name = clean_model_name(model_id)
        return cleaned_name, is_available

    def test_all_models(self) -> Dict[str, Any]:
        """Legacy sync method - use scan_all_models instead"""
        return self.scan_all_models()

    async def chat_completion_stream(self, model_hint: Optional[str], messages: List[Dict[str, str]]):
        """流式聊天 - 返回生成器"""
        api_keys = config.get_api_keys()
        api_key = random.choice(api_keys)

        # 方案1:尝试用户指定的模型
        if model_hint:
            full_model_id = self.find_model_in_list(model_hint)
            if full_model_id:
                async with aiohttp.ClientSession() as session:
                    async for chunk in self.try_model_direct_stream(session, full_model_id, api_key, messages):
                        yield chunk
                    return

        # 方案2:从列表中找到可用模型
        self.refresh_model_list()
        available_free = self.get_all_free_models()

        candidates = []
        if model_hint and available_free:
            for m in available_free:
                model_name = m.replace(":free", "").split("/")[-1]
                if model_hint.lower() in model_name.lower():
                    candidates.append(m)

        if not candidates and available_free:
            candidates = available_free[:10]

        async with aiohttp.ClientSession() as session:
            for model_id in candidates:
                async for chunk in self.try_model_direct_stream(session, model_id, api_key, messages):
                    yield chunk
                return