PrCore Documentation
AI Assistant GitHub Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Get Streaming Result

PrCore employs SSE to stream prescriptions to the client in a timely manner. For SSE, you can check here for more information.

Note
The status of the project should be STREAMING or SIMULATING, otherwise the SSE endpoint will return 400 error.
Warning
If the user attempts to read results from a project that is already being read by another client, the SSE endpoint will return a 400 error. As it is specifically designed to read results from only one client at a time, it is not advisable to attempt to read results from the same project using multiple clients.

Request

Please note that the SSE endpoint is not a REST endpoint. It is a persistent connection endpoint. You should use a client that supports SSE to connect to the endpoint.

Method Endpoint Request body type Description
GET /project/{project_id}/stream/result none Get event stream of the prescriptions

Response

A message will be sent to the client every time a new prescription is available. The message will be in the following format:

event: message
id: 1234
data: []

The data field is a list containing prescriptions. The format of the prescription is described in the following section.

Also, when connection is established, the client will receive a message with the following format:

event: notification
id: 0
data: CONNECTED

After the simulation is stopped, the client will receive a message with the following format:

event: notification
id: 54321
data: FINISHED

The program will also send ping messages to the client every 15 seconds:

event: ping
id: 12345
data: 2023-03-05 06:04:54.698763

Test with Postman

If the user’s Postman version is v10 or higher, they may utilize the following collection to test the SSE endpoint. Since Postman has altered the method for testing SSE, additional information on this topic can be found here.

SSE Prescriptions
SSE Prescriptions

Below is a GIF that shows the prescriptions SSE endpoint being tested with Postman:

SSE Prescriptions
SSE Prescriptions

Example client script

Here is a sample script that connects to the PrCore SSE endpoint and outputs the prescriptions. This script can be customized to meet the user’s specific requirements. It is important to note that the PROJECT_ID value should be replaced with the actual project ID.

 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
import json
import pprint
import requests
import sseclient

BASE_URL = "http**********"  # Please change this to your local instance address
PROJECT_ID = 1
URL = f"{BASE_URL}/project/{PROJECT_ID}/stream/result"
HEADERS = {
  "Authorization": "Bearer UaJW0QvkMA1cVnOXB89E0NbLf3JRRoHwv2wWmaY5v=QYpaxr1UD9/FupeZ85sa2r"
}


def with_requests(url, headers):
    # Get a streaming response for the given event feed using requests.
    return requests.get(url, stream=True, headers=headers)


response = with_requests(URL, HEADERS)
client = sseclient.SSEClient(response)

print("Waiting for events...")

"""
Please note that the word `event` here is the `E` from `SSE`, 
not the `event` in the business processing perspective.
The data of our `event` is in the `event.data` list.
"""

try:
  for event in client.events():
      if event.event != "message":
        continue

      event_data = json.loads(event.data)
      first_event = event_data[0]
      prescriptions = first_event["prescriptions"]
      prescriptions_with_output = [prescriptions[p] for p in prescriptions if prescriptions[p]["output"]]

      if not prescriptions_with_output:
        continue

      print(f"Received message: {event.event}")
      print(f"ID: {event.id}")


      print(f"Data type: {type(event_data)}")
      print(f"Length: {len(event_data)}")

      pprint.pprint(prescriptions_with_output, width=120)

      print("-" * 24)
except KeyboardInterrupt:
  print("Interrupted by user")

print("Done!")

To run this script, please install the sseclient package first.

python3 -m venv ./venv
./venv/bin/python -m pip install requests sseclient-py
./venv/bin/python sse-client.py

Example prescriptions

The following is an example of the event.data object that is received from the SSE endpoint.

The case_completed attribute is a boolean value that indicates whether or not the case has been completed. If the case is complete, the prescriptions array will be empty.

It is worth noting that event.data is a list, which implies that it may contain multiple events, particularly when the user is connecting to the SSE endpoint for the first time, as there may be some events already in the queue.

[
    {
        "id": 12345,
        "timestamp": "2023-01-01 00:02:00",
        "case_completed": false,
        "data": {
            "case_id": "123",
            "activity": "ACTIVITY_3",
            "timestamp": "2023-01-01 00:01:00",
            "data_attribute_1": 1,
            "data_attribute_2": 2
        },
        "prescriptions": [
            {
                "id": 1213,
                "datetime": "2023-01-01 00:02:00",
                "type": "NEXT_ACTIVITY",
                "output": "ACTIVITY_4",
                "plugin": {
                    "name": "plugin_1",
                    "description": "plugin_1",
                    "accuracy": 0.9,
                    "precision": 0.9,
                    "recall": 0.9,
                    "precision": 0.9
                }
            }
        ]
    }
]