Skip to content

Commit 0960345

Browse files
committed
Migrate cookbook examples to Responses API
1 parent 05fe562 commit 0960345

22 files changed

Lines changed: 177 additions & 182 deletions

examples/Clustering.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,15 @@
230230
" {\"role\": \"user\", \"content\": f'What do the following customer reviews have in common?\\n\\nCustomer reviews:\\n\"\"\"\\n{reviews}\\n\"\"\"\\n\\nTheme:'}\n",
231231
" ]\n",
232232
"\n",
233-
" response = client.chat.completions.create(\n",
233+
" response = client.responses.create(\n",
234234
" model=\"gpt-4\",\n",
235-
" messages=messages,\n",
235+
" input=messages,\n",
236236
" temperature=0,\n",
237-
" max_tokens=64,\n",
237+
" max_output_tokens=64,\n",
238238
" top_p=1,\n",
239239
" frequency_penalty=0,\n",
240240
" presence_penalty=0)\n",
241-
" print(response.choices[0].message.content.replace(\"\\n\", \"\"))\n",
241+
" print(response.output_text.replace(\"\\n\", \"\"))\n",
242242
"\n",
243243
" sample_cluster_rows = df[df.Cluster == i].sample(rev_per_cluster, random_state=42)\n",
244244
" for j in range(rev_per_cluster):\n",

examples/Clustering_for_transaction_classification.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"from ast import literal_eval\n",
6060
"\n",
6161
"client = OpenAI(api_key=os.environ.get(\"OPENAI_API_KEY\", \"<your OpenAI API key if not set as env var>\"))\n",
62-
"COMPLETIONS_MODEL = \"gpt-3.5-turbo\"\n",
62+
"COMPLETIONS_MODEL = \"gpt-4o-mini\"\n",
6363
"\n",
6464
"# This path leads to a file with data and precomputed embeddings\n",
6565
"embedding_path = \"data/library_transactions_with_embeddings_359.csv\"\n"
@@ -398,21 +398,21 @@
398398
" .sample(transactions_per_cluster, random_state=42)\n",
399399
" .values\n",
400400
" )\n",
401-
" response = client.chat.completions.create(\n",
401+
" response = client.responses.create(\n",
402402
" model=COMPLETIONS_MODEL,\n",
403403
" # We'll include a prompt to instruct the model what sort of description we're looking for\n",
404-
" messages=[\n",
404+
" input=[\n",
405405
" {\"role\": \"user\",\n",
406406
" \"content\": f'''We want to group these transactions into meaningful clusters so we can target the areas we are spending the most money. \n",
407407
" What do the following transactions have in common?\\n\\nTransactions:\\n\"\"\"\\n{transactions}\\n\"\"\"\\n\\nTheme:'''}\n",
408408
" ],\n",
409409
" temperature=0,\n",
410-
" max_tokens=100,\n",
410+
" max_output_tokens=100,\n",
411411
" top_p=1,\n",
412412
" frequency_penalty=0,\n",
413413
" presence_penalty=0,\n",
414414
" )\n",
415-
" print(response.choices[0].message.content.replace(\"\\n\", \"\"))\n",
415+
" print(response.output_text.replace(\"\\n\", \"\"))\n",
416416
" print(\"\\n\")\n",
417417
"\n",
418418
" sample_cluster_rows = embedding_df[embedding_df.Cluster == i].sample(transactions_per_cluster, random_state=42)\n",

