TangibleAI/mathtext-fastapi
1
1import json2import requests3 4 5def add_message_text_to_sample_object(message_text):6 """7 Builds a sample request object using an example of a student answer8 9 Input10 - message_text: str - an example of user input to test11 12 Example Input13 "test message"14 15 Output16 - b_string: json b-string - simulated Turn.io message data17 18 Example Output19 b'{"context": "hi", "message_data": {"author_id": "+57787919091", "author_type": "OWNER", "contact_uuid": "j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09", "message_body": "test message", "message_direction": "inbound", "message_id": "4kl209sd0-a7b8-2hj3-8563-3hu4a89b32", "message_inserted_at": "2023-01-10T02:37:28.477940Z", "message_updated_at": "2023-01-10T02:37:28.487319Z"}}'20 21 """22 message_data = '{' + f'"author_id": "+57787919091", "author_type": "OWNER", "contact_uuid": "j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09", "message_body": "{message_text}", "message_direction": "inbound", "message_id": "4kl209sd0-a7b8-2hj3-8563-3hu4a89b32", "message_inserted_at": "2023-01-10T02:37:28.477940Z", "message_updated_at": "2023-01-10T02:37:28.487319Z"' + '}'23 # context_data = '{' + '"user":"", "state":"addition-question-sequence", "bot_message":"", "user_message":"{message_text}"' + '}'24 25 # V126 # context_data = '{' + '"user":"", "state":"start-conversation", "bot_message":"", "user_message":"{message_text}"' + '}'27 28 #V229 context_data = '{' + '"contact_uuid": "j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09", "current_state":"", "local_state": "", "user_message":""' + '}'30 31 # context_data = '{' + '"user":"", "state":"addition-question-sequence", "bot_message":"", "user_message":"{message_text}","text": "What is 2+3?","question_numbers": [4,3],"right_answer": 7,"number_correct": 2, "number_incorrect": 0, "hints_used": 0, "level": "easy"' + '}'32 33 json_string = '{' + f'"context_data": {context_data}, "message_data": {message_data}' + '}'34 b_string = json_string.encode("utf-8")35 36 return b_string37 38# """39# "text": "What is 2+3?",40# "question_numbers": [2,3],41# "right_answer": 5,42# "number_correct": 2,43# "hints_used": 0,44# """45 46 47def run_simulated_request(endpoint, sample_payload, context=None):48 print(f"Case: {sample_payload}")49 # Used for testing full message object - deprecated April 750 b_string = add_message_text_to_sample_object(sample_payload)51 52 if endpoint == 'sentiment-analysis' or endpoint == 'text2int' or endpoint =='intent-classification':53 request = requests.post(54 url=f'http://localhost:7860/{endpoint}',55 json={'content': sample_payload}56 ).json()57 else:58 request = requests.post(59 url=f'http://localhost:7860/{endpoint}',60 data=b_string61 ).json()62 63 print(request)64 65 66def run_full_nlu_endpoint_payload_test(sample_payload):67 request = requests.post(68 url=f'http://localhost:7860/nlu',69 data=sample_payload70 ).json()71 print(request)72 73 74# # Case: Wrong key75run_full_nlu_endpoint_payload_test(b'{"message": {"author_id": "@event.message._vnd.v1.chat.owner", "author_type": "@event.message._vnd.v1.author.type", "contact_uuid": "@event.message._vnd.v1.chat.contact_uuid", "message_body": "@event.message.text.body", "message_direction": "@event.message._vnd.v1.direction", "message_id": "@event.message.id", "message_inserted_at": "@event.message._vnd.v1.chat.inserted_at", "message_updated_at": "@event.message._vnd.v1.chat.updated_at"}}')76 77# # Case: Correct payload78run_full_nlu_endpoint_payload_test(b'{"message_data": {"author_id": "57787919091", "author_type": "OWNER", "contact_uuid": "df78gsdf78df", "message_body": "8", "message_direction": "inbound", "message_id": "dfgha789789ag9ga", "message_inserted_at": "2023-01-10T02:37:28.487319Z", "message_updated_at": "2023-01-10T02:37:28.487319Z"}}')79 80# Case: Correct payload + extra fields81run_full_nlu_endpoint_payload_test(b'{"message_data": {"author_id": "57787919091", "author_type": "OWNER", "contact_uuid": "df78gsdf78df", "message_body": "8", "message_direction": "inbound", "message_id": "dfgha789789ag9ga", "message_inserted_at": "2023-01-10T02:37:28.487319Z", "message_updated_at": "2023-01-10T02:37:28.487319Z", "question": "What is next - 2, 6, 8?", "expected_answer": 8}}')82 83# # Case: Incorrect payload values84run_full_nlu_endpoint_payload_test(b'{"message_data": {"author_id": "@event.message._vnd.v1.chat.owner", "author_type": "@event.message._vnd.v1.author.type", "contact_uuid": "@event.message._vnd.v1.chat.contact_uuid", "message_body": "@event.message.text.body", "message_direction": "@event.message._vnd.v1.direction", "message_id": "@event.message.id", "message_inserted_at": "@event.message._vnd.v1.chat.inserted_at", "message_updated_at": "@event.message._vnd.v1.chat.updated_at"}}')85 86# Case: Wrong payload object87# run_full_nlu_endpoint_payload_test(b'{"message_data": {"_vnd": {"v1": {"author": {"id": 54327547257, "name": "Jin", "type": "OWNER"}, "card_uuid": None, "chat": {"assigned_to": None, "contact_uuid": "f7889-f78dfgb798-f786ah89g7-f78f9a", "inserted_at": "2023-03-28T13:21:47.581221Z", "owner": "+43789789146", "permalink": "", "state": "OPEN", "state_reason": "Re-opened by inbound message.", "unread_count": 97, "updated_at": "2023-04-07T21:05:27.389948Z", "uuid": "dfg9a78-d76a786dghas-78d9fga789g-a78d69a9"}, "direction": "inbound", "faq_uuid": None, "in_reply_to": None, "inserted_at": "2023-04-07T21:05:27.368580Z", "labels": [], "last_status": None, "last_status_timestamp": None, "on_fallback_channel": False, "rendered_content": None, "uuid": "hf78s7s89b-789fb68d9fg-789fb789dfb-f79sfb789"}}, "from": 5475248689, "id": "SBDE4zgAAy7887sfdT35SHFS", "text": {"body": 1000}, "timestamp": 1680901527, "type": "text"}, "type": "message"}')88 89 90# run_simulated_request('intent-classification', 'exit')91# run_simulated_request('intent-classification', "I'm not sure")92# run_simulated_request('intent-classification', "easier")93# run_simulated_request('intent-classification', "easy")94# run_simulated_request('intent-classification', "harder")95# run_simulated_request('intent-classification', "hard")96# run_simulated_request('intent-classification', "hint")97# run_simulated_request('intent-classification', "hin")98# run_simulated_request('intent-classification', "hnt")99# run_simulated_request('intent-classification', "stop")100# run_simulated_request('intent-classification', "stp")101# run_simulated_request('intent-classification', "sop")102# run_simulated_request('intent-classification', "please stop")103# run_simulated_request('sentiment-analysis', 'I reject it')104# run_simulated_request('text2int', 'seven thousand nine hundred fifty seven')105# run_simulated_request('nlu', 'test message')106# run_simulated_request('nlu', 'eight')107# run_simulated_request('nlu', 'is it 8')108# run_simulated_request('nlu', 'can I know how its 0.5')109run_simulated_request('nlu', 'eight, nine, ten')110run_simulated_request('nlu', '8, 9, 10')111run_simulated_request('nlu', '8')112run_simulated_request('nlu', "I don't know")113run_simulated_request('nlu', "I don't know eight")114run_simulated_request('nlu', "I don't 9")115# run_simulated_request('nlu', "0.2")116# run_simulated_request('nlu', 'Today is a wonderful day')117# run_simulated_request('nlu', 'IDK 5?')118# run_simulated_request('nlu', 'hin')119# run_simulated_request('nlu', 'exi')120# run_simulated_request('nlu', 'easier')121# run_simulated_request('nlu', 'stp')122# run_simulated_request('nlu', '')123# run_simulated_request('manager', '')124# run_simulated_request('manager', 'add')125# run_simulated_request('manager', 'subtract')126 127# run_simulated_request("start", {128# 'difficulty': 0.04,129# 'do_increase': True130# })131# run_simulated_request("hint", {132# 'start': 5,133# 'step': 1,134# 'difficulty': 0.56 # optional135# })136# run_simulated_request("question", {137# 'start': 2,138# 'step': 1,139# 'question_num': 2 # optional140# })141# run_simulated_request("difficulty", {142# 'difficulty': 0.01,143# 'do_increase': False # True | False144# })145# Need to start with this command to populate users.json146# If users.json is not already made147# run_simulated_request("num_one", {148# "user_id": "1",149# "message_text": "",150# })151# run_simulated_request("num_one", {152# "user_id": "1",153# "message_text": "61",154# })155# run_simulated_request("sequence", {156# 'start': 2,157# 'step': 1,158# 'sep': '... '159# })160 161# run_simulated_request('manager', 'exit')162 163 164# Example of simplified object received from Turn.io stacks165# This is a contrived example to show the structure, not an actual state166# NOTE: This is actually a bstring, not a dict167simplified_json = {168 "context": {169 "user": "+57787919091", 170 "state": "answer-addition-problem", 171 "bot_message": "What is 2+2?", 172 "user_message": "eight",173 "type": "ask"174 },175 "message_data": {176 "author_id": "+57787919091", 177 "author_type": "OWNER", 178 "contact_uuid": "j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09", 179 "message_body": "eight", 180 "message_direction": "inbound", 181 "message_id": "4kl209sd0-a7b8-2hj3-8563-3hu4a89b32", 182 "message_inserted_at": "2023-01-10T02:37:28.477940Z", 183 "message_updated_at": "2023-01-10T02:37:28.487319Z"184 }185}186 187 188# Full example of event data from Turn.io 189# simplified_json is built from this in Turn.io190# full_json = {191# 'message': {192# '_vnd': {193# 'v1': {194# 'author': {195# 'id': 57787919091,196# 'name': 'GT',197# 'type': 'OWNER'198# },199# 'card_uuid': None,200# 'chat': {201# 'assigned_to': {202# 'id': 'jhk151kl-hj42-3752-3hjk-h4jk6hjkk2',203# 'name': 'Greg Thompson',204# 'type': 'OPERATOR'205# },206# 'contact_uuid': 'j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09',207# 'inserted_at': '2022-07-05T04:00:34.033522Z',208# 'owner': '+57787919091',209# 'permalink': 'https://app.turn.io/c/4kl209sd0-a7b8-2hj3-8563-3hu4a89b32',210# 'state': 'OPEN',211# 'state_reason': 'Re-opened by inbound message.',212# 'unread_count': 19,213# 'updated_at': '2023-01-10T02:37:28.487319Z',214# 'uuid': '4kl209sd0-a7b8-2hj3-8563-3hu4a89b32'215# },216# 'direction': 'inbound',217# 'faq_uuid': None,218# 'in_reply_to': None,219# 'inserted_at': '2023-01-10T02:37:28.477940Z',220# 'labels': [{221# 'confidence': 0.506479332,222# 'metadata': {223# 'nlu': {224# 'confidence': 0.506479332,225# 'intent': 'question',226# 'model_name': 'nlu-general-spacy-ngrams-20191014'227# }228# },229# 'uuid': 'ha7890s2k-hjk2-2476-s8d9-fh9779a8a9ds',230# 'value': 'Unclassified'231# }],232# 'last_status': None,233# 'last_status_timestamp': None,234# 'on_fallback_channel': False,235# 'rendered_content': None,236# 'uuid': 's8df79zhws-h89s-hj23-7s8d-thb248d9bh2qn'237# }238# },239# 'from': 57787919091,240# 'id': 'hsjkthzZGehkzs09sijWA3',241# 'text': {'body': 'eight'},242# 'timestamp': 1673318248,243# 'type': 'text'244# },245# 'type': 'message'246# }247 