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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
"""
NullAI モデル管理API

LLMモデルの一覧取得、追加、更新、削除を行う。
HuggingFace Hubからのモデル検索・検証機能も提供。
"""
from fastapi import APIRouter, HTTPException, Depends, Query
from pydantic import BaseModel
from typing import List, Dict, Any, Optional
import sys
import os
import asyncio
import aiohttp

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))))

from backend.app.middleware.auth import get_current_user, get_current_user_optional, require_role, User
from backend.app.config import ConfigManager, ModelConfig, ModelProvider

router = APIRouter()

# グローバル設定マネージャー
_config_manager = None

# 人気モデルのプリセット
POPULAR_MODELS = [
    {"model_id": "deepseek-r1-7b", "model_name": "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B", "display_name": "DeepSeek R1 7B", "size": "7B"},
    {"model_id": "deepseek-r1-14b", "model_name": "deepseek-ai/DeepSeek-R1-Distill-Qwen-14B", "display_name": "DeepSeek R1 14B", "size": "14B"},
    {"model_id": "deepseek-r1-32b", "model_name": "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B", "display_name": "DeepSeek R1 32B", "size": "32B"},
    {"model_id": "qwen2.5-7b", "model_name": "Qwen/Qwen2.5-7B-Instruct", "display_name": "Qwen 2.5 7B", "size": "7B"},
    {"model_id": "qwen2.5-14b", "model_name": "Qwen/Qwen2.5-14B-Instruct", "display_name": "Qwen 2.5 14B", "size": "14B"},
    {"model_id": "qwen2.5-32b", "model_name": "Qwen/Qwen2.5-32B-Instruct", "display_name": "Qwen 2.5 32B", "size": "32B"},
    {"model_id": "llama3.1-8b", "model_name": "meta-llama/Llama-3.1-8B-Instruct", "display_name": "Llama 3.1 8B", "size": "8B"},
    {"model_id": "llama3.2-3b", "model_name": "meta-llama/Llama-3.2-3B-Instruct", "display_name": "Llama 3.2 3B", "size": "3B"},
    {"model_id": "mistral-7b", "model_name": "mistralai/Mistral-7B-Instruct-v0.3", "display_name": "Mistral 7B", "size": "7B"},
    {"model_id": "phi3-mini", "model_name": "microsoft/Phi-3-mini-4k-instruct", "display_name": "Phi-3 Mini", "size": "3.8B"},
    {"model_id": "gemma2-9b", "model_name": "google/gemma-2-9b-it", "display_name": "Gemma 2 9B", "size": "9B"},
    {"model_id": "codellama-7b", "model_name": "codellama/CodeLlama-7b-Instruct-hf", "display_name": "CodeLlama 7B", "size": "7B"},
]


def get_config_manager() -> ConfigManager:
    global _config_manager
    if _config_manager is None:
        _config_manager = ConfigManager()
    return _config_manager


# --- Pydanticスキーマ ---
class ModelCreate(BaseModel):
    model_id: str
    display_name: str
    provider: str  # huggingface, huggingface_api, local, gguf
    api_url: str = ""
    model_name: str = ""  # HuggingFaceモデルID or GGUFパス
    max_tokens: int = 4096
    temperature: float = 0.7
    timeout: int = 120
    is_default: bool = False
    supported_domains: List[str] = ["general"]
    description: str = ""


class ModelUpdate(BaseModel):
    display_name: Optional[str] = None
    api_url: Optional[str] = None
    model_name: Optional[str] = None
    max_tokens: Optional[int] = None
    temperature: Optional[float] = None
    timeout: Optional[int] = None
    is_default: Optional[bool] = None
    supported_domains: Optional[List[str]] = None
    description: Optional[str] = None


class ModelResponse(BaseModel):
    model_id: str
    display_name: str
    provider: str
    api_url: str
    model_name: str
    max_tokens: int
    temperature: float
    timeout: int
    is_default: bool
    supported_domains: List[str]
    description: str


# --- APIエンドポイント ---
@router.get("/", response_model=List[ModelResponse])
async def list_models(
    domain_id: Optional[str] = None,
    current_user: User = Depends(get_current_user)
):
    """
    利用可能なモデル一覧を取得。
    domain_idを指定すると、そのドメインで使用可能なモデルのみを返す。
    """
    config = get_config_manager()
    models = config.list_models(domain_id=domain_id)

    return [
        ModelResponse(
            model_id=m.model_id,
            display_name=m.display_name,
            provider=m.provider.value,
            api_url=m.api_url,
            model_name=m.model_name,
            max_tokens=m.max_tokens,
            temperature=m.temperature,
            timeout=m.timeout,
            is_default=m.is_default,
            supported_domains=m.supported_domains,
            description=m.description
        )
        for m in models
    ]


