Spaces:
Sleeping
Sleeping
Create api/endpoints.py
Browse files- api/endpoints.py +99 -0
api/endpoints.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, HTTPException, Query
|
| 2 |
+
from typing import Optional
|
| 3 |
+
import random
|
| 4 |
+
|
| 5 |
+
from .openrouter import OpenRouterClient
|
| 6 |
+
from .utils import get_openrouter_client
|
| 7 |
+
|
| 8 |
+
router = APIRouter()
|
| 9 |
+
|
| 10 |
+
# Available free models
|
| 11 |
+
FREE_MODELS = [
|
| 12 |
+
"openai/gpt-3.5-turbo",
|
| 13 |
+
"anthropic/claude-instant-v1",
|
| 14 |
+
"google/palm-2-chat-bison",
|
| 15 |
+
"meta-llama/llama-2-13b-chat",
|
| 16 |
+
"mistralai/mistral-7b-instruct"
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
@router.post("/chat")
|
| 20 |
+
async def chat_with_model(
|
| 21 |
+
message: str,
|
| 22 |
+
model: Optional[str] = None,
|
| 23 |
+
temperature: float = 0.7,
|
| 24 |
+
max_tokens: int = 1024
|
| 25 |
+
):
|
| 26 |
+
"""
|
| 27 |
+
Chat with a specified model or let the system choose one
|
| 28 |
+
|
| 29 |
+
Parameters:
|
| 30 |
+
- message: The user's input message
|
| 31 |
+
- model: Optional model identifier (default: random)
|
| 32 |
+
- temperature: Creativity parameter (0-2)
|
| 33 |
+
- max_tokens: Maximum length of response
|
| 34 |
+
"""
|
| 35 |
+
client = get_openrouter_client()
|
| 36 |
+
|
| 37 |
+
if not model:
|
| 38 |
+
model = random.choice(FREE_MODELS)
|
| 39 |
+
elif model not in FREE_MODELS:
|
| 40 |
+
raise HTTPException(status_code=400, detail="Model not available in free tier")
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
response = client.chat_completion(
|
| 44 |
+
model=model,
|
| 45 |
+
messages=[{"role": "user", "content": message}],
|
| 46 |
+
temperature=temperature,
|
| 47 |
+
max_tokens=max_tokens
|
| 48 |
+
)
|
| 49 |
+
return {
|
| 50 |
+
"model": model,
|
| 51 |
+
"response": response["choices"][0]["message"]["content"],
|
| 52 |
+
"usage": response.get("usage", {})
|
| 53 |
+
}
|
| 54 |
+
except Exception as e:
|
| 55 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 56 |
+
|
| 57 |
+
@router.get("/models")
|
| 58 |
+
async def list_available_models():
|
| 59 |
+
"""
|
| 60 |
+
List all available free models
|
| 61 |
+
"""
|
| 62 |
+
return {
|
| 63 |
+
"models": FREE_MODELS,
|
| 64 |
+
"count": len(FREE_MODELS),
|
| 65 |
+
"description": "These models are available in the free tier"
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
@router.post("/smart-chat")
|
| 69 |
+
async def smart_chat_with_model(message: str):
|
| 70 |
+
"""
|
| 71 |
+
Smart chat endpoint that selects the best model based on the input
|
| 72 |
+
|
| 73 |
+
Parameters:
|
| 74 |
+
- message: The user's input message
|
| 75 |
+
"""
|
| 76 |
+
client = get_openrouter_client()
|
| 77 |
+
|
| 78 |
+
# Model selection logic
|
| 79 |
+
if any(word in message.lower() for word in ["code", "program", "python", "javascript"]):
|
| 80 |
+
model = "openai/gpt-3.5-turbo"
|
| 81 |
+
elif any(word in message.lower() for word in ["story", "creative", "write", "poem"]):
|
| 82 |
+
model = "anthropic/claude-instant-v1"
|
| 83 |
+
elif any(word in message.lower() for word in ["fact", "science", "history"]):
|
| 84 |
+
model = "google/palm-2-chat-bison"
|
| 85 |
+
else:
|
| 86 |
+
model = random.choice(FREE_MODELS)
|
| 87 |
+
|
| 88 |
+
try:
|
| 89 |
+
response = client.chat_completion(
|
| 90 |
+
model=model,
|
| 91 |
+
messages=[{"role": "user", "content": message}]
|
| 92 |
+
)
|
| 93 |
+
return {
|
| 94 |
+
"model": model,
|
| 95 |
+
"response": response["choices"][0]["message"]["content"],
|
| 96 |
+
"reason": f"Selected {model} based on input content"
|
| 97 |
+
}
|
| 98 |
+
except Exception as e:
|
| 99 |
+
raise HTTPException(status_code=500, detail=str(e))
|