Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from typing import List, Dict, Any
|
| 4 |
+
import re
|
| 5 |
+
import random
|
| 6 |
+
import string
|
| 7 |
+
from aiohttp import ClientSession
|
| 8 |
+
|
| 9 |
+
# Mocking the ImageResponse and to_data_uri functions
|
| 10 |
+
class ImageResponse:
|
| 11 |
+
def __init__(self, url: str, alt: str):
|
| 12 |
+
self.url = url
|
| 13 |
+
self.alt = alt
|
| 14 |
+
|
| 15 |
+
def to_data_uri(image: Any) -> str:
|
| 16 |
+
# Placeholder for actual image encoding
|
| 17 |
+
return "data:image/png;base64,..." # Replace with actual base64 data
|
| 18 |
+
|
| 19 |
+
class AsyncGeneratorProvider:
|
| 20 |
+
pass
|
| 21 |
+
|
| 22 |
+
class ProviderModelMixin:
|
| 23 |
+
pass
|
| 24 |
+
|
| 25 |
+
class Blackbox(AsyncGeneratorProvider, ProviderModelMixin):
|
| 26 |
+
url = "https://www.blackbox.ai"
|
| 27 |
+
api_endpoint = "https://www.blackbox.ai/api/chat"
|
| 28 |
+
default_model = 'blackbox'
|
| 29 |
+
models = ['blackbox', 'gemini-1.5-flash', "llama-3.1-8b"]
|
| 30 |
+
|
| 31 |
+
@classmethod
|
| 32 |
+
async def create_async_generator(cls, model: str, messages: List[Dict[str, str]]) -> Any:
|
| 33 |
+
# Mock response for demonstration
|
| 34 |
+
return {"content": "This is a mock response from the model."}
|
| 35 |
+
|
| 36 |
+
app = FastAPI()
|
| 37 |
+
|
| 38 |
+
class Message(BaseModel):
|
| 39 |
+
role: str
|
| 40 |
+
content: str
|
| 41 |
+
|
| 42 |
+
class ChatRequest(BaseModel):
|
| 43 |
+
model: str
|
| 44 |
+
messages: List[Message]
|
| 45 |
+
|
| 46 |
+
@app.post("/v1/chat/completions")
|
| 47 |
+
async def chat_completions(request: ChatRequest):
|
| 48 |
+
messages = [{"role": msg.role, "content": msg.content} for msg in request.messages]
|
| 49 |
+
|
| 50 |
+
response = await Blackbox.create_async_generator(
|
| 51 |
+
model=request.model,
|
| 52 |
+
messages=messages
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
return {
|
| 56 |
+
"id": "chatcmpl-1234", # Example ID, generate as needed
|
| 57 |
+
"object": "chat.completion",
|
| 58 |
+
"created": 1690000000, # Replace with actual timestamp
|
| 59 |
+
"model": request.model,
|
| 60 |
+
"choices": [
|
| 61 |
+
{
|
| 62 |
+
"message": {
|
| 63 |
+
"role": "assistant",
|
| 64 |
+
"content": response['content']
|
| 65 |
+
},
|
| 66 |
+
"finish_reason": "stop",
|
| 67 |
+
"index": 0
|
| 68 |
+
}
|
| 69 |
+
]
|
| 70 |
+
}
|