File size: 1,930 Bytes
b0ce8a9
9ecb27b
 
b0ce8a9
9ecb27b
 
 
 
 
b0ce8a9
9ecb27b
 
 
 
 
 
 
 
 
 
 
 
 
b0ce8a9
 
9ecb27b
 
 
 
b0ce8a9
 
 
 
 
 
9ecb27b
 
 
 
 
b0ce8a9
 
 
 
9ecb27b
 
b0ce8a9
9ecb27b
 
 
 
 
 
 
 
 
 
 
 
b0ce8a9
 
 
 
 
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
from fastapi import APIRouter, HTTPException, Query, status
from typing import Optional
import random
import logging

from .openrouter import OpenRouterClient
from .utils import get_openrouter_client

router = APIRouter()
logger = logging.getLogger(__name__)

FREE_MODELS = [
    "openai/gpt-3.5-turbo",
    "anthropic/claude-instant-v1",
    "google/palm-2-chat-bison",
    "meta-llama/llama-2-13b-chat",
    "mistralai/mistral-7b-instruct"
]

@router.post("/chat")
async def chat_with_model(
    message: str,
    model: Optional[str] = None,
    temperature: float = Query(0.7, ge=0, le=2),
    max_tokens: int = Query(1024, ge=1, le=4096)
):
    """
    Chat with a specified model or let the system choose one
    """
    if not message.strip():
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="Message cannot be empty"
        )
    
    client = get_openrouter_client()
    
    if not model:
        model = random.choice(FREE_MODELS)
    elif model not in FREE_MODELS:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=f"Model not available in free tier. Available models: {', '.join(FREE_MODELS)}"
        )
    
    try:
        logger.info(f"Processing chat request with model: {model}")
        response = client.chat_completion(
            model=model,
            messages=[{"role": "user", "content": message}],
            temperature=temperature,
            max_tokens=max_tokens
        )
        return {
            "model": model,
            "response": response["choices"][0]["message"]["content"],
            "usage": response.get("usage", {})
        }
    except Exception as e:
        logger.error(f"Error processing chat request: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Error processing your request"
        )