Skip to content

Commit

Permalink
Refactor "err" to "Err", "ok" to "Ok"
Browse files Browse the repository at this point in the history
  • Loading branch information
icppWorld committed Mar 26, 2024
1 parent c6c142f commit 890efac
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 122 deletions.
12 changes: 8 additions & 4 deletions icpp_llama2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@
# The ICGPT backend canisters
python -m scripts.upload --network local --canister llama2_260K --model stories260K/stories260K.bin --tokenizer stories260K/tok512.bin
python -m scripts.upload --network local --canister llama2 --model models/stories15M.bin --tokenizer tokenizers/tokenizer.bin
python -m scripts.upload --network local --canister llama2 --model models/stories15Mtok4096.bin --tokenizer tokenizers/tok4096.bin
python -m scripts.upload --network local --canister llama2_42M --model models/stories42M.bin --tokenizer tokenizers/tokenizer.bin
python -m scripts.upload --network local --canister llama2_42M --model models/stories42Mtok4096.bin --tokenizer tokenizers/tok4096.bin
python -m scripts.upload --network local --canister llama2_110M --model models/stories110M.bin --tokenizer tokenizers/tokenizer.bin
Expand All @@ -112,6 +112,10 @@
# For an NFT canister: mint the NFTs
=> TODO
# ---------------------------------------------------------------
# Run tests
pytest
```
# stories260K
Expand Down Expand Up @@ -141,15 +145,15 @@ For testing, it is nice to be able to work with a smaller model & tokenizer:
$ dfx canister call llama2_260K inference '(record {prompt = "Lilly went swimming yesterday " : text; steps = 100 : nat64; temperature = 0.9 : float32; topp = 0.9 : float32; rng_seed = 0 : nat64;})'
(
variant {
ok = "Lilly went swimming yesterday order. She had a great eyes that was closed. One day, she asked her mom why the cloud was close to the pond. \n\"Mommy, I will take clothes away,\" Lila said. \"Th\n"
Ok = "Lilly went swimming yesterday order. She had a great eyes that was closed. One day, she asked her mom why the cloud was close to the pond. \n\"Mommy, I will take clothes away,\" Lila said. \"Th\n"
},
)
# Continue the current chat by calling again, with an empty prompt
$ dfx canister call llama2_260K inference '(record {prompt = "" : text; steps = 100 : nat64; temperature = 0.9 : float32; topp = 0.9 : float32; rng_seed = 0 : nat64;})'
(
variant {
ok = "eone replace it.\nThe fox agreed to go as fast as they set on the other birds. They searched, and it didn\'t give up. They started to scared the bird. The forest was so careful and jumped up."
Ok = "eone replace it.\nThe fox agreed to go as fast as they set on the other birds. They searched, and it didn\'t give up. They started to scared the bird. The forest was so careful and jumped up."
},
)
Expand Down
156 changes: 78 additions & 78 deletions icpp_llama2/native/main.cpp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion icpp_llama2/scripts/nft_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def main() -> int:
"nft_description": nft_description,
}
response = canister_llama2.nft_init(record_in)
if "ok" in response[0].keys():
if "Ok" in response[0].keys():
if DEBUG_VERBOSE >= 2:
print("OK!")
else:
Expand Down
2 changes: 1 addition & 1 deletion icpp_llama2/scripts/nft_mint.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def main() -> int:
try:
response = canister_llama2.nft_mint(NFT)
print(response)
if "ok" in response[0].keys():
if "Ok" in response[0].keys():
if DEBUG_VERBOSE >= 2:
print("OK!")
else:
Expand Down
12 changes: 6 additions & 6 deletions icpp_llama2/scripts/nft_update_story.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def main() -> int:
if use_full_prompt:
response = canister_llama2.nft_story_start(NFT, prompt)
print(response)
if "ok" in response[0].keys():
if "Ok" in response[0].keys():
if DEBUG_VERBOSE >= 2:
print("OK!")
else:
Expand All @@ -113,7 +113,7 @@ def main() -> int:
prompt["prompt"] = first_word # Update the prompt with the first word
response = canister_llama2.nft_story_start(NFT, prompt)
print(response)
if "ok" in response[0].keys():
if "Ok" in response[0].keys():
if DEBUG_VERBOSE >= 2:
print("OK!")
else:
Expand All @@ -127,7 +127,7 @@ def main() -> int:
prompt["prompt"] = word # Update the prompt with the current word
response = canister_llama2.nft_story_continue(NFT, prompt)
print(response)
if "ok" in response[0].keys():
if "Ok" in response[0].keys():
if DEBUG_VERBOSE >= 2:
print("OK!")
else:
Expand All @@ -136,7 +136,7 @@ def main() -> int:

response = canister_llama2.nft_story_start(NFT, prompt)
print(response)
if "ok" in response[0].keys():
if "Ok" in response[0].keys():
if DEBUG_VERBOSE >= 2:
print("OK!")
else:
Expand All @@ -152,9 +152,9 @@ def main() -> int:
while True:
response = canister_llama2.nft_story_continue(NFT, prompt)
print(response)
if "ok" in response[0].keys():
if "Ok" in response[0].keys():
# Check if the response is an empty string. If it is, break out of the loop.
if response[0]["ok"] == "":
if response[0]["Ok"] == "":
print("The end!")
break
else:
Expand Down
10 changes: 5 additions & 5 deletions icpp_llama2/scripts/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def main() -> int:
# Reset the tokenizer
print("--\nResetting the tokenizer in canister")
response = canister_llama2.reset_tokenizer() # pylint: disable=no-member
if "ok" in response[0].keys():
if "Ok" in response[0].keys():
if DEBUG_VERBOSE >= 2:
print("OK!")
else:
Expand Down Expand Up @@ -125,7 +125,7 @@ def main() -> int:
response = canister_llama2.upload_tokenizer_bytes_chunk(
chunk
) # pylint: disable=no-member
if "ok" in response[0].keys():
if "Ok" in response[0].keys():
print("OK!")
else:
print("Something went wrong:")
Expand All @@ -142,7 +142,7 @@ def main() -> int:
# Reset the model
print("--\nResetting the model in canister")
response = canister_llama2.reset_model() # pylint: disable=no-member
if "ok" in response[0].keys():
if "Ok" in response[0].keys():
if DEBUG_VERBOSE >= 2:
print("OK!")
else:
Expand Down Expand Up @@ -178,7 +178,7 @@ def main() -> int:
response = canister_llama2.upload_model_bytes_chunk(
chunk
) # pylint: disable=no-member
if "ok" in response[0].keys():
if "Ok" in response[0].keys():
if DEBUG_VERBOSE >= 2:
print("OK!")
else:
Expand All @@ -190,7 +190,7 @@ def main() -> int:
# Initialize the canister
print("--\nInitializing the canister, getting it ready for inference.")
response = canister_llama2.initialize()
if "ok" in response[0].keys():
if "Ok" in response[0].keys():
if DEBUG_VERBOSE >= 2:
print("OK!")
else:
Expand Down
2 changes: 1 addition & 1 deletion icpp_llama2/src/canister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ bool is_canister_owner(IC_API &ic_api, bool err_to_wire) {

if (err_to_wire) {
uint16_t status_code = Http::StatusCode::Unauthorized;
ic_api.to_wire(CandidTypeVariant{"err", CandidTypeNat16{status_code}});
ic_api.to_wire(CandidTypeVariant{"Err", CandidTypeNat16{status_code}});
}
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions icpp_llama2/src/chats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ bool is_ready_and_authorized(IC_API ic_api) {

if (!ready_for_inference) {
uint16_t status_code = Http::StatusCode::InternalServerError;
ic_api.to_wire(CandidTypeVariant{"err", CandidTypeNat16{status_code}});
ic_api.to_wire(CandidTypeVariant{"Err", CandidTypeNat16{status_code}});
return false;
}

// Anonymous user is not allowed
CandidTypePrincipal caller = ic_api.get_caller();
if (caller.is_anonymous()) {
ic_api.to_wire(CandidTypeVariant{
"err",
"Err",
CandidTypeText{
"The Llama2 canister does not allow calling with anonymous principal."}});
return false;
Expand All @@ -170,5 +170,5 @@ void new_chat() {
build_new_chat(principal);

ic_api.to_wire(
CandidTypeVariant{"ok", CandidTypeNat16{Http::StatusCode::OK}});
CandidTypeVariant{"Ok", CandidTypeNat16{Http::StatusCode::OK}});
}
2 changes: 1 addition & 1 deletion icpp_llama2/src/inference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,5 @@ void do_inference(IC_API &ic_api, Prompt wire_prompt, Chat *chat,
// IC_API::debug_print(output);

// Send the generated response to the wire
ic_api.to_wire(CandidTypeVariant{"ok", CandidTypeText{output}});
ic_api.to_wire(CandidTypeVariant{"Ok", CandidTypeText{output}});
}
2 changes: 1 addition & 1 deletion icpp_llama2/src/initialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ void initialize() {
ready_for_inference = true;

ic_api.to_wire(
CandidTypeVariant{"ok", CandidTypeNat16{Http::StatusCode::OK}});
CandidTypeVariant{"Ok", CandidTypeNat16{Http::StatusCode::OK}});
}

void print_config() {
Expand Down
12 changes: 6 additions & 6 deletions icpp_llama2/src/llama2.did
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ type Prompt = record {
};

type Inference = variant {
err : text;
ok : text;
Ok : text;
Err : text;
};

type Config = record {
Expand All @@ -27,13 +27,13 @@ type Config = record {
type StatusCode = nat16;

type Result = variant {
err : StatusCode;
ok : StatusCode;
Ok : StatusCode;
Err : StatusCode;
};

type TextResult = variant {
err : StatusCode;
ok : text;
Ok : text;
Err : StatusCode;
};

type ChatStartTime = nat64;
Expand Down
10 changes: 5 additions & 5 deletions icpp_llama2/src/nft_collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ bool nft_is_whitelisted(IC_API &ic_api, bool err_to_wire) {

if (err_to_wire) {
uint16_t status_code = Http::StatusCode::Unauthorized;
ic_api.to_wire(CandidTypeVariant{"err", CandidTypeNat16{status_code}});
ic_api.to_wire(CandidTypeVariant{"Err", CandidTypeNat16{status_code}});
}
return false;
}
Expand Down Expand Up @@ -102,7 +102,7 @@ void nft_whitelist() {
p_nft_whitelist->whitelist.push_back(item);

ic_api.to_wire(
CandidTypeVariant{"ok", CandidTypeNat16{Http::StatusCode::OK}});
CandidTypeVariant{"Ok", CandidTypeNat16{Http::StatusCode::OK}});
}

// Initialize the NFT Collection
Expand Down Expand Up @@ -143,7 +143,7 @@ void nft_init() {
p_nft_collection->initialized = true;

ic_api.to_wire(
CandidTypeVariant{"ok", CandidTypeNat16{Http::StatusCode::OK}});
CandidTypeVariant{"Ok", CandidTypeNat16{Http::StatusCode::OK}});
}

// Get metadata of the NFT Collection
Expand Down Expand Up @@ -204,7 +204,7 @@ void nft_mint() {
p_nft_collection->nfts.push_back(nft);

ic_api.to_wire(
CandidTypeVariant{"ok", CandidTypeNat16{Http::StatusCode::OK}});
CandidTypeVariant{"Ok", CandidTypeNat16{Http::StatusCode::OK}});
}

// Endpoints for the story of an NFT, callable by whitelisted principals only
Expand Down Expand Up @@ -269,5 +269,5 @@ void nft_get_story() {
}

ic_api.to_wire(CandidTypeVariant{
"ok", CandidTypeText{p_chats_output_history->umap[token_id]}});
"Ok", CandidTypeText{p_chats_output_history->umap[token_id]}});
}
8 changes: 4 additions & 4 deletions icpp_llama2/src/upload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void reset_model() {
delete_model_bytes_memory();

ic_api.to_wire(
CandidTypeVariant{"ok", CandidTypeNat16{Http::StatusCode::OK}});
CandidTypeVariant{"Ok", CandidTypeNat16{Http::StatusCode::OK}});
}

void reset_tokenizer() {
Expand All @@ -120,7 +120,7 @@ void reset_tokenizer() {
delete_tokenizer_bytes_memory();

ic_api.to_wire(
CandidTypeVariant{"ok", CandidTypeNat16{Http::StatusCode::OK}});
CandidTypeVariant{"Ok", CandidTypeNat16{Http::StatusCode::OK}});
}

// Endpoint for uploading the stories15M.bin file as bytes
Expand All @@ -137,7 +137,7 @@ void upload_model_bytes_chunk() {
print_upload_model_bytes_summary(std::string(__func__), v);

ic_api.to_wire(
CandidTypeVariant{"ok", CandidTypeNat16{Http::StatusCode::OK}});
CandidTypeVariant{"Ok", CandidTypeNat16{Http::StatusCode::OK}});
}

// Endpoint for uploading the tokenizer.bin file as bytes
Expand All @@ -155,5 +155,5 @@ void upload_tokenizer_bytes_chunk() {
print_upload_tokenizer_bytes_summary(std::string(__func__), v);

ic_api.to_wire(
CandidTypeVariant{"ok", CandidTypeNat16{Http::StatusCode::OK}});
CandidTypeVariant{"Ok", CandidTypeNat16{Http::StatusCode::OK}});
}
13 changes: 7 additions & 6 deletions icpp_llama2/test/test_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
$ pytest --network=[local/ic] test_apis.py
"""

# pylint: disable=unused-argument, missing-function-docstring, unused-import, wildcard-import, unused-wildcard-import, line-too-long

from pathlib import Path
Expand Down Expand Up @@ -69,7 +70,7 @@ def test__reset_model_err(identity_anonymous: dict[str, str], network: str) -> N
network=network,
timeout_seconds=10,
)
expected_response = "(variant { err = 401 : nat16 })"
expected_response = "(variant { Err = 401 : nat16 })"
assert response == expected_response


Expand All @@ -82,7 +83,7 @@ def test__new_chat_anonymous(identity_anonymous: dict[str, str], network: str) -
network=network,
timeout_seconds=10,
)
assert "err" in response
assert "Err" in response


def test__new_chat(identity_default: dict[str, str], network: str) -> None:
Expand All @@ -94,7 +95,7 @@ def test__new_chat(identity_default: dict[str, str], network: str) -> None:
network=network,
timeout_seconds=10,
)
assert "ok" in response
assert "Ok" in response


def test__inference_1_anonymous(
Expand All @@ -108,7 +109,7 @@ def test__inference_1_anonymous(
network=network,
timeout_seconds=10,
)
assert "err" in response
assert "Err" in response


def test__inference_1(identity_default: dict[str, str], network: str) -> None:
Expand All @@ -120,7 +121,7 @@ def test__inference_1(identity_default: dict[str, str], network: str) -> None:
network=network,
timeout_seconds=10,
)
assert "ok" in response
assert "Ok" in response


def test__inference_2(identity_default: dict[str, str], network: str) -> None:
Expand All @@ -132,7 +133,7 @@ def test__inference_2(identity_default: dict[str, str], network: str) -> None:
network=network,
timeout_seconds=10,
)
assert "ok" in response
assert "Ok" in response


# ----------------------------------------------------------------------------------
Expand Down

0 comments on commit 890efac

Please sign in to comment.