Fetching Thread Artifacts

In certain cases, Alfa will produce a table or graph in its answer. These artifacts will be included in messages as JSON wrapped in markdown code blocks (triple backticks).

This is an artifact in a message:
```{"type": "output_artifact", "artifact_id": "..."}```
Text in a message can come before or after the object.

Extracting Artifact ID’s

In order to fetch the contents of these artifacts, the artifact ID’s must be extracted:

1import re
2
3
4def extract_artifact_ids_from_message(message: str) -> list[str]:
5 regex = r"""```[\s\S]*?\"artifact_id\"\s*:\s*\"([^\"]+)\"[\s\S]*?```"""
6 return re.findall(regex, message)

Fetching Artifacts

1def get_thread_artifact(thread_id: str, artifact_id: str):
2 """
3 Get a thread artifact (table or graph).
4 """
5 url = f"{BASE_URL}/v2/thread/{thread_id}/artifact/{artifact_id}"
6
7 response = requests.get(url, headers=headers)
8
9 if response.status_code == 200:
10 data = response.json()
11 print("Got artifact")
12 print(data["artifact"])
13 else:
14 print(f"Error: {response.status_code}, {response.text}")