File size: 2,595 Bytes
69fec20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Base Router - 共用的路由基础功能
提供模型列表处理、通用响应等共同功能
"""

from typing import List, Optional

from src.models import Model, ModelList


# ==================== 模型列表处理 ====================

def expand_models_with_features(
    base_models: List[str],
    features: Optional[List[str]] = None
) -> List[str]:
    """
    使用特性前缀扩展模型列表
    
    Args:
        base_models: 基础模型列表
        features: 特性前缀列表,如 ["流式抗截断", "假流式"]
        
    Returns:
        扩展后的模型列表(包含原始模型和特性变体)
    """
    if not features:
        return base_models.copy()
    
    expanded = []
    for model in base_models:
        # 添加原始模型
        expanded.append(model)
        
        # 添加特性变体
        for feature in features:
            expanded.append(f"{feature}/{model}")
    
    return expanded


def create_openai_model_list(
    model_ids: List[str],
    owned_by: str = "google"
) -> ModelList:
    """
    创建OpenAI格式的模型列表
    
    Args:
        model_ids: 模型ID列表
        owned_by: 模型所有者
        
    Returns:
        ModelList对象
    """
    from datetime import datetime, timezone
    current_timestamp = int(datetime.now(timezone.utc).timestamp())
    
    models = [
        Model(
            id=model_id,
            object='model',
            created=current_timestamp,
            owned_by=owned_by
        )
        for model_id in model_ids
    ]
    
    return ModelList(data=models)


def create_gemini_model_list(
    model_ids: List[str],
    base_name_extractor=None
) -> dict:
    """
    创建Gemini格式的模型列表
    
    Args:
        model_ids: 模型ID列表
        base_name_extractor: 可选的基础模型名提取函数
        
    Returns:
        包含模型列表的字典
    """
    gemini_models = []
    
    for model_id in model_ids:
        base_model = model_id
        if base_name_extractor:
            try:
                base_model = base_name_extractor(model_id)
            except Exception:
                pass
        
        model_info = {
            "name": f"models/{model_id}",
            "baseModelId": base_model,
            "version": "001",
            "displayName": model_id,
            "description": f"Gemini {base_model} model",
            "supportedGenerationMethods": ["generateContent", "streamGenerateContent"],
        }
        gemini_models.append(model_info)
    
    return {"models": gemini_models}