|
22 | 22 | "\n",
|
23 | 23 | "| Encoding name | OpenAI models |\n",
|
24 | 24 | "|-------------------------|-----------------------------------------------------|\n",
|
| 25 | + "| `o200k_base` | `gpt-4o`, `gpt-4o-mini` |\n", |
25 | 26 | "| `cl100k_base` | `gpt-4`, `gpt-3.5-turbo`, `text-embedding-ada-002`, `text-embedding-3-small`, `text-embedding-3-large` |\n",
|
26 | 27 | "| `p50k_base` | Codex models, `text-davinci-002`, `text-davinci-003`|\n",
|
27 | 28 | "| `r50k_base` (or `gpt2`) | GPT-3 models like `davinci` |\n",
|
28 | 29 | "\n",
|
29 | 30 | "You can retrieve the encoding for a model using `tiktoken.encoding_for_model()` as follows:\n",
|
30 | 31 | "```python\n",
|
31 |
| - "encoding = tiktoken.encoding_for_model('gpt-3.5-turbo')\n", |
| 32 | + "encoding = tiktoken.encoding_for_model('gpt-4o-mini')\n", |
32 | 33 | "```\n",
|
33 | 34 | "\n",
|
34 | 35 | "Note that `p50k_base` overlaps substantially with `r50k_base`, and for non-code applications, they will usually give the same tokens.\n",
|
|
71 | 72 | },
|
72 | 73 | {
|
73 | 74 | "cell_type": "code",
|
74 |
| - "execution_count": null, |
| 75 | + "execution_count": 1, |
75 | 76 | "metadata": {},
|
76 |
| - "outputs": [], |
| 77 | + "outputs": [ |
| 78 | + { |
| 79 | + "name": "stdout", |
| 80 | + "output_type": "stream", |
| 81 | + "text": [ |
| 82 | + "\n", |
| 83 | + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.0\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.2\u001b[0m\n", |
| 84 | + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython3 -m pip install --upgrade pip\u001b[0m\n", |
| 85 | + "Note: you may need to restart the kernel to use updated packages.\n", |
| 86 | + "\n", |
| 87 | + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.0\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.2\u001b[0m\n", |
| 88 | + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython3 -m pip install --upgrade pip\u001b[0m\n", |
| 89 | + "Note: you may need to restart the kernel to use updated packages.\n" |
| 90 | + ] |
| 91 | + } |
| 92 | + ], |
77 | 93 | "source": [
|
78 |
| - "%pip install --upgrade tiktoken\n", |
79 |
| - "%pip install --upgrade openai" |
| 94 | + "%pip install --upgrade tiktoken -q\n", |
| 95 | + "%pip install --upgrade openai -q" |
80 | 96 | ]
|
81 | 97 | },
|
82 | 98 | {
|
|
89 | 105 | },
|
90 | 106 | {
|
91 | 107 | "cell_type": "code",
|
92 |
| - "execution_count": 1, |
| 108 | + "execution_count": 2, |
93 | 109 | "metadata": {},
|
94 | 110 | "outputs": [],
|
95 | 111 | "source": [
|
|
131 | 147 | "metadata": {},
|
132 | 148 | "outputs": [],
|
133 | 149 | "source": [
|
134 |
| - "encoding = tiktoken.encoding_for_model(\"gpt-3.5-turbo\")" |
| 150 | + "encoding = tiktoken.encoding_for_model(\"gpt-4o-mini\")" |
135 | 151 | ]
|
136 | 152 | },
|
137 | 153 | {
|
|
159 | 175 | {
|
160 | 176 | "data": {
|
161 | 177 | "text/plain": [
|
162 |
| - "[83, 1609, 5963, 374, 2294, 0]" |
| 178 | + "[83, 8251, 2488, 382, 2212, 0]" |
163 | 179 | ]
|
164 | 180 | },
|
165 | 181 | "execution_count": 5,
|
|
236 | 252 | {
|
237 | 253 | "data": {
|
238 | 254 | "text/plain": [
|
239 |
| - "'tiktoken is great!'" |
| 255 | + "'turesറلás!'" |
240 | 256 | ]
|
241 | 257 | },
|
242 | 258 | "execution_count": 8,
|
|
272 | 288 | {
|
273 | 289 | "data": {
|
274 | 290 | "text/plain": [
|
275 |
| - "[b't', b'ik', b'token', b' is', b' great', b'!']" |
| 291 | + "[b't', b'ures', b'\\xe0\\xb4\\xb1', b'\\xd9\\x84', b'\\xc3\\xa1s', b'!']" |
276 | 292 | ]
|
277 | 293 | },
|
278 | 294 | "execution_count": 9,
|
|
313 | 329 | " # print the example string\n",
|
314 | 330 | " print(f'\\nExample string: \"{example_string}\"')\n",
|
315 | 331 | " # for each encoding, print the # of tokens, the token integers, and the token bytes\n",
|
316 |
| - " for encoding_name in [\"r50k_base\", \"p50k_base\", \"cl100k_base\"]:\n", |
| 332 | + " for encoding_name in [\"r50k_base\", \"p50k_base\", \"cl100k_base\", \"o200k_base\"]:\n", |
317 | 333 | " encoding = tiktoken.get_encoding(encoding_name)\n",
|
318 | 334 | " token_integers = encoding.encode(example_string)\n",
|
319 | 335 | " num_tokens = len(token_integers)\n",
|
|
347 | 363 | "\n",
|
348 | 364 | "cl100k_base: 6 tokens\n",
|
349 | 365 | "token integers: [519, 85342, 34500, 479, 8997, 2191]\n",
|
350 |
| - "token bytes: [b'ant', b'idis', b'establish', b'ment', b'arian', b'ism']\n" |
| 366 | + "token bytes: [b'ant', b'idis', b'establish', b'ment', b'arian', b'ism']\n", |
| 367 | + "\n", |
| 368 | + "o200k_base: 6 tokens\n", |
| 369 | + "token integers: [493, 129901, 376, 160388, 21203, 2367]\n", |
| 370 | + "token bytes: [b'ant', b'idis', b'est', b'ablishment', b'arian', b'ism']\n" |
351 | 371 | ]
|
352 | 372 | }
|
353 | 373 | ],
|
|
377 | 397 | "\n",
|
378 | 398 | "cl100k_base: 7 tokens\n",
|
379 | 399 | "token integers: [17, 489, 220, 17, 284, 220, 19]\n",
|
| 400 | + "token bytes: [b'2', b' +', b' ', b'2', b' =', b' ', b'4']\n", |
| 401 | + "\n", |
| 402 | + "o200k_base: 7 tokens\n", |
| 403 | + "token integers: [17, 659, 220, 17, 314, 220, 19]\n", |
380 | 404 | "token bytes: [b'2', b' +', b' ', b'2', b' =', b' ', b'4']\n"
|
381 | 405 | ]
|
382 | 406 | }
|
|
407 | 431 | "\n",
|
408 | 432 | "cl100k_base: 9 tokens\n",
|
409 | 433 | "token integers: [33334, 45918, 243, 21990, 9080, 33334, 62004, 16556, 78699]\n",
|
410 |
| - "token bytes: [b'\\xe3\\x81\\x8a', b'\\xe8\\xaa', b'\\x95', b'\\xe7\\x94\\x9f', b'\\xe6\\x97\\xa5', b'\\xe3\\x81\\x8a', b'\\xe3\\x82\\x81', b'\\xe3\\x81\\xa7', b'\\xe3\\x81\\xa8\\xe3\\x81\\x86']\n" |
| 434 | + "token bytes: [b'\\xe3\\x81\\x8a', b'\\xe8\\xaa', b'\\x95', b'\\xe7\\x94\\x9f', b'\\xe6\\x97\\xa5', b'\\xe3\\x81\\x8a', b'\\xe3\\x82\\x81', b'\\xe3\\x81\\xa7', b'\\xe3\\x81\\xa8\\xe3\\x81\\x86']\n", |
| 435 | + "\n", |
| 436 | + "o200k_base: 8 tokens\n", |
| 437 | + "token integers: [8930, 9697, 243, 128225, 8930, 17693, 4344, 48669]\n", |
| 438 | + "token bytes: [b'\\xe3\\x81\\x8a', b'\\xe8\\xaa', b'\\x95', b'\\xe7\\x94\\x9f\\xe6\\x97\\xa5', b'\\xe3\\x81\\x8a', b'\\xe3\\x82\\x81', b'\\xe3\\x81\\xa7', b'\\xe3\\x81\\xa8\\xe3\\x81\\x86']\n" |
411 | 439 | ]
|
412 | 440 | }
|
413 | 441 | ],
|
|
433 | 461 | },
|
434 | 462 | {
|
435 | 463 | "cell_type": "code",
|
436 |
| - "execution_count": 2, |
| 464 | + "execution_count": 14, |
437 | 465 | "metadata": {},
|
438 | 466 | "outputs": [],
|
439 | 467 | "source": [
|
440 |
| - "def num_tokens_from_messages(messages, model=\"gpt-3.5-turbo-0613\"):\n", |
| 468 | + "def num_tokens_from_messages(messages, model=\"gpt-4o-mini-2024-07-18\"):\n", |
441 | 469 | " \"\"\"Return the number of tokens used by a list of messages.\"\"\"\n",
|
442 | 470 | " try:\n",
|
443 | 471 | " encoding = tiktoken.encoding_for_model(model)\n",
|
444 | 472 | " except KeyError:\n",
|
445 | 473 | " print(\"Warning: model not found. Using cl100k_base encoding.\")\n",
|
446 | 474 | " encoding = tiktoken.get_encoding(\"cl100k_base\")\n",
|
447 | 475 | " if model in {\n",
|
448 |
| - " \"gpt-3.5-turbo-0613\",\n", |
449 |
| - " \"gpt-3.5-turbo-16k-0613\",\n", |
| 476 | + " \"gpt-3.5-turbo-0125\",\n", |
450 | 477 | " \"gpt-4-0314\",\n",
|
451 | 478 | " \"gpt-4-32k-0314\",\n",
|
452 | 479 | " \"gpt-4-0613\",\n",
|
453 | 480 | " \"gpt-4-32k-0613\",\n",
|
| 481 | + " \"gpt-4o-mini-2024-07-18\",\n", |
| 482 | + " \"gpt-4o-2024-08-06\"\n", |
454 | 483 | " }:\n",
|
455 | 484 | " tokens_per_message = 3\n",
|
456 | 485 | " tokens_per_name = 1\n",
|
457 |
| - " elif model == \"gpt-3.5-turbo-0301\":\n", |
458 |
| - " tokens_per_message = 4 # every message follows <|start|>{role/name}\\n{content}<|end|>\\n\n", |
459 |
| - " tokens_per_name = -1 # if there's a name, the role is omitted\n", |
460 | 486 | " elif \"gpt-3.5-turbo\" in model:\n",
|
461 |
| - " print(\"Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0613.\")\n", |
462 |
| - " return num_tokens_from_messages(messages, model=\"gpt-3.5-turbo-0613\")\n", |
| 487 | + " print(\"Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0125.\")\n", |
| 488 | + " return num_tokens_from_messages(messages, model=\"gpt-3.5-turbo-0125\")\n", |
463 | 489 | " elif \"gpt-4\" in model:\n",
|
464 | 490 | " print(\"Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-0613.\")\n",
|
465 | 491 | " return num_tokens_from_messages(messages, model=\"gpt-4-0613\")\n",
|
| 492 | + " elif \"gpt-4o\" in model:\n", |
| 493 | + " print(\"Warning: gpt-4o and gpt-4o-mini may update over time. Returning num tokens assuming gpt-4o-2024-08-06.\")\n", |
| 494 | + " return num_tokens_from_messages(messages, model=\"ggpt-4o-2024-08-06\")\n", |
| 495 | + " elif \"gpt-4o-mini\" in model:\n", |
| 496 | + " print(\"Warning: gpt-4o-mini may update over time. Returning num tokens assuming gpt-4o-mini-2024-07-18.\")\n", |
| 497 | + " return num_tokens_from_messages(messages, model=\"gpt-4o-mini-2024-07-18\")\n", |
466 | 498 | " else:\n",
|
467 | 499 | " raise NotImplementedError(\n",
|
468 | 500 | " f\"\"\"num_tokens_from_messages() is not implemented for model {model}.\"\"\"\n",
|
|
480 | 512 | },
|
481 | 513 | {
|
482 | 514 | "cell_type": "code",
|
483 |
| - "execution_count": 4, |
| 515 | + "execution_count": 15, |
484 | 516 | "metadata": {},
|
485 | 517 | "outputs": [
|
486 | 518 | {
|
487 | 519 | "name": "stdout",
|
488 | 520 | "output_type": "stream",
|
489 | 521 | "text": [
|
490 |
| - "gpt-3.5-turbo-0301\n", |
491 |
| - "127 prompt tokens counted by num_tokens_from_messages().\n", |
492 |
| - "127 prompt tokens counted by the OpenAI API.\n", |
493 |
| - "\n", |
494 |
| - "gpt-3.5-turbo-0613\n", |
| 522 | + "gpt-3.5-turbo\n", |
| 523 | + "Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0125.\n", |
495 | 524 | "129 prompt tokens counted by num_tokens_from_messages().\n",
|
496 | 525 | "129 prompt tokens counted by the OpenAI API.\n",
|
497 | 526 | "\n",
|
498 |
| - "gpt-3.5-turbo\n", |
499 |
| - "Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0613.\n", |
| 527 | + "gpt-4-0613\n", |
500 | 528 | "129 prompt tokens counted by num_tokens_from_messages().\n",
|
501 | 529 | "129 prompt tokens counted by the OpenAI API.\n",
|
502 | 530 | "\n",
|
503 |
| - "gpt-4-0314\n", |
| 531 | + "gpt-4\n", |
| 532 | + "Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-0613.\n", |
504 | 533 | "129 prompt tokens counted by num_tokens_from_messages().\n",
|
505 | 534 | "129 prompt tokens counted by the OpenAI API.\n",
|
506 | 535 | "\n",
|
507 |
| - "gpt-4-0613\n", |
| 536 | + "gpt-4o\n", |
| 537 | + "Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-0613.\n", |
508 | 538 | "129 prompt tokens counted by num_tokens_from_messages().\n",
|
509 |
| - "129 prompt tokens counted by the OpenAI API.\n", |
| 539 | + "124 prompt tokens counted by the OpenAI API.\n", |
510 | 540 | "\n",
|
511 |
| - "gpt-4\n", |
| 541 | + "gpt-4o-mini\n", |
512 | 542 | "Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-0613.\n",
|
513 | 543 | "129 prompt tokens counted by num_tokens_from_messages().\n",
|
514 |
| - "129 prompt tokens counted by the OpenAI API.\n", |
| 544 | + "124 prompt tokens counted by the OpenAI API.\n", |
515 | 545 | "\n"
|
516 | 546 | ]
|
517 | 547 | }
|
|
556 | 586 | "]\n",
|
557 | 587 | "\n",
|
558 | 588 | "for model in [\n",
|
559 |
| - " \"gpt-3.5-turbo-0301\",\n", |
560 |
| - " \"gpt-3.5-turbo-0613\",\n", |
561 | 589 | " \"gpt-3.5-turbo\",\n",
|
562 |
| - " \"gpt-4-0314\",\n", |
563 | 590 | " \"gpt-4-0613\",\n",
|
564 | 591 | " \"gpt-4\",\n",
|
| 592 | + " \"gpt-4o\",\n", |
| 593 | + " \"gpt-4o-mini\"\n", |
565 | 594 | " ]:\n",
|
566 | 595 | " print(model)\n",
|
567 | 596 | " # example token count from the function defined above\n",
|
|
574 | 603 | " print(f'{response.usage.prompt_tokens} prompt tokens counted by the OpenAI API.')\n",
|
575 | 604 | " print()\n"
|
576 | 605 | ]
|
577 |
| - }, |
578 |
| - { |
579 |
| - "cell_type": "code", |
580 |
| - "execution_count": null, |
581 |
| - "metadata": {}, |
582 |
| - "outputs": [], |
583 |
| - "source": [] |
584 | 606 | }
|
585 | 607 | ],
|
586 | 608 | "metadata": {
|
|
599 | 621 | "name": "python",
|
600 | 622 | "nbconvert_exporter": "python",
|
601 | 623 | "pygments_lexer": "ipython3",
|
602 |
| - "version": "3.11.5" |
| 624 | + "version": "3.12.1" |
603 | 625 | },
|
604 | 626 | "vscode": {
|
605 | 627 | "interpreter": {
|
|
0 commit comments