ZENLLC commited on
Commit
354f1d9
·
verified ·
1 Parent(s): 7390526

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -17
app.py CHANGED
@@ -18,8 +18,11 @@ OpenAI-only teaching rig for building AI model UIs.
18
  • Uses GPT-5 for text generation.
19
  • Uses DALL·E 3 for image generation.
20
  • Lets you edit the system prompt, role, tone, and output format.
21
- • Provides sliders and controls to experiment with sampling and behavior.
22
  • Includes starter prompts to show different use cases (chat, reports, infographics, visuals).
 
 
 
23
  """
24
 
25
  DEFAULT_TEMPERATURE = 0.7
@@ -55,10 +58,15 @@ def build_system_instructions(
55
  assistant_role: str,
56
  output_mode: str,
57
  tone: str,
 
 
 
 
58
  ) -> str:
59
  """
60
  Build a system prompt string combining user-provided base instructions
61
- with role + format + tone metadata.
 
62
  """
63
  role_map = {
64
  "General Assistant": "Behave as a highly capable, calm general-purpose AI assistant.",
@@ -88,6 +96,16 @@ def build_system_instructions(
88
  "Minimalist": "Be extremely concise. Prefer fewer words and high information density.",
89
  }
90
 
 
 
 
 
 
 
 
 
 
 
91
  parts = [
92
  (user_system_prompt or "").strip(),
93
  "",
@@ -95,6 +113,8 @@ def build_system_instructions(
95
  f"OUTPUT MODE: {output_map.get(output_mode, '')}",
96
  f"TONE: {tone_map.get(tone, '')}",
97
  "",
 
 
98
  "Always output clean Markdown.",
99
  ]
100
  return "\n".join(p for p in parts if p.strip())
@@ -131,23 +151,21 @@ def history_to_openai_messages(
131
  def call_openai_text(
132
  openai_key: Optional[str],
133
  messages: List[Dict[str, str]],
134
- temperature: float,
135
- top_p: float,
136
  max_tokens: int,
137
- presence_penalty: float,
138
- frequency_penalty: float,
139
  ) -> str:
140
  """
141
- Call GPT-5 via Chat Completions using the new max_completion_tokens parameter.
 
 
 
 
 
 
142
  """
143
  client = get_openai_client(openai_key)
144
  completion = client.chat.completions.create(
145
  model="gpt-5", # change to exact variant you have (e.g. "gpt-5.1-mini") if needed
146
  messages=messages,
147
- temperature=temperature,
148
- top_p=top_p,
149
- presence_penalty=presence_penalty,
150
- frequency_penalty=frequency_penalty,
151
  max_completion_tokens=max_tokens,
152
  )
153
  return completion.choices[0].message.content
@@ -234,12 +252,16 @@ def agent_assembler_chat(
234
  if not user_message.strip():
235
  return chat_history, None
236
 
237
- # Build system instructions
238
  system_instructions = build_system_instructions(
239
  user_system_prompt=system_prompt_ui,
240
  assistant_role=assistant_role,
241
  output_mode=output_mode,
242
  tone=tone,
 
 
 
 
243
  )
244
 
245
  # Prepare messages for OpenAI
@@ -249,16 +271,12 @@ def agent_assembler_chat(
249
  system_instructions=system_instructions,
250
  )
251
 
252
- # Call GPT-5
253
  try:
254
  ai_reply = call_openai_text(
255
  openai_key=openai_key_ui,
256
  messages=openai_messages,
257
- temperature=temperature,
258
- top_p=top_p,
259
  max_tokens=max_tokens,
260
- presence_penalty=presence_penalty,
261
- frequency_penalty=frequency_penalty,
262
  )
263
  except Exception as e:
264
  ai_reply = (
 
18
  • Uses GPT-5 for text generation.
19
  • Uses DALL·E 3 for image generation.
20
  • Lets you edit the system prompt, role, tone, and output format.
21
+ • Provides sliders and controls to experiment with behavior.
22
  • Includes starter prompts to show different use cases (chat, reports, infographics, visuals).
23
+
24
+ NOTE: Some newer GPT-5 variants ignore temperature/top-p/penalty parameters.
25
+ This app keeps the controls for teaching, and encodes their values into the instructions instead.
26
  """
