Greg Thompson commited on
Commit
8f1bf52
1 Parent(s): ac44250

Update nlu with to work with simplified event data object

Browse files
Files changed (2) hide show
  1. app.py +5 -5
  2. mathtext_fastapi/nlu.py +9 -9
app.py CHANGED
@@ -81,6 +81,7 @@ async def programmatic_message_manager(request: Request):
81
  context = manage_conversational_response(data_dict)
82
  return JSONResponse(context)
83
 
 
84
  @app.post("/nlu")
85
  async def evaluate_user_message_with_nlu_api(request: Request):
86
  """ Calls NLU APIs on the most recent user message from Turn.io message data and logs the message data
@@ -96,12 +97,12 @@ async def evaluate_user_message_with_nlu_api(request: Request):
96
  data_dict = await request.json()
97
 
98
  message_data = data_dict.get('message_data', '')
99
- message_text = message_data['message']['text']['body']
100
 
101
  # Handles if a student answer is already an integer or a float (ie., 8)
102
  if type(message_text) == int or type(message_text) == float:
103
  nlu_response = {'type': 'integer', 'data': message_text, 'confidence': ''}
104
- prepare_message_data_for_logging(message_data, nlu_response, message_data)
105
  return JSONResponse(content=nlu_response)
106
 
107
  # Removes whitespace and converts str to arr to handle multiple numbers
@@ -110,7 +111,7 @@ async def evaluate_user_message_with_nlu_api(request: Request):
110
  # Handle if a student answer is a string of numbers (ie., "8,9, 10")
111
  if all(ele.isdigit() for ele in message_text_arr):
112
  nlu_response = {'type': 'integer', 'data': ','.join(message_text_arr), 'confidence': ''}
113
- prepare_message_data_for_logging(message_data, nlu_response, message_data)
114
  return JSONResponse(content=nlu_response)
115
 
116
  student_response_arr = []
@@ -134,6 +135,5 @@ async def evaluate_user_message_with_nlu_api(request: Request):
134
  else:
135
  nlu_response = {'type': 'integer', 'data': student_response_arr[0], 'confidence': ''}
136
 
137
- # Uncomment to enable logging to Supabase
138
- prepare_message_data_for_logging(message_data, nlu_response, message_data)
139
  return JSONResponse(content=nlu_response)
 
81
  context = manage_conversational_response(data_dict)
82
  return JSONResponse(context)
83
 
84
+
85
  @app.post("/nlu")
86
  async def evaluate_user_message_with_nlu_api(request: Request):
87
  """ Calls NLU APIs on the most recent user message from Turn.io message data and logs the message data
 
97
  data_dict = await request.json()
98
 
99
  message_data = data_dict.get('message_data', '')
100
+ message_text = message_data['message_body']
101
 
102
  # Handles if a student answer is already an integer or a float (ie., 8)
103
  if type(message_text) == int or type(message_text) == float:
104
  nlu_response = {'type': 'integer', 'data': message_text, 'confidence': ''}
105
+ prepare_message_data_for_logging(message_data, nlu_response)
106
  return JSONResponse(content=nlu_response)
107
 
108
  # Removes whitespace and converts str to arr to handle multiple numbers
 
111
  # Handle if a student answer is a string of numbers (ie., "8,9, 10")
112
  if all(ele.isdigit() for ele in message_text_arr):
113
  nlu_response = {'type': 'integer', 'data': ','.join(message_text_arr), 'confidence': ''}
114
+ prepare_message_data_for_logging(message_data, nlu_response)
115
  return JSONResponse(content=nlu_response)
116
 
117
  student_response_arr = []
 
135
  else:
136
  nlu_response = {'type': 'integer', 'data': student_response_arr[0], 'confidence': ''}
137
 
138
+ prepare_message_data_for_logging(message_data, nlu_response)
 
139
  return JSONResponse(content=nlu_response)
mathtext_fastapi/nlu.py CHANGED
@@ -42,7 +42,7 @@ def get_or_create_supabase_entry(table_name, insert_data, check_variable=None):
42
 
43
 
44
 
45
- def prepare_message_data_for_logging(message_data, nlu_response, request_object):
46
  """ Builds the message data for each table and ensures it's logged to the database
47
 
48
  Input:
@@ -56,7 +56,7 @@ def prepare_message_data_for_logging(message_data, nlu_response, request_object)
56
 
57
  contact_data = {
58
  'project': project_data_log.data[0]['id'], # FK
59
- 'original_contact_id': message_data['message']['_vnd']['v1']['chat']['contact_uuid'],
60
  'urn': "",
61
  'language_code': "en",
62
  'contact_inserted_at': format_datetime_in_isoformat(datetime.now())
@@ -66,16 +66,16 @@ def prepare_message_data_for_logging(message_data, nlu_response, request_object)
66
 
67
  message_data = {
68
  'contact': contact_data_log.data[0]['id'], # FK
69
- 'original_message_id': message_data['message']['id'],
70
- 'text': message_data['message']['text']['body'],
71
- 'direction': message_data['message']['_vnd']['v1']['direction'],
72
- 'sender_type': message_data['message']['_vnd']['v1']['author']['type'],
73
  'channel_type': "whatsapp / turn.io",
74
- 'message_inserted_at': message_data['message']['_vnd']['v1']['chat']['inserted_at'],
75
- 'message_modified_at': message_data['message']['_vnd']['v1']['chat']['updated_at'],
76
  'message_sent_at': format_datetime_in_isoformat(datetime.now()),
77
  'nlu_response': nlu_response,
78
- 'request_object': request_object
79
  # Autogenerated fields: created_at, modified_at
80
  }
81
  message_data_log = log_message_data_through_supabase_api('message', message_data)
 
42
 
43
 
44
 
45
+ def prepare_message_data_for_logging(message_data, nlu_response):
46
  """ Builds the message data for each table and ensures it's logged to the database
47
 
48
  Input:
 
56
 
57
  contact_data = {
58
  'project': project_data_log.data[0]['id'], # FK
59
+ 'original_contact_id': message_data['contact_uuid'],
60
  'urn': "",
61
  'language_code': "en",
62
  'contact_inserted_at': format_datetime_in_isoformat(datetime.now())
 
66
 
67
  message_data = {
68
  'contact': contact_data_log.data[0]['id'], # FK
69
+ 'original_message_id': message_data['message_id'],
70
+ 'text': message_data['message_body'],
71
+ 'direction': message_data['message_direction'],
72
+ 'sender_type': message_data['author_type'],
73
  'channel_type': "whatsapp / turn.io",
74
+ 'message_inserted_at': message_data['message_inserted_at'],
75
+ 'message_modified_at': message_data['message_updated_at'],
76
  'message_sent_at': format_datetime_in_isoformat(datetime.now()),
77
  'nlu_response': nlu_response,
78
+ 'request_object': message_data
79
  # Autogenerated fields: created_at, modified_at
80
  }
81
  message_data_log = log_message_data_through_supabase_api('message', message_data)