Skip to content

Commit

Permalink
started updates for convo logging
Browse files Browse the repository at this point in the history
  • Loading branch information
star-nox committed Mar 26, 2024
1 parent 015fd75 commit fc69d4c
Show file tree
Hide file tree
Showing 4 changed files with 332 additions and 160 deletions.
22 changes: 9 additions & 13 deletions ai_ta_backend/beam/nomic_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
supabase_url=os.getenv('SUPABASE_URL'), # type: ignore
supabase_key=os.getenv('SUPABASE_API_KEY')) # type: ignore

NOMIC_MAP_NAME_PREFIX = 'Document Map for '

## -------------------------------- DOCUMENT MAP FUNCTIONS --------------------------------- ##

def create_document_map(course_name: str):
Expand All @@ -33,21 +35,15 @@ def create_document_map(course_name: str):
"""
print("in create_document_map()")
nomic.login(os.getenv('NOMIC_API_KEY'))
NOMIC_MAP_NAME_PREFIX = 'Document Map for '

# initialize supabase
supabase_client = supabase.create_client( # type: ignore
supabase_url=os.getenv('SUPABASE_URL'), # type: ignore
supabase_key=os.getenv('SUPABASE_API_KEY')) # type: ignore


try:
# check if map exists
response = supabase_client.table("projects").select("doc_map_id").eq("course_name", course_name).execute()
response = SUPABASE_CLIENT.table("projects").select("doc_map_id").eq("course_name", course_name).execute()
if response.data:
return "Map already exists for this course."

# fetch relevant document data from Supabase
response = supabase_client.table("documents").select("id",
response = SUPABASE_CLIENT.table("documents").select("id",
count="exact").eq("course_name",
course_name).order('id',
desc=False).execute()
Expand All @@ -71,7 +67,7 @@ def create_document_map(course_name: str):
# iteratively query in batches of 25
while curr_total_doc_count < total_doc_count:

response = supabase_client.table("documents").select(
response = SUPABASE_CLIENT.table("documents").select(
"id, created_at, s3_path, url, base_url, readable_filename, contexts").eq("course_name", course_name).gte(
'id', first_id).order('id', desc=False).limit(25).execute()
df = pd.DataFrame(response.data)
Expand Down Expand Up @@ -105,7 +101,7 @@ def create_document_map(course_name: str):
project_id = project.id
last_id = int(final_df['id'].iloc[-1])
project_info = {'course_name': course_name, 'doc_map_id': project_id, 'last_uploaded_doc_id': last_id}
update_response = supabase_client.table("projects").insert(project_info).execute()
update_response = SUPABASE_CLIENT.table("projects").insert(project_info).execute()
print("Response from supabase: ", update_response)

else:
Expand All @@ -118,7 +114,7 @@ def create_document_map(course_name: str):
# update the last uploaded id in supabase
last_id = int(final_df['id'].iloc[-1])
info = {'last_uploaded_doc_id': last_id}
update_response = supabase_client.table("projects").update(info).eq("course_name", course_name).execute()
update_response = SUPABASE_CLIENT.table("projects").update(info).eq("course_name", course_name).execute()
print("Response from supabase: ", update_response)

# reset variables
Expand Down Expand Up @@ -148,7 +144,7 @@ def create_document_map(course_name: str):
last_id = int(final_df['id'].iloc[-1])
project_info = {'last_uploaded_doc_id': last_id}
print("project_info: ", project_info)
update_response = supabase_client.table("projects").update(project_info).eq("course_name", course_name).execute()
update_response = SUPABASE_CLIENT.table("projects").update(project_info).eq("course_name", course_name).execute()
print("Response from supabase: ", update_response)


Expand Down
20 changes: 17 additions & 3 deletions ai_ta_backend/database/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,14 @@ def getAllFromTableForDownloadType(self, course_name: str, download_type: str, f

return response

def getAllConversationsBetweenIds(self, course_name: str, first_id: int, last_id: int):
return self.supabase_client.table("llm-convo-monitor").select("*").eq("course_name", course_name).gte(
'id', first_id).lte('id', last_id).order('id', desc=False).limit(25).execute()
def getAllConversationsBetweenIds(self, course_name: str, first_id: int, last_id: int, limit: int = 50):
if last_id == 0:
return self.supabase_client.table("llm-convo-monitor").select("*").eq("course_name", course_name).gte(
'id', first_id).order('id', desc=False).limit(limit).execute()
else:
return self.supabase_client.table("llm-convo-monitor").select("*").eq("course_name", course_name).gte(
'id', first_id).lte('id', last_id).order('id', desc=False).limit(limit).execute()


def getDocsForIdsGte(self, course_name: str, first_id: int, fields: str = "*", limit: int = 100):
return self.supabase_client.table("documents").select(fields).eq("course_name", course_name).gte(
Expand All @@ -86,3 +91,12 @@ def insertProjectInfo(self, project_info):

def getAllFromLLMConvoMonitor(self, course_name: str):
return self.supabase_client.table("llm-convo-monitor").select("*").eq("course_name", course_name).execute()

def getCountFromLLMConvoMonitor(self, course_name: str):
return self.supabase_client.table("llm-convo-monitor").select("id", count='exact').eq("course_name", course_name).execute()

def getDocMapFromProjects(self, course_name: str):
return self.supabase_client.table("projects").select("doc_map_id").eq("course_name", course_name).execute()

def getConvoMapFromProjects(self, course_name: str):
return self.supabase_client.table("projects").select("convo_map_id").eq("course_name", course_name).execute()
14 changes: 14 additions & 0 deletions ai_ta_backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,20 @@ def createDocumentMap(service: NomicService):
response.headers.add('Access-Control-Allow-Origin', '*')
return response

@app.route('/createConversationMap', methods=['GET'])
def createConversationMap(service: NomicService):
course_name: str = request.args.get('course_name', default='', type=str)

if course_name == '':
# proper web error "400 Bad request"
abort(400, description=f"Missing required parameter: 'course_name' must be provided. Course name: `{course_name}`")

map_id = service.create_conversation_map(course_name)

response = jsonify(map_id)
response.headers.add('Access-Control-Allow-Origin', '*')
return response


@app.route('/onResponseCompletion', methods=['POST'])
def logToNomic(service: NomicService, flaskExecutor: ExecutorInterface):
Expand Down
Loading

0 comments on commit fc69d4c

Please sign in to comment.