@router.get("/{model_id}", response_model=ModelResponse)
async def get_model(
    model_id: str,
    current_user: User = Depends(get_current_user)
):
    """特定のモデル設定を取得"""
    config = get_config_manager()
    model = config.get_model(model_id)

    if not model:
        raise HTTPException(status_code=404, detail=f"Model '{model_id}' not found")

    return ModelResponse(
        model_id=model.model_id,
        display_name=model.display_name,
        provider=model.provider.value,
        api_url=model.api_url,
        model_name=model.model_name,
        max_tokens=model.max_tokens,
        temperature=model.temperature,
        timeout=model.timeout,
        is_default=model.is_default,
        supported_domains=model.supported_domains,
        description=model.description
    )


@router.post("/", response_model=ModelResponse)
async def create_model(
    model: ModelCreate,
    current_user: User = Depends(require_role("admin"))
):
    """
    新しいモデルを追加。
    adminロールが必要。
    """
    config = get_config_manager()

    # プロバイダーの検証
    # 注意: OpenAI/Anthropic等の外部APIは利用規約上の理由からサポートされていません
    try:
        provider = ModelProvider(model.provider)
    except ValueError:
        raise HTTPException(
            status_code=400,
            detail=f"Invalid provider: {model.provider}. Must be one of: huggingface, huggingface_api, local, gguf"
        )

    new_model = ModelConfig(
        model_id=model.model_id,
        display_name=model.display_name,
        provider=provider,
        api_url=model.api_url,
        model_name=model.model_name,
        max_tokens=model.max_tokens,
        temperature=model.temperature,
        timeout=model.timeout,
        is_default=model.is_default,
        supported_domains=model.supported_domains,
        description=model.description
    )

    if not config.add_model(new_model):
        raise HTTPException(
            status_code=400,
            detail=f"Model '{model.model_id}' already exists"
        )

    return ModelResponse(
        model_id=new_model.model_id,
        display_name=new_model.display_name,
        provider=new_model.provider.value,
        api_url=new_model.api_url,
        model_name=new_model.model_name,
        max_tokens=new_model.max_tokens,
        temperature=new_model.temperature,
        timeout=new_model.timeout,
        is_default=new_model.is_default,
        supported_domains=new_model.supported_domains,
        description=new_model.description
    )


@router.put("/{model_id}", response_model=ModelResponse)
async def update_model(
    model_id: str,
    updates: ModelUpdate,
    current_user: User = Depends(require_role("admin"))
):
    """
    モデル設定を更新。
    adminロールが必要。
    """
    config = get_config_manager()
    existing = config.get_model(model_id)

    if not existing:
        raise HTTPException(status_code=404, detail=f"Model '{model_id}' not found")

    # 更新を適用
    update_dict = updates.dict(exclude_unset=True)
    for key, value in update_dict.items():
        if hasattr(existing, key):
            setattr(existing, key, value)

    config.update_model(existing)

    return ModelResponse(
        model_id=existing.model_id,
        display_name=existing.display_name,
        provider=existing.provider.value,
        api_url=existing.api_url,
        model_name=existing.model_name,
        max_tokens=existing.max_tokens,
        temperature=existing.temperature,
        timeout=existing.timeout,
        is_default=existing.is_default,
        supported_domains=existing.supported_domains,
        description=existing.description
    )


@router.delete("/{model_id}")
async def delete_model(
    model_id: str,
    current_user: User = Depends(require_role("admin"))
):
    """
    モデルを削除。
    adminロールが必要。
    """
    config = get_config_manager()

    if not config.delete_model(model_id):
        raise HTTPException(status_code=404, detail=f"Model '{model_id}' not found")

    return {"message": f"Model '{model_id}' deleted successfully"}


@router.get("/providers/info")
async def get_providers_info():
    """
    サポートされているプロバイダー情報を取得。
    どのプロバイダーが利用可能か、どれが削除されたかを確認できる。
    """
    from null_ai.model_router import ModelRouter
    config = get_config_manager()
    router = ModelRouter(config)
    return router.get_provider_info()


