first test
Browse files- app.py +44 -1
- requirements.txt +3 -1
- 说明.md +12 -0
app.py
CHANGED
|
@@ -1,7 +1,50 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
app = FastAPI()
|
| 4 |
|
| 5 |
@app.get("/")
|
| 6 |
def greet_json():
|
| 7 |
return {"Hello": "World!"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# uvicorn app:app --host 0.0.0.0 --port 7860 --reload
|
| 2 |
+
|
| 3 |
+
from fastapi import FastAPI, Request, HTTPException
|
| 4 |
+
import re
|
| 5 |
+
import httpx
|
| 6 |
+
# import requests
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
@app.get("/")
|
| 11 |
def greet_json():
|
| 12 |
return {"Hello": "World!"}
|
| 13 |
+
|
| 14 |
+
@app.get("/v1/{dest_url:path}")
|
| 15 |
+
async def gre_dest_url(dest_url:str):
|
| 16 |
+
return dest_url
|
| 17 |
+
|
| 18 |
+
@app.post("/v1/{dest_url:path}")
|
| 19 |
+
async def proxy_url(dest_url: str, request: Request):
|
| 20 |
+
body = await request.body()
|
| 21 |
+
headers = dict(request.headers) # 将请求头转换为字典
|
| 22 |
+
|
| 23 |
+
# 移除 Content-Length 和 Host 头部
|
| 24 |
+
if 'content-length' in headers:
|
| 25 |
+
del headers['content-length']
|
| 26 |
+
if 'host' in headers:
|
| 27 |
+
del headers['host']
|
| 28 |
+
|
| 29 |
+
dest_url = re.sub('/', '://', dest_url, count=1)
|
| 30 |
+
|
| 31 |
+
async with httpx.AsyncClient() as client:
|
| 32 |
+
try:
|
| 33 |
+
# 向目标 URL 发送 POST 请求
|
| 34 |
+
response = await client.post(dest_url, content=body, headers=headers)
|
| 35 |
+
|
| 36 |
+
# 检查响应状态码
|
| 37 |
+
if response.status_code == 200:
|
| 38 |
+
# 返回响应内容(假设是 JSON)
|
| 39 |
+
return response.json()
|
| 40 |
+
else:
|
| 41 |
+
# 将错误响应转换为 JSON 格式并返回给客户端
|
| 42 |
+
try:
|
| 43 |
+
error_data = response.json()
|
| 44 |
+
except ValueError:
|
| 45 |
+
error_data = {"status_code": response.status_code, "detail": response.text}
|
| 46 |
+
return error_data
|
| 47 |
+
|
| 48 |
+
except httpx.RequestError as e:
|
| 49 |
+
# 处理请求错误
|
| 50 |
+
raise HTTPException(status_code=500, detail=str(e))
|
requirements.txt
CHANGED
|
@@ -1,2 +1,4 @@
|
|
| 1 |
fastapi
|
| 2 |
-
uvicorn[standard]
|
|
|
|
|
|
|
|
|
| 1 |
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
httpx
|
| 4 |
+
requests
|
说明.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
设计目标:只解决api_key的共享和负载均衡,模型数据格式请自己查询相应模型的文档,此项目不做对应处理
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
代理api格式,以智谱AI为例
|
| 5 |
+
本地 LC:http://127.0.0.1:7860/v1/https/open.bigmodel.cn/api/paas/v4/chat/completions
|
| 6 |
+
远程 HF:https://tanbushi-api-proxy.hf.space/v1/https/open.bigmodel.cn/api/paas/v4/chat/completions
|
| 7 |
+
格式说明:
|
| 8 |
+
- http协议栈:http:// 或 https://
|
| 9 |
+
- 主机名+端口: 127.0.0.1:7860 或 tanbushi-api-proxy.hf.space
|
| 10 |
+
- 代理协议版本:v1(或其他)
|
| 11 |
+
- 目标URL地址的协议栈:http 或 https (注意此处后面跟/,而不是://)
|
| 12 |
+
- 目标URL地址的主机名:open.bigmodel.cn(或其他)
|