-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
38 lines (29 loc) · 975 Bytes
/
app.py
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
import os
from contextlib import asynccontextmanager
from fastapi import FastAPI
from llama_index.core import Settings
from configs.config import AppConfig
from elastic import get_async_client, create_query_engine, get_llm, initialize_index
config = None
index = None
@asynccontextmanager
async def lifespan(app):
# On app startup
global index
global config
config = AppConfig.from_yaml(os.getenv("CONFIG_FILE_PATH"))
Settings.embed_model = None
Settings.llm = get_llm(config.query.llm_path, env_path=".env")
async_es_client = await get_async_client()
index = await initialize_index(config.ingest, async_es_client)
yield
# On app shutdown
await async_es_client.close()
app = FastAPI(lifespan=lifespan)
@app.get("/query")
async def send_query(q: str = None) -> str:
global index
global config
query_engine = create_query_engine(config.query, index)
response = query_engine.query(q)
return str(response)