BG5 commited on
Commit
d03bd31
·
verified ·
1 Parent(s): 5c7358c

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +36 -11
main.py CHANGED
@@ -1,6 +1,5 @@
1
  import os
2
  from datetime import datetime
3
- import time
4
  from fastapi import FastAPI, HTTPException, Request, Depends
5
  import httpx
6
  import json
@@ -11,6 +10,8 @@ import asyncio
11
  import threading
12
  import string
13
 
 
 
14
  app = FastAPI()
15
  app.add_middleware(
16
  CORSMiddleware,
@@ -44,7 +45,6 @@ async def chat_chunk(body, headers):
44
  headers = await get_headers()
45
  continue
46
  async for line in response.aiter_lines():
47
- time.sleep(0.05)
48
  if line.startswith("data: [DONE]"):
49
  char_data["choices"][0]["finish_reason"] = "stop"
50
  char_data["choices"][0]["delta"] = {}
@@ -150,10 +150,11 @@ async def auth(request: Request):
150
  token = key
151
  #获取Authorization头部
152
  auth_header = request.headers.get('Authorization')
153
- # print(f'{formatted_now()} auth_chunk: {request.headers}')
 
154
  # 如果没有Authorization头部,返回401
155
- # if not auth_header:
156
- # print('没有验证')
157
  # raise HTTPException(status_code=401, detail="Unauthorized")
158
  # # 分割头部字符串
159
  # parts = auth_header.split()
@@ -213,6 +214,16 @@ async def chat_completions(req: Request, authorized: str = Depends(auth)):
213
  raise HTTPException(status_code=400, detail=str(e))
214
  except httpx.ReadTimeout: # 捕获超时异常
215
  raise HTTPException(status_code=408, detail="Request Timeout")
 
 
 
 
 
 
 
 
 
 
216
 
217
  else:
218
  chat_url = f"{baseurl}/duckchat/v1/chat"
@@ -263,7 +274,7 @@ async def chat_completions(req: Request, authorized: str = Depends(auth)):
263
  for attempt in range(max_retries):
264
  try:
265
  async with httpx.AsyncClient() as client:
266
- print(f'{formatted_now()} duckgo_chat: {headers}')
267
  resp = await client.post(chat_url, json=payload, headers=headers, timeout=60)
268
  resp.raise_for_status()
269
  if resp.status_code == 200:
@@ -450,6 +461,16 @@ async def zhipu_chat_completions(req: Request, authorized: str = Depends(auth)):
450
  except httpx.ReadTimeout: # 捕获超时异常
451
  raise HTTPException(status_code=408, detail="Request Timeout")
452
 
 
 
 
 
 
 
 
 
 
 
453
  def formatted_now():
454
  # 获取当前时间
455
  now = datetime.now()
@@ -457,7 +478,7 @@ def formatted_now():
457
 
458
  async def zhipu_chat(url, headers, body):
459
  while True:
460
- print(f'{formatted_now()} zhipu_chat: {headers}')
461
  async with httpx.AsyncClient() as client:
462
  response = await client.post(url, headers=headers, json=body, timeout=120)
463
  if response.status_code == 401:
@@ -465,7 +486,7 @@ async def zhipu_chat(url, headers, body):
465
  continue
466
  break
467
 
468
- print(f'{formatted_now()}: {response.json()}')
469
  return response.json()
470
 
471
 
@@ -513,16 +534,18 @@ def get_token():
513
 
514
  def get_external_ip():
515
  try:
516
- response = httpx.get('https://api.ipify.org?format=json')
517
  response.raise_for_status() # 检查请求是否成功
518
  ip_info = response.json()
519
- print(f"外网 IP 地址: {ip_info}")
 
 
 
520
  return ip_info['ip']
521
  except Exception as e:
522
  print(f"获取外网 IP 地址时出错: {e}")
523
  return None
524
  def schedule_token_update():
525
- # get_external_ip()
526
  get_token()
527
  # 创建一个新的定时器,设置为300秒后再次调用schedule_token_update函数
528
  threading.Timer(300, schedule_token_update).start()
@@ -552,7 +575,9 @@ if __name__ == "__main__":
552
  "uvicorn.access": {"handlers": ["default"], "level": "INFO", "propagate": False},
553
  },
554
  }
 
555
  import uvicorn
 
556
  schedule_token_update()
557
  # uvicorn.run(app, host='0.0.0.0', port=17890, log_level='warning')
558
  uvicorn.run(app, host='0.0.0.0', port=7860, log_config=log_config)
 
1
  import os
2
  from datetime import datetime
 
