# uvicorn app:app --host 0.0.0.0 --port 7860 --reload from fastapi import FastAPI, Header, HTTPException from starlette.requests import Request from pydantic import BaseModel, RootModel import starlette # 继承自 BaseModel 而不是直接继承 Request,因为 Request 本身不是一个 Pydantic 模型 class AirsRequest(Request): # request: Request # 添加额外的 api_key 属性 api_key: str app = FastAPI() @app.get("/") def greet_json(): return {"Hello": "World!"} @app.post("/v1/chat/completions") async def chat_completions(request:Request): auth_header = request.headers['authorization'] api_key = auth_header.split()[1] # 分割字符串并取第二个元素 if api_key != "iam-tanbushi": raise HTTPException(status_code=401, detail="Invalid API Key") data = await request.json() # print('data',data) return {"data": data}