@router.post("/{model_id}/test")
async def test_model(
    model_id: str,
    current_user: User = Depends(get_current_user)
):
    """
    モデルの接続テスト。
    簡単なプロンプトを送信して応答を確認する。
    """
    config = get_config_manager()
    model = config.get_model(model_id)

    if not model:
        raise HTTPException(status_code=404, detail=f"Model '{model_id}' not found")

    # ModelRouterを使用してテスト
    from null_ai.model_router import ModelRouter
    model_router = ModelRouter(config)

    try:
        result = await model_router.infer(
            prompt="Hello, please respond with 'OK' if you can receive this message.",
            domain_id="general",
            model_id=model_id,
            save_to_memory=False,
            max_tokens=50
        )

        return {
            "status": "success",
            "model_id": model_id,
            "response": result.get("response", "")[:200],
            "latency_ms": result.get("latency_ms", 0)
        }

    except Exception as e:
        return {
            "status": "error",
            "model_id": model_id,
            "error": str(e)
        }


# --- HuggingFace Hub連携 ---

class HuggingFaceModelInfo(BaseModel):
    """HuggingFace Hubのモデル情報"""
    model_id: str
    model_name: str
    author: str
    downloads: int
    likes: int
    tags: List[str]
    pipeline_tag: Optional[str]
    is_gated: bool


class QuickAddRequest(BaseModel):
    """簡単追加リクエスト"""
    huggingface_model_name: str  # e.g., "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B"
    custom_model_id: Optional[str] = None  # カスタムID(省略時は自動生成)
    provider: str = "huggingface"  # huggingface or huggingface_api
    supported_domains: List[str] = ["general"]


@router.get("/huggingface/search")
async def search_huggingface_models(
    query: str = Query(..., description="検索クエリ"),
    limit: int = Query(20, ge=1, le=100),
    filter_type: str = Query("text-generation", description="モデルタイプでフィルタ")
):
    """
    HuggingFace Hubでモデルを検索。
    認証不要で誰でも利用可能。
    """
    try:
        async with aiohttp.ClientSession() as session:
            url = "https://huggingface.co/api/models"
            params = {
                "search": query,
                "limit": limit,
                "filter": filter_type,
                "sort": "downloads",
                "direction": "-1"
            }

            async with session.get(url, params=params, timeout=aiohttp.ClientTimeout(total=30)) as response:
                if response.status != 200:
                    raise HTTPException(status_code=502, detail="HuggingFace API error")

                models = await response.json()

                return {
                    "query": query,
                    "count": len(models),
                    "models": [
                        {
                            "model_name": m.get("modelId", ""),
                            "author": m.get("author", ""),
                            "downloads": m.get("downloads", 0),
                            "likes": m.get("likes", 0),
                            "tags": m.get("tags", []),
                            "pipeline_tag": m.get("pipeline_tag"),
                            "is_gated": m.get("gated", False),
                            "last_modified": m.get("lastModified")
                        }
                        for m in models
                    ]
                }
    except aiohttp.ClientError as e:
        raise HTTPException(status_code=502, detail=f"Failed to connect to HuggingFace: {str(e)}")


@router.get("/huggingface/validate/{model_name:path}")
async def validate_huggingface_model(
    model_name: str
):
    """
    HuggingFaceモデルの存在と互換性を検証。
    認証不要。
    """
    try:
        async with aiohttp.ClientSession() as session:
            url = f"https://huggingface.co/api/models/{model_name}"

            async with session.get(url, timeout=aiohttp.ClientTimeout(total=15)) as response:
                if response.status == 404:
                    return {
                        "valid": False,
                        "model_name": model_name,
                        "error": "Model not found on HuggingFace Hub"
                    }

                if response.status != 200:
                    return {
                        "valid": False,
                        "model_name": model_name,
                        "error": f"HuggingFace API returned status {response.status}"
                    }

                model_info = await response.json()

                # テキスト生成モデルかどうかをチェック
                pipeline_tag = model_info.get("pipeline_tag", "")
                is_text_gen = pipeline_tag in ["text-generation", "text2text-generation", "conversational"]

                # ゲートモデル(アクセス申請が必要)かどうか
                is_gated = model_info.get("gated", False)

                # モデルサイズの推定(configファイルから)
                siblings = model_info.get("siblings", [])
                has_safetensors = any("safetensors" in s.get("rfilename", "") for s in siblings)
                has_pytorch = any("pytorch_model" in s.get("rfilename", "") or "model.safetensors" in s.get("rfilename", "") for s in siblings)

                return {
                    "valid": True,
                    "model_name": model_name,
                    "author": model_info.get("author", ""),
                    "pipeline_tag": pipeline_tag,
                    "is_text_generation": is_text_gen,
                    "is_gated": is_gated,
                    "downloads": model_info.get("downloads", 0),
                    "likes": model_info.get("likes", 0),
                    "tags": model_info.get("tags", []),
                    "has_safetensors": has_safetensors,
                    "has_pytorch": has_pytorch,
                    "warnings": [] if is_text_gen else ["This model may not be compatible with text generation"]
                }

    except aiohttp.ClientError as e:
        return {
            "valid": False,
            "model_name": model_name,
            "error": f"Failed to validate: {str(e)}"
        }


