2024-11-02 00:17:30 -07:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
2025-02-19 15:44:30 -08:00
"# SGLang Native APIs\n",
2024-11-03 22:33:03 -08:00
"\n",
2025-08-10 19:49:45 -07:00
"Apart from the OpenAI compatible APIs, the SGLang Runtime also provides its native server APIs. We introduce the following APIs:\n",
2024-11-02 00:17:30 -07:00
"\n",
2024-11-03 22:33:03 -08:00
"- `/generate` (text generation model)\n",
2024-11-02 00:17:30 -07:00
"- `/get_model_info`\n",
2024-11-24 01:37:58 -08:00
"- `/get_server_info`\n",
2024-11-02 00:17:30 -07:00
"- `/health`\n",
"- `/health_generate`\n",
"- `/flush_cache`\n",
2024-11-02 11:57:22 -07:00
"- `/update_weights`\n",
2024-11-03 22:33:03 -08:00
"- `/encode`(embedding model)\n",
2025-06-20 12:53:54 +08:00
"- `/v1/rerank`(cross encoder rerank model)\n",
2024-11-04 23:53:44 -08:00
"- `/classify`(reward model)\n",
2025-03-24 21:34:19 -07:00
"- `/start_expert_distribution_record`\n",
"- `/stop_expert_distribution_record`\n",
"- `/dump_expert_distribution_record`\n",
2025-10-08 10:08:48 +05:30
"- `/tokenize`\n",
"- `/detokenize`\n",
2025-08-10 19:49:45 -07:00
"- A full list of these APIs can be found at [http_server.py](https://github.com/sgl-project/sglang/blob/main/python/sglang/srt/entrypoints/http_server.py)\n",
2024-11-02 00:17:30 -07:00
"\n",
2025-08-10 19:49:45 -07:00
"We mainly use `requests` to test these APIs in the following examples. You can also use `curl`.\n"
2024-11-02 00:17:30 -07:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Launch A Server"
]
},
{
"cell_type": "code",
"execution_count": null,
2024-11-23 05:04:51 +08:00
"metadata": {},
2024-11-02 00:17:30 -07:00
"outputs": [],
"source": [
2025-08-10 19:49:45 -07:00
"from sglang.test.doc_patch import launch_server_cmd\n",
2025-02-15 03:57:00 +00:00
"from sglang.utils import wait_for_server, print_highlight, terminate_process\n",
"\n",
"server_process, port = launch_server_cmd(\n",
2025-09-04 09:52:53 -04:00
" \"python3 -m sglang.launch_server --model-path qwen/qwen2.5-0.5b-instruct --host 0.0.0.0 --log-level warning\"\n",
2024-11-02 00:17:30 -07:00
")\n",
"\n",
2025-02-15 03:57:00 +00:00
"wait_for_server(f\"http://localhost:{port}\")"
2024-11-02 00:17:30 -07:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2024-11-03 22:33:03 -08:00
"## Generate (text generation model)\n",
2025-08-10 19:49:45 -07:00
"Generate completions. This is similar to the `/v1/completions` in OpenAI API. Detailed parameters can be found in the [sampling parameters](sampling_params.md)."
2024-11-02 00:17:30 -07:00
]
},
{
"cell_type": "code",
"execution_count": null,
2024-11-23 05:04:51 +08:00
"metadata": {},
2024-11-02 00:17:30 -07:00
"outputs": [],
"source": [
2025-08-10 19:49:45 -07:00
"import requests\n",
"\n",
2025-02-15 03:57:00 +00:00
"url = f\"http://localhost:{port}/generate\"\n",
2024-11-02 11:57:22 -07:00
"data = {\"text\": \"What is the capital of France?\"}\n",
2024-11-02 00:17:30 -07:00
"\n",
"response = requests.post(url, json=data)\n",
2024-11-02 13:26:32 -07:00
"print_highlight(response.json())"
2024-11-02 00:17:30 -07:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Get Model Info\n",
"\n",
2024-11-02 13:26:32 -07:00
"Get the information of the model.\n",
2024-11-02 00:17:30 -07:00
"\n",
"- `model_path`: The path/name of the model.\n",
2024-11-29 17:17:00 -08:00
"- `is_generation`: Whether the model is used as generation model or embedding model.\n",
2025-07-09 12:05:58 +08:00
"- `tokenizer_path`: The path/name of the tokenizer.\n",
2025-08-15 03:13:46 +08:00
"- `preferred_sampling_params`: The default sampling params specified via `--preferred-sampling-params`. `None` is returned in this example as we did not explicitly configure it in server args.\n",
"- `weight_version`: This field contains the version of the model weights. This is often used to track changes or updates to the model’ s trained parameters."
2024-11-02 00:17:30 -07:00
]
},
{
"cell_type": "code",
"execution_count": null,
2024-11-23 05:04:51 +08:00
"metadata": {},
2024-11-02 00:17:30 -07:00
"outputs": [],
"source": [
2025-02-15 03:57:00 +00:00
"url = f\"http://localhost:{port}/get_model_info\"\n",
2024-11-02 00:17:30 -07:00
"\n",
"response = requests.get(url)\n",
"response_json = response.json()\n",
"print_highlight(response_json)\n",
2025-04-21 02:38:25 +02:00
"assert response_json[\"model_path\"] == \"qwen/qwen2.5-0.5b-instruct\"\n",
2024-11-02 13:26:32 -07:00
"assert response_json[\"is_generation\"] is True\n",
2025-04-21 02:38:25 +02:00
"assert response_json[\"tokenizer_path\"] == \"qwen/qwen2.5-0.5b-instruct\"\n",
2025-07-09 12:05:58 +08:00
"assert response_json[\"preferred_sampling_params\"] is None\n",
2025-07-03 15:25:00 -07:00
"assert response_json.keys() == {\n",
" \"model_path\",\n",
" \"is_generation\",\n",
" \"tokenizer_path\",\n",
" \"preferred_sampling_params\",\n",
2025-08-15 03:13:46 +08:00
" \"weight_version\",\n",
2025-07-03 15:25:00 -07:00
"}"
2024-11-02 00:17:30 -07:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2024-11-24 01:37:58 -08:00
"## Get Server Info\n",
"Gets the server information including CLI arguments, token limits, and memory pool sizes.\n",
"- Note: `get_server_info` merges the following deprecated endpoints:\n",
" - `get_server_args`\n",
" - `get_memory_pool_size` \n",
" - `get_max_total_num_tokens`"
2024-11-02 00:17:30 -07:00
]
},
{
"cell_type": "code",
"execution_count": null,
2024-11-23 05:04:51 +08:00
"metadata": {},
2024-11-02 00:17:30 -07:00
"outputs": [],
"source": [
2025-02-15 03:57:00 +00:00
"url = f\"http://localhost:{port}/get_server_info\"\n",
2024-11-02 00:17:30 -07:00
"\n",
"response = requests.get(url)\n",
"print_highlight(response.text)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2024-11-24 01:37:58 -08:00
"## Health Check\n",
"- `/health`: Check the health of the server.\n",
"- `/health_generate`: Check the health of the server by generating one token."
2024-11-02 00:17:30 -07:00
]
},
{
"cell_type": "code",
"execution_count": null,
2024-11-23 05:04:51 +08:00
"metadata": {},
2024-11-02 00:17:30 -07:00
"outputs": [],
"source": [
2025-02-15 03:57:00 +00:00
"url = f\"http://localhost:{port}/health_generate\"\n",
2024-11-02 00:17:30 -07:00
"\n",
2024-11-24 01:37:58 -08:00
"response = requests.get(url)\n",
2024-11-02 00:17:30 -07:00
"print_highlight(response.text)"
]
},
{
"cell_type": "code",
"execution_count": null,
2024-11-23 05:04:51 +08:00
"metadata": {},
2024-11-02 00:17:30 -07:00
"outputs": [],
"source": [
2025-02-15 03:57:00 +00:00
"url = f\"http://localhost:{port}/health\"\n",
2024-11-02 00:17:30 -07:00
"\n",
"response = requests.get(url)\n",
"print_highlight(response.text)"
]
},
2024-11-22 15:10:10 -08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2024-11-24 01:37:58 -08:00
"## Flush Cache\n",
2024-11-22 15:10:10 -08:00
"\n",
2024-11-24 01:37:58 -08:00
"Flush the radix cache. It will be automatically triggered when the model weights are updated by the `/update_weights` API."
2024-11-22 15:10:10 -08:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2025-02-15 03:57:00 +00:00
"url = f\"http://localhost:{port}/flush_cache\"\n",
2024-11-22 15:10:10 -08:00
"\n",
2024-11-24 01:37:58 -08:00
"response = requests.post(url)\n",
2024-11-22 15:10:10 -08:00
"print_highlight(response.text)"
]
},
2024-11-02 00:17:30 -07:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2024-11-29 17:17:00 -08:00
"## Update Weights From Disk\n",
2024-11-02 00:17:30 -07:00
"\n",
2024-11-29 17:17:00 -08:00
"Update model weights from disk without restarting the server. Only applicable for models with the same architecture and parameter size.\n",
"\n",
"SGLang support `update_weights_from_disk` API for continuous evaluation during training (save checkpoint to disk and update weights from disk).\n"
2024-11-02 00:17:30 -07:00
]
},
{
"cell_type": "code",
"execution_count": null,
2024-11-23 05:04:51 +08:00
"metadata": {},
2024-11-02 00:17:30 -07:00
"outputs": [],
"source": [
"# successful update with same architecture and size\n",
"\n",
2025-02-15 03:57:00 +00:00
"url = f\"http://localhost:{port}/update_weights_from_disk\"\n",
2025-04-21 02:38:25 +02:00
"data = {\"model_path\": \"qwen/qwen2.5-0.5b-instruct\"}\n",
2024-11-02 00:17:30 -07:00
"\n",
"response = requests.post(url, json=data)\n",
"print_highlight(response.text)\n",
2024-11-05 01:12:10 -08:00
"assert response.json()[\"success\"] is True\n",
2025-03-03 00:12:04 -08:00
"assert response.json()[\"message\"] == \"Succeeded to update model weights.\""
2024-11-02 00:17:30 -07:00
]
},
{
"cell_type": "code",
"execution_count": null,
2024-11-23 05:04:51 +08:00
"metadata": {},
2024-11-02 00:17:30 -07:00
"outputs": [],
"source": [
2024-12-04 11:19:46 -08:00
"# failed update with different parameter size or wrong name\n",
2024-11-02 00:17:30 -07:00
"\n",
2025-02-15 03:57:00 +00:00
"url = f\"http://localhost:{port}/update_weights_from_disk\"\n",
2025-04-21 02:38:25 +02:00
"data = {\"model_path\": \"qwen/qwen2.5-0.5b-instruct-wrong\"}\n",
2024-11-02 00:17:30 -07:00
"\n",
"response = requests.post(url, json=data)\n",
"response_json = response.json()\n",
"print_highlight(response_json)\n",
2024-11-05 01:12:10 -08:00
"assert response_json[\"success\"] is False\n",
2024-11-02 00:17:30 -07:00
"assert response_json[\"message\"] == (\n",
2024-12-04 11:19:46 -08:00
" \"Failed to get weights iterator: \"\n",
2025-04-21 02:38:25 +02:00
" \"qwen/qwen2.5-0.5b-instruct-wrong\"\n",
2024-12-04 11:19:46 -08:00
" \" (repository not found).\"\n",
2024-11-02 00:17:30 -07:00
")"
]
},
2025-04-21 02:38:25 +02:00
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"terminate_process(server_process)"
]
},
2024-11-02 11:57:22 -07:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2024-11-03 22:33:03 -08:00
"## Encode (embedding model)\n",
2024-11-02 11:57:22 -07:00
"\n",
2025-08-10 19:49:45 -07:00
"Encode text into embeddings. Note that this API is only available for [embedding models](openai_api_embeddings.ipynb) and will raise an error for generation models.\n",
2024-11-02 19:08:49 -07:00
"Therefore, we launch a new server to server an embedding model."
2024-11-02 11:57:22 -07:00
]
},
{
"cell_type": "code",
"execution_count": null,
2024-11-23 05:04:51 +08:00
"metadata": {},
2024-11-02 11:57:22 -07:00
"outputs": [],
"source": [
2025-02-15 03:57:00 +00:00
"embedding_process, port = launch_server_cmd(\n",
2024-11-02 11:57:22 -07:00
" \"\"\"\n",
2025-04-21 02:38:25 +02:00
"python3 -m sglang.launch_server --model-path Alibaba-NLP/gte-Qwen2-1.5B-instruct \\\n",
2025-09-04 09:52:53 -04:00
" --host 0.0.0.0 --is-embedding --log-level warning\n",
2024-11-02 11:57:22 -07:00
"\"\"\"\n",
")\n",
"\n",
2025-02-15 03:57:00 +00:00
"wait_for_server(f\"http://localhost:{port}\")"
2024-11-02 11:57:22 -07:00
]
},
{
"cell_type": "code",
"execution_count": null,
2024-11-23 05:04:51 +08:00
"metadata": {},
2024-11-02 11:57:22 -07:00
"outputs": [],
"source": [
"# successful encode for embedding model\n",
"\n",
2025-02-15 03:57:00 +00:00
"url = f\"http://localhost:{port}/encode\"\n",
2025-04-21 02:38:25 +02:00
"data = {\"model\": \"Alibaba-NLP/gte-Qwen2-1.5B-instruct\", \"text\": \"Once upon a time\"}\n",
2024-11-02 11:57:22 -07:00
"\n",
"response = requests.post(url, json=data)\n",
"response_json = response.json()\n",
"print_highlight(f\"Text embedding (first 10): {response_json['embedding'][:10]}\")"
]
},
2025-02-15 03:57:00 +00:00
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2025-02-19 19:15:44 +00:00
"terminate_process(embedding_process)"
2025-02-15 03:57:00 +00:00
]
},
2025-06-20 12:53:54 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## v1/rerank (cross encoder rerank model)\n",
"Rerank a list of documents given a query using a cross-encoder model. Note that this API is only available for cross encoder model like [BAAI/bge-reranker-v2-m3](https://huggingface.co/BAAI/bge-reranker-v2-m3) with `attention-backend` `triton` and `torch_native`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reranker_process, port = launch_server_cmd(\n",
" \"\"\"\n",
"python3 -m sglang.launch_server --model-path BAAI/bge-reranker-v2-m3 \\\n",
2025-09-04 09:52:53 -04:00
" --host 0.0.0.0 --disable-radix-cache --chunked-prefill-size -1 --attention-backend triton --is-embedding --log-level warning\n",
2025-06-20 12:53:54 +08:00
"\"\"\"\n",
")\n",
"\n",
"wait_for_server(f\"http://localhost:{port}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# compute rerank scores for query and documents\n",
"\n",
"url = f\"http://localhost:{port}/v1/rerank\"\n",
"data = {\n",
" \"model\": \"BAAI/bge-reranker-v2-m3\",\n",
" \"query\": \"what is panda?\",\n",
" \"documents\": [\n",
" \"hi\",\n",
" \"The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.\",\n",
" ],\n",
"}\n",
"\n",
"response = requests.post(url, json=data)\n",
"response_json = response.json()\n",
"for item in response_json:\n",
" print_highlight(f\"Score: {item['score']:.2f} - Document: '{item['document']}'\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"terminate_process(reranker_process)"
]
},
2024-11-03 22:33:03 -08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2024-11-04 23:53:44 -08:00
"## Classify (reward model)\n",
2024-11-03 22:33:03 -08:00
"\n",
2024-11-04 23:53:44 -08:00
"SGLang Runtime also supports reward models. Here we use a reward model to classify the quality of pairwise generations."
2024-11-03 22:33:03 -08:00
]
},
{
"cell_type": "code",
"execution_count": null,
2024-11-23 05:04:51 +08:00
"metadata": {},
2024-11-03 22:33:03 -08:00
"outputs": [],
"source": [
"# Note that SGLang now treats embedding models and reward models as the same type of models.\n",
"# This will be updated in the future.\n",
"\n",
2025-02-15 03:57:00 +00:00
"reward_process, port = launch_server_cmd(\n",
2024-11-03 22:33:03 -08:00
" \"\"\"\n",
2025-09-04 09:52:53 -04:00
"python3 -m sglang.launch_server --model-path Skywork/Skywork-Reward-Llama-3.1-8B-v0.2 --host 0.0.0.0 --is-embedding --log-level warning\n",
2024-11-03 22:33:03 -08:00
"\"\"\"\n",
")\n",
"\n",
2025-02-15 03:57:00 +00:00
"wait_for_server(f\"http://localhost:{port}\")"
2024-11-03 22:33:03 -08:00
]
},
{
"cell_type": "code",
"execution_count": null,
2024-11-23 05:04:51 +08:00
"metadata": {},
2024-11-03 22:33:03 -08:00
"outputs": [],
"source": [
"from transformers import AutoTokenizer\n",
"\n",
"PROMPT = (\n",
" \"What is the range of the numeric output of a sigmoid node in a neural network?\"\n",
")\n",
"\n",
"RESPONSE1 = \"The output of a sigmoid node is bounded between -1 and 1.\"\n",
"RESPONSE2 = \"The output of a sigmoid node is bounded between 0 and 1.\"\n",
"\n",
"CONVS = [\n",
" [{\"role\": \"user\", \"content\": PROMPT}, {\"role\": \"assistant\", \"content\": RESPONSE1}],\n",
" [{\"role\": \"user\", \"content\": PROMPT}, {\"role\": \"assistant\", \"content\": RESPONSE2}],\n",
"]\n",
"\n",
"tokenizer = AutoTokenizer.from_pretrained(\"Skywork/Skywork-Reward-Llama-3.1-8B-v0.2\")\n",
"prompts = tokenizer.apply_chat_template(CONVS, tokenize=False)\n",
"\n",
2025-02-15 03:57:00 +00:00
"url = f\"http://localhost:{port}/classify\"\n",
2024-11-07 15:42:47 -08:00
"data = {\"model\": \"Skywork/Skywork-Reward-Llama-3.1-8B-v0.2\", \"text\": prompts}\n",
2024-11-03 22:33:03 -08:00
"\n",
"responses = requests.post(url, json=data).json()\n",
"for response in responses:\n",
" print_highlight(f\"reward: {response['embedding'][0]}\")"
]
},
2024-11-02 00:17:30 -07:00
{
"cell_type": "code",
2024-11-23 05:04:51 +08:00
"execution_count": null,
"metadata": {},
2024-11-02 00:17:30 -07:00
"outputs": [],
"source": [
2025-02-19 19:15:44 +00:00
"terminate_process(reward_process)"
2024-11-02 00:17:30 -07:00
]
2025-01-11 23:14:26 +00:00
},
2025-03-24 21:34:19 -07:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Capture expert selection distribution in MoE models\n",
"\n",
2025-04-08 21:09:26 +02:00
"SGLang Runtime supports recording the number of times an expert is selected in a MoE model run for each expert in the model. This is useful when analyzing the throughput of the model and plan for optimization.\n",
"\n",
"*Note: We only print out the first 10 lines of the csv below for better readability. Please adjust accordingly if you want to analyze the results more deeply.*"
2025-03-24 21:34:19 -07:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"expert_record_server_process, port = launch_server_cmd(\n",
2025-09-04 09:52:53 -04:00
" \"python3 -m sglang.launch_server --model-path Qwen/Qwen1.5-MoE-A2.7B --host 0.0.0.0 --expert-distribution-recorder-mode stat --log-level warning\"\n",
2025-03-24 21:34:19 -07:00
")\n",
"\n",
"wait_for_server(f\"http://localhost:{port}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"response = requests.post(f\"http://localhost:{port}/start_expert_distribution_record\")\n",
"print_highlight(response)\n",
"\n",
"url = f\"http://localhost:{port}/generate\"\n",
"data = {\"text\": \"What is the capital of France?\"}\n",
"\n",
"response = requests.post(url, json=data)\n",
"print_highlight(response.json())\n",
"\n",
"response = requests.post(f\"http://localhost:{port}/stop_expert_distribution_record\")\n",
"print_highlight(response)\n",
"\n",
"response = requests.post(f\"http://localhost:{port}/dump_expert_distribution_record\")\n",
2025-05-20 11:07:43 +08:00
"print_highlight(response)"
2025-03-24 21:34:19 -07:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"terminate_process(expert_record_server_process)"
]
2025-10-08 10:08:48 +05:30
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tokenize/Detokenize Example (Round Trip)\n",
"\n",
"This example demonstrates how to use the /tokenize and /detokenize endpoints together. We first tokenize a string, then detokenize the resulting IDs to reconstruct the original text. This workflow is useful when you need to handle tokenization externally but still leverage the server for detokenization."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tokenizer_free_server_process, port = launch_server_cmd(\n",
" \"\"\"\n",
"python3 -m sglang.launch_server --model-path qwen/qwen2.5-0.5b-instruct\n",
"\"\"\"\n",
")\n",
"\n",
"wait_for_server(f\"http://localhost:{port}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"from sglang.utils import print_highlight\n",
"\n",
"base_url = f\"http://localhost:{port}\"\n",
"tokenize_url = f\"{base_url}/tokenize\"\n",
"detokenize_url = f\"{base_url}/detokenize\"\n",
"\n",
"model_name = \"qwen/qwen2.5-0.5b-instruct\"\n",
"input_text = \"SGLang provides efficient tokenization endpoints.\"\n",
"print_highlight(f\"Original Input Text:\\n'{input_text}'\")\n",
"\n",
"# --- tokenize the input text ---\n",
"tokenize_payload = {\n",
" \"model\": model_name,\n",
" \"prompt\": input_text,\n",
" \"add_special_tokens\": False,\n",
"}\n",
"try:\n",
" tokenize_response = requests.post(tokenize_url, json=tokenize_payload)\n",
" tokenize_response.raise_for_status()\n",
" tokenization_result = tokenize_response.json()\n",
" token_ids = tokenization_result.get(\"tokens\")\n",
"\n",
" if not token_ids:\n",
" raise ValueError(\"Tokenization returned empty tokens.\")\n",
"\n",
" print_highlight(f\"\\nTokenized Output (IDs):\\n{token_ids}\")\n",
" print_highlight(f\"Token Count: {tokenization_result.get('count')}\")\n",
" print_highlight(f\"Max Model Length: {tokenization_result.get('max_model_len')}\")\n",
"\n",
" # --- detokenize the obtained token IDs ---\n",
" detokenize_payload = {\n",
" \"model\": model_name,\n",
" \"tokens\": token_ids,\n",
" \"skip_special_tokens\": True,\n",
" }\n",
"\n",
" detokenize_response = requests.post(detokenize_url, json=detokenize_payload)\n",
" detokenize_response.raise_for_status()\n",
" detokenization_result = detokenize_response.json()\n",
" reconstructed_text = detokenization_result.get(\"text\")\n",
"\n",
" print_highlight(f\"\\nDetokenized Output (Text):\\n'{reconstructed_text}'\")\n",
"\n",
" if input_text == reconstructed_text:\n",
" print_highlight(\n",
" \"\\nRound Trip Successful: Original and reconstructed text match.\"\n",
" )\n",
" else:\n",
" print_highlight(\n",
" \"\\nRound Trip Mismatch: Original and reconstructed text differ.\"\n",
" )\n",
"\n",
"except requests.exceptions.RequestException as e:\n",
" print_highlight(f\"\\nHTTP Request Error: {e}\")\n",
"except Exception as e:\n",
" print_highlight(f\"\\nAn error occurred: {e}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"terminate_process(tokenizer_free_server_process)"
]
2024-11-02 00:17:30 -07:00
}
],
"metadata": {
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
2024-11-23 05:04:51 +08:00
"pygments_lexer": "ipython3"
2024-11-02 00:17:30 -07:00
}
},
"nbformat": 4,
2025-10-08 10:08:48 +05:30
"nbformat_minor": 4
2024-11-02 00:17:30 -07:00
}