Skip to content

Commit

Permalink
merged Main into feature branch
Browse files Browse the repository at this point in the history
  • Loading branch information
KastanDay committed Sep 25, 2023
2 parents f5ec2c6 + 9eeef83 commit 78692d2
Show file tree
Hide file tree
Showing 6 changed files with 430 additions and 245 deletions.
39 changes: 28 additions & 11 deletions ai_ta_backend/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gc
import json
import os
import time
from typing import List
Expand All @@ -9,7 +10,7 @@
from flask_executor import Executor
from sqlalchemy import JSON

from ai_ta_backend.nomic_logging import get_nomic_map, log_query_to_nomic
from ai_ta_backend.nomic_logging import get_nomic_map, log_convo_to_nomic
from ai_ta_backend.vector_database import Ingest
from ai_ta_backend.web_scrape import mit_course_download, WebScrape

Expand Down Expand Up @@ -137,16 +138,13 @@ def getTopContexts() -> Response:
abort(
400,
description=
f"Missing one or me required parameters: 'search_query' and 'course_name' must be provided. Search query: `{search_query}`, Course name: `{course_name}`"
f"Missing one or more required parameters: 'search_query' and 'course_name' must be provided. Search query: `{search_query}`, Course name: `{course_name}`"
)

ingester = Ingest()
found_documents = ingester.getTopContexts(search_query, course_name, token_limit)
del ingester

# background execution of tasks!!
executor.submit(log_query_to_nomic, course_name, search_query)

response = jsonify(found_documents)
response.headers.add('Access-Control-Allow-Origin', '*')
return response
Expand Down Expand Up @@ -294,22 +292,22 @@ def delete():
Delete a single file from all our database: S3, Qdrant, and Supabase (for now).
Note, of course, we still have parts of that file in our logs.
"""

course_name: str = request.args.get('course_name', default='', type=str)
s3_path: str = request.args.get('s3_path', default='', type=str)
source_url: str = request.args.get('url', default='', type=str)

if course_name == '' or s3_path == '':
if course_name == '' or (s3_path == '' and source_url == ''):
# proper web error "400 Bad request"
abort(
400,
description=
f"Missing one or more required parameters: 'course_name' and 's3_path' must be provided. Course name: `{course_name}`, S3 path: `{s3_path}`"
f"Missing one or more required parameters: 'course_name' and ('s3_path' or 'source_url') must be provided. Course name: `{course_name}`, S3 path: `{s3_path}`, source_url: `{source_url}`"
)

start_time = time.monotonic()
ingester = Ingest()
# background execution of tasks!!
executor.submit(ingester.delete_data, s3_path, course_name)
executor.submit(ingester.delete_data, course_name, s3_path, source_url)
print(f"From {course_name}, deleted file: {s3_path}")
print(f"⏰ Runtime of FULL delete func: {(time.monotonic() - start_time):.2f} seconds")
del ingester
Expand All @@ -326,7 +324,7 @@ def scrape() -> Response:
max_urls: int = request.args.get('max_urls', default=100, type=int)
max_depth: int = request.args.get('max_depth', default=2, type=int)
timeout: int = request.args.get('timeout', default=3, type=int)
stay_on_baseurl: bool | None = request.args.get('`stay_on_baseurl`', default=True, type=bool)
stay_on_baseurl: bool | None = request.args.get('stay_on_baseurl', default=True, type=bool)

if url == '' or max_urls == -1 or max_depth == -1 or timeout == -1 or course_name == '' or stay_on_baseurl is None:
# proper web error "400 Bad request"
Expand All @@ -351,7 +349,6 @@ def scrape() -> Response:
gc.collect() # manually invoke garbage collection, try to reduce memory on Railway $$$
return response


@app.route('/mit-download', methods=['GET'])
def mit_download_course() -> Response:
""" Web scraper built for
Expand Down Expand Up @@ -393,6 +390,26 @@ def nomic_map():
response.headers.add('Access-Control-Allow-Origin', '*')
return response

@app.route('/onResponseCompletion', methods=['POST'])
def logToNomic():
data = request.get_json()
course_name = data['course_name']
conversation = data['conversation']
if course_name == '' or conversation == '':
# proper web error "400 Bad request"
abort(
400,
description=
f"Missing one or more required parameters: 'course_name' and 'conversation' must be provided. Course name: `{course_name}`, Conversation: `{conversation}`"
)
print(f"In /onResponseCompletion for course: {course_name}")

# background execution of tasks!!
response = executor.submit(log_convo_to_nomic, course_name, data)
response = jsonify({'outcome': 'success'})
response.headers.add('Access-Control-Allow-Origin', '*')
return response


if __name__ == '__main__':
app.run(debug=True, port=int(os.getenv("PORT", default=8000)))
Loading

0 comments on commit 78692d2

Please sign in to comment.