File size: 2,028 Bytes
f810b2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import aiohttp
from typing import List, Dict, Any

async def fetch_openrouter_models() -> List[Dict[str, Any]]:
    """
    Asynchronously fetch available models from the OpenRouter API.
        
    Returns:
        A list of dictionaries containing model information.
        Empty list if the request fails.
    """
    # Define the endpoint
    models_endpoint = "https://openrouter.ai/api/v1/models"
    
    try:
        # Prepare headers
        headers = {
            "Content-Type": "application/json"
        }
        
        # Make the async request
        async with aiohttp.ClientSession() as session:
            async with session.get(models_endpoint, headers=headers) as response:
                # Check if request was successful
                if response.status == 200:
                    data = await response.json()
                    return data.get("data", [])
                else:
                    error_text = await response.text()
                    print(f"Error fetching models: HTTP {response.status} - {error_text}")
                    return []
            
    except Exception as e:
        print(f"Error fetching models: {str(e)}")
        return []
    

async def list_openrouter_models_summary() -> List[Dict[str, Any]]:
    """
    Asynchronously returns a simplified list of available OpenRouter models with key information.
           
    Returns:
        A list of dictionaries with model ID, name, context length, and pricing.
    """
    models = await fetch_openrouter_models()
    
    summary = []
    for model in models:
        summary.append({
            "id": model.get("id"),
            "name": model.get("name"),
            "context_length": model.get("context_length"),
            "modality": model.get("architecture", {}).get("modality"),
            "pricing": {
                "prompt": model.get("pricing", {}).get("prompt"),
                "completion": model.get("pricing", {}).get("completion")
            }
        })
        
    return summary