1
+ {
2
+ "cells" : [
3
+ {
4
+ "cell_type" : " markdown" ,
5
+ "metadata" : {
6
+ "id" : " view-in-github" ,
7
+ "colab_type" : " text"
8
+ },
9
+ "source" : [
10
+ " <a href=\" https://colab.research.google.com/github/mshumer/gpt-prompt-engineer/blob/main/Claude_3_5_Sonnet_to_gpt_4o_mini_Conversion.ipynb\" target=\" _parent\" ><img src=\" https://colab.research.google.com/assets/colab-badge.svg\" alt=\" Open In Colab\" /></a>"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type" : " markdown" ,
15
+ "metadata" : {
16
+ "id" : " WljjH8K3s7kG"
17
+ },
18
+ "source" : [
19
+ " # Claude 3.5 Sonnet to gpt-4o-mini - part of the `gpt-prompt-engineer` repo\n " ,
20
+ " \n " ,
21
+ " This notebook gives you the ability to go from Claude 3.5 Sonnet to GPT-4o-mini -- reducing costs massively while keeping quality high.\n " ,
22
+ " \n " ,
23
+ " By Matt Shumer (https://twitter.com/mattshumer_)\n " ,
24
+ " \n " ,
25
+ " Github repo: https://github.com/mshumer/gpt-prompt-engineer"
26
+ ]
27
+ },
28
+ {
29
+ "cell_type" : " code" ,
30
+ "execution_count" : null ,
31
+ "metadata" : {
32
+ "id" : " dQmMZdkG_RA5"
33
+ },
34
+ "outputs" : [],
35
+ "source" : [
36
+ " !pip install openai\n " ,
37
+ " \n " ,
38
+ " OPENAI_API_KEY = \" YOUR API KEY HERE\" # enter your OpenAI API key here\n " ,
39
+ " ANTHROPIC_API_KEY = \" YOUR API KEY HERE\" # enter your Anthropic API key here"
40
+ ]
41
+ },
42
+ {
43
+ "cell_type" : " code" ,
44
+ "execution_count" : null ,
45
+ "metadata" : {
46
+ "id" : " wXeqMQpzzosx"
47
+ },
48
+ "outputs" : [],
49
+ "source" : [
50
+ " import re\n " ,
51
+ " import json\n " ,
52
+ " import requests\n " ,
53
+ " from openai import OpenAI\n " ,
54
+ " \n " ,
55
+ " client = OpenAI(api_key=OPENAI_API_KEY)\n " ,
56
+ " \n " ,
57
+ " def generate_candidate_prompts(task, prompt_example, response_example):\n " ,
58
+ " headers = {\n " ,
59
+ " \" x-api-key\" : ANTHROPIC_API_KEY,\n " ,
60
+ " \" anthropic-version\" : \" 2023-06-01\" ,\n " ,
61
+ " \" content-type\" : \" application/json\"\n " ,
62
+ " }\n " ,
63
+ " \n " ,
64
+ " data = {\n " ,
65
+ " \" model\" : 'claude-3-5-sonnet-20240620',\n " ,
66
+ " \" max_tokens\" : 4000,\n " ,
67
+ " \" temperature\" : .5,\n " ,
68
+ " \" system\" : \"\"\" <task>Given an example training sample, create seven additional samples for the same task that are even better. Each example should contain a <prompt> and a <response>.</task>\n " ,
69
+ " \n " ,
70
+ " <rules>\n " ,
71
+ " 1. Ensure the new examples are diverse and unique from one another.\n " ,
72
+ " 2. They should all be perfect. If you make a mistake, this system won't work.\n " ,
73
+ " </rules>\n " ,
74
+ " \n " ,
75
+ " Respond in this format:\n " ,
76
+ " <response_format>\n " ,
77
+ " <example_one>\n " ,
78
+ " <prompt>\n " ,
79
+ " PUT_PROMPT_HERE\n " ,
80
+ " </prompt>\n " ,
81
+ " <response>\n " ,
82
+ " PUT_RESPONSE_HERE\n " ,
83
+ " </response>\n " ,
84
+ " </example_one>\n " ,
85
+ " \n " ,
86
+ " <example_two>\n " ,
87
+ " <prompt>\n " ,
88
+ " PUT_PROMPT_HERE\n " ,
89
+ " </prompt>\n " ,
90
+ " <response>\n " ,
91
+ " PUT_RESPONSE_HERE\n " ,
92
+ " </response>\n " ,
93
+ " </example_two>\n " ,
94
+ " \n " ,
95
+ " ...\n " ,
96
+ " </response_format>\"\"\" ,\n " ,
97
+ " \" messages\" : [\n " ,
98
+ " {\" role\" : \" user\" , \" content\" : f\"\"\" <training_task>{task}</training_task>\n " ,
99
+ " \n " ,
100
+ " <prompt_example>\n " ,
101
+ " {prompt_example}\n " ,
102
+ " </prompt_example>\n " ,
103
+ " \n " ,
104
+ " <response_example>\n " ,
105
+ " {response_example}\n " ,
106
+ " </response_example>\"\"\" },\n " ,
107
+ " ]\n " ,
108
+ " }\n " ,
109
+ " \n " ,
110
+ " \n " ,
111
+ " response = requests.post(\" https://api.anthropic.com/v1/messages\" , headers=headers, json=data)\n " ,
112
+ " \n " ,
113
+ " response_text = response.json()['content'][0]['text']\n " ,
114
+ " \n " ,
115
+ " # Parse out the prompts and responses\n " ,
116
+ " prompts_and_responses = []\n " ,
117
+ " examples = re.findall(r'<example_\\ w+>(.*?)</example_\\ w+>', response_text, re.DOTALL)\n " ,
118
+ " for example in examples:\n " ,
119
+ " prompt = re.findall(r'<prompt>(.*?)</prompt>', example, re.DOTALL)[0].strip()\n " ,
120
+ " response = re.findall(r'<response>(.*?)</response>', example, re.DOTALL)[0].strip()\n " ,
121
+ " prompts_and_responses.append({'prompt': prompt, 'response': response})\n " ,
122
+ " \n " ,
123
+ " return prompts_and_responses\n " ,
124
+ " \n " ,
125
+ " def generate_system_prompt(task, prompt_examples):\n " ,
126
+ " headers = {\n " ,
127
+ " \" x-api-key\" : ANTHROPIC_API_KEY,\n " ,
128
+ " \" anthropic-version\" : \" 2023-06-01\" ,\n " ,
129
+ " \" content-type\" : \" application/json\"\n " ,
130
+ " }\n " ,
131
+ " \n " ,
132
+ " data = {\n " ,
133
+ " \" model\" : 'claude-3-5-sonnet-20240620',\n " ,
134
+ " \" max_tokens\" : 1000,\n " ,
135
+ " \" temperature\" : .5,\n " ,
136
+ " \" system\" : \"\"\" <your_role>Given a user-description of their <task> a set of prompt / response pairs (it'll be in JSON for easy reading) for the types of outputs we want to generate given inputs, write a fantastic system prompt that describes the task to be done perfectly.</your_role>\n " ,
137
+ " \n " ,
138
+ " <rules>\n " ,
139
+ " 1. Do this perfectly.\n " ,
140
+ " 2. Respond only with the system prompt, and nothing else. No other text will be allowed.\n " ,
141
+ " </rules>\n " ,
142
+ " \n " ,
143
+ " Respond in this format:\n " ,
144
+ " <system_prompt>\n " ,
145
+ " WRITE_SYSTEM_PROMPT_HERE\n " ,
146
+ " </system_prompt>\"\"\" ,\n " ,
147
+ " \" messages\" : [\n " ,
148
+ " {\" role\" : \" user\" , \" content\" : f\"\"\" <task>{task}</task>\n " ,
149
+ " \n " ,
150
+ " <prompt_response_examples>\n " ,
151
+ " {str(prompt_examples)}\n " ,
152
+ " </prompt_response_examples>\"\"\" },\n " ,
153
+ " ]\n " ,
154
+ " }\n " ,
155
+ " \n " ,
156
+ " \n " ,
157
+ " response = requests.post(\" https://api.anthropic.com/v1/messages\" , headers=headers, json=data)\n " ,
158
+ " \n " ,
159
+ " response_text = response.json()['content'][0]['text']\n " ,
160
+ " \n " ,
161
+ " # Parse out the prompt\n " ,
162
+ " system_prompt = response_text.split('<system_prompt>')[1].split('</system_prompt>')[0].strip()\n " ,
163
+ " \n " ,
164
+ " return system_prompt\n " ,
165
+ " \n " ,
166
+ " def test_mini(generated_examples, prompt_example, system_prompt):\n " ,
167
+ " messages = [{\" role\" : \" system\" , \" content\" : system_prompt}]\n " ,
168
+ " \n " ,
169
+ " for example in generated_examples:\n " ,
170
+ " messages.append({\" role\" : \" user\" , \" content\" : example['prompt']})\n " ,
171
+ " messages.append({\" role\" : \" assistant\" , \" content\" : example['response']})\n " ,
172
+ " \n " ,
173
+ " messages.append({\" role\" : \" user\" , \" content\" : prompt_example.strip()})\n " ,
174
+ " \n " ,
175
+ " response = client.chat.completions.create(\n " ,
176
+ " model=\" gpt-4o-mini\" ,\n " ,
177
+ " messages=messages,\n " ,
178
+ " max_tokens=2000,\n " ,
179
+ " temperature=0.5\n " ,
180
+ " )\n " ,
181
+ " \n " ,
182
+ " response_text = response.choices[0].message.content\n " ,
183
+ " \n " ,
184
+ " return response_text\n " ,
185
+ " \n " ,
186
+ " def run_mini_conversion_process(task, prompt_example, response_example):\n " ,
187
+ " print('Generating the prompts / responses...')\n " ,
188
+ " # Generate candidate prompts\n " ,
189
+ " generated_examples = generate_candidate_prompts(task, prompt_example, response_example)\n " ,
190
+ " \n " ,
191
+ " print('Prompts / responses generated. Now generating system prompt...')\n " ,
192
+ " \n " ,
193
+ " # Generate the system prompt\n " ,
194
+ " system_prompt = generate_system_prompt(task, generated_examples)\n " ,
195
+ " \n " ,
196
+ " print('System prompt generated:', system_prompt)\n " ,
197
+ " \n " ,
198
+ " print('\\ n\\ nTesting the new prompt on GPT-4o-mini, using your input example...')\n " ,
199
+ " # Test the generated examples and system prompt with the GPT-4o-mini model\n " ,
200
+ " mini_response = test_mini(generated_examples, prompt_example, system_prompt)\n " ,
201
+ " \n " ,
202
+ " print('GPT-4o-mini responded with:')\n " ,
203
+ " print(mini_response)\n " ,
204
+ " \n " ,
205
+ " print('\\ n\\ n!! CHECK THE FILE DIRECTORY, THE PROMPT IS NOW SAVED THERE !!')\n " ,
206
+ " \n " ,
207
+ " # Create a dictionary with all the relevant information\n " ,
208
+ " result = {\n " ,
209
+ " \" task\" : task,\n " ,
210
+ " \" initial_prompt_example\" : prompt_example,\n " ,
211
+ " \" initial_response_example\" : response_example,\n " ,
212
+ " \" generated_examples\" : generated_examples,\n " ,
213
+ " \" system_prompt\" : system_prompt,\n " ,
214
+ " \" mini_response\" : mini_response\n " ,
215
+ " }\n " ,
216
+ " \n " ,
217
+ " # Save the GPT-4o-mini prompt to a Python file\n " ,
218
+ " with open(\" gpt4o_mini_prompt.py\" , \" w\" ) as file:\n " ,
219
+ " file.write('system_prompt = \"\"\" ' + system_prompt + '\"\"\"\\ n\\ n')\n " ,
220
+ " \n " ,
221
+ " file.write('messages = [\\ n')\n " ,
222
+ " for example in generated_examples:\n " ,
223
+ " file.write(' {\" role\" : \" user\" , \" content\" : \"\"\" ' + example['prompt'] + '\"\"\" },\\ n')\n " ,
224
+ " file.write(' {\" role\" : \" assistant\" , \" content\" : \"\"\" ' + example['response'] + '\"\"\" },\\ n')\n " ,
225
+ " \n " ,
226
+ " file.write(' {\" role\" : \" user\" , \" content\" : \"\"\" ' + prompt_example.strip() + '\"\"\" }\\ n')\n " ,
227
+ " file.write(']\\ n')\n " ,
228
+ " \n " ,
229
+ " return result"
230
+ ]
231
+ },
232
+ {
233
+ "cell_type" : " markdown" ,
234
+ "source" : [
235
+ " ## Fill in your task, prompt_example, and response_example here. Make sure you keep the quality really high here... this is the most important step!"
236
+ ],
237
+ "metadata" : {
238
+ "id" : " ZujTAzhuBMea"
239
+ }
240
+ },
241
+ {
242
+ "cell_type" : " code" ,
243
+ "source" : [
244
+ " task = \" refactoring complex code\"\n " ,
245
+ " \n " ,
246
+ " prompt_example = \"\"\" def calculate_total(prices, tax, discount, shipping_fee, gift_wrap_fee, membership_discount):\n " ,
247
+ " \n " ,
248
+ " total = 0\n " ,
249
+ " \n " ,
250
+ " for i in range(len(prices)):\n " ,
251
+ " \n " ,
252
+ " total += prices[i]\n " ,
253
+ " \n " ,
254
+ " if membership_discount != 0:\n " ,
255
+ " \n " ,
256
+ " total = total - (total * (membership_discount / 100))\n " ,
257
+ " \n " ,
258
+ " if discount != 0:\n " ,
259
+ " \n " ,
260
+ " total = total - (total * (discount / 100))\n " ,
261
+ " \n " ,
262
+ " total = total + (total * (tax / 100))\n " ,
263
+ " \n " ,
264
+ " if total < 50:\n " ,
265
+ " \n " ,
266
+ " total += shipping_fee\n " ,
267
+ " \n " ,
268
+ " else:\n " ,
269
+ " \n " ,
270
+ " total += shipping_fee / 2\n " ,
271
+ " \n " ,
272
+ " if gift_wrap_fee != 0:\n " ,
273
+ " \n " ,
274
+ " total += gift_wrap_fee * len(prices)\n " ,
275
+ " \n " ,
276
+ " if total > 1000:\n " ,
277
+ " \n " ,
278
+ " total -= 50\n " ,
279
+ " \n " ,
280
+ " elif total > 500:\n " ,
281
+ " \n " ,
282
+ " total -= 25\n " ,
283
+ " \n " ,
284
+ " total = round(total, 2)\n " ,
285
+ " \n " ,
286
+ " if total < 0:\n " ,
287
+ " \n " ,
288
+ " total = 0\n " ,
289
+ " \n " ,
290
+ " return total\"\"\"\n " ,
291
+ " \n " ,
292
+ " response_example = \"\"\" def calculate_total(prices, tax_rate, discount_rate, shipping_fee, gift_wrap_fee, membership_discount_rate):\n " ,
293
+ " \n " ,
294
+ " def apply_percentage_discount(amount, percentage):\n " ,
295
+ " \n " ,
296
+ " return amount * (1 - percentage / 100)\n " ,
297
+ " \n " ,
298
+ " def calculate_shipping_fee(total):\n " ,
299
+ " \n " ,
300
+ " return shipping_fee if total < 50 else shipping_fee / 2\n " ,
301
+ " \n " ,
302
+ " def apply_tier_discount(total):\n " ,
303
+ " \n " ,
304
+ " if total > 1000:\n " ,
305
+ " \n " ,
306
+ " return total - 50\n " ,
307
+ " \n " ,
308
+ " elif total > 500:\n " ,
309
+ " \n " ,
310
+ " return total - 25\n " ,
311
+ " \n " ,
312
+ " return total\n " ,
313
+ " \n " ,
314
+ " subtotal = sum(prices)\n " ,
315
+ " \n " ,
316
+ " subtotal = apply_percentage_discount(subtotal, membership_discount_rate)\n " ,
317
+ " \n " ,
318
+ " subtotal = apply_percentage_discount(subtotal, discount_rate)\n " ,
319
+ " \n " ,
320
+ " \n " ,
321
+ " \n " ,
322
+ " total = subtotal * (1 + tax_rate / 100)\n " ,
323
+ " \n " ,
324
+ " total += calculate_shipping_fee(total)\n " ,
325
+ " \n " ,
326
+ " total += gift_wrap_fee * len(prices)\n " ,
327
+ " \n " ,
328
+ " \n " ,
329
+ " \n " ,
330
+ " total = apply_tier_discount(total)\n " ,
331
+ " \n " ,
332
+ " total = max(0, round(total, 2))\n " ,
333
+ " \n " ,
334
+ " \n " ,
335
+ " \n " ,
336
+ " return total\"\"\" "
337
+ ],
338
+ "metadata" : {
339
+ "id" : " XSZqqOoQ-5_E"
340
+ },
341
+ "execution_count" : null ,
342
+ "outputs" : []
343
+ },
344
+ {
345
+ "cell_type" : " markdown" ,
346
+ "source" : [
347
+ " ### Now, let's run this system and get our new prompt! At the end, you'll see a new file pop up in the directory that contains everything you'll need to reduce your costs while keeping quality high w/ gpt-4o-mini!"
348
+ ],
349
+ "metadata" : {
350
+ "id" : " cMO3cJzWA-O0"
351
+ }
352
+ },
353
+ {
354
+ "cell_type" : " code" ,
355
+ "source" : [
356
+ " result = run_mini_conversion_process(task, prompt_example, response_example)"
357
+ ],
358
+ "metadata" : {
359
+ "id" : " O-Bn0rupAJqb"
360
+ },
361
+ "execution_count" : null ,
362
+ "outputs" : []
363
+ }
364
+ ],
365
+ "metadata" : {
366
+ "colab" : {
367
+ "provenance" : [],
368
+ "include_colab_link" : true
369
+ },
370
+ "kernelspec" : {
371
+ "display_name" : " Python 3" ,
372
+ "name" : " python3"
373
+ },
374
+ "language_info" : {
375
+ "codemirror_mode" : {
376
+ "name" : " ipython" ,
377
+ "version" : 3
378
+ },
379
+ "file_extension" : " .py" ,
380
+ "mimetype" : " text/x-python" ,
381
+ "name" : " python" ,
382
+ "nbconvert_exporter" : " python" ,
383
+ "pygments_lexer" : " ipython3" ,
384
+ "version" : " 3.8.8"
385
+ }
386
+ },
387
+ "nbformat" : 4 ,
388
+ "nbformat_minor" : 0
389
+ }
0 commit comments