Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
-
import
|
| 4 |
import httpx
|
| 5 |
from fastapi import FastAPI, Request
|
| 6 |
-
from fastapi.responses import
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
|
| 9 |
app = FastAPI()
|
|
@@ -60,42 +60,43 @@ async def chat_completions(request: Request):
|
|
| 60 |
}
|
| 61 |
}
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
"
|
| 86 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
}
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
yield "data: [DONE]\n\n"
|
| 100 |
-
|
| 101 |
-
return StreamingResponse(generate(), media_type="text/event-stream")
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
+
import time
|
| 4 |
import httpx
|
| 5 |
from fastapi import FastAPI, Request
|
| 6 |
+
from fastapi.responses import JSONResponse
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
|
| 9 |
app = FastAPI()
|
|
|
|
| 60 |
}
|
| 61 |
}
|
| 62 |
|
| 63 |
+
# 设置 120 秒超时,防止长文本生成失败
|
| 64 |
+
async with httpx.AsyncClient(timeout=120.0) as client:
|
| 65 |
+
url = f"https://app.airops.com/public_api/airops_apps/{AIROPS_APP_ID}/execute"
|
| 66 |
+
|
| 67 |
+
try:
|
| 68 |
+
response = await client.post(url, headers=headers, json=payload)
|
| 69 |
+
if response.status_code == 200:
|
| 70 |
+
data = response.json()
|
| 71 |
+
# 提取 AirOps 返回的文本结果
|
| 72 |
+
result_text = data.get("result", "")
|
| 73 |
+
|
| 74 |
+
if isinstance(result_text, (dict, list)):
|
| 75 |
+
result_text = json.dumps(result_text, ensure_ascii=False)
|
| 76 |
+
|
| 77 |
+
# 核心修改:取消流式,直接返回标准的 OpenAI 格式 JSON 响应
|
| 78 |
+
return {
|
| 79 |
+
"id": f"chatcmpl-airops-{int(time.time())}",
|
| 80 |
+
"object": "chat.completion",
|
| 81 |
+
"created": int(time.time()),
|
| 82 |
+
"model": "airops-model",
|
| 83 |
+
"choices": [
|
| 84 |
+
{
|
| 85 |
+
"index": 0,
|
| 86 |
+
"message": {
|
| 87 |
+
"role": "assistant",
|
| 88 |
+
"content": result_text
|
| 89 |
+
},
|
| 90 |
+
"finish_reason": "stop"
|
| 91 |
}
|
| 92 |
+
],
|
| 93 |
+
"usage": {
|
| 94 |
+
"prompt_tokens": 0,
|
| 95 |
+
"completion_tokens": 0,
|
| 96 |
+
"total_tokens": 0
|
| 97 |
+
}
|
| 98 |
+
}
|
| 99 |
+
else:
|
| 100 |
+
return JSONResponse(status_code=500, content={"error": f"AirOps 接口报错: {response.text}"})
|
| 101 |
+
except Exception as e:
|
| 102 |
+
return JSONResponse(status_code=500, content={"error": f"代理服务器内部错误: {str(e)}"})
|
|
|
|
|
|
|
|
|