60 lines
1.4 KiB
Python
60 lines
1.4 KiB
Python
import datetime
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi import FastAPI ,Header
|
|
from openai import AsyncOpenAI
|
|
from routes.rag_base import router as rag_base
|
|
|
|
|
|
async def get_oss_client():
|
|
LLM_URL = "http://172.16.29.102:8001/v1/"
|
|
client = await AsyncOpenAI(base_url= LLM_URL, api_key="EMPTY")
|
|
return client
|
|
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
app = FastAPI(title="qachat2 Backend", version="0.1.0")
|
|
origins = ["*"]
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=origins,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# app.state.settings = get_settings()
|
|
|
|
@app.on_event("startup")
|
|
async def on_startup() -> None:
|
|
print("startup app")
|
|
client = getattr(app.state, "oss_client", None)
|
|
if not client :
|
|
client = get_oss_client()
|
|
app.state.oss_client = client
|
|
|
|
|
|
@app.on_event("shutdown")
|
|
async def on_shutdown() -> None:
|
|
client = getattr(app.state, "elastic_client", None)
|
|
if client is not None:
|
|
await client.close()
|
|
|
|
|
|
@app.get("/")
|
|
async def simple():
|
|
return "ai rag caht qanon OK"
|
|
|
|
@app.get("/ping")
|
|
async def ping():
|
|
return "ai rag caht qanon OK"
|
|
|
|
app.include_router(rag_base, prefix="")
|
|
return app
|
|
|
|
|
|
app = create_app()
|
|
|
|
|