Skip to content

Commit 638ccd2

Browse files
committed
fix ipynb
1 parent 19e96e5 commit 638ccd2

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

docs/backend/structured_outputs.ipynb

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,12 @@
385385
"import json\n",
386386
"from pydantic import BaseModel, Field\n",
387387
"\n",
388+
"from transformers import AutoTokenizer\n",
389+
"tokenizer = AutoTokenizer.from_pretrained(\"meta-llama/Meta-Llama-3.1-8B-Instruct\")\n",
390+
"\n",
391+
"prompt = lambda x : tokenizer.apply_chat_template(\n",
392+
" x, tokenize=False, add_generation_prompt=True\n",
393+
")\n",
388394
"\n",
389395
"# Define the schema using Pydantic\n",
390396
"class CapitalInfo(BaseModel):\n",
@@ -396,7 +402,7 @@
396402
"response = requests.post(\n",
397403
" f\"http://localhost:{port}/generate\",\n",
398404
" json={\n",
399-
" \"text\": \"Here is the information of the capital of France in the JSON format.\\n\",\n",
405+
" \"text\": prompt(\"Here is the information of the capital of France in the JSON format.\\n\"),\n",
400406
" \"sampling_params\": {\n",
401407
" \"temperature\": 0,\n",
402408
" \"max_new_tokens\": 64,\n",
@@ -441,7 +447,7 @@
441447
"response = requests.post(\n",
442448
" f\"http://localhost:{port}/generate\",\n",
443449
" json={\n",
444-
" \"text\": \"Here is the information of the capital of France in the JSON format.\\n\",\n",
450+
" \"text\": prompt(\"Here is the information of the capital of France in the JSON format.\\n\"),\n",
445451
" \"sampling_params\": {\n",
446452
" \"temperature\": 0,\n",
447453
" \"max_new_tokens\": 64,\n",
@@ -469,7 +475,7 @@
469475
"response = requests.post(\n",
470476
" f\"http://localhost:{port}/generate\",\n",
471477
" json={\n",
472-
" \"text\": \"Give me the information of the capital of France.\",\n",
478+
" \"text\": prompt(\"Give me the information of the capital of France.\"),\n",
473479
" \"sampling_params\": {\n",
474480
" \"max_new_tokens\": 128,\n",
475481
" \"temperature\": 0,\n",
@@ -506,7 +512,7 @@
506512
"response = requests.post(\n",
507513
" f\"http://localhost:{port}/generate\",\n",
508514
" json={\n",
509-
" \"text\": \"Paris is the capital of\",\n",
515+
" \"text\": prompt(\"Paris is the capital of\"),\n",
510516
" \"sampling_params\": {\n",
511517
" \"temperature\": 0,\n",
512518
" \"max_new_tokens\": 64,\n",
@@ -530,16 +536,8 @@
530536
"metadata": {},
531537
"outputs": [],
532538
"source": [
533-
"from transformers import AutoTokenizer\n",
534-
"\n",
535-
"# generate an answer\n",
536-
"tokenizer = AutoTokenizer.from_pretrained(\"meta-llama/Meta-Llama-3.1-8B-Instruct\")\n",
537-
"\n",
538-
"text = tokenizer.apply_chat_template(\n",
539-
" messages, tokenize=False, add_generation_prompt=True\n",
540-
")\n",
541539
"payload = {\n",
542-
" \"text\": text,\n",
540+
" \"text\": prompt(messages),\n",
543541
" \"sampling_params\": {\n",
544542
" \"structural_tag\": json.dumps(\n",
545543
" {\n",

0 commit comments

Comments
 (0)