Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ from fastapi.responses import StreamingResponse, JSONResponse
|
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from typing import List, Optional
|
| 6 |
|
|
|
|
| 7 |
import json
|
| 8 |
import time
|
| 9 |
import uuid
|
|
@@ -34,7 +35,7 @@ API_KEY = "sk-your-secret-key"
|
|
| 34 |
|
| 35 |
app = FastAPI(
|
| 36 |
title="Universal AI Gateway",
|
| 37 |
-
version="5.
|
| 38 |
)
|
| 39 |
|
| 40 |
|
|
@@ -106,12 +107,12 @@ async def root():
|
|
| 106 |
return {
|
| 107 |
"status": "online",
|
| 108 |
"service": "Universal AI Gateway",
|
| 109 |
-
"version": "5.
|
| 110 |
}
|
| 111 |
|
| 112 |
|
| 113 |
# =====================================================
|
| 114 |
-
#
|
| 115 |
# =====================================================
|
| 116 |
|
| 117 |
@app.get("/v1/models")
|
|
@@ -145,20 +146,34 @@ async def get_models():
|
|
| 145 |
|
| 146 |
|
| 147 |
# =====================================================
|
| 148 |
-
#
|
| 149 |
-
#
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
# =====================================================
|
| 153 |
|
| 154 |
-
def
|
| 155 |
|
| 156 |
client = Client()
|
| 157 |
|
| 158 |
response = client.chat.completions.create(
|
| 159 |
model=model,
|
| 160 |
messages=messages,
|
| 161 |
-
stream=
|
| 162 |
)
|
| 163 |
|
| 164 |
return response
|
|
@@ -200,10 +215,10 @@ async def chat_completions(
|
|
| 200 |
|
| 201 |
try:
|
| 202 |
|
| 203 |
-
response =
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
)
|
| 208 |
|
| 209 |
has_content = False
|
|
@@ -337,10 +352,10 @@ async def chat_completions(
|
|
| 337 |
|
| 338 |
try:
|
| 339 |
|
| 340 |
-
response =
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
)
|
| 345 |
|
| 346 |
assistant_message = ""
|
|
|
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from typing import List, Optional
|
| 6 |
|
| 7 |
+
import asyncio
|
| 8 |
import json
|
| 9 |
import time
|
| 10 |
import uuid
|
|
|
|
| 35 |
|
| 36 |
app = FastAPI(
|
| 37 |
title="Universal AI Gateway",
|
| 38 |
+
version="5.2.0"
|
| 39 |
)
|
| 40 |
|
| 41 |
|
|
|
|
| 107 |
return {
|
| 108 |
"status": "online",
|
| 109 |
"service": "Universal AI Gateway",
|
| 110 |
+
"version": "5.2.0"
|
| 111 |
}
|
| 112 |
|
| 113 |
|
| 114 |
# =====================================================
|
| 115 |
+
# MODELS
|
| 116 |
# =====================================================
|
| 117 |
|
| 118 |
@app.get("/v1/models")
|
|
|
|
| 146 |
|
| 147 |
|
| 148 |
# =====================================================
|
| 149 |
+
# NORMAL COMPLETION
|
| 150 |
+
# =====================================================
|
| 151 |
+
|
| 152 |
+
def normal_completion(model, messages):
|
| 153 |
+
|
| 154 |
+
client = Client()
|
| 155 |
+
|
| 156 |
+
response = client.chat.completions.create(
|
| 157 |
+
model=model,
|
| 158 |
+
messages=messages,
|
| 159 |
+
stream=False
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
return response
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
# =====================================================
|
| 166 |
+
# STREAM COMPLETION
|
| 167 |
# =====================================================
|
| 168 |
|
| 169 |
+
def stream_completion(model, messages):
|
| 170 |
|
| 171 |
client = Client()
|
| 172 |
|
| 173 |
response = client.chat.completions.create(
|
| 174 |
model=model,
|
| 175 |
messages=messages,
|
| 176 |
+
stream=True
|
| 177 |
)
|
| 178 |
|
| 179 |
return response
|
|
|
|
| 215 |
|
| 216 |
try:
|
| 217 |
|
| 218 |
+
response = await asyncio.to_thread(
|
| 219 |
+
stream_completion,
|
| 220 |
+
body.model,
|
| 221 |
+
messages
|
| 222 |
)
|
| 223 |
|
| 224 |
has_content = False
|
|
|
|
| 352 |
|
| 353 |
try:
|
| 354 |
|
| 355 |
+
response = await asyncio.to_thread(
|
| 356 |
+
normal_completion,
|
| 357 |
+
body.model,
|
| 358 |
+
messages
|
| 359 |
)
|
| 360 |
|
| 361 |
assistant_message = ""
|