examples/Entity_extraction_for_long_documents.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,16 @@
136136
" {\"role\": \"user\", \"content\": prompt}\n",
137137
" ]\n",
138138
"\n",
139-
" response = client.chat.completions.create(\n",
139+
" response = client.responses.create(\n",
140140
" model='gpt-4', \n",
141-
" messages=messages,\n",
141+
" input=messages,\n",
142142
" temperature=0,\n",
143-
" max_tokens=1500,\n",
143+
" max_output_tokens=1500,\n",
144144
" top_p=1,\n",
145145
" frequency_penalty=0,\n",
146146
" presence_penalty=0\n",
147147
" )\n",
148-
" return \"1.\" + response.choices[0].message.content"
148+
" return \"1.\" + response.output_text"
149149
]
150150
},
151151
{

examples/Question_answering_using_a_search_API.ipynb

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,22 @@
7373
"# Load environment variables\n",
7474
"news_api_key = os.getenv(\"NEWS_API_KEY\")\n",
7575
"\n",
76-
"GPT_MODEL = \"gpt-3.5-turbo\"\n",
76+
"GPT_MODEL = \"gpt-4o-mini\"\n",
7777
"\n",
7878
"\n",
7979
"# Helper functions\n",
8080
"def json_gpt(input: str):\n",
81-
" completion = client.chat.completions.create(model=GPT_MODEL,\n",
82-
" messages=[\n",
83-
" {\"role\": \"system\", \"content\": \"Output only valid JSON\"},\n",
84-
" {\"role\": \"user\", \"content\": input},\n",
85-
" ],\n",
86-
" temperature=0.5)\n",
87-
"\n",
88-
" text = completion.choices[0].message.content\n",
81+
" completion = client.responses.create(\n",
82+
" model=GPT_MODEL,\n",
83+
" input=[\n",
84+
" {\"role\": \"system\", \"content\": \"Output only valid JSON\"},\n",
85+
" {\"role\": \"user\", \"content\": input},\n",
86+
" ],\n",
87+
" temperature=0.5,\n",
88+
" text={\"format\": {\"type\": \"json_object\"}},\n",
89+
" )\n",
90+
"\n",
91+
" text = completion.output_text\n",
8992
" parsed = json.loads(text)\n",
9093
"\n",
9194
" return parsed\n",
@@ -489,18 +492,19 @@
489492
"Include as much information as possible in the answer. Reference the relevant search result urls as markdown links.\n",
490493
"\"\"\"\n",
491494
"\n",
492-
"completion = client.chat.completions.create(\n",
495+
"completion = client.responses.create(\n",
493496
" model=GPT_MODEL,\n",
494-
" messages=[{\"role\": \"user\", \"content\": ANSWER_INPUT}],\n",
497+
" input=[{\"role\": \"user\", \"content\": ANSWER_INPUT}],\n",
495498
" temperature=0.5,\n",
496499
" stream=True,\n",
497500
")\n",
498501
"\n",
499502
"text = \"\"\n",
500-
"for chunk in completion:\n",
501-
" text += chunk.choices[0].delta.content\n",
502-
" display.clear_output(wait=True)\n",
503-
" display.display(display.Markdown(text))"
503+
"for event in completion:\n",
504+
" if event.type == \"response.output_text.delta\":\n",
505+
" text += event.delta\n",
506+
" display.clear_output(wait=True)\n",
507+
" display.display(display.Markdown(text))"
504508
]
505509
}
506510
],

examples/Question_answering_using_embeddings.ipynb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,16 @@
197197
"# an example question about the 2022 Olympics\n",
198198
"query = 'Which athletes won the most number of gold medals in 2024 Summer Olympics?'\n",
199199
"\n",
200-
"response = client.chat.completions.create(\n",
201-
" messages=[\n",
200+
"response = client.responses.create(\n",
201+
" input=[\n",
202202
" {'role': 'system', 'content': 'You answer questions about the 2024 Games or latest events.'},\n",
203203
" {'role': 'user', 'content': query},\n",
204204
" ],\n",
205205
" model=GPT_MODELS[0],\n",
206206
" temperature=0,\n",
207207
")\n",
208208
"\n",
209-
"print(response.choices[0].message.content)"
209+
"print(response.output_text)"
210210
]
211211
},
212212
{
@@ -236,16 +236,16 @@
236236
"# an example question about the 2024 Elections\n",
237237
"query = 'Who won the elections in the US in 2024?'\n",
238238
"\n",
239-
"response = client.chat.completions.create(\n",
240-
" messages=[\n",
239+
"response = client.responses.create(\n",
240+
" input=[\n",
241241
" {'role': 'system', 'content': 'You answer questions about the 2024 Games or latest events.'},\n",
242242
" {'role': 'user', 'content': query},\n",
243243
" ],\n",
244244
" model=GPT_MODELS[1],\n",
245245
" temperature=0,\n",
246246
")\n",
247247
"\n",
248-
"print(response.choices[0].message.content)"
248+
"print(response.output_text)"
249249
]
250250
},
251251
{
@@ -385,16 +385,16 @@
385385
"\n",
386386
"Question: Which countries won the maximum number of gold, silver and bronze medals respectively at 2024 Summer Olympics? List the countries in the order of gold, silver and bronze medals.\"\"\"\n",
387387
"\n",
388-
"response = client.chat.completions.create(\n",
389-
" messages=[\n",
388+
"response = client.responses.create(\n",
389+
" input=[\n",
390390
" {'role': 'system', 'content': 'You answer questions about the recent events.'},\n",
391391
" {'role': 'user', 'content': query},\n",
392392
" ],\n",
393393
" model=GPT_MODELS[0],\n",
394394
" temperature=0,\n",
395395
")\n",
396396
"\n",
397-
"print(response.choices[0].message.content)"
397+
"print(response.output_text)"
398398
]
399399
},
400400
{
@@ -790,12 +790,12 @@
790790
" {\"role\": \"system\", \"content\": \"You answer questions about the 2022 Winter Olympics.\"},\n",
791791
" {\"role\": \"user\", \"content\": message},\n",
792792
" ]\n",
793-
" response = client.chat.completions.create(\n",
793+
" response = client.responses.create(\n",
794794
" model=model,\n",
795-
" messages=messages,\n",
795+
" input=messages,\n",
796796
" temperature=0\n",
797797
" )\n",
798-
" response_message = response.choices[0].message.content\n",
798+
" response_message = response.output_text\n",
799799
" return response_message\n",
800800
"\n"
801801
]

