-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
556 lines (446 loc) Β· 17.4 KB
/
app.py
File metadata and controls
556 lines (446 loc) Β· 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
"""
Streamlit web interface for LLM Playground.
Interactive UI for experimenting with LLMs and observing behavior.
"""
import streamlit as st
import sys
from pathlib import Path
# Add project root to path
sys.path.insert(0, str(Path(__file__).parent))
import config
from models import get_model, OPENAI_AVAILABLE
from logger import get_logger
from experiments.zero_shot import run_zero_shot_experiment, EXAMPLE_TASKS
from experiments.few_shot import run_few_shot_experiment, EXAMPLE_SCENARIOS
from experiments.sampling_params import (
run_temperature_experiment,
analyze_temperature_effects,
EXAMPLE_PROMPTS
)
from experiments.context_window import run_context_window_experiment, LONG_TEXT_EXAMPLES
from experiments.prompt_sensitivity import (
run_prompt_sensitivity_experiment,
get_prompt_variations,
PROMPT_VARIATION_EXAMPLES
)
# Page configuration
st.set_page_config(
page_title="LLM Playground",
page_icon="π",
layout="wide",
initial_sidebar_state="expanded",
)
def init_session_state():
"""Initialize session state variables."""
if "logger" not in st.session_state:
st.session_state.logger = get_logger(format="json")
if "model" not in st.session_state:
st.session_state.model = None
if "response_history" not in st.session_state:
st.session_state.response_history = []
def sidebar():
"""Render sidebar with model selection and parameters."""
st.sidebar.title("βοΈ Configuration")
# Model provider selection
providers = ["ollama"]
if OPENAI_AVAILABLE and config.OPENAI_API_KEY:
providers.append("openai")
provider = st.sidebar.selectbox(
"Provider",
providers,
index=0,
)
# Model selection
if provider == "ollama":
model_name = st.sidebar.text_input(
"Model Name",
value=config.DEFAULT_MODEL,
help="e.g., llama2, mistral, phi",
)
else:
model_name = st.sidebar.selectbox(
"Model Name",
["gpt-3.5-turbo", "gpt-4", "gpt-4-turbo"],
index=0,
)
# Create model button
if st.sidebar.button("π Connect to Model", type="primary"):
try:
with st.spinner(f"Connecting to {provider}:{model_name}..."):
st.session_state.model = get_model(provider, model_name)
st.sidebar.success(f"β
Connected to {provider}:{model_name}")
except Exception as e:
st.sidebar.error(f"β Connection failed: {e}")
# Show connection status
if st.session_state.model:
st.sidebar.info(f"π‘ Connected: {st.session_state.model}")
else:
st.sidebar.warning("β οΈ No model connected")
st.sidebar.divider()
# Sampling parameters
st.sidebar.subheader("ποΈ Sampling Parameters")
temperature = st.sidebar.slider(
"Temperature",
min_value=config.TEMPERATURE_RANGE[0],
max_value=config.TEMPERATURE_RANGE[1],
value=config.DEFAULT_TEMPERATURE,
step=config.TEMPERATURE_STEP,
help="Lower = more focused, Higher = more creative",
)
top_p = st.sidebar.slider(
"Top P",
min_value=config.TOP_P_RANGE[0],
max_value=config.TOP_P_RANGE[1],
value=config.DEFAULT_TOP_P,
step=config.TOP_P_STEP,
help="Nucleus sampling threshold",
)
max_tokens = st.sidebar.slider(
"Max Tokens",
min_value=config.MAX_TOKENS_RANGE[0],
max_value=config.MAX_TOKENS_RANGE[1],
value=config.MAX_TOKENS_DEFAULT,
step=50,
help="Maximum tokens to generate",
)
return {
"temperature": temperature,
"top_p": top_p,
"max_tokens": max_tokens,
}
def quick_chat_tab(params):
"""Simple chat interface."""
st.header("π¬ Quick Chat")
st.markdown("Test the model with a simple prompt.")
if not st.session_state.model:
st.warning("β οΈ Please connect to a model first (see sidebar)")
return
# Input
prompt = st.text_area(
"Enter your prompt:",
height=150,
placeholder="Ask anything...",
)
# Generate button
col1, col2 = st.columns([1, 5])
with col1:
if st.button("π Generate", type="primary", disabled=not prompt):
with st.spinner("Generating..."):
try:
response = st.session_state.model.generate(
prompt=prompt,
temperature=params["temperature"],
max_tokens=params["max_tokens"],
top_p=params["top_p"],
)
# Log interaction
st.session_state.logger.log_interaction(
prompt=prompt,
response=response,
parameters=params,
experiment_type="quick_chat",
)
# Display response
st.subheader("Response:")
st.write(response.text)
# Show metrics
col1, col2, col3, col4 = st.columns(4)
col1.metric("Tokens", response.total_tokens)
col2.metric("Latency", f"{response.latency_ms:.0f}ms")
col3.metric("Speed", f"{response.latency_ms/response.completion_tokens:.1f}ms/tok")
if response.cost_usd > 0:
col4.metric("Cost", f"${response.cost_usd:.4f}")
except Exception as e:
st.error(f"β Generation failed: {e}")
def zero_shot_tab(params):
"""Zero-shot prompting experiments."""
st.header("π― Zero-Shot Prompting")
st.markdown("Test the model's ability to perform tasks **without examples**.")
if not st.session_state.model:
st.warning("β οΈ Please connect to a model first (see sidebar)")
return
# Example task selector
col1, col2 = st.columns([1, 3])
with col1:
example_task = st.selectbox(
"Example Task",
list(EXAMPLE_TASKS.keys()),
)
# Load example or custom
if st.checkbox("Use example task", value=True):
prompt = st.text_area(
"Prompt:",
value=EXAMPLE_TASKS[example_task],
height=150,
)
else:
prompt = st.text_area(
"Custom prompt:",
height=150,
placeholder="Enter your task...",
)
# Run experiment
if st.button("π§ͺ Run Experiment", type="primary", disabled=not prompt):
with st.spinner("Running..."):
try:
response = run_zero_shot_experiment(
model=st.session_state.model,
task=prompt,
logger=st.session_state.logger,
temperature=params["temperature"],
max_tokens=params["max_tokens"],
)
st.success("β
Experiment completed")
st.subheader("Result:")
st.write(response.text)
# Metrics
col1, col2, col3 = st.columns(3)
col1.metric("Tokens", response.total_tokens)
col2.metric("Latency", f"{response.latency_ms:.0f}ms")
col3.metric("Finish Reason", response.finish_reason)
except Exception as e:
st.error(f"β Experiment failed: {e}")
def few_shot_tab(params):
"""Few-shot prompting experiments."""
st.header("π Few-Shot Prompting")
st.markdown("Compare performance **with** and **without** examples.")
if not st.session_state.model:
st.warning("β οΈ Please connect to a model first (see sidebar)")
return
# Scenario selector
scenario = st.selectbox(
"Select Scenario",
list(EXAMPLE_SCENARIOS.keys()),
)
scenario_data = EXAMPLE_SCENARIOS[scenario]
# Show examples
st.subheader("Examples:")
for i, (input_text, output_text) in enumerate(scenario_data["examples"], 1):
st.text(f"{i}. Input: {input_text}")
st.text(f" Output: {output_text}")
st.divider()
# Test case
test_case = st.selectbox(
"Test Case",
scenario_data["test_cases"],
)
# Run experiment
if st.button("π§ͺ Run Comparison", type="primary"):
with st.spinner("Running both zero-shot and few-shot..."):
try:
zero_shot_resp, few_shot_resp = run_few_shot_experiment(
model=st.session_state.model,
task=test_case,
examples=scenario_data["examples"],
logger=st.session_state.logger,
temperature=params["temperature"],
max_tokens=params["max_tokens"],
)
# Display comparison
col1, col2 = st.columns(2)
with col1:
st.subheader("β Zero-Shot (No Examples)")
st.write(zero_shot_resp.text)
st.caption(f"Tokens: {zero_shot_resp.total_tokens} | "
f"Latency: {zero_shot_resp.latency_ms:.0f}ms")
with col2:
st.subheader("β
Few-Shot (With Examples)")
st.write(few_shot_resp.text)
st.caption(f"Tokens: {few_shot_resp.total_tokens} | "
f"Latency: {few_shot_resp.latency_ms:.0f}ms")
except Exception as e:
st.error(f"β Experiment failed: {e}")
def temperature_tab(params):
"""Temperature comparison experiments."""
st.header("π‘οΈ Temperature Effects")
st.markdown("See how temperature affects **creativity** vs **determinism**.")
if not st.session_state.model:
st.warning("β οΈ Please connect to a model first (see sidebar)")
return
# Prompt selection
prompt_type = st.selectbox(
"Prompt Type",
list(EXAMPLE_PROMPTS.keys()),
)
prompt = st.text_area(
"Prompt:",
value=EXAMPLE_PROMPTS[prompt_type],
height=100,
)
# Temperature range
st.subheader("Temperature Range")
col1, col2, col3 = st.columns(3)
with col1:
temp_min = st.number_input("Min", value=0.1, step=0.1)
with col2:
temp_max = st.number_input("Max", value=1.5, step=0.1)
with col3:
temp_steps = st.number_input("Steps", value=3, min_value=2, max_value=10)
# Generate temperature list
temps = [temp_min + i * (temp_max - temp_min) / (temp_steps - 1)
for i in range(temp_steps)]
# Samples per temperature
num_samples = st.slider("Samples per temperature", 1, 5, 1)
# Run experiment
if st.button("π§ͺ Run Temperature Test", type="primary", disabled=not prompt):
with st.spinner(f"Testing {len(temps)} temperatures with {num_samples} samples each..."):
try:
results = run_temperature_experiment(
model=st.session_state.model,
prompt=prompt,
temperatures=temps,
logger=st.session_state.logger,
max_tokens=params["max_tokens"],
num_samples=num_samples,
)
st.success("β
Experiment completed")
# Display results
for temp in sorted(results.keys()):
st.subheader(f"Temperature: {temp}")
for i, response in enumerate(results[temp], 1):
with st.expander(f"Sample {i}"):
st.write(response.text)
st.caption(f"Tokens: {response.completion_tokens} | "
f"Latency: {response.latency_ms:.0f}ms")
# Analysis
st.divider()
st.subheader("π Analysis")
analysis = analyze_temperature_effects(results)
st.text(analysis)
except Exception as e:
st.error(f"β Experiment failed: {e}")
def prompt_sensitivity_tab(params):
"""Prompt sensitivity experiments."""
st.header("π Prompt Sensitivity")
st.markdown("Observe how **small changes** dramatically affect outputs.")
if not st.session_state.model:
st.warning("β οΈ Please connect to a model first (see sidebar)")
return
# Category selection
category = st.selectbox(
"Variation Type",
list(PROMPT_VARIATION_EXAMPLES.keys()),
)
# Get variations
variations = get_prompt_variations(category)
st.subheader("Prompt Variations:")
for i, var in enumerate(variations, 1):
st.text(f"{i}. {var}")
# Samples
num_samples = st.slider("Samples per variation", 1, 3, 1)
# Run experiment
if st.button("π§ͺ Run Sensitivity Test", type="primary"):
with st.spinner(f"Testing {len(variations)} variations..."):
try:
results = run_prompt_sensitivity_experiment(
model=st.session_state.model,
prompt_variations=variations,
logger=st.session_state.logger,
temperature=params["temperature"],
max_tokens=params["max_tokens"],
num_samples=num_samples,
)
st.success("β
Experiment completed")
# Display results
for prompt, responses in results.items():
st.subheader(f"Prompt: \"{prompt}\"")
for i, response in enumerate(responses, 1):
with st.expander(f"Sample {i}"):
st.write(response.text)
st.caption(f"Tokens: {response.completion_tokens}")
except Exception as e:
st.error(f"β Experiment failed: {e}")
def logs_tab():
"""View logged interactions."""
st.header("π Interaction Logs")
st.markdown("Review all logged interactions for analysis.")
# Read logs
logger = st.session_state.logger
logs = logger.read_logs(limit=50)
if not logs:
st.info("No logs yet. Run some experiments to see them here!")
return
st.write(f"Showing last {len(logs)} interactions")
st.caption(f"Log file: {logger.get_log_file_path()}")
# Display logs
for i, log in enumerate(reversed(logs), 1):
with st.expander(f"{i}. {log.get('model', 'Unknown')} - {log.get('experiment_type', 'N/A')}"):
st.text(f"Timestamp: {log.get('timestamp', 'N/A')}")
st.text(f"\nPrompt:\n{log.get('prompt', 'N/A')[:200]}...")
st.text(f"\nResponse:\n{log.get('response', 'N/A')[:200]}...")
if 'metrics' in log:
metrics = log['metrics']
col1, col2, col3 = st.columns(3)
col1.metric("Total Tokens", metrics.get('total_tokens', 0))
col2.metric("Latency (ms)", f"{metrics.get('latency_ms', 0):.0f}")
col3.metric("Cost (USD)", f"${metrics.get('cost_usd', 0):.4f}")
def docs_tab():
"""Display documentation."""
st.header("π Documentation")
# Document selector
doc_choice = st.selectbox(
"Select Document",
[
"Quick Start",
"LLM Concepts",
"Tutorial",
"Learning Outcomes",
"Architecture",
]
)
# Map to filenames
doc_files = {
"Quick Start": "QUICKSTART.md",
"LLM Concepts": "CONCEPTS.md",
"Tutorial": "TUTORIAL.md",
"Learning Outcomes": "LEARNING_OUTCOMES.md",
"Architecture": "ARCHITECTURE.md",
}
filename = doc_files[doc_choice]
filepath = Path(__file__).parent / filename
try:
with open(filepath, "r", encoding="utf-8") as f:
content = f.read()
# Display the markdown content
st.markdown(content, unsafe_allow_html=False)
except FileNotFoundError:
st.error(f"Documentation file not found: {filename}")
except Exception as e:
st.error(f"Error reading documentation: {e}")
def main():
"""Main application."""
init_session_state()
# Header
st.title(config.APP_TITLE)
st.markdown(config.APP_DESCRIPTION)
# Sidebar
params = sidebar()
# Main tabs
tabs = st.tabs([
"π¬ Quick Chat",
"π― Zero-Shot",
"π Few-Shot",
"π‘οΈ Temperature",
"π Sensitivity",
"π Logs",
"π Docs",
])
with tabs[0]:
quick_chat_tab(params)
with tabs[1]:
zero_shot_tab(params)
with tabs[2]:
few_shot_tab(params)
with tabs[3]:
temperature_tab(params)
with tabs[4]:
prompt_sensitivity_tab(params)
with tabs[5]:
logs_tab()
with tabs[6]:
docs_tab()
# Footer
st.divider()
st.caption("π LLM Playground - Learn by doing")
if __name__ == "__main__":
main()