27
 
28
  DEFAULT_TEMPERATURE = 0.7
 
58
  assistant_role: str,
59
  output_mode: str,
60
  tone: str,
61
+ temperature: float,
62
+ top_p: float,
63
+ presence_penalty: float,
64
+ frequency_penalty: float,
65
  ) -> str:
66
  """
67
  Build a system prompt string combining user-provided base instructions
68
+ with role + format + tone + "virtual sampling" metadata.
69
+ (We encode the slider settings here since GPT-5 may not support those params directly.)
70
  """
71
  role_map = {
72
  "General Assistant": "Behave as a highly capable, calm general-purpose AI assistant.",
 
96
  "Minimalist": "Be extremely concise. Prefer fewer words and high information density.",
97
  }
98
 
99
+ sampling_hint = (
100
+ "SAMPLING HINTS FROM UI SLIDERS:\n"
101
+ f"- Temperature slider: {temperature:.2f} (higher = more creative and speculative).\n"
102
+ f"- Top-p slider: {top_p:.2f} (lower = more conservative).\n"
103
+ f"- Presence penalty slider: {presence_penalty:.2f} (higher = encourage new topics).\n"
104
+ f"- Frequency penalty slider: {frequency_penalty:.2f} (higher = reduce repetition).\n"
105
+ "You must interpret these values as behavioral guidance even if the underlying "
106
+ "model ignores sampling parameters."
107
+ )
108
+
109
  parts = [
110
  (user_system_prompt or "").strip(),
111
  "",
 
113
  f"OUTPUT MODE: {output_map.get(output_mode, '')}",
114
  f"TONE: {tone_map.get(tone, '')}",
115
  "",
116
+ sampling_hint,
117
+ "",
118
  "Always output clean Markdown.",
119
  ]
120
  return "\n".join(p for p in parts if p.strip())
 
151
  def call_openai_text(
152
  openai_key: Optional[str],
153
  messages: List[Dict[str, str]],
 
 
154
  max_tokens: int,
 
 
155
  ) -> str:
156
  """
157
+ Call GPT-5 via Chat Completions using only supported parameters:
158
+ - model
159
+ - messages
160
+ - max_completion_tokens
161
+
162
+ Newer GPT-5 variants may not support custom temperature/top_p/penalties,
163
+ so we rely on the system prompt for behavior control instead.
164
  """
165
  client = get_openai_client(openai_key)
166
  completion = client.chat.completions.create(
167
  model="gpt-5", # change to exact variant you have (e.g. "gpt-5.1-mini") if needed
168
  messages=messages,
 
 
 
 
169
  max_completion_tokens=max_tokens,
170
  )
171
  return completion.choices[0].message.content
 
252
  if not user_message.strip():
253
  return chat_history, None
254
 
255
+ # Build system instructions (including slider hints)
256
  system_instructions = build_system_instructions(
257
  user_system_prompt=system_prompt_ui,
258
  assistant_role=assistant_role,
259
  output_mode=output_mode,
260
  tone=tone,
261
+ temperature=temperature,
262
+ top_p=top_p,
263
+ presence_penalty=presence_penalty,
264
+ frequency_penalty=frequency_penalty,
265
  )
266
 
267
  # Prepare messages for OpenAI
 
271
  system_instructions=system_instructions,
272
  )
273
 
274
+ # Call GPT-5 (minimal parameters)
275
  try:
276
  ai_reply = call_openai_text(
277
  openai_key=openai_key_ui,
278
  messages=openai_messages,
 
 
279
  max_tokens=max_tokens,
 
 
280
  )
281
  except Exception as e:
282
  ai_reply = (