File size: 9,702 Bytes
962d69a cc938ac 962d69a cc938ac 962d69a 66e3d13 962d69a 66e3d13 962d69a cc938ac 962d69a 66e3d13 962d69a e65a6fb 962d69a 91c69cd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 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 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | import json
import traceback
import uuid
from datetime import datetime
import fastapi
import httpx
import time
from fastapi import FastAPI, BackgroundTasks, Request
from starlette.responses import StreamingResponse, Response
app = FastAPI()
token = ''
async def make_request_to_139_ai_streaming_chat(user_message):
url = "https://ai.yun.139.com/api/outer/assistant/chat/add"
# 设置请求头
headers = {
"sec-ch-ua-platform": "\"Windows\"",
"authorization": f"Basic {token}",
"x-yun-client-info": "4g||30|||||||1685/948|zh-CN||||",
"sec-ch-ua": "\"Microsoft Edge\";v=\"135\", \"Not-A.Brand\";v=\"8\", \"Chromium\";v=\"135\"",
"sec-ch-ua-mobile": "?0",
"x-yun-api-version": "v2",
"x-yun-app-channel": "10175",
"accept": "text/event-stream",
"dnt": "1",
"content-type": "application/json",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0",
"origin": "https://appmail.mail.10086.cn",
"sec-fetch-site": "cross-site",
"sec-fetch-mode": "cors",
"sec-fetch-dest": "empty",
"referer": "https://appmail.mail.10086.cn/",
"accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,en-GB;q=0.6",
"priority": "u=1, i"
}
# 设置请求体
payload = {
"applicationId": "3000000002",
"applicationType": "intelligent",
"sessionId": "",
"content": {
"dialogue": user_message,
"prompt": "",
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"commands": "000",
"resourceType": "0",
"resourceId": "",
"dialogueType": "0",
"sourceChannel": "10175",
"commandType": "2",
"extInfo": "{\"h5Version\":\"1.5.1\"}"
}
}
# 使用异步客户端发送请求
async with httpx.AsyncClient() as client:
async with client.stream(
"POST",
url,
headers=headers,
json=payload,
timeout=30.0 # 设置超时时间
) as response:
response.raise_for_status() # 检查响应状态码
response_id = f"chatcmpl-{str(uuid.uuid4())}"
async for line in response.aiter_lines():
# print(line)
if line.startswith('data:'):
data_json = json.loads(line[5:].strip())
if 'data' in data_json and 'flowResult' in data_json['data'] and 'outContent' in data_json['data']['flowResult']:
delta_text = data_json['data']['flowResult']['outContent']
# line += delta_text
chunk = {
"id": response_id,
"object": "chat.completion.chunk",
"created": int(time.time()),
"model": "DeepSeek-R1",
"choices": [
{
"index": 0,
"delta": {
"content": delta_text
},
"finish_reason": None
}
]
}
yield f"data: {json.dumps(chunk)}\n\n"
final_chunk = {
"id": response_id,
"object": "chat.completion.chunk",
"created": int(time.time()),
"model": "DeepSeek-R1",
"choices": [
{
"index": 0,
"delta": {},
"finish_reason": "stop"
}
]
}
yield f"data: {json.dumps(final_chunk)}\n\n"
yield "data: [DONE]\n\n"
async def make_request_to_139_ai_non_streaming_chat(user_message):
"""
向139 AI发送异步请求
"""
url = "https://ai.yun.139.com/api/outer/assistant/chat/add"
# 设置请求头
headers = {
"sec-ch-ua-platform": "\"Windows\"",
"authorization": f"Basic {token}",
"x-yun-client-info": "4g||30|||||||1685/948|zh-CN||||",
"sec-ch-ua": "\"Microsoft Edge\";v=\"135\", \"Not-A.Brand\";v=\"8\", \"Chromium\";v=\"135\"",
"sec-ch-ua-mobile": "?0",
"x-yun-api-version": "v2",
"x-yun-app-channel": "10175",
"dnt": "1",
"content-type": "application/json",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0",
"origin": "https://appmail.mail.10086.cn",
"sec-fetch-site": "cross-site",
"sec-fetch-mode": "cors",
"sec-fetch-dest": "empty",
"referer": "https://appmail.mail.10086.cn/",
"accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,en-GB;q=0.6",
"priority": "u=1, i"
}
# 设置请求体
payload = {
"applicationId": "3000000002",
"applicationType": "intelligent",
"sessionId": "",
"content": {
"dialogue": user_message,
"prompt": "",
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"commands": "000",
"resourceType": "0",
"resourceId": "",
"dialogueType": "0",
"sourceChannel": "10175",
"commandType": "2",
"extInfo": "{\"h5Version\":\"1.5.1\"}"
}
}
async with httpx.AsyncClient() as client:
async with client.stream(
"POST",
url,
headers=headers,
json=payload,
timeout=30.0 # 设置超时时间
) as response:
response.raise_for_status() # 检查响应状态码
reasoning_content = ""
full_content = ""
async for line in response.aiter_lines():
# print(line)
if line.startswith('data:'):
data_json = json.loads(line[5:].strip())
if 'data' in data_json and 'flowResult' in data_json['data'] and 'outContent' in data_json['data']['flowResult']:
delta_text = data_json['data']['flowResult']['outContent']
full_content += delta_text
response_data = {
"id": f"chatcmpl-{str(uuid.uuid4())}",
"object": "chat.completion",
"created": int(time.time()),
"model": data_json.get('modelType', 'DeepSeek-R1'),
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": full_content},
"finish_reason": "stop"
}
],
"usage":
{
"prompt_tokens": len(user_message),
"completion_tokens": len(full_content),
"total_tokens": len(user_message) + len(full_content)
}
}
return response_data
@app.post("/v1/chat/completions")
async def chat_endpoint(request: Request, background_tasks: BackgroundTasks):
"""
API 端点,用于发送聊天请求到139 AI
"""
try:
auth_header = request.headers.get('Authorization', '')
if auth_header.startswith('Bearer '):
global token
token = auth_header[7:]
else:
raise fastapi.HTTPException(status_code=401, detail="Authorization header must be a Bearer token")
user_message = ""
request_body = await request.json()
for msg in reversed(request_body.get('messages', [])):
if msg.get('role') == 'user':
user_message = msg.get('content', '')
break
if not user_message:
raise fastapi.HTTPException(status_code=400, detail="No user message found")
print(f"user input: {user_message}")
if request_body.get('stream'):
return StreamingResponse(
content=make_request_to_139_ai_streaming_chat(user_message),
media_type="text/event-stream",
# media_type="text/plain",
headers={
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Transfer-Encoding": "chunked"
}
)
else:
# 非流式
response_data = await make_request_to_139_ai_non_streaming_chat(user_message)
return Response(
content=json.dumps(response_data),
media_type="application/json",
status_code=200,
# headers=response_headers
)
except Exception as e:
traceback.print_exc()
return {"status": "error", "message": str(e)}
@app.get('/v1/models')
async def list_models():
models = {
"object": "list",
"data": [
{
"id": "DeepSeek-R1",
"object": "model",
"created": int(time.time()),
"owned_by": "139.com"
}
]
}
return models
if __name__ == "__main__":
# 或者使用 uvicorn 启动 FastAPI 应用
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)
|