-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathapp.py
More file actions
74 lines (55 loc) · 2.05 KB
/
Copy pathapp.py
File metadata and controls
74 lines (55 loc) · 2.05 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
FastAPI application entry point for the Conversation Knowledge Mining Solution Accelerator.
This module sets up the FastAPI app, configures middleware, loads environment variables,
registers API routers, and manages application lifespan events such as agent initialization
and cleanup.
"""
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from dotenv import load_dotenv
import uvicorn
from common.config.config import Config
from agents.agent_factory import AgentFactory
from api.api_routes import router as backend_router
from api.history_routes import router as history_router
load_dotenv()
@asynccontextmanager
async def lifespan(fastapi_app: FastAPI):
"""
Manages the application lifespan events for the FastAPI app.
On startup, initializes the Azure AI agent using the configuration and attaches it to the app state.
On shutdown, deletes the agent instance and performs any necessary cleanup.
"""
config = Config()
fastapi_app.state.agent = await AgentFactory.get_instance(config=config)
yield
await AgentFactory.delete_instance()
fastapi_app.state.agent = None
def build_app() -> FastAPI:
"""
Creates and configures the FastAPI application instance.
"""
fastapi_app = FastAPI(
title="Conversation Knowledge Mining Solution Accelerator",
version="1.0.0",
lifespan=lifespan
)
fastapi_app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include routers
fastapi_app.include_router(backend_router, prefix="/api", tags=["backend"])
fastapi_app.include_router(history_router, prefix="/history", tags=["history"])
@fastapi_app.get("/health")
async def health_check():
"""Health check endpoint"""
return {"status": "healthy"}
return fastapi_app
app = build_app()
if __name__ == "__main__":
uvicorn.run("app:app", host="127.0.0.1", port=8000, reload=True)