Skip to content

Commit 944fe12

Browse files
committed
move notebooks to latest openai api version
1 parent 3ae3634 commit 944fe12

10 files changed

+546
-2619
lines changed

1. Generative AI .ipynb

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
"import matplotlib\n",
3333
"import matplotlib.pyplot as plt \n",
3434
"\n",
35-
"import openai\n",
36-
"import tiktoken\n",
35+
"from openai import OpenAI\n",
3736
"\n",
37+
"import tiktoken\n",
3838
"import nltk\n",
3939
"from nltk.corpus import reuters\n",
4040
"from nltk import bigrams, trigrams\n",
@@ -72,27 +72,26 @@
7272
"output_type": "stream",
7373
"text": [
7474
"Python implementation: CPython\n",
75-
"Python version : 3.10.9\n",
76-
"IPython version : 8.10.0\n",
75+
"Python version : 3.11.7\n",
76+
"IPython version : 8.12.3\n",
7777
"\n",
7878
"Compiler : Clang 14.0.6 \n",
7979
"OS : Darwin\n",
80-
"Release : 23.1.0\n",
81-
"Machine : x86_64\n",
82-
"Processor : i386\n",
80+
"Release : 23.4.0\n",
81+
"Machine : arm64\n",
82+
"Processor : arm\n",
8383
"CPU cores : 16\n",
8484
"Architecture: 64bit\n",
8585
"\n",
86-
"Git hash: dd79ad5dfd349dc3b1ae00a307ee636a9ab6b2ef\n",
86+
"Git hash: 529fe8a3ea9769b549bab95c993473f8266299a8\n",
8787
"\n",
88-
"tqdm : 4.66.1\n",
89-
"watermark : 2.4.2\n",
90-
"numpy : 1.23.5\n",
91-
"openai : 0.28.1\n",
92-
"pandas : 1.5.3\n",
93-
"matplotlib: 3.7.2\n",
94-
"nltk : 3.7\n",
95-
"tiktoken : 0.5.1\n",
88+
"matplotlib: 3.8.0\n",
89+
"numpy : 1.26.4\n",
90+
"tqdm : 4.66.2\n",
91+
"watermark : 2.4.3\n",
92+
"pandas : 2.1.4\n",
93+
"nltk : 3.8.1\n",
94+
"tiktoken : 0.6.0\n",
9695
"\n"
9796
]
9897
}
@@ -500,9 +499,27 @@
500499
" return num_tokens\n"
501500
]
502501
},
502+
{
503+
"cell_type": "markdown",
504+
"id": "39d1b4a4-555f-4431-90d9-e365f9b67070",
505+
"metadata": {},
506+
"source": [
507+
"Instatiate the client"
508+
]
509+
},
503510
{
504511
"cell_type": "code",
505512
"execution_count": 14,
513+
"id": "beb9e74e-1c2c-4be5-b57c-f69e8078f0fa",
514+
"metadata": {},
515+
"outputs": [],
516+
"source": [
517+
"client = OpenAI()"
518+
]
519+
},
520+
{
521+
"cell_type": "code",
522+
"execution_count": 15,
506523
"id": "c75f4844",
507524
"metadata": {},
508525
"outputs": [
@@ -578,13 +595,13 @@
578595
" # example token count from the function defined above\n",
579596
" print(f\"{num_tokens_from_messages(example_messages, model)} prompt tokens counted by num_tokens_from_messages().\")\n",
580597
" # example token count from the OpenAI API\n",
581-
" response = openai.ChatCompletion.create(\n",
598+
" response = client.chat.completions.create(\n",
582599
" model=model,\n",
583600
" messages=example_messages,\n",
584601
" temperature=0,\n",
585602
" max_tokens=1, # we're only counting input tokens here, so let's not waste tokens on the output\n",
586603
" )\n",
587-
" print(f'{response[\"usage\"][\"prompt_tokens\"]} prompt tokens counted by the OpenAI API.')\n",
604+
" print(f'{response.usage.prompt_tokens} prompt tokens counted by the OpenAI API.')\n",
588605
" print()\n"
589606
]
590607
},
@@ -598,7 +615,7 @@
598615
},
599616
{
600617
"cell_type": "code",
601-
"execution_count": 15,
618+
"execution_count": 16,
602619
"id": "85acd120",
603620
"metadata": {},
604621
"outputs": [],
@@ -616,14 +633,14 @@
616633
},
617634
{
618635
"cell_type": "code",
619-
"execution_count": 16,
636+
"execution_count": 17,
620637
"id": "b925cf88",
621638
"metadata": {},
622639
"outputs": [
623640
{
624641
"data": {
625642
"application/vnd.jupyter.widget-view+json": {
626-
"model_id": "e59a2da7b3e34e4ca2035b61db84785c",
643+
"model_id": "067e20a935294beabb4af3f0dc50bf3b",
627644
"version_major": 2,
628645
"version_minor": 0
629646
},
@@ -652,7 +669,7 @@
652669
},
653670
{
654671
"cell_type": "code",
655-
"execution_count": 17,
672+
"execution_count": 18,
656673
"id": "3058681d",
657674
"metadata": {},
658675
"outputs": [],
@@ -674,7 +691,7 @@
674691
},
675692
{
676693
"cell_type": "code",
677-
"execution_count": 18,
694+
"execution_count": 19,
678695
"id": "a4ae8a55",
679696
"metadata": {},
680697
"outputs": [
@@ -697,7 +714,7 @@
697714
" 'Rubber': 0.0016806722689075631})"
698715
]
699716
},
700-
"execution_count": 18,
717+
"execution_count": 19,
701718
"metadata": {},
702719
"output_type": "execute_result"
703720
}
@@ -708,7 +725,7 @@
708725
},
709726
{
710727
"cell_type": "code",
711-
"execution_count": 19,
728+
"execution_count": 20,
712729
"id": "d5911761",
713730
"metadata": {},
714731
"outputs": [
@@ -727,7 +744,7 @@
727744
" '.': 0.07142857142857142})"
728745
]
729746
},
730-
"execution_count": 19,
747+
"execution_count": 20,
731748
"metadata": {},
732749
"output_type": "execute_result"
733750
}
@@ -746,7 +763,7 @@
746763
},
747764
{
748765
"cell_type": "code",
749-
"execution_count": 20,
766+
"execution_count": 21,
750767
"id": "544fef3a",
751768
"metadata": {},
752769
"outputs": [],
@@ -795,17 +812,17 @@
795812
},
796813
{
797814
"cell_type": "code",
798-
"execution_count": 21,
815+
"execution_count": 22,
799816
"id": "bbfdd491",
800817
"metadata": {},
801818
"outputs": [
802819
{
803820
"data": {
804821
"text/plain": [
805-
"'United States and the assumption of certain assets by Olson .'"
822+
"'United States if it did not endorse a resolution of capacity in the year in Tokyo , which is not in the system today .'"
806823
]
807824
},
808-
"execution_count": 21,
825+
"execution_count": 22,
809826
"metadata": {},
810827
"output_type": "execute_result"
811828
}
@@ -816,17 +833,17 @@
816833
},
817834
{
818835
"cell_type": "code",
819-
"execution_count": 22,
836+
"execution_count": 23,
820837
"id": "e73054d6",
821838
"metadata": {},
822839
"outputs": [
823840
{
824841
"data": {
825842
"text/plain": [
826-
"'today the company , seeking equilibrium , sends a surge in investments and pave the way for better business in the Middle East and Midwest .'"
843+
"\"today the options at a lower quota next year on March 5 . 93 and 142 . 50 dlrs a share , a government to supply the remaining 30 pct over Pioneer ' s next general elections , due to overhaul work on the St Louis Merchants ' Exchange call session , bond managers said .\""
827844
]
828845
},
829-
"execution_count": 22,
846+
"execution_count": 23,
830847
"metadata": {},
831848
"output_type": "execute_result"
832849
}
@@ -837,17 +854,17 @@
837854
},
838855
{
839856
"cell_type": "code",
840-
"execution_count": 23,
857+
"execution_count": 24,
841858
"id": "15d6b819",
842859
"metadata": {},
843860
"outputs": [
844861
{
845862
"data": {
846863
"text/plain": [
847-
"'financial markets .'"
864+
"'financial markets have become highly optimistic about improved profit margins previously constrained by the Holly board and conditioned on receipt of third country destinations , USDA Secretary Richard Lyng opens talks with American trade negotiators in a statement .'"
848865
]
849866
},
850-
"execution_count": 23,
867+
"execution_count": 24,
851868
"metadata": {},
852869
"output_type": "execute_result"
853870
}
@@ -883,7 +900,7 @@
883900
"name": "python",
884901
"nbconvert_exporter": "python",
885902
"pygments_lexer": "ipython3",
886-
"version": "3.10.9"
903+
"version": "3.11.7"
887904
},
888905
"varInspector": {
889906
"cols": {

0 commit comments

Comments
 (0)