Skip to content

Commit 194781f

Browse files
committed
health & ready endpoints return StatusCodeRecordResult
1 parent 4225588 commit 194781f

11 files changed

+57
-22
lines changed

icpp_llama2/native/main.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,16 @@ int main() {
202202

203203
// -----------------------------------------------------------------------------
204204
// The canister health & readiness checks
205-
// '()' -> '(true)'
206-
mockIC.run_test("health", health, "4449444c0000", "4449444c00017e01",
205+
// '()' -> '(variant { Ok = 200 : nat16 })'
206+
mockIC.run_test("health", health, "4449444c0000",
207+
"4449444c026c019aa1b2f90c7a6b01bc8a0100010100c800",
207208
silent_on_trap, anonymous_principal);
208209

209-
// '()' -> '(false)'
210-
mockIC.run_test("ready", ready, "4449444c0000", "4449444c00017e00",
211-
silent_on_trap, anonymous_principal);
210+
// '()' -> '(variant { Err = record { Other = "Model not yet uploaded or initialize endpoint not yet called"} })'
211+
mockIC.run_test(
212+
"ready", ready, "4449444c0000",
213+
"4449444c026b01b0ad8fcd0c716b01c5fed20100010100003c4d6f64656c206e6f74207965742075706c6f61646564206f7220696e697469616c697a6520656e64706f696e74206e6f74207965742063616c6c6564",
214+
silent_on_trap, anonymous_principal);
212215

213216
// -----------------------------------------------------------------------------
214217

icpp_llama2/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
-r scripts/requirements.txt
22
icpp-pro==3.13.0
33
ic-py==1.0.1
4+
requests

icpp_llama2/scripts/nft_init.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def main() -> int:
5858
# check health (liveness)
5959
print("--\nChecking liveness of canister (did we deploy it!)")
6060
response = canister_llama2.health()
61-
if response == [True]:
61+
if "Ok" in response[0].keys():
6262
print("Ok!")
6363
else:
6464
print("Not OK, response is:")

icpp_llama2/scripts/nft_metadata.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def main() -> int:
4949
# check health (liveness)
5050
print("--\nChecking liveness of canister (did we deploy it!)")
5151
response = canister_llama2.health()
52-
if response == [True]:
52+
if "Ok" in response[0].keys():
5353
print("Ok!")
5454
else:
5555
print("Not OK, response is:")

icpp_llama2/scripts/nft_mint.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def main() -> int:
5959
# check health (liveness)
6060
print("--\nChecking liveness of canister (did we deploy it!)")
6161
response = canister_llama2.health()
62-
if response == [True]:
62+
if "Ok" in response[0].keys():
6363
print("Ok!")
6464
else:
6565
print("Not OK, response is:")
@@ -69,7 +69,7 @@ def main() -> int:
6969
# check readiness for inference
7070
print("--\nChecking if the canister is ready for inference.")
7171
response = canister_llama2.ready()
72-
if response[0] is True:
72+
if "Ok" in response[0].keys():
7373
if DEBUG_VERBOSE >= 2:
7474
print("OK!")
7575
else:

icpp_llama2/scripts/nft_update_story.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def main() -> int:
5959
# check health (liveness)
6060
print("--\nChecking liveness of canister (did we deploy it!)")
6161
response = canister_llama2.health()
62-
if response == [True]:
62+
if "Ok" in response[0].keys():
6363
print("Ok!")
6464
else:
6565
print("Not OK, response is:")
@@ -69,7 +69,7 @@ def main() -> int:
6969
# check readiness for inference
7070
print("--\nChecking if the canister is ready for inference.")
7171
response = canister_llama2.ready()
72-
if response[0] is True:
72+
if "Ok" in response[0].keys():
7373
if DEBUG_VERBOSE >= 2:
7474
print("OK!")
7575
else:

icpp_llama2/scripts/requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ pylint==2.13.9
1313
matplotlib
1414
fastparquet
1515
openpyxl
16-
seaborn
16+
seaborn
17+
requests

icpp_llama2/scripts/upload.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def main() -> int:
7575
# check health (liveness)
7676
print("--\nChecking liveness of canister (did we deploy it!)")
7777
response = canister_llama2.health()
78-
if response == [True]:
78+
if "Ok" in response[0].keys():
7979
print("Ok!")
8080
else:
8181
print("Not OK, response is:")
@@ -204,7 +204,7 @@ def main() -> int:
204204
# check readiness for inference
205205
print("--\nChecking if the canister is ready for inference.")
206206
response = canister_llama2.ready()
207-
if response[0] is True:
207+
if "Ok" in response[0].keys():
208208
if DEBUG_VERBOSE >= 2:
209209
print("OK!")
210210
else:

icpp_llama2/src/canister.cpp

+34-4
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,43 @@ void canister_init() {
132132
void health() {
133133
IC_API ic_api(CanisterQuery{std::string(__func__)}, false);
134134
IC_API::debug_print("llama2 is healthy!");
135-
ic_api.to_wire(CandidTypeBool{true});
135+
136+
CandidTypeRecord status_code_record;
137+
status_code_record.append("status_code",
138+
CandidTypeNat16{Http::StatusCode::OK});
139+
ic_api.to_wire(CandidTypeVariant{"Ok", status_code_record});
136140
}
137141

138142
// readiness endpoint (ready for inference & NFT Collection initialized
139143
void ready() {
140144
IC_API ic_api(CanisterQuery{std::string(__func__)}, false);
141-
bool ready = ready_for_inference && p_nft_collection &&
142-
p_nft_collection->initialized && is_canister_mode_set();
143-
ic_api.to_wire(CandidTypeBool{ready});
145+
146+
if (!ready_for_inference) {
147+
std::string error_msg =
148+
"Model not yet uploaded or initialize endpoint not yet called";
149+
ic_api.to_wire(CandidTypeVariant{
150+
"Err", CandidTypeVariant{"Other", CandidTypeText{error_msg}}});
151+
return;
152+
}
153+
154+
if (!is_canister_mode_set()) {
155+
std::string error_msg = "canister_mode is not yet set";
156+
ic_api.to_wire(CandidTypeVariant{
157+
"Err", CandidTypeVariant{"Other", CandidTypeText{error_msg}}});
158+
return;
159+
}
160+
161+
if (is_canister_mode_nft_ordinal()) {
162+
if (!(p_nft_collection && p_nft_collection->initialized)) {
163+
std::string error_msg =
164+
"canister_mode=nft_ordinal, but nft_collection is not initialized";
165+
ic_api.to_wire(CandidTypeVariant{
166+
"Err", CandidTypeVariant{"Other", CandidTypeText{error_msg}}});
167+
return;
168+
}
169+
}
170+
CandidTypeRecord status_code_record;
171+
status_code_record.append("status_code",
172+
CandidTypeNat16{Http::StatusCode::OK});
173+
ic_api.to_wire(CandidTypeVariant{"Ok", status_code_record});
144174
}

icpp_llama2/src/llama2.did

+2-2
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ service : {
177177
// canister endpoints
178178
canister_init : () -> ();
179179
set_canister_mode : (text) -> (CanisterModeRecordResult);
180-
health : () -> (bool) query;
181-
ready : () -> (bool) query;
180+
health : () -> (StatusCodeRecordResult) query;
181+
ready : () -> (StatusCodeRecordResult) query;
182182

183183
// LLM initialization endpoints
184184
reset_model : () -> (StatusCodeRecordResult);

icpp_llama2/test/test_apis.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test__health(identity_anonymous: dict[str, str], network: str) -> None:
3131
network=network,
3232
timeout_seconds=10,
3333
)
34-
expected_response = "(true)"
34+
expected_response = "(variant { Ok = record { status_code = 200 : nat16;} })"
3535
assert response == expected_response
3636

3737

@@ -44,7 +44,7 @@ def test__ready(identity_anonymous: dict[str, str], network: str) -> None:
4444
network=network,
4545
timeout_seconds=10,
4646
)
47-
expected_response = "(true)"
47+
expected_response = "(variant { Ok = record { status_code = 200 : nat16;} })"
4848
assert response == expected_response
4949

5050

0 commit comments

Comments
 (0)