comparison scobot/service/query.py @ 17:0d72635fc501

reloadIndexIfChanged
author drewp@bigasterisk.com
date Fri, 19 Jul 2024 00:59:45 -0700
parents 7a87ba2f00d9
children
comparison
equal deleted inserted replaced
16:7a87ba2f00d9 17:0d72635fc501
43 # embedding_fn = OnnxEmbeddingFunction(model_name="jinaai/jina-embeddings-v2-base-en") 43 # embedding_fn = OnnxEmbeddingFunction(model_name="jinaai/jina-embeddings-v2-base-en")
44 # client = MilvusClient("milvus_demo.db") 44 # client = MilvusClient("milvus_demo.db")
45 # rebuild(client, embedding_fn, dim=embedding_fn.dim) 45 # rebuild(client, embedding_fn, dim=embedding_fn.dim)
46 # search(q, embedding_fn, client) 46 # search(q, embedding_fn, client)
47 47
48 indexPath = Path('data/build/index0')
49
48 50
49 @asynccontextmanager 51 @asynccontextmanager
50 async def lifespan(app: FastAPI): 52 async def lifespan(app: FastAPI):
51 app.state.index = SearchIndexRO(Path('data/build/index0')) 53 reloadIndexIfChanged(app)
52 yield 54 yield
55
53 56
54 app = FastAPI(lifespan=lifespan) 57 app = FastAPI(lifespan=lifespan)
55 58
59
60 def reloadIndexIfChanged(app: FastAPI):
61 if ((not hasattr(app.state, 'indexMtime'))
62 or (app.state.indexMtime != indexPath.stat().st_mtime)):
63 print('reloading index')
64 app.state.indexMtime = indexPath.stat().st_mtime
65 app.state.index = SearchIndexRO(indexPath)
66
67
56 @app.get("/sco/query") 68 @app.get("/sco/query")
57 def read_query1(q: str): 69 def read_query1(q: str):
70 reloadIndexIfChanged(app)
58 index = app.state.index 71 index = app.state.index
59 72
60 query = QueryParser("phrase", index.ix.schema).parse(q) 73 query = QueryParser("phrase", index.ix.schema).parse(q)
61 pprint(query) 74 pprint(query)
62 results = index.searcher.search(query) 75 results = index.searcher.search(query)
63 docs=[] 76 docs = []
64 for res in results: 77 for res in results:
65 doc=dict(res) 78 doc = dict(res)
66 doc['snippetHtml'] = html.escape(doc['phrase']) 79 doc['snippetHtml'] = html.escape(doc['phrase'])
67 docs.append(doc) 80 docs.append(doc)
68 return {"results": docs} 81 return {"results": docs}