Update main.py
Browse files
main.py
CHANGED
|
@@ -16,7 +16,7 @@ if not REPLICATE_API_TOKEN:
|
|
| 16 |
raise ValueError("REPLICATE_API_TOKEN environment variable not set.")
|
| 17 |
|
| 18 |
# FastAPI Init
|
| 19 |
-
app = FastAPI(title="Replicate to OpenAI Compatibility Layer", version="4.
|
| 20 |
|
| 21 |
# --- Pydantic Models ---
|
| 22 |
class ModelCard(BaseModel):
|
|
@@ -33,43 +33,43 @@ class OpenAIChatCompletionRequest(BaseModel):
|
|
| 33 |
SUPPORTED_MODELS = {
|
| 34 |
"llama3-8b-instruct": "meta/meta-llama-3-8b-instruct",
|
| 35 |
"claude-4.5-haiku": "anthropic/claude-4.5-haiku"
|
|
|
|
| 36 |
}
|
| 37 |
|
| 38 |
# --- Core Logic ---
|
| 39 |
-
def prepare_replicate_input(request: OpenAIChatCompletionRequest
|
| 40 |
-
"""
|
|
|
|
|
|
|
| 41 |
payload = {}
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
# Llama 3 and others often support the 'messages' array natively.
|
| 68 |
-
else:
|
| 69 |
-
# Convert Pydantic models to pure dicts
|
| 70 |
-
payload["prompt"] = [msg.dict() for msg in request.messages]
|
| 71 |
|
| 72 |
# Map common OpenAI parameters to Replicate equivalents
|
|
|
|
| 73 |
if request.max_tokens: payload["max_new_tokens"] = request.max_tokens
|
| 74 |
if request.temperature: payload["temperature"] = request.temperature
|
| 75 |
if request.top_p: payload["top_p"] = request.top_p
|
|
@@ -78,85 +78,116 @@ def prepare_replicate_input(request: OpenAIChatCompletionRequest, replicate_mode
|
|
| 78 |
|
| 79 |
async def stream_replicate_sse(replicate_model_id: str, input_payload: dict):
|
| 80 |
"""Handles the full streaming lifecycle using standard Replicate endpoints."""
|
| 81 |
-
# 1. Start Prediction
|
| 82 |
url = f"https://api.replicate.com/v1/models/{replicate_model_id}/predictions"
|
| 83 |
headers = {"Authorization": f"Bearer {REPLICATE_API_TOKEN}", "Content-Type": "application/json"}
|
| 84 |
|
| 85 |
async with httpx.AsyncClient(timeout=60.0) as client:
|
| 86 |
try:
|
| 87 |
-
#
|
| 88 |
response = await client.post(url, headers=headers, json={"input": input_payload, "stream": True})
|
| 89 |
response.raise_for_status()
|
| 90 |
prediction = response.json()
|
| 91 |
stream_url = prediction.get("urls", {}).get("stream")
|
| 92 |
-
prediction_id = prediction.get("id")
|
| 93 |
|
| 94 |
if not stream_url:
|
| 95 |
yield json.dumps({"error": {"message": "Model did not return a stream URL."}})
|
| 96 |
return
|
| 97 |
|
| 98 |
except httpx.HTTPStatusError as e:
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
return
|
| 101 |
|
| 102 |
-
# 2. Connect to the provided Stream URL
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
#
|
| 115 |
-
#
|
| 116 |
-
# We try to load as JSON first, if it fails, use raw data.
|
| 117 |
-
try:
|
| 118 |
-
content = json.loads(data)
|
| 119 |
-
except json.JSONDecodeError:
|
| 120 |
-
content = data
|
| 121 |
-
|
| 122 |
-
if content: # Ensure we don't send empty chunks
|
| 123 |
chunk = {
|
| 124 |
"id": prediction_id, "object": "chat.completion.chunk", "created": int(time.time()), "model": replicate_model_id,
|
| 125 |
-
"choices": [{"index": 0, "delta": {"content":
|
| 126 |
}
|
| 127 |
yield json.dumps(chunk)
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
yield "[DONE]"
|
| 137 |
|
| 138 |
# --- Endpoints ---
|
| 139 |
@app.get("/v1/models")
|
| 140 |
async def list_models():
|
|
|
|
| 141 |
return ModelList(data=[ModelCard(id=k) for k in SUPPORTED_MODELS.keys()])
|
| 142 |
|
| 143 |
@app.post("/v1/chat/completions")
|
| 144 |
async def create_chat_completion(request: OpenAIChatCompletionRequest):
|
|
|
|
| 145 |
if request.model not in SUPPORTED_MODELS:
|
| 146 |
-
raise HTTPException(404, f"Model not found. Available: {list(SUPPORTED_MODELS.keys())}")
|
| 147 |
|
| 148 |
replicate_id = SUPPORTED_MODELS[request.model]
|
| 149 |
-
replicate_input = prepare_replicate_input(request
|
| 150 |
|
| 151 |
if request.stream:
|
| 152 |
-
|
|
|
|
| 153 |
|
| 154 |
# Non-streaming fallback
|
| 155 |
url = f"https://api.replicate.com/v1/models/{replicate_id}/predictions"
|
| 156 |
-
headers = {"Authorization": f"Bearer {REPLICATE_API_TOKEN}", "Content-Type": "application/json", "Prefer": "wait=
|
| 157 |
async with httpx.AsyncClient() as client:
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
raise ValueError("REPLICATE_API_TOKEN environment variable not set.")
|
| 17 |
|
| 18 |
# FastAPI Init
|
| 19 |
+
app = FastAPI(title="Replicate to OpenAI Compatibility Layer", version="4.1.0 (Context Fixed)")
|
| 20 |
|
| 21 |
# --- Pydantic Models ---
|
| 22 |
class ModelCard(BaseModel):
|
|
|
|
| 33 |
SUPPORTED_MODELS = {
|
| 34 |
"llama3-8b-instruct": "meta/meta-llama-3-8b-instruct",
|
| 35 |
"claude-4.5-haiku": "anthropic/claude-4.5-haiku"
|
| 36 |
+
# You can add more models here
|
| 37 |
}
|
| 38 |
|
| 39 |
# --- Core Logic ---
|
| 40 |
+
def prepare_replicate_input(request: OpenAIChatCompletionRequest) -> Dict[str, Any]:
|
| 41 |
+
"""
|
| 42 |
+
Formats the input for Replicate API, preserving the conversational context.
|
| 43 |
+
"""
|
| 44 |
payload = {}
|
| 45 |
|
| 46 |
+
# --- CONTEXT FIX START ---
|
| 47 |
+
# Modern chat models on Replicate (like Llama 3 and Claude 4.5) expect
|
| 48 |
+
# the 'messages' array directly, just like OpenAI.
|
| 49 |
+
# We no longer need to flatten the conversation into a single prompt string.
|
| 50 |
+
|
| 51 |
+
# Extract system prompt if it exists, as some models take it as a separate parameter.
|
| 52 |
+
messages_for_payload = []
|
| 53 |
+
system_prompt = None
|
| 54 |
+
for msg in request.messages:
|
| 55 |
+
if msg.role == "system":
|
| 56 |
+
# Claude and some other models prefer a dedicated system_prompt field.
|
| 57 |
+
system_prompt = str(msg.content)
|
| 58 |
+
else:
|
| 59 |
+
# Handle user/assistant roles. Convert Pydantic model to a standard dict.
|
| 60 |
+
messages_for_payload.append(msg.dict())
|
| 61 |
+
|
| 62 |
+
# The main input for conversation is the 'messages' array.
|
| 63 |
+
payload["messages"] = messages_for_payload
|
| 64 |
+
|
| 65 |
+
# Add system_prompt to the payload if it was found.
|
| 66 |
+
if system_prompt:
|
| 67 |
+
payload["system_prompt"] = system_prompt
|
| 68 |
+
|
| 69 |
+
# --- CONTEXT FIX END ---
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
# Map common OpenAI parameters to Replicate equivalents
|
| 72 |
+
# Note: Replicate's parameter for max tokens is often 'max_new_tokens'
|
| 73 |
if request.max_tokens: payload["max_new_tokens"] = request.max_tokens
|
| 74 |
if request.temperature: payload["temperature"] = request.temperature
|
| 75 |
if request.top_p: payload["top_p"] = request.top_p
|
|
|
|
| 78 |
|
| 79 |
async def stream_replicate_sse(replicate_model_id: str, input_payload: dict):
|
| 80 |
"""Handles the full streaming lifecycle using standard Replicate endpoints."""
|
| 81 |
+
# 1. Start Prediction
|
| 82 |
url = f"https://api.replicate.com/v1/models/{replicate_model_id}/predictions"
|
| 83 |
headers = {"Authorization": f"Bearer {REPLICATE_API_TOKEN}", "Content-Type": "application/json"}
|
| 84 |
|
| 85 |
async with httpx.AsyncClient(timeout=60.0) as client:
|
| 86 |
try:
|
| 87 |
+
# Request a streaming prediction
|
| 88 |
response = await client.post(url, headers=headers, json={"input": input_payload, "stream": True})
|
| 89 |
response.raise_for_status()
|
| 90 |
prediction = response.json()
|
| 91 |
stream_url = prediction.get("urls", {}).get("stream")
|
| 92 |
+
prediction_id = prediction.get("id", "stream-unknown")
|
| 93 |
|
| 94 |
if not stream_url:
|
| 95 |
yield json.dumps({"error": {"message": "Model did not return a stream URL."}})
|
| 96 |
return
|
| 97 |
|
| 98 |
except httpx.HTTPStatusError as e:
|
| 99 |
+
error_details = e.response.text
|
| 100 |
+
try:
|
| 101 |
+
# Try to parse the error for a cleaner message
|
| 102 |
+
error_json = e.response.json()
|
| 103 |
+
error_details = error_json.get("detail", error_details)
|
| 104 |
+
except json.JSONDecodeError:
|
| 105 |
+
pass # Use raw text if not JSON
|
| 106 |
+
yield json.dumps({"error": {"message": f"Upstream Error: {error_details}", "type": "replicate_error"}})
|
| 107 |
return
|
| 108 |
|
| 109 |
+
# 2. Connect to the provided Stream URL and process Server-Sent Events (SSE)
|
| 110 |
+
try:
|
| 111 |
+
async with client.stream("GET", stream_url, headers={"Accept": "text/event-stream"}, timeout=None) as sse:
|
| 112 |
+
current_event = None
|
| 113 |
+
async for line in sse.aiter_lines():
|
| 114 |
+
if line.startswith("event:"):
|
| 115 |
+
current_event = line[len("event:"):].strip()
|
| 116 |
+
elif line.startswith("data:"):
|
| 117 |
+
data = line[len("data:"):].strip()
|
| 118 |
+
|
| 119 |
+
if current_event == "output":
|
| 120 |
+
# The 'output' event for chat models sends one token at a time as a plain string.
|
| 121 |
+
# We don't need to parse it as JSON.
|
| 122 |
+
if data: # Ensure we don't send empty chunks
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
chunk = {
|
| 124 |
"id": prediction_id, "object": "chat.completion.chunk", "created": int(time.time()), "model": replicate_model_id,
|
| 125 |
+
"choices": [{"index": 0, "delta": {"content": data}, "finish_reason": None}]
|
| 126 |
}
|
| 127 |
yield json.dumps(chunk)
|
| 128 |
+
|
| 129 |
+
elif current_event == "done":
|
| 130 |
+
# The 'done' event signals the end of the stream.
|
| 131 |
+
break
|
| 132 |
+
except httpx.ReadTimeout:
|
| 133 |
+
# Handle cases where the stream times out
|
| 134 |
+
yield json.dumps({"error": {"message": "Stream timed out.", "type": "timeout_error"}})
|
| 135 |
+
return
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
# 3. Send the final termination chunk in OpenAI format
|
| 139 |
+
final_chunk = {
|
| 140 |
+
"id": prediction_id, "object": "chat.completion.chunk", "created": int(time.time()), "model": replicate_model_id,
|
| 141 |
+
"choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]
|
| 142 |
+
}
|
| 143 |
+
yield json.dumps(final_chunk)
|
| 144 |
+
# Some clients (like curl) expect a final "[DONE]" message to close the connection.
|
| 145 |
yield "[DONE]"
|
| 146 |
|
| 147 |
# --- Endpoints ---
|
| 148 |
@app.get("/v1/models")
|
| 149 |
async def list_models():
|
| 150 |
+
"""Lists the currently supported models."""
|
| 151 |
return ModelList(data=[ModelCard(id=k) for k in SUPPORTED_MODELS.keys()])
|
| 152 |
|
| 153 |
@app.post("/v1/chat/completions")
|
| 154 |
async def create_chat_completion(request: OpenAIChatCompletionRequest):
|
| 155 |
+
"""Handles chat completion requests, streaming or non-streaming."""
|
| 156 |
if request.model not in SUPPORTED_MODELS:
|
| 157 |
+
raise HTTPException(status_code=404, detail=f"Model not found. Available models: {list(SUPPORTED_MODELS.keys())}")
|
| 158 |
|
| 159 |
replicate_id = SUPPORTED_MODELS[request.model]
|
| 160 |
+
replicate_input = prepare_replicate_input(request)
|
| 161 |
|
| 162 |
if request.stream:
|
| 163 |
+
# Return a streaming response
|
| 164 |
+
return EventSourceResponse(stream_replicate_sse(replicate_id, replicate_input), media_type="text/event-stream")
|
| 165 |
|
| 166 |
# Non-streaming fallback
|
| 167 |
url = f"https://api.replicate.com/v1/models/{replicate_id}/predictions"
|
| 168 |
+
headers = {"Authorization": f"Bearer {REPLICATE_API_TOKEN}", "Content-Type": "application/json", "Prefer": "wait=120"} # Increased wait time
|
| 169 |
async with httpx.AsyncClient() as client:
|
| 170 |
+
try:
|
| 171 |
+
resp = await client.post(url, headers=headers, json={"input": replicate_input}, timeout=130.0)
|
| 172 |
+
resp.raise_for_status()
|
| 173 |
+
pred = resp.json()
|
| 174 |
+
# The output of chat models is typically a list of strings (tokens)
|
| 175 |
+
output = "".join(pred.get("output", []))
|
| 176 |
+
return {
|
| 177 |
+
"id": pred.get("id"),
|
| 178 |
+
"object": "chat.completion",
|
| 179 |
+
"created": int(time.time()),
|
| 180 |
+
"model": request.model,
|
| 181 |
+
"choices": [{
|
| 182 |
+
"index": 0,
|
| 183 |
+
"message": {"role": "assistant", "content": output},
|
| 184 |
+
"finish_reason": "stop"
|
| 185 |
+
}],
|
| 186 |
+
"usage": { # Placeholder usage object
|
| 187 |
+
"prompt_tokens": 0,
|
| 188 |
+
"completion_tokens": 0,
|
| 189 |
+
"total_tokens": 0
|
| 190 |
+
}
|
| 191 |
+
}
|
| 192 |
+
except httpx.HTTPStatusError as e:
|
| 193 |
+
raise HTTPException(status_code=e.response.status_code, detail=f"Error from Replicate API: {e.response.text}")
|