Update main.py
Browse files
main.py
CHANGED
|
@@ -151,18 +151,27 @@ async def list_models():
|
|
| 151 |
async def chat_completions(
|
| 152 |
request: ChatRequest, app_secret: str = Depends(verify_app_secret)
|
| 153 |
):
|
|
|
|
| 154 |
logger.info(f"Received chat completion request for model: {request.model}")
|
| 155 |
|
|
|
|
|
|
|
|
|
|
| 156 |
if request.model not in [model['id'] for model in ALLOWED_MODELS]:
|
|
|
|
| 157 |
raise HTTPException(
|
| 158 |
status_code=400,
|
| 159 |
detail=f"Model {request.model} is not allowed. Allowed models are: {', '.join(model['id'] for model in ALLOWED_MODELS)}",
|
| 160 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
# Generate a UUID
|
| 162 |
original_uuid = uuid.uuid4()
|
| 163 |
uuid_str = str(original_uuid).replace("-", "")
|
| 164 |
|
| 165 |
-
#
|
| 166 |
json_data = {
|
| 167 |
'prompt': "\n".join(
|
| 168 |
[
|
|
@@ -205,6 +214,9 @@ async def chat_completions(
|
|
| 205 |
async def generate():
|
| 206 |
async with httpx.AsyncClient() as client:
|
| 207 |
try:
|
|
|
|
|
|
|
|
|
|
| 208 |
async with client.stream('POST', 'https://sider.ai/api/v3/completion/text', headers=headers, json=json_data, timeout=120.0) as response:
|
| 209 |
response.raise_for_status()
|
| 210 |
async for line in response.aiter_lines():
|
|
@@ -228,11 +240,14 @@ async def chat_completions(
|
|
| 228 |
full_response = ""
|
| 229 |
async for chunk in generate():
|
| 230 |
if chunk.startswith("data: ") and not chunk[6:].startswith("[DONE]"):
|
| 231 |
-
#
|
| 232 |
data = json.loads(chunk[6:])
|
|
|
|
|
|
|
| 233 |
if data["choices"][0]["delta"].get("content"):
|
| 234 |
full_response += data["choices"][0]["delta"]["content"]
|
| 235 |
|
|
|
|
| 236 |
return {
|
| 237 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 238 |
"object": "chat.completion",
|
|
@@ -247,8 +262,3 @@ async def chat_completions(
|
|
| 247 |
],
|
| 248 |
"usage": None,
|
| 249 |
}
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
if __name__ == "__main__":
|
| 254 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 151 |
async def chat_completions(
|
| 152 |
request: ChatRequest, app_secret: str = Depends(verify_app_secret)
|
| 153 |
):
|
| 154 |
+
# Log the model requested by the client
|
| 155 |
logger.info(f"Received chat completion request for model: {request.model}")
|
| 156 |
|
| 157 |
+
# Log allowed models for verification purposes
|
| 158 |
+
logger.info(f"Allowed models: {[model['id'] for model in ALLOWED_MODELS]}")
|
| 159 |
+
|
| 160 |
if request.model not in [model['id'] for model in ALLOWED_MODELS]:
|
| 161 |
+
logger.error(f"Model {request.model} is not allowed.")
|
| 162 |
raise HTTPException(
|
| 163 |
status_code=400,
|
| 164 |
detail=f"Model {request.model} is not allowed. Allowed models are: {', '.join(model['id'] for model in ALLOWED_MODELS)}",
|
| 165 |
)
|
| 166 |
+
|
| 167 |
+
# Log the JSON payload for the request to the external API
|
| 168 |
+
logger.info(f"Sending request to external API with model: {request.model}")
|
| 169 |
+
|
| 170 |
# Generate a UUID
|
| 171 |
original_uuid = uuid.uuid4()
|
| 172 |
uuid_str = str(original_uuid).replace("-", "")
|
| 173 |
|
| 174 |
+
# Prepare json_data for the external request
|
| 175 |
json_data = {
|
| 176 |
'prompt': "\n".join(
|
| 177 |
[
|
|
|
|
| 214 |
async def generate():
|
| 215 |
async with httpx.AsyncClient() as client:
|
| 216 |
try:
|
| 217 |
+
# Log request details before making the API call
|
| 218 |
+
logger.info(f"External API request json_data: {json.dumps(json_data, indent=2)}")
|
| 219 |
+
|
| 220 |
async with client.stream('POST', 'https://sider.ai/api/v3/completion/text', headers=headers, json=json_data, timeout=120.0) as response:
|
| 221 |
response.raise_for_status()
|
| 222 |
async for line in response.aiter_lines():
|
|
|
|
| 240 |
full_response = ""
|
| 241 |
async for chunk in generate():
|
| 242 |
if chunk.startswith("data: ") and not chunk[6:].startswith("[DONE]"):
|
| 243 |
+
# Parse the chunk data and log it for debugging
|
| 244 |
data = json.loads(chunk[6:])
|
| 245 |
+
logger.info(f"Chunk data received: {data}")
|
| 246 |
+
|
| 247 |
if data["choices"][0]["delta"].get("content"):
|
| 248 |
full_response += data["choices"][0]["delta"]["content"]
|
| 249 |
|
| 250 |
+
logger.info(f"Full response generated for model {request.model}: {full_response}")
|
| 251 |
return {
|
| 252 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 253 |
"object": "chat.completion",
|
|
|
|
| 262 |
],
|
| 263 |
"usage": None,
|
| 264 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|