pxovela commited on
Commit
7c69cac
1 Parent(s): 5912aaa

Upload prompt_dumber.ipynb

Browse files
Files changed (1) hide show
  1. prompt_dumber.ipynb +134 -0
prompt_dumber.ipynb ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "import os\n",
10
+ "import pandas as pd\n",
11
+ "\n",
12
+ "df = pd.read_csv('selected_prompts.csv')"
13
+ ]
14
+ },
15
+ {
16
+ "cell_type": "code",
17
+ "execution_count": 2,
18
+ "metadata": {},
19
+ "outputs": [],
20
+ "source": [
21
+ "from openai import ChatCompletion,Completion, api_key\n",
22
+ "import openai\n",
23
+ "import time"
24
+ ]
25
+ },
26
+ {
27
+ "cell_type": "code",
28
+ "execution_count": 3,
29
+ "metadata": {},
30
+ "outputs": [],
31
+ "source": [
32
+ "openai.api_key = ''"
33
+ ]
34
+ },
35
+ {
36
+ "cell_type": "code",
37
+ "execution_count": 4,
38
+ "metadata": {},
39
+ "outputs": [],
40
+ "source": [
41
+ "prompt_sys = \"I'll send you a prompt that's too wordy. You will summarize it in a short for 5-7 words, capture subject, composition, verb first and maybe add a few adjectives. Capture intent precisely. Example output from you: A girl in paris, modern art. Reply only with the new short prompt\""
42
+ ]
43
+ },
44
+ {
45
+ "cell_type": "code",
46
+ "execution_count": 5,
47
+ "metadata": {},
48
+ "outputs": [],
49
+ "source": [
50
+ "first_1000 = df.iloc[:1000]\n",
51
+ "second_1000 = df.iloc[1000:2000]\n",
52
+ "last_1000 = df.iloc[2000:3000]"
53
+ ]
54
+ },
55
+ {
56
+ "cell_type": "code",
57
+ "execution_count": null,
58
+ "metadata": {},
59
+ "outputs": [],
60
+ "source": [
61
+ "import os\n",
62
+ "import time\n",
63
+ "from openai import ChatCompletion\n",
64
+ "from requests.exceptions import ReadTimeout, Timeout\n",
65
+ "\n",
66
+ "# Create a subfolder for batch outputs\n",
67
+ "os.makedirs('batch_outputs', exist_ok=True)\n",
68
+ "\n",
69
+ "batch_size = 20 # Define your desired batch size\n",
70
+ "num_batches = len(last_1000) // batch_size + (1 if len(last_1000) % batch_size != 0 else 0)\n",
71
+ "\n",
72
+ "for i in range(num_batches):\n",
73
+ " print(f\"Starting Batch {i+1}...\")\n",
74
+ " start_idx = i * batch_size\n",
75
+ " end_idx = start_idx + batch_size\n",
76
+ " batch = last_1000.iloc[start_idx:end_idx].copy()\n",
77
+ " \n",
78
+ " short_versions = []\n",
79
+ "\n",
80
+ " for idx, prompt in enumerate(batch['prompt']):\n",
81
+ " success = False\n",
82
+ " retries = 3\n",
83
+ " while not success and retries > 0:\n",
84
+ " start_time = time.time()\n",
85
+ " try:\n",
86
+ " c = ChatCompletion.create(\n",
87
+ " model=\"gpt-3.5-turbo\",\n",
88
+ " messages=[{\"role\": \"system\", \"content\": prompt_sys},\n",
89
+ " {\"role\": \"user\", \"content\": prompt}],\n",
90
+ " timeout=20)\n",
91
+ " short_versions.append(c['choices'][0]['message']['content'])\n",
92
+ " success = True\n",
93
+ " end_time = time.time()\n",
94
+ " elapsed_time = end_time - start_time\n",
95
+ " print(f\"Processed Batch {i+1}, Prompt {idx+1}. Took {elapsed_time:.2f} seconds.\")\n",
96
+ " except (ReadTimeout, Timeout) as e:\n",
97
+ " print(f\"Timeout error for Batch {i+1}, Prompt {idx+1}: {str(e)}. Retrying...\")\n",
98
+ " except Exception as e: # Catch all other exceptions\n",
99
+ " print(f\"Unhandled error for Batch {i+1}, Prompt {idx+1}: {str(e)}. Retrying...\")\n",
100
+ " retries -= 1\n",
101
+ " time.sleep(10) # Waiting for 10 seconds before retrying\n",
102
+ "\n",
103
+ " \n",
104
+ " # Introduce a small delay between prompts\n",
105
+ " time.sleep(1)\n",
106
+ " \n",
107
+ " batch['short_version'] = short_versions\n",
108
+ " batch.to_csv(f'batch_outputs/batch_{i+1}_output.csv', index=False)\n",
109
+ " print(f\"Saved Batch {i+1} to CSV.\")"
110
+ ]
111
+ }
112
+ ],
113
+ "metadata": {
114
+ "kernelspec": {
115
+ "display_name": "fastai",
116
+ "language": "python",
117
+ "name": "python3"
118
+ },
119
+ "language_info": {
120
+ "codemirror_mode": {
121
+ "name": "ipython",
122
+ "version": 3
123
+ },
124
+ "file_extension": ".py",
125
+ "mimetype": "text/x-python",
126
+ "name": "python",
127
+ "nbconvert_exporter": "python",
128
+ "pygments_lexer": "ipython3",
129
+ "version": "3.10.10"
130
+ }
131
+ },
132
+ "nbformat": 4,
133
+ "nbformat_minor": 2
134
+ }