2021-08-17 03:59:58 -07:00
|
|
|
# Standard Packages
|
|
|
|
import sys
|
|
|
|
import pathlib
|
2021-08-15 17:50:08 -07:00
|
|
|
from typing import Optional
|
2021-08-17 03:59:58 -07:00
|
|
|
|
|
|
|
# External Packages
|
|
|
|
import uvicorn
|
2021-08-15 17:50:08 -07:00
|
|
|
from fastapi import FastAPI
|
2021-08-17 03:59:58 -07:00
|
|
|
|
|
|
|
# Internal Packages
|
2021-08-22 03:16:57 -07:00
|
|
|
from search_type import asymmetric, symmetric_ledger
|
2021-08-21 19:21:38 -07:00
|
|
|
from utils.helpers import get_from_dict
|
|
|
|
from utils.cli import cli
|
2021-08-17 03:59:58 -07:00
|
|
|
|
2021-08-15 17:50:08 -07:00
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
@app.get('/search')
|
2021-08-17 04:36:48 -07:00
|
|
|
def search(q: str, n: Optional[int] = 5, t: Optional[str] = None):
|
2021-08-15 17:50:08 -07:00
|
|
|
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
|
|
|
|
|
2021-08-21 18:47:55 -07:00
|
|
|
if (t == 'notes' or t == None) and notes_search_enabled:
|
2021-08-15 17:50:08 -07:00
|
|
|
# query notes
|
2021-08-16 16:52:48 -07:00
|
|
|
hits = asymmetric.query_notes(
|
2021-08-16 18:52:38 -07:00
|
|
|
user_query,
|
2021-08-16 16:52:48 -07:00
|
|
|
corpus_embeddings,
|
|
|
|
entries,
|
|
|
|
bi_encoder,
|
|
|
|
cross_encoder,
|
|
|
|
top_k)
|
2021-08-15 17:50:08 -07:00
|
|
|
|
|
|
|
# collate and return results
|
2021-08-15 19:09:50 -07:00
|
|
|
return asymmetric.collate_results(hits, entries, results_count)
|
2021-08-15 17:50:08 -07:00
|
|
|
|
2021-08-22 03:16:57 -07:00
|
|
|
if (t == 'ledger' or t == None) and ledger_search_enabled:
|
|
|
|
# query transactions
|
|
|
|
hits = symmetric_ledger.query_transactions(
|
|
|
|
user_query,
|
|
|
|
transaction_embeddings,
|
|
|
|
transactions,
|
|
|
|
symmetric_encoder,
|
|
|
|
symmetric_cross_encoder)
|
|
|
|
|
|
|
|
# collate and return results
|
|
|
|
return symmetric_ledger.collate_results(hits, transactions, results_count)
|
|
|
|
|
2021-08-15 17:50:08 -07:00
|
|
|
else:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
2021-08-16 18:52:38 -07:00
|
|
|
@app.get('/regenerate')
|
2021-08-17 04:36:48 -07:00
|
|
|
def regenerate(t: Optional[str] = None):
|
2021-08-21 18:47:55 -07:00
|
|
|
if (t == 'notes' or t == None) and notes_search_enabled:
|
2021-08-17 04:36:48 -07:00
|
|
|
# Extract Entries, Generate Embeddings
|
|
|
|
global corpus_embeddings
|
|
|
|
global entries
|
2021-08-21 18:47:55 -07:00
|
|
|
entries, corpus_embeddings, _, _, _ = asymmetric.setup(
|
|
|
|
org_config['input-files'],
|
|
|
|
org_config['input-filter'],
|
|
|
|
pathlib.Path(org_config['compressed-jsonl']),
|
|
|
|
pathlib.Path(org_config['embeddings-file']),
|
|
|
|
regenerate=True,
|
|
|
|
verbose=args.verbose)
|
|
|
|
|
2021-08-22 03:16:57 -07:00
|
|
|
if (t == 'ledger' or t == None) and ledger_search_enabled:
|
|
|
|
# Extract Entries, Generate Embeddings
|
|
|
|
global transaction_embeddings
|
|
|
|
global transactions
|
|
|
|
transactions, transaction_embeddings, _, _, _ = symmetric_ledger.setup(
|
|
|
|
ledger_config['input-files'],
|
|
|
|
ledger_config['input-filter'],
|
|
|
|
pathlib.Path(ledger_config['compressed-jsonl']),
|
|
|
|
pathlib.Path(ledger_config['embeddings-file']),
|
|
|
|
regenerate=True,
|
|
|
|
verbose=args.verbose)
|
2021-08-16 23:47:33 -07:00
|
|
|
|
|
|
|
return {'status': 'ok', 'message': 'regeneration completed'}
|
2021-08-16 18:52:38 -07:00
|
|
|
|
|
|
|
|
2021-08-17 04:00:45 -07:00
|
|
|
if __name__ == '__main__':
|
2021-08-21 19:21:38 -07:00
|
|
|
args = cli(sys.argv[1:])
|
2021-08-21 18:47:55 -07:00
|
|
|
|
2021-08-22 03:16:57 -07:00
|
|
|
# Initialize Org Notes Search
|
|
|
|
org_config = get_from_dict(args.config, 'content-type', 'org')
|
2021-08-21 18:47:55 -07:00
|
|
|
notes_search_enabled = False
|
2021-08-22 03:16:57 -07:00
|
|
|
if org_config and ('input-files' in org_config or 'input-filter' in org_config):
|
2021-08-21 18:47:55 -07:00
|
|
|
notes_search_enabled = True
|
|
|
|
entries, corpus_embeddings, bi_encoder, cross_encoder, top_k = asymmetric.setup(
|
|
|
|
org_config['input-files'],
|
|
|
|
org_config['input-filter'],
|
|
|
|
pathlib.Path(org_config['compressed-jsonl']),
|
|
|
|
pathlib.Path(org_config['embeddings-file']),
|
|
|
|
args.regenerate,
|
|
|
|
args.verbose)
|
2021-08-15 17:50:08 -07:00
|
|
|
|
2021-08-22 03:16:57 -07:00
|
|
|
# Initialize Ledger Search
|
|
|
|
ledger_config = get_from_dict(args.config, 'content-type', 'ledger')
|
|
|
|
ledger_search_enabled = False
|
|
|
|
if ledger_config and ('input-files' in ledger_config or 'input-filter' in ledger_config):
|
|
|
|
ledger_search_enabled = True
|
|
|
|
transactions, transaction_embeddings, symmetric_encoder, symmetric_cross_encoder, _ = symmetric_ledger.setup(
|
|
|
|
ledger_config['input-files'],
|
|
|
|
ledger_config['input-filter'],
|
|
|
|
pathlib.Path(ledger_config['compressed-jsonl']),
|
|
|
|
pathlib.Path(ledger_config['embeddings-file']),
|
|
|
|
args.regenerate,
|
|
|
|
args.verbose)
|
2021-08-15 17:50:08 -07:00
|
|
|
|
|
|
|
# Start Application Server
|
|
|
|
uvicorn.run(app)
|