Skip to content

Commit 73cbaf7

Browse files
Hervé IshimweHervé Ishimwe
authored andcommitted
Updated embedding model (text-embedding-3-small) and openai API access
updated deprecated model name replaced ivvfalt with streamingdiskann final fixes Updated embedding (text-embedding-3-small) and LLM model (gpt-4o) & openai API access
1 parent 84a156e commit 73cbaf7

File tree

4 files changed

+197
-187
lines changed

4 files changed

+197
-187
lines changed

openai_pgvector_helloworld/blog_data_and_embeddings.csv

Lines changed: 129 additions & 129 deletions
Large diffs are not rendered by default.

openai_pgvector_helloworld/blog_data_and_embeddings.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

openai_pgvector_helloworld/openai_pgvector_helloworld.ipynb

Lines changed: 66 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,21 @@
3737
"- Signup for an OpenAI Developer Account and create an API Key. See [OpenAI's developer platform](https://platform.openai.com/overview).\n",
3838
"- Install Python\n",
3939
"- Install and configure a python virtual environment. We recommend [Pyenv](https://github.com/pyenv/pyenv)\n",
40-
"- Install the requirements for this notebook using the following command:\n",
41-
"\n",
42-
"```\n",
43-
"pip install -r requirements.txt\n",
44-
"```"
40+
"- Install the requirements for this notebook using the following command:"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": 188,
46+
"metadata": {},
47+
"outputs": [],
48+
"source": [
49+
"%pip install -r requirements.txt"
4550
]
4651
},
4752
{
4853
"cell_type": "code",
49-
"execution_count": null,
54+
"execution_count": 189,
5055
"metadata": {},
5156
"outputs": [],
5257
"source": [
@@ -66,7 +71,7 @@
6671
},
6772
{
6873
"cell_type": "code",
69-
"execution_count": null,
74+
"execution_count": 190,
7075
"metadata": {},
7176
"outputs": [],
7277
"source": [
@@ -89,7 +94,7 @@
8994
},
9095
{
9196
"cell_type": "code",
92-
"execution_count": null,
97+
"execution_count": 191,
9398
"metadata": {},
9499
"outputs": [],
95100
"source": [
@@ -112,7 +117,7 @@
112117
},
113118
{
114119
"cell_type": "code",
115-
"execution_count": null,
120+
"execution_count": 192,
116121
"metadata": {},
117122
"outputs": [],
118123
"source": [
@@ -137,7 +142,7 @@
137142
"# Assumes we're using the text-embedding-ada-002 model\n",
138143
"# See https://openai.com/pricing\n",
139144
"def get_embedding_cost(num_tokens):\n",
140-
" return num_tokens/1000*0.0001\n",
145+
" return num_tokens/1000*0.00002\n",
141146
"\n",
142147
"# Helper function: calculate total cost of embedding all content in the dataframe\n",
143148
"def get_total_embeddings_cost():\n",
@@ -147,21 +152,12 @@
147152
" token_len = num_tokens_from_string(text)\n",
148153
" total_tokens = total_tokens + token_len\n",
149154
" total_cost = get_embedding_cost(total_tokens)\n",
150-
" return total_cost\n",
151-
"\n",
152-
"# Helper function: get embeddings for a text\n",
153-
"def get_embeddings(text):\n",
154-
" response = openai.Embedding.create(\n",
155-
" model=\"text-embedding-ada-002\",\n",
156-
" input = text.replace(\"\\n\",\" \")\n",
157-
" )\n",
158-
" embedding = response['data'][0]['embedding']\n",
159-
" return embedding"
155+
" return total_cost"
160156
]
161157
},
162158
{
163159
"cell_type": "code",
164-
"execution_count": null,
160+
"execution_count": 193,
165161
"metadata": {},
166162
"outputs": [],
167163
"source": [
@@ -189,14 +185,14 @@
189185
},
190186
{
191187
"cell_type": "code",
192-
"execution_count": null,
188+
"execution_count": 194,
193189
"metadata": {},
194190
"outputs": [],
195191
"source": [
196192
"###############################################################################\n",
197193
"# Create new list with small content chunks to not hit max token limits\n",
198194
"# Note: the maximum number of tokens for a single request is 8191\n",
199-
"# https://openai.com/docs/api-reference/requests\n",
195+
"# https://platform.openai.com/docs/guides/embeddings/embedding-models\n",
200196
"###############################################################################\n",
201197
"# list for chunked content and embeddings\n",
202198
"new_list = []\n",
@@ -241,7 +237,24 @@
241237
},
242238
{
243239
"cell_type": "code",
244-
"execution_count": null,
240+
"execution_count": 195,
241+
"metadata": {},
242+
"outputs": [],
243+
"source": [
244+
"openai_client = openai.OpenAI()\n",
245+
"\n",
246+
"# Helper function: get embeddings for a text\n",
247+
"def get_embeddings(text):\n",
248+
" response = openai_client.embeddings.create(\n",
249+
" model=\"text-embedding-3-small\",\n",
250+
" input = text.replace(\"\\n\",\" \")\n",
251+
" )\n",
252+
" return response.data[0].embedding"
253+
]
254+
},
255+
{
256+
"cell_type": "code",
257+
"execution_count": 196,
245258
"metadata": {},
246259
"outputs": [],
247260
"source": [
@@ -258,14 +271,14 @@
258271
},
259272
{
260273
"cell_type": "code",
261-
"execution_count": null,
274+
"execution_count": 197,
262275
"metadata": {},
263276
"outputs": [],
264277
"source": [
265278
"# Save the dataframe with embeddings as a CSV file\n",
266279
"df_new.to_csv('blog_data_and_embeddings.csv', index=False)\n",
267280
"# It may also be useful to save as a json file, but we won't use this in the tutorial\n",
268-
"#df_new.to_json('blog_data_and_embeddings.json')"
281+
"#df_new.to_json('blog_data_and_embeddings.json') "
269282
]
270283
},
271284
{
@@ -291,7 +304,7 @@
291304
},
292305
{
293306
"cell_type": "code",
294-
"execution_count": null,
307+
"execution_count": 198,
295308
"metadata": {},
296309
"outputs": [],
297310
"source": [
@@ -304,7 +317,7 @@
304317
},
305318
{
306319
"cell_type": "code",
307-
"execution_count": null,
320+
"execution_count": 199,
308321
"metadata": {},
309322
"outputs": [],
310323
"source": [
@@ -313,7 +326,11 @@
313326
"cur = conn.cursor()\n",
314327
"\n",
315328
"#install pgvector \n",
316-
"cur.execute(\"CREATE EXTENSION IF NOT EXISTS vector\");\n",
329+
"cur.execute(\"CREATE EXTENSION IF NOT EXISTS vector;\")\n",
330+
"conn.commit()\n",
331+
"\n",
332+
"#install pgvectorscale \n",
333+
"cur.execute(\"CREATE EXTENSION IF NOT EXISTS vectorscale CASCADE;\")\n",
317334
"conn.commit()\n",
318335
"\n",
319336
"# Register the vector type with psycopg2\n",
@@ -346,7 +363,7 @@
346363
},
347364
{
348365
"cell_type": "code",
349-
"execution_count": null,
366+
"execution_count": 200,
350367
"metadata": {},
351368
"outputs": [],
352369
"source": [
@@ -381,7 +398,7 @@
381398
},
382399
{
383400
"cell_type": "code",
384-
"execution_count": null,
401+
"execution_count": 201,
385402
"metadata": {},
386403
"outputs": [],
387404
"source": [
@@ -391,7 +408,7 @@
391408
},
392409
{
393410
"cell_type": "code",
394-
"execution_count": null,
411+
"execution_count": 202,
395412
"metadata": {},
396413
"outputs": [],
397414
"source": [
@@ -409,7 +426,7 @@
409426
},
410427
{
411428
"cell_type": "code",
412-
"execution_count": null,
429+
"execution_count": 203,
413430
"metadata": {},
414431
"outputs": [],
415432
"source": [
@@ -433,7 +450,7 @@
433450
},
434451
{
435452
"cell_type": "code",
436-
"execution_count": null,
453+
"execution_count": 204,
437454
"metadata": {},
438455
"outputs": [],
439456
"source": [
@@ -445,7 +462,7 @@
445462
},
446463
{
447464
"cell_type": "code",
448-
"execution_count": null,
465+
"execution_count": 205,
449466
"metadata": {},
450467
"outputs": [],
451468
"source": [
@@ -465,24 +482,17 @@
465482
},
466483
{
467484
"cell_type": "code",
468-
"execution_count": null,
485+
"execution_count": 206,
469486
"metadata": {},
470487
"outputs": [],
471488
"source": [
472489
"# Create an index on the data for faster retrieval\n",
473490
"# this isn't really needed for 129 vectors, but it shows the usage for larger datasets\n",
474491
"# Note: always create this type of index after you have data already inserted into the DB\n",
475492
"\n",
476-
"#calculate the index parameters according to best practices\n",
477-
"num_lists = num_records / 1000\n",
478-
"if num_lists < 10:\n",
479-
" num_lists = 10\n",
480-
"if num_records > 1000000:\n",
481-
" num_lists = math.sqrt(num_records)\n",
482-
"\n",
483-
"#use the cosine distance measure, which is what we'll later use for querying\n",
484-
"cur.execute(f'CREATE INDEX ON embeddings USING ivfflat (embedding vector_cosine_ops) WITH (lists = {num_lists});')\n",
485-
"conn.commit() "
493+
"# for different tuning suggestions check this: https://github.com/timescale/pgvectorscale?tab=readme-ov-file#tuning\n",
494+
"cur.execute('CREATE INDEX embedding_idx ON embeddings USING diskann (embedding);')\n",
495+
"conn.commit()"
486496
]
487497
},
488498
{
@@ -499,26 +509,26 @@
499509
},
500510
{
501511
"cell_type": "code",
502-
"execution_count": null,
512+
"execution_count": 207,
503513
"metadata": {},
504514
"outputs": [],
505515
"source": [
506516
"# Helper function: get text completion from OpenAI API\n",
507517
"# Note max tokens is 4097\n",
508518
"# Note we're using the latest gpt-3.5-turbo-0613 model\n",
509-
"def get_completion_from_messages(messages, model=\"gpt-3.5-turbo-0613\", temperature=0, max_tokens=1000):\n",
510-
" response = openai.ChatCompletion.create(\n",
519+
"def get_completion_from_messages(messages, model=\"gpt-4o\", temperature=0, max_tokens=1000):\n",
520+
" response = openai_client.chat.completions.create(\n",
511521
" model=model,\n",
512522
" messages=messages,\n",
513523
" temperature=temperature, \n",
514524
" max_tokens=max_tokens, \n",
515525
" )\n",
516-
" return response.choices[0].message[\"content\"]"
526+
" return response.choices[0].message.content"
517527
]
518528
},
519529
{
520530
"cell_type": "code",
521-
"execution_count": null,
531+
"execution_count": 208,
522532
"metadata": {},
523533
"outputs": [],
524534
"source": [
@@ -547,7 +557,7 @@
547557
},
548558
{
549559
"cell_type": "code",
550-
"execution_count": null,
560+
"execution_count": 209,
551561
"metadata": {},
552562
"outputs": [],
553563
"source": [
@@ -557,7 +567,7 @@
557567
},
558568
{
559569
"cell_type": "code",
560-
"execution_count": null,
570+
"execution_count": 210,
561571
"metadata": {},
562572
"outputs": [],
563573
"source": [
@@ -590,7 +600,7 @@
590600
},
591601
{
592602
"cell_type": "code",
593-
"execution_count": null,
603+
"execution_count": 211,
594604
"metadata": {},
595605
"outputs": [],
596606
"source": [
@@ -601,7 +611,7 @@
601611
},
602612
{
603613
"cell_type": "code",
604-
"execution_count": null,
614+
"execution_count": 212,
605615
"metadata": {},
606616
"outputs": [],
607617
"source": [
@@ -629,7 +639,7 @@
629639
"name": "python",
630640
"nbconvert_exporter": "python",
631641
"pygments_lexer": "ipython3",
632-
"version": "3.8.16"
642+
"version": "3.9.6"
633643
}
634644
},
635645
"nbformat": 4,

openai_pgvector_helloworld/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ openai
22
pandas
33
numpy
44
tiktoken
5-
psycopg2
5+
psycopg2-binary
66
pgvector
77
python-dotenv

0 commit comments

Comments
 (0)