Spaces:
Sleeping
Sleeping
| 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" | |
| ] | |
| 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" | |
| ) |