File size: 890 Bytes
6d1fe81
 
62b1a00
 
 
 
 
 
 
 
 
 
6d1fe81
 
 
 
 
62b1a00
 
 
 
 
 
 
 
 
 
 
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
# 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}