DAN-2 Backend Support for Filters (#8)

Additionally added an __init__.py for mypy issue
This commit is contained in:
Yuhong Sun
2023-05-01 22:29:09 -07:00
committed by GitHub
parent 02a6677e21
commit e7b901f292
8 changed files with 152 additions and 87 deletions

View File

@@ -1,3 +1,4 @@
import argparse
import json
import requests
@@ -8,35 +9,60 @@ from danswer.configs.constants import SOURCE_TYPE
if __name__ == "__main__":
previous_query = None
parser = argparse.ArgumentParser()
parser.add_argument(
"-k",
"--keyword-search",
action="store_true",
help="Use keyword search if set, semantic search otherwise",
)
parser.add_argument(
"-s",
"--source-types",
type=str,
help="Comma separated list of source types to filter by",
)
parser.add_argument("query", nargs="*", help="The query to process")
while True:
try:
keyword_search = False
query = input(
"\n\nAsk any question:\n - prefix with -k for keyword search\n - input an empty string to "
"rerun last query\n\t"
user_input = input(
"\n\nAsk any question:\n"
" - prefix with -s to add a filter by source(s)\n"
" - input an empty string to rerun last query\n\t"
)
if query.lower() in ["q", "quit", "exit", "exit()"]:
break
if query:
previous_query = query
if user_input:
previous_input = user_input
else:
if not previous_query:
print("No previous query")
if not previous_input:
print("No previous input")
continue
print(f"Re-executing previous question:\n\t{previous_query}")
query = previous_query
print(f"Re-executing previous question:\n\t{previous_input}")
user_input = previous_input
args = parser.parse_args(user_input.split())
keyword_search = args.keyword_search
source_types = args.source_types.split(",") if args.source_types else None
if source_types and len(source_types) == 1:
source_types = source_types[0]
query = " ".join(args.query)
endpoint = f"http://127.0.0.1:{APP_PORT}/direct-qa"
if query.startswith("-k "):
keyword_search = True
query = query[2:]
if args.keyword_search:
endpoint = f"http://127.0.0.1:{APP_PORT}/keyword-search"
response = requests.post(
endpoint, json={"query": query, "collection": QDRANT_DEFAULT_COLLECTION}
)
query_json = {
"query": query,
"collection": QDRANT_DEFAULT_COLLECTION,
"filters": [{SOURCE_TYPE: source_types}],
}
response = requests.post(endpoint, json=query_json)
contents = json.loads(response.content)
if keyword_search:
if contents["results"]: