File size: 11,256 Bytes
48c823d
7ff1fe0
 
48c823d
 
 
 
 
 
 
 
 
 
 
 
cc67cbe
48c823d
 
 
 
 
 
47c0422
 
9be6a6a
 
 
 
 
48c823d
47c0422
 
48c823d
 
 
 
 
47c0422
 
 
 
 
 
 
 
48c823d
1399a2f
 
 
 
48c823d
1399a2f
 
 
 
 
 
 
 
 
 
6e3f695
1399a2f
6e3f695
48c823d
1399a2f
 
 
 
 
 
 
 
8f01606
 
46b6696
8f01606
 
46b6696
8f01606
 
cc67cbe
46b6696
8f01606
46b6696
 
0dc3012
 
a61d64f
 
 
 
 
 
 
 
 
 
 
b71b7ec
 
1399a2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b71b7ec
c69c12b
 
 
b06e4f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cbbb51a
 
ee60b4d
 
 
 
a24de84
 
 
 
b06e4f7
 
 
 
 
c69c12b
b71b7ec
48c823d
 
 
 
 
 
 
cc67cbe
 
 
 
48c823d
b71b7ec
48c823d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc67cbe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import json
import requests


def add_message_text_to_sample_object(message_text):
    """
    Builds a sample request object using an example of a student answer

    Input
    - message_text: str - an example of user input to test

    Example Input
    "test message"

    Output
    - b_string: json b-string - simulated Turn.io message data

    Example Output
    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"}}'
    
    """
    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"' + '}'
    # context_data = '{' + '"user":"", "state":"addition-question-sequence", "bot_message":"", "user_message":"{message_text}"' + '}'

    # V1
    # context_data = '{' + '"user":"", "state":"start-conversation", "bot_message":"", "user_message":"{message_text}"' + '}'

    #V2
    context_data = '{' + '"contact_uuid": "j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09", "current_state":"", "local_state": "", "user_message":""' + '}'

    # 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"' + '}'

    json_string = '{' + f'"context_data": {context_data}, "message_data": {message_data}' + '}'
    b_string = json_string.encode("utf-8")

    return b_string

# """
#     "text": "What is 2+3?",
#     "question_numbers": [2,3],
#     "right_answer": 5,
#     "number_correct": 2,
#     "hints_used": 0,
# """


def run_simulated_request(endpoint, sample_payload, context=None):
    print(f"Case: {sample_payload}")
    # Used for testing full message object - deprecated April 7
    # b_string = add_message_text_to_sample_object(sample_answer)

    if endpoint == 'sentiment-analysis' or endpoint == 'text2int' or endpoint =='intent-classification':
        request = requests.post(
            url=f'http://localhost:7860/{endpoint}',
            json={'content': sample_payload}
        ).json()
    else:
        request = requests.post(
            url=f'http://localhost:7860/{endpoint}',
            data=b_string
        ).json()

    print(request)


def run_full_nlu_endpoint_payload_test(sample_payload):
    request = requests.post(
        url=f'http://localhost:7860/nlu',
        data=sample_payload
    ).json()
    print(request)


# # Case: Wrong key
# run_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"}}')

# # Case: Correct payload
# run_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"}}')

# # Case: Incorrect payload values
# run_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"}}')

# Case: Wrong payload object
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"}')


# run_simulated_request('intent-classification', 'exit')
# run_simulated_request('intent-classification', "I'm not sure")
# run_simulated_request('intent-classification', "easier")
# run_simulated_request('intent-classification', "easy")
# run_simulated_request('intent-classification', "harder")
# run_simulated_request('intent-classification', "hard")
# run_simulated_request('intent-classification', "hint")
# run_simulated_request('intent-classification', "hin")
# run_simulated_request('intent-classification', "hnt")
# run_simulated_request('intent-classification', "stop")
# run_simulated_request('intent-classification', "stp")
# run_simulated_request('intent-classification', "sop")
# run_simulated_request('intent-classification', "please stop")
# run_simulated_request('sentiment-analysis', 'I reject it')
# run_simulated_request('text2int', 'seven thousand nine hundred fifty seven')
# run_simulated_request('nlu', 'test message')
# run_simulated_request('nlu', 'eight')
# run_simulated_request('nlu', 'is it 8')
# run_simulated_request('nlu', 'can I know how its 0.5')
# run_simulated_request('nlu', 'eight, nine, ten')
# run_simulated_request('nlu', '8, 9, 10')
# run_simulated_request('nlu', '8')
# run_simulated_request('nlu', "I don't know")
# run_simulated_request('nlu', "I don't know eight")
# run_simulated_request('nlu', "I don't 9")
# run_simulated_request('nlu', "0.2")
# run_simulated_request('nlu', 'Today is a wonderful day')
# run_simulated_request('nlu', 'IDK 5?')
# run_simulated_request('nlu', 'hin')
# run_simulated_request('nlu', 'exi')
# run_simulated_request('nlu', 'easier')
# run_simulated_request('nlu', 'stp')
# run_simulated_request('nlu', '')
# run_simulated_request('manager', '')
# run_simulated_request('manager', 'add')
# run_simulated_request('manager', 'subtract')

# run_simulated_request("start", {
#     'difficulty': 0.04,
#     'do_increase': True
# })
# run_simulated_request("hint", {
#     'start': 5,
#     'step': 1,
#     'difficulty': 0.56  # optional
# })
# run_simulated_request("question", {
#     'start': 2,
#     'step': 1,
#     'question_num': 2  # optional
# })
# run_simulated_request("difficulty", {
#     'difficulty': 0.01,
#     'do_increase': False  # True | False
# })
# Need to start with this command to populate users.json
# If users.json is not already made
# run_simulated_request("num_one", {
#     "user_id": "1",
#     "message_text": "",
# })
# run_simulated_request("num_one", {
#     "user_id": "1",
#     "message_text": "61",
# })
# run_simulated_request("sequence", {
#     'start': 2,
#     'step': 1,
#     'sep': '... '
# })

# run_simulated_request('manager', 'exit')


# Example of simplified object received from Turn.io stacks
# This is a contrived example to show the structure, not an actual state
# NOTE: This is actually a bstring, not a dict
simplified_json = {
    "context": {
        "user": "+57787919091", 
        "state": "answer-addition-problem", 
        "bot_message": "What is 2+2?", 
        "user_message": "eight",
        "type": "ask"
    },
    "message_data": {
        "author_id": "+57787919091", 
        "author_type": "OWNER", 
        "contact_uuid": "j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09", 
        "message_body": "eight", 
        "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"
    }
}


# Full example of event data from Turn.io 
# simplified_json is built from this in Turn.io
# full_json = {
#     'message': {
#         '_vnd': {
#             'v1': {
#                 'author': {
#                     'id': 57787919091,
#                     'name': 'GT',
#                     'type': 'OWNER'
#                 },
#                 'card_uuid': None,
#                 'chat': {
#                     'assigned_to': {
#                         'id': 'jhk151kl-hj42-3752-3hjk-h4jk6hjkk2',
#                         'name': 'Greg Thompson',
#                         'type': 'OPERATOR'
#                     },
#                     'contact_uuid': 'j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09',
#                     'inserted_at': '2022-07-05T04:00:34.033522Z',
#                     'owner': '+57787919091',
#                     'permalink': 'https://app.turn.io/c/4kl209sd0-a7b8-2hj3-8563-3hu4a89b32',
#                     'state': 'OPEN',
#                     'state_reason': 'Re-opened by inbound message.',
#                     'unread_count': 19,
#                     'updated_at': '2023-01-10T02:37:28.487319Z',
#                     'uuid': '4kl209sd0-a7b8-2hj3-8563-3hu4a89b32'
#                 },
#                 'direction': 'inbound',
#                 'faq_uuid': None,
#                 'in_reply_to': None,
#                 'inserted_at': '2023-01-10T02:37:28.477940Z',
#                 'labels': [{
#                     'confidence': 0.506479332,
#                     'metadata': {
#                         'nlu': {
#                             'confidence': 0.506479332,
#                             'intent': 'question',
#                             'model_name': 'nlu-general-spacy-ngrams-20191014'
#                         }
#                     },
#                     'uuid': 'ha7890s2k-hjk2-2476-s8d9-fh9779a8a9ds',
#                     'value': 'Unclassified'
#                 }],
#                 'last_status': None,
#                 'last_status_timestamp': None,
#                 'on_fallback_channel': False,
#                 'rendered_content': None,
#                 'uuid': 's8df79zhws-h89s-hj23-7s8d-thb248d9bh2qn'
#             }
#         },
#         'from': 57787919091,
#         'id': 'hsjkthzZGehkzs09sijWA3',
#         'text': {'body': 'eight'},
#         'timestamp': 1673318248,
#         'type': 'text'
#     },
#     'type': 'message'
# }