@router.get("/popular")
async def get_popular_models():
    """
    人気のHuggingFaceモデル一覧を取得。
    認証不要。
    """
    config = get_config_manager()

    # 既に追加済みのモデルをチェック
    existing_ids = set(config.models.keys())

    return {
        "models": [
            {
                **model,
                "already_added": model["model_id"] in existing_ids
            }
            for model in POPULAR_MODELS
        ]
    }


@router.post("/quick-add")
async def quick_add_model(
    request: QuickAddRequest,
    current_user: User = Depends(get_current_user)
):
    """
    HuggingFaceモデルを簡単に追加。
    認証済みユーザーなら誰でも利用可能(admin不要)。

    モデルは自動的に検証され、問題がなければ追加される。
    """
    config = get_config_manager()

    # HuggingFaceモデルを検証
    validation = None
    try:
        async with aiohttp.ClientSession() as session:
            url = f"https://huggingface.co/api/models/{request.huggingface_model_name}"
            async with session.get(url, timeout=aiohttp.ClientTimeout(total=15)) as response:
                if response.status == 404:
                    raise HTTPException(
                        status_code=400,
                        detail=f"Model '{request.huggingface_model_name}' not found on HuggingFace Hub"
                    )
                if response.status == 200:
                    validation = await response.json()
    except aiohttp.ClientError as e:
        raise HTTPException(
            status_code=502,
            detail=f"Failed to validate model: {str(e)}"
        )

    # ゲートモデルの警告
    if validation and validation.get("gated"):
        raise HTTPException(
            status_code=400,
            detail="This model requires access approval on HuggingFace. Please request access first."
        )

    # モデルIDを生成
    if request.custom_model_id:
        model_id = request.custom_model_id
    else:
        # huggingface_model_nameから自動生成
        model_id = request.huggingface_model_name.replace("/", "-").lower()

    # 既存チェック
    if config.get_model(model_id):
        raise HTTPException(
            status_code=400,
            detail=f"Model '{model_id}' already exists. Use a different custom_model_id."
        )

    # プロバイダーを検証
    try:
        provider = ModelProvider(request.provider)
    except ValueError:
        raise HTTPException(
            status_code=400,
            detail=f"Invalid provider: {request.provider}. Use 'huggingface' or 'huggingface_api'"
        )

    # 表示名を生成
    display_name = validation.get("modelId", request.huggingface_model_name).split("/")[-1]

    # モデルを追加
    new_model = ModelConfig(
        model_id=model_id,
        display_name=display_name,
        provider=provider,
        model_name=request.huggingface_model_name,
        max_tokens=4096,
        temperature=0.7,
        timeout=120,
        is_default=False,
        supported_domains=request.supported_domains,
        description=f"Added from HuggingFace Hub by {current_user.display_name or current_user.id}"
    )

    config.add_model(new_model)

    return {
        "status": "success",
        "message": f"Model '{model_id}' added successfully",
        "model": {
            "model_id": model_id,
            "display_name": display_name,
            "provider": provider.value,
            "model_name": request.huggingface_model_name,
            "supported_domains": request.supported_domains
        }
    }


@router.post("/switch")
async def switch_active_model(
    model_id: str,
    domain_id: str = "general",
    current_user: User = Depends(get_current_user)
):
    """
    アクティブなモデルを切り替え。
    セッション単位で使用するモデルを変更できる。
    """
    config = get_config_manager()
    model = config.get_model(model_id)

    if not model:
        raise HTTPException(status_code=404, detail=f"Model '{model_id}' not found")

    # ドメインの確認
    domain = config.get_domain(domain_id)
    if not domain:
        raise HTTPException(status_code=404, detail=f"Domain '{domain_id}' not found")

    return {
        "status": "success",
        "active_model": {
            "model_id": model.model_id,
            "display_name": model.display_name,
            "provider": model.provider.value
        },
        "domain_id": domain_id,
        "message": f"Switched to {model.display_name} for {domain.name} domain"
    }