-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added code for JSON oriented model approach #4
Changes from 8 commits
3abfcdd
43c77e6
b0ddec5
2727fc8
c770b71
cd314da
80e24dc
a7f98f7
9a06fa5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
prompts/__pycache__ | ||
rich_query_index | ||
.DS_Store | ||
.env | ||
__pycache__ | ||
__pycache__ | ||
src/query_index |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
{ | ||
"git.ignoreLimitWarning": true, | ||
"editor.formatOnSave": true, | ||
"flake8.args": ["--max-line-length=88"], | ||
"flake8.args": [ | ||
"--max-line-length=88" | ||
], | ||
"[python]": { | ||
"editor.codeActionsOnSave": { | ||
"source.organizeImports.python": "explicit" | ||
}, | ||
"editor.defaultFormatter": "ms-python.black-formatter" | ||
"editor.defaultFormatter": "ms-python.black-formatter", | ||
"editor.formatOnSave": true, | ||
} | ||
} | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import json | ||
import logging | ||
import os | ||
rjchallis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import sys | ||
import urllib | ||
|
||
from flask import Flask, jsonify, render_template, request | ||
|
||
from index import load_index, query_engine | ||
|
||
app = Flask("goat_nlp") | ||
|
||
handler = logging.StreamHandler(sys.stdout) | ||
handler.setFormatter( | ||
logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") | ||
) | ||
app.logger.addHandler(handler) | ||
app.logger.setLevel(logging.INFO) | ||
|
||
|
||
def construct_url(json_output): | ||
base_url = "https://goat.genomehubs.org/" | ||
endpoint = "search?" | ||
suffix = ( | ||
"&result=taxon&summaryValues=count&taxonomy=ncbi&offset=0" | ||
+ "&fields=assembly_level%2Cassembly_span%2Cgenome_size%2C" | ||
+ "chromosome_number%2Chaploid_number&names=common_name&ranks=" | ||
+ "&includeEstimates=false&size=100" | ||
) | ||
|
||
if json_output["intent"] == "count": | ||
endpoint = "count?" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for now this could be |
||
elif json_output["intent"] == "record": | ||
endpoint = "record?" | ||
|
||
params = [] | ||
|
||
if "taxon" in json_output: | ||
params.append(f"tax_tree(* {json_output['taxon']})") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fine for now - for next iteration use tax_name search to replace user tax name with taxon_id |
||
if "rank" in json_output: | ||
params.append(f"tax_rank({json_output['rank']})") | ||
if "field" in json_output: | ||
params.append(f"{json_output['field']}") | ||
if "time_frame_query" in json_output: | ||
params.append(f"{json_output['time_frame_query']}") | ||
suffix = ( | ||
"&result=assembly&summaryValues=count&taxonomy=ncbi&offset=0" | ||
+ "&fields=assembly_level%2Cassembly_span%2Cgenome_size%2C" | ||
+ "chromosome_number%2Chaploid_number&names=common_name&ranks=" | ||
+ "&includeEstimates=false&size=100" | ||
) | ||
|
||
query_string = " AND ".join(params) | ||
return ( | ||
base_url + endpoint + "query=" + urllib.parse.quote_plus(query_string) + suffix | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for API spaces need to be escaped as |
||
) | ||
|
||
|
||
def chat_bot_rag(query): | ||
# entity_taxon_map = fetch_related_taxons(query) | ||
for _ in range(int(os.getenv("RETRY_COUNT", 3))): | ||
try: | ||
return construct_url(json.loads(query_engine.custom_query(query))) | ||
except Exception as e: | ||
app.logger.error(f"Error: {e}") | ||
app.logger.error("Retrying...") | ||
return construct_url(json.loads(query_engine.custom_query(query))) | ||
|
||
|
||
@app.route("/") | ||
def home(): | ||
return render_template("chat.html") | ||
|
||
|
||
@app.route("/rebuildIndex") | ||
def index(): | ||
load_index(force_reload=True) | ||
|
||
|
||
@app.route("/chat", methods=["POST"]) | ||
def chat(): | ||
user_message = request.form["user_input"] | ||
bot_message = chat_bot_rag(user_message) | ||
|
||
try: | ||
bot_message = json.loads(bot_message)["url"] | ||
except Exception: | ||
pass | ||
|
||
return jsonify({"response": str(bot_message)}) | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(debug=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
W504
and go in this list too. I hadn't spotted the difference betweenignore
andextend-ignore
so should list bothW503
andW504
to match setup.cfg