examples/SDG1.ipynb

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,14 @@
136136
"Make sure that the numbers make sense (i.e. more rooms is usually bigger size, more expensive locations increase price. more size is usually higher price etc. make sure all the numbers make sense). Also only respond with the CSV.\n",
137137
"\"\"\"\n",
138138
"\n",
139-
"response = client.chat.completions.create(\n",
139+
"response = client.responses.create(\n",
140140
" model=datagen_model,\n",
141-
" messages=[\n",
141+
" input=[\n",
142142
" {\"role\": \"system\", \"content\": \"You are a helpful assistant designed to generate synthetic data.\"},\n",
143143
" {\"role\": \"user\", \"content\": question}\n",
144144
" ]\n",
145145
")\n",
146-
"res = response.choices[0].message.content\n",
146+
"res = response.output_text\n",
147147
"print(res)"
148148
]
149149
},
@@ -248,14 +248,14 @@
248248
"Make sure that the numbers make sense (i.e. more rooms is usually bigger size, more expensive locations increase price. more size is usually higher price etc. make sure all the numbers make sense).\n",
249249
"\"\"\"\n",
250250
"\n",
251-
"response = client.chat.completions.create(\n",
251+
"response = client.responses.create(\n",
252252
" model=datagen_model,\n",
253-
" messages=[\n",
253+
" input=[\n",
254254
" {\"role\": \"system\", \"content\": \"You are a helpful assistant designed to generate synthetic data.\"},\n",
255255
" {\"role\": \"user\", \"content\": question}\n",
256256
" ]\n",
257257
")\n",
258-
"res = response.choices[0].message.content\n",
258+
"res = response.output_text\n",
259259
"print(res)"
260260
]
261261
},
@@ -419,14 +419,14 @@
419419
"You can use the previously generated dataframe to generate the next dataframe.\n",
420420
"\"\"\"\n",
421421
"\n",
422-
"response = client.chat.completions.create(\n",
422+
"response = client.responses.create(\n",
423423
" model=datagen_model,\n",
424-
" messages=[\n",
424+
" input=[\n",
425425
" {\"role\": \"system\", \"content\": \"You are a helpful assistant designed to generate synthetic data.\"},\n",
426426
" {\"role\": \"user\", \"content\": question}\n",
427427
" ]\n",
428428
")\n",
429-
"res = response.choices[0].message.content\n",
429+
"res = response.output_text\n",
430430
"print(res)"
431431
]
432432
},
@@ -499,14 +499,14 @@
499499
" Create as many training pairs as possible.\n",
500500
" \"\"\"\n",
501501
"\n",
502-
" response = client.chat.completions.create(\n",
502+
" response = client.responses.create(\n",
503503
" model=datagen_model,\n",
504-
" messages=[\n",
504+
" input=[\n",
505505
" {\"role\": \"system\", \"content\": \"You are a helpful assistant designed to generate synthetic data.\"},\n",
506506
" {\"role\": \"user\", \"content\": question}\n",
507507
" ]\n",
508508
" )\n",
509-
" res = response.choices[0].message.content\n",
509+
" res = response.output_text\n",
510510
" output_string += res + \"\\n\" + \"\\n\"\n",
511511
"print(output_string[:1000]) #displaying truncated response\n"
512512
]
@@ -682,14 +682,14 @@
682682
" Output: \"Experience unparalleled comfort. These shoes feature a blend of modern style and the traditional superior cushioning, perfect for those always on the move.\"\n",
683683
" \"\"\"\n",
684684
"\n",
685-
" response = client.chat.completions.create(\n",
685+
" response = client.responses.create(\n",
686686
" model=\"gpt-4o-mini\",\n",
687-
" messages=[\n",
687+
" input=[\n",
688688
" {\"role\": \"system\", \"content\": \"You are a helpful assistant designed to generate synthetic data.\"},\n",
689689
" {\"role\": \"user\", \"content\": question}\n",
690690
" ]\n",
691691
" )\n",
692-
" res = response.choices[0].message.content\n",
692+
" res = response.output_text\n",
693693
" output_string += res + \"\\n\" + \"\\n\"\n",
694694
"print(output_string[:1000]) #displaying truncated response"
695695
]
@@ -1777,14 +1777,14 @@
17771777
" Do not add any extra characters around that formatting as it will make the output parsing break.\n",
17781778
" \"\"\"\n",
17791779
"\n",
1780-
"response = client.chat.completions.create(\n",
1780+
"response = client.responses.create(\n",
17811781
" model=datagen_model,\n",
1782-
" messages=[\n",
1782+
" input=[\n",
17831783
" {\"role\": \"system\", \"content\": \"You are a helpful assistant designed analyze clustered data\"},\n",
17841784
" {\"role\": \"user\", \"content\": topic_prompt}\n",
17851785
" ]\n",
17861786
")\n",
1787-
"res = response.choices[0].message.content\n",
1787+
"res = response.output_text\n",
17881788
"\n",
17891789
"pattern = r\"Cluster: (\\d+), topic: ([^\\n]+)\"\n",
17901790
"matches = re.findall(pattern, res)\n",
@@ -1880,14 +1880,14 @@
18801880
" Do not add any extra characters around that formatting as it will make the output parsing break. It is very important you stick to that output format\n",
18811881
" \"\"\"\n",
18821882
"\n",
1883-
"response = client.chat.completions.create(\n",
1883+
"response = client.responses.create(\n",
18841884
" model=datagen_model,\n",
1885-
" messages=[\n",
1885+
" input=[\n",
18861886
" {\"role\": \"system\", \"content\": \"You are a helpful assistant designed to analyze clustered data\"},\n",
18871887
" {\"role\": \"user\", \"content\": topic_prompt}\n",
18881888
" ]\n",
18891889
")\n",
1890-
"res = response.choices[0].message.content\n",
1890+
"res = response.output_text\n",
18911891
"print(res)\n"
18921892
]
18931893
},
@@ -2096,14 +2096,14 @@
20962096
" Output: \"Experience unparalleled comfort. These shoes feature a blend of modern style and the traditional superior cushioning, perfect for those always on the move.\"\n",
20972097
" \"\"\"\n",
20982098
"\n",
2099-
" response = client.chat.completions.create(\n",
2099+
" response = client.responses.create(\n",
21002100
" model=\"gpt-4o-mini\",\n",
2101-
" messages=[\n",
2101+
" input=[\n",
21022102
" {\"role\": \"system\", \"content\": \"You are a helpful assistant designed to generate synthetic data.\"},\n",
21032103
" {\"role\": \"user\", \"content\": question}\n",
21042104
" ]\n",
21052105
" )\n",
2106-
" res = response.choices[0].message.content\n",
2106+
" res = response.output_text\n",
21072107
" output_string += res + \"\\n\" + \"\\n\"\n",
21082108
"print(output_string)"
21092109
]

