2021-08-16 02:50:08 +02:00
from typing import Optional
from fastapi import FastAPI
2021-08-17 01:04:45 +02:00
from search_type import asymmetric
2021-08-16 04:09:50 +02:00
import argparse
import pathlib
2021-08-16 02:50:08 +02:00
import uvicorn
app = FastAPI ( )
@app.get ( ' /search ' )
def search ( q : str , n : Optional [ int ] = 5 , t : Optional [ str ] = ' notes ' ) :
if q is None or q == ' ' :
print ( f ' No query param (q) passed in API call to initiate search ' )
return { }
user_query = q
results_count = n
if t == ' notes ' :
# query notes
2021-08-17 01:52:48 +02:00
hits = asymmetric . query_notes (
q ,
corpus_embeddings ,
entries ,
bi_encoder ,
cross_encoder ,
top_k )
2021-08-16 02:50:08 +02:00
# collate and return results
2021-08-16 04:09:50 +02:00
return asymmetric . collate_results ( hits , entries , results_count )
2021-08-16 02:50:08 +02:00
else :
return { }
if __name__ == ' __main__ ' :
# Setup Argument Parser
parser = argparse . ArgumentParser ( description = " Expose API for Semantic Search " )
2021-08-17 02:15:41 +02:00
parser . add_argument ( ' --compressed-jsonl ' , ' -j ' , type = pathlib . Path , default = pathlib . Path ( " .notes.jsonl.gz " ) , help = " Compressed JSONL formatted notes file to compute embeddings from " )
parser . add_argument ( ' --embeddings ' , ' -e ' , type = pathlib . Path , default = pathlib . Path ( " .notes_embeddings.pt " ) , help = " File to save/load model embeddings to/from " )
parser . add_argument ( ' --verbose ' , action = ' count ' , help = " Show verbose conversion logs. Default: 0 " )
2021-08-16 02:50:08 +02:00
args = parser . parse_args ( )
# Initialize Model
2021-08-16 04:09:50 +02:00
bi_encoder , cross_encoder , top_k = asymmetric . initialize_model ( )
2021-08-16 02:50:08 +02:00
# Extract Entries
2021-08-16 22:44:42 +02:00
entries = asymmetric . extract_entries ( args . compressed_jsonl , args . verbose )
2021-08-16 02:50:08 +02:00
# Compute or Load Embeddings
2021-08-16 22:44:42 +02:00
corpus_embeddings = asymmetric . compute_embeddings ( entries , bi_encoder , args . embeddings , args . verbose )
2021-08-16 02:50:08 +02:00
# Start Application Server
uvicorn . run ( app )