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| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
import requests
|
| 5 |
+
import json
|
| 6 |
+
import os
|
| 7 |
+
import uvicorn
|
| 8 |
+
|
| 9 |
+
# Configuration
|
| 10 |
+
TOKEN = os.getenv("POLLINATIONS_TOKEN")
|
| 11 |
+
POLLINATIONS_URL = "https://text.pollinations.ai/openai"
|
| 12 |
+
|
| 13 |
+
app = FastAPI(
|
| 14 |
+
title="Pollinations OpenAI API",
|
| 15 |
+
description="OpenAI Compatible API powered by Pollinations AI",
|
| 16 |
+
version="1.0.0"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# CORS middleware
|
| 20 |
+
app.add_middleware(
|
| 21 |
+
CORSMiddleware,
|
| 22 |
+
allow_origins=["*"],
|
| 23 |
+
allow_credentials=True,
|
| 24 |
+
allow_methods=["*"],
|
| 25 |
+
allow_headers=["*"],
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Available models
|
| 29 |
+
MODELS = [
|
| 30 |
+
"openai", "openai-large", "openai-reasoning", "qwen-coder", "llama",
|
| 31 |
+
"llamascout", "mistral", "unity", "gemini", "phi", "deepseek",
|
| 32 |
+
"deepseek-reasoning", "rtist", "searchgpt", "evil", "openai-audio"
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
@app.get("/")
|
| 36 |
+
async def root():
|
| 37 |
+
return {
|
| 38 |
+
"message": "Pollinations OpenAI Compatible API",
|
| 39 |
+
"status": "running",
|
| 40 |
+
"endpoints": {
|
| 41 |
+
"models": "/models",
|
| 42 |
+
"chat_completions": "/chat/completions",
|
| 43 |
+
"openai_compatible": "/v1/chat/completions"
|
| 44 |
+
},
|
| 45 |
+
"usage": {
|
| 46 |
+
"models": "GET /models",
|
| 47 |
+
"chat": "POST /chat/completions",
|
| 48 |
+
"openai": "POST /v1/chat/completions"
|
| 49 |
+
}
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
@app.get("/health")
|
| 53 |
+
async def health():
|
| 54 |
+
return {"status": "healthy"}
|
| 55 |
+
|
| 56 |
+
@app.get("/models")
|
| 57 |
+
async def get_models():
|
| 58 |
+
"""OpenAI compatible models endpoint"""
|
| 59 |
+
return {
|
| 60 |
+
"object": "list",
|
| 61 |
+
"data": [
|
| 62 |
+
{
|
| 63 |
+
"id": model,
|
| 64 |
+
"object": "model",
|
| 65 |
+
"created": 1677610602,
|
| 66 |
+
"owned_by": "pollinations"
|
| 67 |
+
}
|
| 68 |
+
for model in MODELS
|
| 69 |
+
]
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
@app.post("/chat/completions")
|
| 73 |
+
@app.post("/v1/chat/completions")
|
| 74 |
+
async def chat_completions(data: dict):
|
| 75 |
+
"""OpenAI compatible chat completions endpoint"""
|
| 76 |
+
|
| 77 |
+
model = data.get("model", "openai")
|
| 78 |
+
messages = data.get("messages", [])
|
| 79 |
+
temperature = data.get("temperature", 0.7)
|
| 80 |
+
max_tokens = data.get("max_tokens", 1000)
|
| 81 |
+
stream = data.get("stream", False)
|
| 82 |
+
|
| 83 |
+
if not messages:
|
| 84 |
+
return JSONResponse(
|
| 85 |
+
status_code=400,
|
| 86 |
+
content={"error": {"message": "messages field is required"}}
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# Prepare payload for Pollinations
|
| 90 |
+
payload = {
|
| 91 |
+
"model": model,
|
| 92 |
+
"messages": messages,
|
| 93 |
+
"temperature": temperature,
|
| 94 |
+
"max_tokens": max_tokens,
|
| 95 |
+
"referrer": "hf-openai-api"
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
if stream:
|
| 99 |
+
payload["stream"] = True
|
| 100 |
+
|
| 101 |
+
# Headers
|
| 102 |
+
headers = {"Content-Type": "application/json"}
|
| 103 |
+
if TOKEN:
|
| 104 |
+
headers["Authorization"] = f"Bearer {TOKEN}"
|
| 105 |
+
|
| 106 |
+
try:
|
| 107 |
+
response = requests.post(
|
| 108 |
+
POLLINATIONS_URL,
|
| 109 |
+
json=payload,
|
| 110 |
+
headers=headers,
|
| 111 |
+
timeout=120
|
| 112 |
+
)
|
| 113 |
+
response.raise_for_status()
|
| 114 |
+
result = response.json()
|
| 115 |
+
|
| 116 |
+
# Convert to OpenAI format
|
| 117 |
+
openai_response = {
|
| 118 |
+
"id": f"chatcmpl-{hash(str(payload)) % 10000000}",
|
| 119 |
+
"object": "chat.completion",
|
| 120 |
+
"created": 1677858242,
|
| 121 |
+
"model": model,
|
| 122 |
+
"choices": [{
|
| 123 |
+
"index": 0,
|
| 124 |
+
"message": {
|
| 125 |
+
"role": "assistant",
|
| 126 |
+
"content": result["choices"][0]["message"]["content"]
|
| 127 |
+
},
|
| 128 |
+
"finish_reason": "stop"
|
| 129 |
+
}],
|
| 130 |
+
"usage": {
|
| 131 |
+
"prompt_tokens": len(str(messages)),
|
| 132 |
+
"completion_tokens": len(result["choices"][0]["message"]["content"]),
|
| 133 |
+
"total_tokens": len(str(messages)) + len(result["choices"][0]["message"]["content"])
|
| 134 |
+
}
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
return openai_response
|
| 138 |
+
|
| 139 |
+
except requests.exceptions.RequestException as e:
|
| 140 |
+
return JSONResponse(
|
| 141 |
+
status_code=500,
|
| 142 |
+
content={"error": {"message": f"Pollinations API error: {str(e)}"}}
|
| 143 |
+
)
|
| 144 |
+
except Exception as e:
|
| 145 |
+
return JSONResponse(
|
| 146 |
+
status_code=500,
|
| 147 |
+
content={"error": {"message": f"Server error: {str(e)}"}}
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
if __name__ == "__main__":
|
| 151 |
+
port = int(os.environ.get("PORT", 7860))
|
| 152 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|