wanazesi commited on
Commit
e196795
·
verified ·
1 Parent(s): fb31778

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +41 -40
main.py CHANGED
@@ -1,9 +1,9 @@
1
  import os
2
  import json
3
- import asyncio
4
  import httpx
5
  from fastapi import FastAPI, Request
6
- from fastapi.responses import StreamingResponse
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
- async def generate():
64
- # 设置 120 秒超时,防止长文本生成失败
65
- async with httpx.AsyncClient(timeout=120.0) as client:
66
- # 已修复:改为 app.airops.com
67
- url = f"https://app.airops.com/public_api/airops_apps/{AIROPS_APP_ID}/execute"
68
-
69
- try:
70
- response = await client.post(url, headers=headers, json=payload)
71
- if response.status_code == 200:
72
- data = response.json()
73
- # 提取 AirOps 返回的文本结果
74
- result_text = data.get("result", "")
75
-
76
- if isinstance(result_text, dict) or isinstance(result_text, list):
77
- result_text = json.dumps(result_text, ensure_ascii=False)
78
-
79
- # 核心:将静态结果转换为完美的 OpenAI 流式打字机输出
80
- chunk_size = 3
81
- for i in range(0, len(result_text), chunk_size):
82
- chunk = result_text[i:i+chunk_size]
83
- openai_chunk = {
84
- "id": "chatcmpl-airops",
85
- "object": "chat.completion.chunk",
86
- "choices": [{"delta": {"content": chunk}}]
 
 
 
 
87
  }
88
- yield f"data: {json.dumps(openai_chunk)}\n\n"
89
- await asyncio.sleep(0.01) # 极速打字机效果
90
-
91
- yield "data: [DONE]\n\n"
92
- else:
93
- err = {"error": f"AirOps 接口报错: {response.text}"}
94
- yield f"data: {json.dumps(err)}\n\n"
95
- yield "data: [DONE]\n\n"
96
- except Exception as e:
97
- err = {"error": f"代理服务器内部错误: {str(e)}"}
98
- yield f"data: {json.dumps(err)}\n\n"
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)}"})