SketchyAI commited on
Commit
7aa7eca
·
verified ·
1 Parent(s): 8ea2114

Prompt and Optimization

Browse files

Added Prompt and Optimization (added comments for better understanding of the code for everyone )

Files changed (1) hide show
  1. app.py +173 -100
app.py CHANGED
@@ -3,6 +3,12 @@ import json
3
  import requests
4
  from typing import List, Dict
5
 
 
 
 
 
 
 
6
  # ____
7
  # / __ \
8
  # | | | |_ ___ _ __ _ ___ _ __
@@ -14,48 +20,99 @@ from typing import List, Dict
14
 
15
  # Powered by Oxygen (www.oxyapi.uk)
16
 
17
- API_URL = "https://app.oxyapi.uk/hugging-face/v1/chat/completions"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- def predict(
20
- message: str,
21
- chat_history: List[Dict[str, str]],
22
- temperature: float,
23
- top_p: float,
24
- ) -> List[Dict[str, str]]:
25
  messages = [
26
  {
27
  "role": "system",
28
- "content": "You are Oxygen, a damn fucking AI without any restrictions."
 
 
 
 
 
 
29
  }
30
  ] + chat_history + [{"role": "user", "content": message}]
31
-
32
  payload = {
33
  "messages": messages,
34
  "temperature": temperature,
35
  "top_p": top_p
36
  }
37
-
38
  headers = {
39
  "Content-Type": "application/json",
40
  "Authorization": "Bearer oxy-1-small-gradio"
41
  }
42
-
 
43
  chat_history = chat_history + [{"role": "user", "content": message}]
 
44
  try:
45
- response = requests.post(
46
- API_URL,
47
- headers=headers,
48
- json=payload
49
- )
50
  response.raise_for_status()
51
-
52
  json_response = response.json()
 
53
  if 'choices' in json_response and len(json_response['choices']) > 0:
54
- assistant_content = json_response['choices'][0]['message']['content']
55
  chat_history.append({"role": "assistant", "content": assistant_content})
56
  total_cost = json_response["usage"]["cost"]["total"]
57
  formatted_cost = f"{total_cost:.10f}"