examples/Summarizing_long_documents.ipynb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@
100100
"source": [
101101
"client = OpenAI(api_key=os.getenv(\"OPENAI_API_KEY\"))\n",
102102
"\n",
103-
"def get_chat_completion(messages, model='gpt-4-turbo'):\n",
104-
" response = client.chat.completions.create(\n",
103+
"def get_response(messages, model='gpt-4o-mini'):\n",
104+
" response = client.responses.create(\n",
105105
" model=model,\n",
106-
" messages=messages,\n",
106+
" input=messages,\n",
107107
" temperature=0,\n",
108108
" )\n",
109-
" return response.choices[0].message.content"
109+
" return response.output_text"
110110
]
111111
},
112112
{
@@ -226,7 +226,7 @@
226226
" - text (str): The text to be summarized.\n",
227227
" - detail (float, optional): A value between 0 and 1 indicating the desired level of detail in the summary.\n",
228228
" 0 leads to a higher level summary, and 1 results in a more detailed summary. Defaults to 0.\n",
229-
" - model (str, optional): The model to use for generating summaries. Defaults to 'gpt-3.5-turbo'.\n",
229+
" - model (str, optional): The model to use for generating summaries. Defaults to 'gpt-4o-mini'.\n",
230230
" - additional_instructions (Optional[str], optional): Additional instructions to provide to the model for customizing summaries.\n",
231231
" - minimum_chunk_size (Optional[int], optional): The minimum size for text chunks. Defaults to 500.\n",
232232
" - chunk_delimiter (str, optional): The delimiter used to split the text into chunks. Defaults to \".\".\n",
@@ -279,7 +279,7 @@
279279
" ]\n",
280280
"\n",
281281
" # Assuming this function gets the completion and works as expected\n",
282-
" response = get_chat_completion(messages, model=model)\n",
282+
" response = get_response(messages, model=model)\n",
283283
" accumulated_summaries.append(response)\n",
284284
"\n",
285285
" # Compile final summary from partial summaries\n",

0 commit comments

Comments
 (0)