Skip to content

Commit

Permalink
Added code for JSON oriented model approach
Browse files Browse the repository at this point in the history
  • Loading branch information
deepnayak committed Jun 17, 2024
1 parent 99c3669 commit 3abfcdd
Show file tree
Hide file tree
Showing 12 changed files with 11,008 additions and 260 deletions.
4 changes: 2 additions & 2 deletions .gitignore
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
49 changes: 0 additions & 49 deletions app.py

This file was deleted.

49 changes: 0 additions & 49 deletions prompt.py

This file was deleted.

75 changes: 0 additions & 75 deletions query_reformulation.py

This file was deleted.

70 changes: 0 additions & 70 deletions rich_queries/queryV1.json

This file was deleted.

78 changes: 78 additions & 0 deletions src/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import os
import sys
from flask import Flask, request, render_template, jsonify

Check failure on line 3 in src/app.py

View workflow job for this annotation

GitHub Actions / flake8 Lint

isort found an import in the wrong position
import urllib
from index import load_index, query_engine

Check failure on line 5 in src/app.py

View workflow job for this annotation

GitHub Actions / flake8 Lint

isort expected 1 blank line in imports, found 0

Check failure on line 5 in src/app.py

View workflow job for this annotation

GitHub Actions / flake8 Lint

isort expected 1 blank line in imports, found 0

Check failure on line 5 in src/app.py

View workflow job for this annotation

GitHub Actions / flake8 Lint

isort found an unexpected missing import
import json

Check failure on line 6 in src/app.py

View workflow job for this annotation

GitHub Actions / flake8 Lint

isort found an import in the wrong position
import logging

Check failure on line 7 in src/app.py

View workflow job for this annotation

GitHub Actions / flake8 Lint

isort found an import in the wrong position

Check failure on line 8 in src/app.py

View workflow job for this annotation

GitHub Actions / flake8 Lint

isort found an unexpected blank line in imports

app = Flask("goat_nlp")

handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'))

Check failure on line 14 in src/app.py

View workflow job for this annotation

GitHub Actions / flake8 Lint

Single quotes found but double quotes preferred
app.logger.addHandler(handler)
app.logger.setLevel(logging.INFO)


def construct_url(json_output):
base_url = "https://goat.genomehubs.org/"
endpoint = "search?"

if json_output['intent'] == 'count':

Check failure on line 23 in src/app.py

View workflow job for this annotation

GitHub Actions / flake8 Lint

Single quotes found but double quotes preferred

Check failure on line 23 in src/app.py

View workflow job for this annotation

GitHub Actions / flake8 Lint

Single quotes found but double quotes preferred
endpoint = "count?"
elif json_output['intent'] == 'record':
endpoint = "record?"

params = []

if 'taxon' in json_output:
params.append(f"tax_tree(* {json_output['taxon']})")
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']}")

query_string = " AND ".join(params)
return base_url + endpoint + "query=" + urllib.parse.quote_plus(query_string) + "&result=taxon&summaryValues=count&taxonomy=ncbi&offset=0&fields=assembly_level%2Cassembly_span%2Cgenome_size%2Cchromosome_number%2Chaploid_number&names=common_name&ranks=&includeEstimates=false&size=100"


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)
Loading

0 comments on commit 3abfcdd

Please sign in to comment.