Spaces:
Sleeping
Sleeping
get api_key ok
Browse files
app.py
CHANGED
|
@@ -1,9 +1,28 @@
|
|
| 1 |
# uvicorn app:app --host 0.0.0.0 --port 7860 --reload
|
| 2 |
|
| 3 |
-
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
@app.get("/")
|
| 8 |
def greet_json():
|
| 9 |
-
return {"Hello": "World!"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# uvicorn app:app --host 0.0.0.0 --port 7860 --reload
|
| 2 |
|
| 3 |
+
from fastapi import FastAPI, Header, HTTPException
|
| 4 |
+
from starlette.requests import Request
|
| 5 |
+
from pydantic import BaseModel, RootModel
|
| 6 |
+
import starlette
|
| 7 |
+
|
| 8 |
+
# 继承自 BaseModel 而不是直接继承 Request,因为 Request 本身不是一个 Pydantic 模型
|
| 9 |
+
class AirsRequest(Request):
|
| 10 |
+
# request: Request
|
| 11 |
+
# 添加额外的 api_key 属性
|
| 12 |
+
api_key: str
|
| 13 |
|
| 14 |
app = FastAPI()
|
| 15 |
|
| 16 |
@app.get("/")
|
| 17 |
def greet_json():
|
| 18 |
+
return {"Hello": "World!"}
|
| 19 |
+
|
| 20 |
+
@app.post("/v1/chat/completions")
|
| 21 |
+
async def chat_completions(request:Request):
|
| 22 |
+
auth_header = request.headers['authorization']
|
| 23 |
+
api_key = auth_header.split()[1] # 分割字符串并取第二个元素
|
| 24 |
+
if api_key != "iam-tanbushi":
|
| 25 |
+
raise HTTPException(status_code=401, detail="Invalid API Key")
|
| 26 |
+
data = await request.json()
|
| 27 |
+
# print('data',data)
|
| 28 |
+
return {"data": data}
|