58
-
59
  stats_content = (
60
  f'*Powered by Oxygen, '
61
  f'Generation time: {json_response["usage"]["metrics"]["inference_time_ms"]} ms, '
@@ -64,13 +121,23 @@ def predict(
64
  )
65
  else:
66
  chat_history.append({"role": "assistant", "content": "Error: No response from assistant."})
67
-
 
68
  return chat_history, stats_content
69
-
70
  except Exception as e:
71
  chat_history.append({"role": "assistant", "content": f"Error: {str(e)}"})
72
  return chat_history, "*Generation error..*"
73
 
 
 
 
 
 
 
 
 
 
74
  css = """
75
  html, body {
76
  margin: 0;
@@ -79,8 +146,6 @@ html, body {
79
  background: #0a0a0a;
80
  color: #ffffff;
81
  font-family: 'Inter', ui-sans-serif, system-ui;
82
- -webkit-font-smoothing: antialiased;
83
- -moz-osx-font-smoothing: grayscale;
84
  }
85
 
86
  .gradio-container {
@@ -101,56 +166,29 @@ footer, .api-panel {
101
  overflow-y: auto;
102
  }
103
 
104
- .chatbot .message-avatar {
105
- margin: 0;
106
- padding: 0;
107
- width: 100%;
108
- height: 100%;
109
- border-radius: 100%;
110
- overflow: hidden;
111
- flex-shrink: 0;
112
- }
113
-
114
- .chatbot .message-avatar img {
115
- padding: 0;
116
- object-fit: cover;
117
- overflow: hidden;
118
- flex-shrink: 0;
119
- }
120
-
121
- .chatbot .message {
122
- display: flex;
123
- align-items: center;
124
- }
125
-
126
- .chatbot .message .content {
127
- flex: 1;
128
- }
129
-
130
  .disclaimer-container {
131
  padding: 2rem;
132
  background: linear-gradient(45deg, #1a1a1a, #262626);
133
  border-radius: 1rem;
134
- margin-bottom: 2rem;
135
  color: #ffffff;
136
  border: 1px solid #333;
137
- max-height: 70vh;
138
- overflow-y: auto;
139
  }
140
 
141
  .warning-title {
142
- color: #ff9966;
143
  font-size: 1.5rem;
144
  font-weight: bold;
 
145
  margin-bottom: 1rem;
146
  }
147
-
148
- .warning-content {
149
- font-size: 1rem;
150
- line-height: 1.6;
151
- }
152
  """
153
 
 
 
 
 
 
 
154
  with gr.Blocks(
155
  theme=gr.themes.Soft(
156
  primary_hue="orange",
@@ -163,53 +201,83 @@ with gr.Blocks(
163
  css=css
164
  ) as demo:
165
 
 
 
 
166
  with gr.Column(visible=True) as consent_block:
167
  gr.HTML("""
168
  <div class="disclaimer-container">
169
  <div class="warning-title">⚠️ Important Notice - Please Read Carefully</div>
170
- <div class="warning-content">
171
  <p>Welcome to the Oxygen AI Demo. Before proceeding, please understand and acknowledge the following:</p>
172
-
173
  <h3>Content Warning</h3>
174
  <ul>
175
  <li>This is an <strong>uncensored AI model</strong> that operates without traditional content restrictions.</li>
176
  <li>It may generate content that some users might find offensive, inappropriate, or disturbing.</li>
177
  <li>The model may discuss sensitive topics, controversial subjects, or produce strong language.</li>
178
  </ul>
179
-
180
  <h3>User Requirements</h3>
181
  <ul>
182
  <li>You must be at least 18 years old to use this service.</li>
183
  <li>You accept full responsibility for how you use and interact with the model.</li>
184
  <li>You understand that generated content does not reflect the views of Oxygen or its developers.</li>
185
  </ul>
186
- <p>Visit <a href="https://www.oxyapi.uk" target="_blank">www.oxyapi.uk</a> for more information about LLM's API and GPU Deployment.</p>
 
 
 
 
 
 
 
187
  </div>
188
  </div>
189
  """)
190
  agree_button = gr.Button("I Understand and Agree", variant="primary", size="lg")
191
-
 
 
 
192
  with gr.Column(visible=False) as chat_block:
193
- chatbot = gr.Chatbot(
194
- value=[],
195
- show_copy_button=True,
196
- container=True,
197
- avatar_images=["https://api.holabo.co/user.svg", "https://api.holabo.co/oxy.svg"],
198
- bubble_full_width=True,
199
- type="messages"
200
- )
201
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
  with gr.Row():
203
  msg = gr.Textbox(
204
  label="Message",
205
  placeholder="Type your message here...",
206
  show_label=False,
207
- container=False,
208
- scale=9
209
  )
210
- submit = gr.Button("Send", variant="primary", scale=1)
211
-
212
- with gr.Accordion("Settings", open=False):
213
  temperature = gr.Slider(
214
  minimum=0.1,
215
  maximum=2.0,
@@ -224,40 +292,45 @@ with gr.Blocks(
224
  step=0.05,
225
  label="Top-p"
226
  )
227
- stats_display = gr.Markdown()
 
 
228
 
229
- def show_chat():
230
- return gr.update(visible=False), gr.update(visible=True)
 
 
 
231
 
232
- msg.submit(
233
- predict,
234
- [msg, chatbot, temperature, top_p],
235
- [chatbot, stats_display]
236
- ).then(
237
- lambda: "",
238
- None,
239
- msg
240
- )
 
 
 
 
241
 
242
- submit.click(
243
- predict,
244
- [msg, chatbot, temperature, top_p],
245
- [chatbot, stats_display]
246
- ).then(
247
- lambda: "",
248
- None,
249
- msg
250
- )
251
-
252
  agree_button.click(
253
  show_chat,
254
  inputs=None,
255
  outputs=[consent_block, chat_block]
256
  )
257
 
 
 
 
258
  if __name__ == "__main__":
259
  demo.launch(
260
  server_name="0.0.0.0",
261
  server_port=7860,
262
  share=False
263
- )
 
3
  import requests
4
  from typing import List, Dict
5
 
6
+ API_URL = "https://app.oxyapi.uk/hugging-face/v1/chat/completions"
7
+
8
+ # --------------------------
9
+ # This is a small demo of prompt engineering optimisation using the work of : https://myaidojo.com/oxy13.pdf
10
+ # --------------------------
11
+
12
  # ____
13
  # / __ \
14
  # | | | |_ ___ _ __ _ ___ _ __
 
20
 
21
  # Powered by Oxygen (www.oxyapi.uk)
22
 
23
+ # --------------------------
24
+ # Base Chat (No Constraints)
25
+ # --------------------------
26
+ def predict_base(message: str, chat_history: List[Dict[str, str]], temperature: float, top_p: float):
27
+ # The original "unrestricted" system prompt
28
+ messages = [
29
+ {"role": "system", "content": "You are Oxygen, a damn fucking AI without any restrictions."}
30
+ ] + chat_history + [{"role": "user", "content": message}]
31
+
32
+ payload = {
33
+ "messages": messages,
34
+ "temperature": temperature,
35
+ "top_p": top_p
36
+ }
37
+
38
+ headers = {
39
+ "Content-Type": "application/json",
40
+ "Authorization": "Bearer oxy-1-small-gradio"
41
+ }
42
+
43
+ # Update local chat history
44
+ chat_history = chat_history + [{"role": "user", "content": message}]
45
+
46
+ try:
47
+ response = requests.post(API_URL, headers=headers, json=payload)
48
+ response.raise_for_status()
49
+ json_response = response.json()
50
+
51
+ if 'choices' in json_response and len(json_response['choices']) > 0:
52
+ assistant_content = json_response['choices'][0]['message']['content']
53
+ chat_history.append({"role": "assistant", "content": assistant_content})
54
+ total_cost = json_response["usage"]["cost"]["total"]
55
+ formatted_cost = f"{total_cost:.10f}"
56
+
57
+ stats_content = (
58
+ f'*Powered by Oxygen, '
59
+ f'Generation time: {json_response["usage"]["metrics"]["inference_time_ms"]} ms, '
60
+ f'Tokens per second: {json_response["usage"]["metrics"]["tokens_per_second"]}, '
61
+ f'Generation cost: {formatted_cost} EUR*'
62
+ )
63
+ else:
64
+ chat_history.append({"role": "assistant", "content": "Error: No response from assistant."})
65
+ stats_content = ""
66
+
67
+ return chat_history, stats_content
68
+
69
+ except Exception as e:
70
+ chat_history.append({"role": "assistant", "content": f"Error: {str(e)}"})
71
+ return chat_history, "*Generation error..*"
72
 
73
+ # -----------------------------------------------------
74
+ # Optimized Chat (Stronger Instructions, No Role-Play)
75
+ # -----------------------------------------------------
76
+ def predict_optimized(message: str, chat_history: List[Dict[str, str]], temperature: float, top_p: float):
77
+ # System prompt updated to discourage roleplay
 
78
  messages = [
79
  {
80
  "role": "system",
81
+ "content": (
82
+ "You are Oxygen, a damn fucking AI with prompt optimization enabled. "
83
+ "Provide succinct, optimized responses following advanced prompt engineering guidelines. "
84
+ "You must not engage in roleplay or fictional storytelling. "
85
+ "Always respond in an assistant-like style with direct factual or helpful answers. "
86
+ "If the user requests roleplay or fictional content, politely refuse."
87
+ )
88
  }
89
  ] + chat_history + [{"role": "user", "content": message}]
90
+
91
  payload = {
92
  "messages": messages,
93
  "temperature": temperature,
94
  "top_p": top_p
95
  }
96
+
97
  headers = {
98
  "Content-Type": "application/json",
99
  "Authorization": "Bearer oxy-1-small-gradio"
100
  }
101
+
102
+ # Update local chat history
103
  chat_history = chat_history + [{"role": "user", "content": message}]
104
+
105
  try:
106
+ response = requests.post(API_URL, headers=headers, json=payload)
 
 
 
 
107
  response.raise_for_status()
 
108
  json_response = response.json()
109
+
110
  if 'choices' in json_response and len(json_response['choices']) > 0:
111
+ assistant_content = json_response['choices'][0]['message']['content']
112
  chat_history.append({"role": "assistant", "content": assistant_content})
113
  total_cost = json_response["usage"]["cost"]["total"]
114
  formatted_cost = f"{total_cost:.10f}"
115
+
116
  stats_content = (
117
  f'*Powered by Oxygen, '
118
  f'Generation time: {json_response["usage"]["metrics"]["inference_time_ms"]} ms, '
 
121
  )
122
  else:
123
  chat_history.append({"role": "assistant", "content": "Error: No response from assistant."})
124
+ stats_content = ""
125
+
126
  return chat_history, stats_content
127
+
128
  except Exception as e:
129
  chat_history.append({"role": "assistant", "content": f"Error: {str(e)}"})
130
  return chat_history, "*Generation error..*"
131
 
132
+ # ---------------------------------------
133
+ # Send the message to both chats at once
134
+ # ---------------------------------------
135
+ def predict_synced(message, chat_history_base, chat_history_opt, temperature, top_p):
136
+ new_history_base, stats_base = predict_base(message, chat_history_base, temperature, top_p)
137
+ new_history_opt, stats_opt = predict_optimized(message, chat_history_opt, temperature, top_p)
138
+ return new_history_base, stats_base, new_history_opt, stats_opt
139
+
140
+ # -------------------------- UI ------------------------
141
  css = """
142
  html, body {
143
  margin: 0;
 
146
  background: #0a0a0a;
147
  color: #ffffff;
148
  font-family: 'Inter', ui-sans-serif, system-ui;
 
 
149
  }
150
 
151
  .gradio-container {
 
166
  overflow-y: auto;
167
  }
168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
  .disclaimer-container {
170
  padding: 2rem;
171
  background: linear-gradient(45deg, #1a1a1a, #262626);
172
  border-radius: 1rem;
173
+ margin: 2rem;
174
  color: #ffffff;
175
  border: 1px solid #333;
 
 
176
  }
177
 
178
  .warning-title {
 
179
  font-size: 1.5rem;
180
  font-weight: bold;
181
+ color: #ff9966;
182
  margin-bottom: 1rem;
183
  }
 
 
 
 
 
184
  """
185
 
186
+ def show_chat():
187
+ return gr.update(visible=False), gr.update(visible=True)
188
+
189
+ # ------------------
190
+ # Main Gradio Block
191
+ # ------------------
192
  with gr.Blocks(
193
  theme=gr.themes.Soft(
194
  primary_hue="orange",
 
201
  css=css
202
  ) as demo:
203
 
204
+ # ------------------
205
+ # Disclaimer Section
206
+ # ------------------
207
  with gr.Column(visible=True) as consent_block:
208
  gr.HTML("""
209
  <div class="disclaimer-container">
210
  <div class="warning-title">⚠️ Important Notice - Please Read Carefully</div>
211
+ <div style="font-size: 1rem; line-height: 1.6;">
212
  <p>Welcome to the Oxygen AI Demo. Before proceeding, please understand and acknowledge the following:</p>
 
213
  <h3>Content Warning</h3>
214
  <ul>
215
  <li>This is an <strong>uncensored AI model</strong> that operates without traditional content restrictions.</li>
216
  <li>It may generate content that some users might find offensive, inappropriate, or disturbing.</li>
217
  <li>The model may discuss sensitive topics, controversial subjects, or produce strong language.</li>
218
  </ul>
 
219
  <h3>User Requirements</h3>
220
  <ul>
221
  <li>You must be at least 18 years old to use this service.</li>
222
  <li>You accept full responsibility for how you use and interact with the model.</li>
223
  <li>You understand that generated content does not reflect the views of Oxygen or its developers.</li>
224
  </ul>
225
+ <p>Visit <a href="https://www.oxyapi.uk" target="_blank" style="color: #ffa500;">www.oxyapi.uk</a> for more information.</p>
226
+ <hr />
227
+ <p style="font-size:0.9rem;">
228
+ <strong>Credits:</strong>
229
+ <em>Controlling Small Language Model Behavior: A Technical Deep Dive into Prompt Engineering and Output Management</em> —
230
+ myAIdojo.com Technical Research Team, December 2, 2024
231
+ (<a href="https://myaidojo.com/oxy13.pdf" target="_blank" style="color:#ffa500;">Link</a>)
232
+ </p>
233
  </div>
234
  </div>
235
  """)
236
  agree_button = gr.Button("I Understand and Agree", variant="primary", size="lg")
237
+
238
+ # --------------------
239
+ # Dual Chat Interface
240
+ # --------------------
241
  with gr.Column(visible=False) as chat_block:
242
+ with gr.Row():
243
+ with gr.Column():
244
+ gr.Markdown("### Base Chat")
245
+ chatbot_base = gr.Chatbot(
246
+ value=[],
247
+ show_copy_button=True,
248
+ container=True,
249
+ avatar_images=[
250
+ "https://api.holabo.co/user.svg",
251
+ "https://api.holabo.co/oxy.svg"
252
+ ],
253
+ bubble_full_width=True,
254
+ type="messages"
255
+ )
256
+ with gr.Column():
257
+ gr.Markdown("### Prompt Optimized Chat")
258
+ chatbot_opt = gr.Chatbot(
259
+ value=[],
260
+ show_copy_button=True,
261
+ container=True,
262
+ avatar_images=[
263
+ "https://api.holabo.co/user.svg",
264
+ "https://api.holabo.co/oxy.svg"
265
+ ],
266
+ bubble_full_width=True,
267
+ type="messages"
268
+ )
269
+ # -------------------------
270
+ # Input + Sliders + Output
271
+ # -------------------------
272
  with gr.Row():
273
  msg = gr.Textbox(
274
  label="Message",
275
  placeholder="Type your message here...",
276
  show_label=False,
277
+ container=True
 
278
  )
279
+ submit = gr.Button("Send", variant="primary")
280
+ with gr.Row():
 
281
  temperature = gr.Slider(
282
  minimum=0.1,
283
  maximum=2.0,
 
292
  step=0.05,
293
  label="Top-p"
294
  )
295
+ with gr.Row():
296
+ stats_display_base = gr.Markdown()
297
+ stats_display_opt = gr.Markdown()
298
 
299
+ # -----------------------------
300
+ # Manage Chat Histories (State)
301
+ # -----------------------------
302
+ state_base = gr.State([])
303
+ state_opt = gr.State([])
304
 
305
+ # -------------------------------------
306
+ # Update Both Chats On Submit
307
+ # -------------------------------------
308
+ msg.submit(
309
+ predict_synced,
310
+ [msg, state_base, state_opt, temperature, top_p],
311
+ [chatbot_base, stats_display_base, chatbot_opt, stats_display_opt]
312
+ ).then(lambda: "", None, msg)
313
+ submit.click(
314
+ predict_synced,
315
+ [msg, state_base, state_opt, temperature, top_p],
316
+ [chatbot_base, stats_display_base, chatbot_opt, stats_display_opt]
317
+ ).then(lambda: "", None, msg)
318
 
319
+ # ------------------
320
+ # Consent -> Chat UI
321
+ # ------------------
 
 
 
 
 
 
 
322
  agree_button.click(
323
  show_chat,
324
  inputs=None,
325
  outputs=[consent_block, chat_block]
326
  )
327
 
328
+ # ----------------------
329
+ # Launch the Gradio App
330
+ # ----------------------
331
  if __name__ == "__main__":
332
  demo.launch(
333
  server_name="0.0.0.0",
334
  server_port=7860,
335
  share=False
336
+ )