3
  from fastapi import FastAPI, HTTPException, Request, Depends
4
  import httpx
5
  import json
 
10
  import threading
11
  import string
12
 
13
+ import logging
14
+
15
  app = FastAPI()
16
  app.add_middleware(
17
  CORSMiddleware,
 
45
  headers = await get_headers()
46
  continue
47
  async for line in response.aiter_lines():
 
48
  if line.startswith("data: [DONE]"):
49
  char_data["choices"][0]["finish_reason"] = "stop"
50
  char_data["choices"][0]["delta"] = {}
 
150
  token = key
151
  #获取Authorization头部
152
  auth_header = request.headers.get('Authorization')
153
+ print()
154
+ print(f'{formatted_now()} auth_chunk: {request.headers}')
155
  # 如果没有Authorization头部,返回401
156
+ if not auth_header:
157
+ print('没有验证')
158
  # raise HTTPException(status_code=401, detail="Unauthorized")
159
  # # 分割头部字符串
160
  # parts = auth_header.split()
 
214
  raise HTTPException(status_code=400, detail=str(e))
215
  except httpx.ReadTimeout: # 捕获超时异常
216
  raise HTTPException(status_code=408, detail="Request Timeout")
217
+ elif 'embedding' in usermodel.lower():
218
+ try:
219
+
220
+ url = 'https://open.bigmodel.cn/api/paas/v4/embeddings'
221
+ headers = {'Content-Type': 'application/json',
222
+ 'Authorization': f'Bearer {apiToken}'
223
+ }
224
+ return await zhipu_chat(url, headers, body)
225
+ except json.JSONDecodeError as e:
226
+ raise HTTPException(status_code=400, detail=str(e))
227
 
228
  else:
229
  chat_url = f"{baseurl}/duckchat/v1/chat"
 
274
  for attempt in range(max_retries):
275
  try:
276
  async with httpx.AsyncClient() as client:
277
+ # print(f'{formatted_now()} duckgo_chat: {headers}')
278
  resp = await client.post(chat_url, json=payload, headers=headers, timeout=60)
279
  resp.raise_for_status()
280
  if resp.status_code == 200:
 
461
  except httpx.ReadTimeout: # 捕获超时异常
462
  raise HTTPException(status_code=408, detail="Request Timeout")
463
 
464
+ @app.post("/api/paas/v4/embeddings")
465
+ async def zhipu_embeddings(req: Request, authorized: str = Depends(auth)):
466
+ body = await req.json()
467
+ url = 'https://open.bigmodel.cn/api/paas/v4/embeddings'
468
+ headers = {'Content-Type': 'application/json',
469
+ 'Authorization': f'Bearer {apiToken}'
470
+ }
471
+ return await zhipu_chat(url, headers, body)
472
+
473
+
474
  def formatted_now():
475
  # 获取当前时间
476
  now = datetime.now()
 
478
 
479
  async def zhipu_chat(url, headers, body):
480
  while True:
481
+ # print(f'{formatted_now()} zhipu_chat: {headers}')
482
  async with httpx.AsyncClient() as client:
483
  response = await client.post(url, headers=headers, json=body, timeout=120)
484
  if response.status_code == 401:
 
486
  continue
487
  break
488
 
489
+ # print(f'{formatted_now()}: {response.json()}')
490
  return response.json()
491
 
492
 
 
534
 
535
  def get_external_ip():
536
  try:
537
+ response = httpx.get('https://ipinfo.io/json')
538
  response.raise_for_status() # 检查请求是否成功
539
  ip_info = response.json()
540
+ ip_info2 = f"ip:{ip_info['ip']} city:{ip_info['city']} region:{ip_info['region']} country:{ip_info['country']} timezone:{ip_info['timezone']}"
541
+ # print(f"外网 IP 地址: {ip_info}")
542
+ logging.error(ip_info2)
543
+ logging.shutdown()
544
  return ip_info['ip']
545
  except Exception as e:
546
  print(f"获取外网 IP 地址时出错: {e}")
547
  return None
548
  def schedule_token_update():
 
549
  get_token()
550
  # 创建一个新的定时器,设置为300秒后再次调用schedule_token_update函数
551
  threading.Timer(300, schedule_token_update).start()
 
575
  "uvicorn.access": {"handlers": ["default"], "level": "INFO", "propagate": False},
576
  },
577
  }
578
+ get_external_ip()
579
  import uvicorn
580
+
581
  schedule_token_update()
582
  # uvicorn.run(app, host='0.0.0.0', port=17890, log_level='warning')
583
  uvicorn.run(app, host='0.0.0.0', port=7860, log_config=log_config)