khoj/main.py
Debanjum Singh Solanky 66238004d8 Use verbosity level instead of bool across application
For consistent, more granular verbosity controls across app
Allows user to increase verbosity by passing -vvv flags passed to main.py
2021-08-16 17:15:41 -07:00

55 lines
1.7 KiB
Python

from typing import Optional
from fastapi import FastAPI
from search_type import asymmetric
import argparse
import pathlib
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
hits = asymmetric.query_notes(
q,
corpus_embeddings,
entries,
bi_encoder,
cross_encoder,
top_k)
# collate and return results
return asymmetric.collate_results(hits, entries, results_count)
else:
return {}
if __name__ == '__main__':
# Setup Argument Parser
parser = argparse.ArgumentParser(description="Expose API for Semantic Search")
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")
args = parser.parse_args()
# Initialize Model
bi_encoder, cross_encoder, top_k = asymmetric.initialize_model()
# Extract Entries
entries = asymmetric.extract_entries(args.compressed_jsonl, args.verbose)
# Compute or Load Embeddings
corpus_embeddings = asymmetric.compute_embeddings(entries, bi_encoder, args.embeddings, args.verbose)
# Start Application Server
uvicorn.run(app)