-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathprompt_engineering_workshop_app.py
More file actions
1669 lines (1374 loc) · 70.2 KB
/
Copy pathprompt_engineering_workshop_app.py
File metadata and controls
1669 lines (1374 loc) · 70.2 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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
from dotenv import load_dotenv
import sys
import openai
import anthropic
import streamlit as st
import base64
# Add project root to path for module imports
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
# Load environment variables
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
anthropic_api_key = os.getenv("ANTHROPIC_API_KEY")
anthropic_client = None
# Initialize Anthropic client if API key is available
if anthropic_api_key:
try:
anthropic_client = anthropic.Anthropic(api_key=anthropic_api_key)
except Exception as e:
print(f"Error initializing Anthropic client: {str(e)}")
# Initialize session state for API keys
if "openai_api_key" not in st.session_state:
st.session_state.openai_api_key = os.getenv("OPENAI_API_KEY", "")
if "anthropic_api_key" not in st.session_state:
st.session_state.anthropic_api_key = os.getenv("ANTHROPIC_API_KEY", "")
def get_image_base64(image_path):
"""Convert image to base64 string for embedding in HTML"""
try:
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode()
except Exception as e:
print(f"Error converting image to base64: {e}")
return None
class StreamlitPromptEngineering:
"""Frontend version of PromptEngineering class adapted for Streamlit"""
def __init__(self, model="gpt-3.5-turbo", temperature=0.7, max_tokens=256):
self.model = model
self.temperature = temperature
self.max_tokens = max_tokens
# Determine model provider (OpenAI or Anthropic)
self.provider = "openai" if "gpt" in model else "anthropic"
def set_parameters(
self,
model=None,
temperature=None,
max_tokens=None,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
):
# Same as original
params = {
"model": model or self.model,
"temperature": temperature if temperature is not None else self.temperature,
"max_tokens": max_tokens or self.max_tokens,
"top_p": top_p,
"frequency_penalty": frequency_penalty,
"presence_penalty": presence_penalty,
}
# Update provider if model changed
if model and model != self.model:
self.provider = "openai" if "gpt" in model else "anthropic"
return params
def get_completion(self, messages, **kwargs):
"""Get completion with Streamlit progress indicator"""
params = self.set_parameters(**kwargs)
provider = "anthropic" if "claude" in params["model"] else "openai"
# Validate required API keys using session state
if provider == "openai" and not st.session_state.openai_api_key:
return f"[OpenAI API key not set. Example response for: {messages[-1]['content'][:50]}...]"
if provider == "anthropic" and not st.session_state.anthropic_api_key:
return f"[Anthropic API key not set. Example response for: {messages[-1]['content'][:50]}...]"
try:
with st.spinner("Getting AI response..."):
if provider == "openai":
# Use OpenAI API with session state key
# Set the API key on the client
openai.api_key = st.session_state.openai_api_key
response = openai.chat.completions.create(
model=params["model"],
messages=messages,
temperature=params["temperature"],
max_tokens=params["max_tokens"],
top_p=params["top_p"],
frequency_penalty=params["frequency_penalty"],
presence_penalty=params["presence_penalty"],
)
return response.choices[0].message.content
else:
# Use Anthropic API with session state key
system_message = ""
user_messages = []
# Convert ChatML format to Anthropic format
for msg in messages:
if msg["role"] == "system":
system_message = msg["content"]
else:
user_messages.append(msg["content"])
# Join user messages if multiple (simplification for demo)
user_content = "\n".join(user_messages) if user_messages else ""
# Create temporary client with session state key
temp_anthropic_client = anthropic.Anthropic(
api_key=st.session_state.anthropic_api_key
)
response = temp_anthropic_client.messages.create(
model=params["model"],
max_tokens=params["max_tokens"],
temperature=params["temperature"],
top_p=params["top_p"],
system=system_message,
messages=[{"role": "user", "content": user_content}],
)
return response.content[0].text
except Exception as e:
return f"Error: {str(e)}"
def display_result(self, prompt, response, concept="Basic Prompting"):
"""Display results in Streamlit UI"""
st.subheader(f"CONCEPT: {concept}")
col1, col2 = st.columns(2)
with col1:
st.text_area("PROMPT", prompt, height=400)
with col2:
st.text_area("RESPONSE", response, height=400)
st.divider()
# Streamlit app layout
def main():
st.set_page_config(
page_title="Prompt Engineering Workshop",
page_icon="🚀",
layout="wide",
initial_sidebar_state="expanded",
)
# Add custom CSS to increase font size and sidebar width
st.markdown(
"""
<style>
@import url('https://fonts.googleapis.com/css2?family=Space+Mono:wght@400;700&display=swap');
html, body, [class*="css"] {
font-family: 'Space Mono', monospace;
font-size: 22px;
}
h1 {
font-family: 'Space Mono', monospace;
font-size: 2.5rem !important;
}
h2 {
font-family: 'Space Mono', monospace;
font-size: 2rem !important;
}
h3 {
font-family: 'Space Mono', monospace;
font-size: 1.5rem !important;
}
.stTextArea textarea {
font-family: 'Space Mono', monospace;
font-size: 20px !important;
min-height: 400px !important;
}
.stButton button {
font-family: 'Space Mono', monospace;
font-size: 22px !important;
}
.stRadio label {
font-family: 'Space Mono', monospace;
font-size: 22px !important;
}
.stSelectbox label {
font-family: 'Space Mono', monospace;
font-size: 22px !important;
}
.sidebar .sidebar-content {
font-family: 'Space Mono', monospace;
font-size: 22px !important;
}
/* Make sliders wider */
.stSlider {
width: 100% !important;
}
.stSlider > div > div {
width: 100% !important;
}
/* Make sidebar wider */
[data-testid="stSidebar"] {
font-family: 'Space Mono', monospace;
min-width: 400px !important;
max-width: 400px !important;
}
/* Make sidebar collapsible */
[data-testid="stSidebar"][aria-expanded="false"] {
margin-left: -400px !important;
}
/* Ensure main content adjusts accordingly */
.main .block-container {
max-width: calc(100% - 450px) !important;
padding-left: 7rem !important;
}
</style>
""",
unsafe_allow_html=True,
)
# Create a layout with columns for the title and logo
col1, col2 = st.columns([3, 1])
with col1:
st.title("Prompt Engineering Workshop")
st.subheader("@AI Convention 2025 (IHK Schwaben)")
with col2:
# Display the DATANOMIQ logo flush right and make it clickable
try:
# Apply CSS for right alignment
st.markdown(
"""
<style>
[data-testid="column"] > div:has(img) {
display: flex;
justify-content: flex-end;
}
</style>
""",
unsafe_allow_html=True,
)
logo_path = os.path.join(os.getcwd(), "assets", "DATANOMIQ.png")
if os.path.exists(logo_path):
# Create clickable logo using HTML
image_base64 = get_image_base64(logo_path)
if image_base64:
st.markdown(
f"""
<div style="display: flex; justify-content: flex-end;">
<a href="https://datanomiq.ai" target="_blank" style="text-decoration: none;">
<img src="data:image/png;base64,{image_base64}"
width="300"
style="cursor: pointer;"
title="Visit DATANOMIQ.ai">
</a>
</div>
""",
unsafe_allow_html=True,
)
else:
# Fallback to text link if base64 conversion fails
st.markdown(
"""
<div style="display: flex; justify-content: flex-end;">
<a href="https://datanomiq.ai" target="_blank" style="text-decoration: none; color: #0066cc; font-weight: bold;">
DATANOMIQ.ai
</a>
</div>
""",
unsafe_allow_html=True,
)
except Exception as e:
st.error(f"Could not load logo: {e}")
# Fallback to text link
st.markdown(
"""
<div style="display: flex; justify-content: flex-end;">
<a href="https://datanomiq.ai" target="_blank" style="text-decoration: none; color: #0066cc; font-weight: bold;">
DATANOMIQ.ai
</a>
</div>
""",
unsafe_allow_html=True,
)
# Initialize frontend prompt engineering instance
pe_demo = StreamlitPromptEngineering()
# Initialize session state to store settings
if "temperature" not in st.session_state:
st.session_state.temperature = 0.7
if "top_p" not in st.session_state:
st.session_state.top_p = 1.0
if "max_tokens" not in st.session_state:
st.session_state.max_tokens = 256
if "model" not in st.session_state:
st.session_state.model = "gpt-3.5-turbo"
if "provider" not in st.session_state:
st.session_state.provider = "openai"
# Sidebar for navigation
st.sidebar.title("Workshop Sections")
section = st.sidebar.radio(
"Choose a section:",
[
"Introduction",
"Settings", # New settings section
"1. Basic Prompting",
"2. Instruction-based Prompting",
"3. Zero/One/Few-Shot Prompting",
"4. Chain-of-Thought Reasoning",
"5. Self-Consistency Techniques",
"6. Tree of Thoughts",
"7. ReAct Framework",
"8. Real-world Applications",
],
)
# Add model indicator to the sidebar
st.sidebar.divider()
st.sidebar.subheader("Current Model")
model_name = st.session_state.model
provider = "OpenAI" if "gpt" in model_name else "Anthropic"
model_display = model_name.replace("-", " ").title().replace("Gpt", "GPT")
st.sidebar.markdown(f"**Provider**: {provider}")
st.sidebar.markdown(f"**Model**: {model_display}")
st.sidebar.markdown(f"**Temperature**: {st.session_state.temperature}")
# add Sources
st.sidebar.divider()
# add Sources section
st.sidebar.subheader("Sources")
st.sidebar.markdown(
"""
- [Prompt Engineering Paper](https://github.com/thunlp/PromptPapers#papers)
- [Survey Paper](https://arxiv.org/abs/2402.07927)
"""
)
st.sidebar.divider()
# copyright
st.sidebar.markdown(
"""
© 2025 Alexander Lammers (DATANOMIQ GmbH)
\nAll rights reserved.
"""
)
# Display content based on selected section
if section == "Introduction":
st.write("""
## Introduction
Welcome to the Prompt Engineering Workshop! This workshop will help you learn effective techniques for working with AI language models.
### What is Prompt Engineering?
Prompt engineering is the practice of crafting effective inputs (prompts) for AI models to get desired outputs.
Good prompts can significantly improve the quality, accuracy, and relevance of AI responses.
### Techniques Overview
**1. Basic Prompting**
Simple text inputs that rely on the model's pretrained capabilities with minimal structure.
**2. Instruction-based Prompting**
Explicit directions about what you want the AI to do, providing more control over format and content.
**3. Zero/One/Few-Shot Prompting**
- Zero-shot: No examples provided
- One-shot: One example provided before asking for a similar task
- Few-shot: Multiple examples to establish a pattern
**4. Chain-of-Thought Reasoning**
Guiding the model to break down complex problems into logical steps, improving performance on multi-step reasoning.
**5. Self-Consistency Techniques**
Generating multiple independent solutions and finding consensus to increase reliability.
**6. Tree of Thoughts**
Exploring multiple reasoning paths in parallel to approach complex problems from different angles.
**7. ReAct Framework**
Combining reasoning with actions to solve problems by interacting with external tools.
**8. Real-world Applications**
Practical examples combining multiple techniques for effective solutions to complex problems.
""")
elif section == "Settings":
st.write("## Model Configuration Settings")
st.write("""
Configure how the AI model generates responses by adjusting these parameters.
Changes made here will affect all examples throughout the workshop.
""")
with st.expander("Understanding Model Parameters", expanded=False):
st.write("""
### Temperature
**Temperature** controls the randomness of the model's output. Higher values (closer to 1.0) make the
output more random and creative, while lower values (closer to 0.0) make it more focused and deterministic.
- **Low temperature (0.1-0.3)**: Good for factual responses, classification tasks, or when you need
consistent, predictable outputs.
- **Medium temperature (0.4-0.7)**: Balanced between creativity and coherence, suitable for most general-purpose tasks.
- **High temperature (0.8-1.0)**: Produces more diverse and creative responses, good for brainstorming,
creative writing, or generating multiple alternatives.
### Top P (Nucleus Sampling)
**Top P** determines how the model selects words when generating text. It filters the output by keeping only
the tokens whose cumulative probability exceeds the Top P value.
- Lower values (e.g., 0.5) restrict the model to higher-probability words, making output more conservative
- Higher values (e.g., 0.95) allow more diversity in word choice
- Setting Top P = 1.0 means no filtering is applied
### Maximum Tokens
**Maximum tokens** limits how long the model's response can be. One token is roughly 4 characters or 3/4 of a word.
Setting this properly helps to:
- Prevent overly lengthy responses
- Reduce API costs for production applications
- Control response time
For this workshop, we recommend values between 200-1000 tokens.
### Model Selection
You can choose from different AI models:
**OpenAI Models**:
- **GPT-3.5 Turbo**: Balances capability and speed, suitable for most general tasks.
- **GPT-4**: Higher capability especially for complex reasoning, coding, and creative tasks.
**Anthropic Models**:
- **Claude 3 Sonnet**: Balanced performance, suitable for most tasks.
- **Claude 3 Haiku**: Faster and more cost-effective for simpler tasks.
- **Claude 3 Opus**: Maximum capability for the most complex tasks.
Each model has different strengths, weaknesses, and pricing. Try different models for the same prompt to see how they compare!
""")
col1, col2 = st.columns(2)
with col1:
# Temperature slider
temp = st.slider(
"Temperature:",
min_value=0.0,
max_value=1.0,
value=st.session_state.temperature,
step=0.05,
help="Controls randomness: Lower values are more deterministic, higher values more creative",
)
st.session_state.temperature = temp
# Top P slider
top_p = st.slider(
"Top P (Nucleus Sampling):",
min_value=0.0,
max_value=1.0,
value=st.session_state.top_p,
step=0.05,
help="Controls diversity: 1.0 = consider all options, 0.5 = consider only the most likely options",
)
st.session_state.top_p = top_p
with col2:
# Max tokens slider
max_tokens = st.slider(
"Maximum Tokens:",
min_value=50,
max_value=1000,
value=st.session_state.max_tokens,
step=50,
help="Maximum length of the response",
)
st.session_state.max_tokens = max_tokens
# Updated model selection with provider groups
provider = st.selectbox(
"Provider:",
["OpenAI", "Anthropic"],
index=0 if "gpt" in st.session_state.model else 1,
help="Select the AI provider",
)
if provider == "OpenAI":
model = st.selectbox(
"OpenAI Model:",
["gpt-3.5-turbo", "gpt-4"],
index=0 if st.session_state.model == "gpt-3.5-turbo" else 1,
help="Select which OpenAI model to use",
)
else:
model = st.selectbox(
"Claude Model:",
[
"claude-3-5-sonnet-latest",
"claude-3-haiku-20240307",
"claude-3-opus-latest",
],
index=0,
help="Select which Anthropic Claude model to use",
)
st.session_state.model = model
st.session_state.provider = provider.lower()
# Add API key settings
with st.expander("API Keys", expanded=False):
st.info(
"⚠️ **Security Note**: API keys are only stored for your current session and will be cleared when you close the browser or refresh the page."
)
openai_key = st.text_input(
"OpenAI API Key:",
type="password",
value=st.session_state.openai_api_key,
help="Your OpenAI API key (stored in session only)",
)
if openai_key:
st.session_state.openai_api_key = openai_key
anthropic_key = st.text_input(
"Anthropic API Key:",
type="password",
value=st.session_state.anthropic_api_key,
help="Your Anthropic API key (stored in session only)",
)
if anthropic_key:
st.session_state.anthropic_api_key = anthropic_key
# Add clear keys button
col1, col2 = st.columns(2)
with col1:
if st.button("Clear OpenAI Key"):
st.session_state.openai_api_key = ""
st.rerun()
with col2:
if st.button("Clear Anthropic Key"):
st.session_state.anthropic_api_key = ""
st.rerun()
st.divider()
st.subheader("Try Different Settings")
test_prompt = st.text_area(
"Test prompt:", "Write a short paragraph about artificial intelligence."
)
if st.button("Test Settings"):
messages = [{"role": "user", "content": test_prompt}]
# Use the session state values
response = pe_demo.get_completion(
messages,
temperature=st.session_state.temperature,
max_tokens=st.session_state.max_tokens,
top_p=st.session_state.top_p,
model=st.session_state.model,
)
st.write("### Response:")
st.write(response)
st.info(f"""
This response was generated using:
- Provider: {st.session_state.provider.title()}
- Model: {st.session_state.model}
- Temperature: {st.session_state.temperature}
- Top P: {st.session_state.top_p}
- Max Tokens: {st.session_state.max_tokens}
""")
# Settings usage reminder
st.success("""
✅ Your settings have been saved and will be used for all examples in the workshop.
Feel free to return to this page anytime to adjust the parameters.
""")
elif section == "1. Basic Prompting":
st.write("## Basic Prompting Techniques")
st.write(
"Simple text completions that demonstrate how models respond to basic prompts."
)
with st.expander("About Basic Prompting", expanded=False):
st.write("""
### Basic Prompting
Basic prompting involves providing a simple text input to the AI and allowing it to complete or respond to that text.
These prompts have minimal structure and rely on the model's pretrained capabilities.
#### Key characteristics:
- **Simple structure**: Uses natural language without specialized formatting
- **Open-ended**: Often allows the model to determine the appropriate response format
- **Leverages pretrained knowledge**: Relies on the model's existing training rather than explicit instructions
#### Common use cases:
- Text completion exercises
- Simple question answering
- Generating creative content
- Exploratory interactions to test model capabilities
#### Strengths:
- Quick and easy to implement
- Works well for straightforward tasks
- Requires minimal prompt engineering effort
#### Limitations:
- Less control over response format and content
- May produce inconsistent or unpredictable outputs
- Not ideal for complex or structured tasks
#### Tips for effective basic prompts:
- Be clear and concise
- Avoid ambiguous phrasing
- Use proper spelling and grammar
- Start with clear context if needed
""")
# Initialize chat history in session state if it doesn't exist
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
# Add a key for managing input field resets
if "input_key" not in st.session_state:
st.session_state.input_key = 0
st.subheader("Try it yourself - Conversational Mode:")
# Display previous chat messages
chat_container = st.container()
with chat_container:
for i, message in enumerate(st.session_state.chat_history):
if message["role"] == "user":
st.markdown(f"**You:** {message['content']}")
else:
st.markdown(f"**AI:** {message['content']}")
# Add a small divider between messages except the last one
if i < len(st.session_state.chat_history) - 1:
st.markdown("---")
# Input for new message - using a unique key that changes when we want to clear the field
current_input_key = f"user_message_{st.session_state.input_key}"
user_message = st.text_area("Your message:", key=current_input_key, height=100)
col1, col2 = st.columns([1, 5])
with col1:
if st.button("Send", key="send_message"):
if user_message:
# Add user message to chat history
st.session_state.chat_history.append(
{"role": "user", "content": user_message}
)
# Get AI response
messages = [
{"role": msg["role"], "content": msg["content"]}
for msg in st.session_state.chat_history
]
response = pe_demo.get_completion(
messages,
temperature=st.session_state.temperature,
max_tokens=st.session_state.max_tokens,
top_p=st.session_state.top_p,
model=st.session_state.model,
)
# Add AI response to chat history
st.session_state.chat_history.append(
{"role": "assistant", "content": response}
)
# Increment the input key to create a fresh text area (clearing the previous input)
st.session_state.input_key += 1
# Rerun to update the display with the new messages
st.rerun()
with col2:
if st.button("Clear Chat", key="clear_chat"):
st.session_state.chat_history = []
st.rerun()
elif section == "2. Instruction-based Prompting":
st.write("## Instruction-based Prompting")
st.write("Provide clear instructions to guide the AI's response.")
with st.expander("About Instruction Prompting", expanded=False):
st.write("""
### Instruction-based Prompting
Instruction-based prompting involves giving the AI explicit directions about what you want it to do.
This approach provides more control over the format and content of the response.
#### Key characteristics:
- **Explicit directions**: Clear instructions on task, format, and expected output
- **Task-oriented**: Focuses on specific actions for the model to perform
- **Structured approach**: Often includes formatting guidelines or output constraints
#### Common use cases:
- Data transformation tasks
- Content classification
- Structured information extraction
- Format conversion (e.g., text to JSON)
- Specific analytical tasks
#### Best practices:
- **Be specific**: Clearly state what you want the model to do
- **Define scope**: Set boundaries for the response
- **Format instructions**: Specify how the answer should be structured
- **Use action verbs**: Start with "Classify," "Summarize," "List," etc.
- **Provide examples**: Show the expected output format when needed
#### Advanced techniques:
- **Multi-step instructions**: Break down complex tasks into sequential steps
- **Conditional instructions**: "If X applies, do Y; otherwise, do Z"
- **Reference documents**: "Based on the text below, answer the following questions..."
#### Example structure:
```
Task: [Clear description of what to do]
Input: [Content to analyze/transform]
Format: [Instructions about how to structure the output]
Additional constraints: [Any other requirements]
```
""")
st.subheader("Try it yourself:")
task = st.text_area(
"Enter task:",
"Classify the following text as positive, negative, or neutral.",
)
text = st.text_area(
"Enter text to analyze:",
"This movie was absolutely fantastic! The acting was superb and the plot kept me engaged throughout.",
)
format_instructions = st.text_area(
"Format instructions (optional):",
"Output only the classification without explanation.",
)
if st.button("Generate Response", key="instruction"):
prompt = f"Task: {task}\nText: {text}\n"
if format_instructions:
prompt += f"Format: {format_instructions}\n"
messages = [{"role": "user", "content": prompt}]
response = pe_demo.get_completion(
messages,
temperature=st.session_state.temperature,
max_tokens=st.session_state.max_tokens,
top_p=st.session_state.top_p,
model=st.session_state.model,
)
pe_demo.display_result(prompt, response, "Instruction-based Prompting")
elif section == "3. Zero/One/Few-Shot Prompting":
st.write("## Shot-based Prompting Techniques")
st.write("Provide examples to guide the model's understanding of the task.")
shot_type = st.radio("Select shot type:", ["Zero-shot", "One-shot", "Few-shot"])
with st.expander("About Shot-based Prompting", expanded=False):
st.write("""
### Shot-based Prompting Techniques
Shot-based prompting refers to providing a varying number of examples before asking the model to perform a task.
#### Zero-shot learning
No examples are provided; the model must perform a task based solely on instructions.
- **Characteristics**: Relies entirely on pretrained knowledge
- **Best for**: Simple tasks within the model's training domain
- **Example**: "Classify this movie review as positive or negative: [review]"
- **Limitations**: Less reliable for complex, ambiguous, or domain-specific tasks
#### One-shot learning
One example is provided before asking the model to perform a similar task.
- **Characteristics**: Uses a single demonstration to establish the pattern
- **Best for**: When you need to clarify task format but have limited context space
- **Example**:
```
Input: "I loved this movie!"
Classification: Positive
Input: "Worst experience ever."
Classification:
```
- **Benefits**: Significantly improves performance over zero-shot for many tasks
#### Few-shot learning
Multiple examples are provided to establish a clear pattern.
- **Characteristics**: Uses 2+ examples to demonstrate the desired behavior
- **Best for**: Complex tasks, unusual formats, or domain-specific knowledge
- **Example**:
```
Input: "Great product, fast shipping."
Sentiment: Positive
Category: Customer Service
Input: "Item arrived damaged and customer service was unhelpful."
Sentiment: Negative
Category: Product Quality, Customer Service
Input: "The price was reasonable but delivery took longer than expected."
Sentiment:
Category:
```
- **Benefits**: Most reliable approach for consistent, formatted responses
#### Implementation strategies:
- **Diverse examples**: Include edge cases and various formats
- **Ordered complexity**: Arrange examples from simple to complex
- **Balance**: For classification tasks, include examples from all classes
- **Format consistency**: Maintain the same structure across examples
""")
st.subheader("Sentiment Classification Example")
if shot_type == "Zero-shot":
prompt = st.text_area(
"Zero-shot prompt:",
'Classify the sentiment of the following text as positive, negative, or neutral.\n\nText: "The weather today is quite unpredictable, but I\'m managing to get things done."\n\nSentiment:',
)
elif shot_type == "One-shot":
prompt = st.text_area(
"One-shot prompt:",
'Classify the sentiment of the following text as positive, negative, or neutral.\n\nExample:\nText: "I love spending time in nature, it\'s so peaceful."\nSentiment: Positive\n\nNow classify this:\nText: "The weather today is quite unpredictable, but I\'m managing to get things done."\nSentiment:',
)
else: # Few-shot
prompt = st.text_area(
"Few-shot prompt:",
'Classify the sentiment of the following text as positive, negative, or neutral.\n\nExamples:\nText: "I love spending time in nature, it\'s so peaceful."\nSentiment: Positive\n\nText: "This traffic jam is really frustrating and making me late."\nSentiment: Negative\n\nText: "The meeting was okay, nothing particularly exciting happened."\nSentiment: Neutral\n\nNow classify this:\nText: "The weather today is quite unpredictable, but I\'m managing to get things done."\nSentiment:',
)
if st.button("Generate Response", key="shot"):
messages = [{"role": "user", "content": prompt}]
response = pe_demo.get_completion(
messages,
temperature=st.session_state.temperature,
max_tokens=st.session_state.max_tokens,
top_p=st.session_state.top_p,
model=st.session_state.model,
)
pe_demo.display_result(prompt, response, f"{shot_type} Prompting")
elif section == "4. Chain-of-Thought Reasoning":
st.write("## Chain-of-Thought Reasoning")
st.write("Guide the model to break down complex problems into logical steps.")
with st.expander("About Chain-of-Thought Reasoning", expanded=False):
st.write("""
### Chain-of-Thought (CoT) Reasoning
Chain-of-thought (CoT) prompting encourages the model to generate a series of intermediate reasoning steps
that lead to a final answer. This technique significantly improves performance on complex tasks that require
multi-step reasoning, such as math word problems, logical reasoning, and complex analyses.
#### Key principles:
- **Externalized reasoning**: Makes the model's thinking process visible
- **Step-by-step approach**: Breaks complex problems into manageable parts
- **Reduced error rates**: Helps prevent logical mistakes and oversights
- **Self-verification**: Allows the model to check its work during the process
#### Implementation methods:
1. **Prompt-based CoT**
- Add phrases like "Let's think about this step-by-step" or "Let's solve this systematically"
- Example: "Problem: [problem description]. Let's solve this step-by-step:"
2. **Few-shot CoT**
- Provide examples of step-by-step reasoning for similar problems
- Demonstrate the reasoning process you want the model to follow
3. **Generated CoT**
- First ask the model to generate reasoning steps
- Then ask it to use those steps to provide a final answer
#### Research findings:
- CoT improves performance on mathematical problems by 20-40% on standard benchmarks
- Particularly effective for problems requiring multi-step logical reasoning
- Works best with more capable models (e.g., GPT-4, Claude, etc.)
- Can be combined with techniques like Self-Consistency for even better results
#### Best practices:
- Explicitly request intermediate reasoning steps
- Break down complex problems into clear stages
- For math problems, encourage calculation details
- For logical reasoning, promote consideration of different cases
- Allow sufficient token space for detailed reasoning
- Instruct the model to verify its calculations when appropriate
#### Example structure:
```
Problem: [Complex problem]
Let's solve this step-by-step:
Step 1: [Understanding the problem]
Step 2: [Identifying relevant information]
Step 3: [Applying relevant formulas/concepts]
Step 4: [Performing calculations/reasoning]
Step 5: [Verifying the solution]
Final answer: [Conclusion]
```
""")
st.subheader("Try it yourself:")
cot_type = st.radio(
"Select reasoning type:",
["Basic", "Complex Business Scenario", "Custom Problem"],
)
if cot_type == "Basic":
problem = st.text_area(
"Problem to solve:",
"A store has 23 apples. They sell 8 apples in the morning and 5 apples in the afternoon. "
"Then they receive a delivery of 12 more apples. How many apples do they have now?",
)
elif cot_type == "Complex Business Scenario":
problem = st.text_area(
"Business scenario:",
"A company is planning to launch a new product. They need to decide between two marketing strategies:\n\n"
"Strategy A:\n- Initial cost: $100,000\n- Expected monthly revenue: $25,000\n"
"- Monthly operational costs: $8,000\n\n"
"Strategy B:\n- Initial cost: $150,000\n- Expected monthly revenue: $35,000\n"
"- Monthly operational costs: $12,000\n\n"
"Which strategy will be more profitable after 18 months, and by how much?",
)
else: # Custom Problem
problem = st.text_area(
"Enter your own problem:",
"Write your problem here. Make sure it requires multi-step reasoning.",
)
if st.button("Generate Step-by-Step Solution", key="cot"):
prompt = f"Problem: {problem}\n\nLet's solve this step-by-step:"
messages = [{"role": "user", "content": prompt}]
response = pe_demo.get_completion(
messages,
temperature=st.session_state.temperature,
max_tokens=st.session_state.max_tokens,
top_p=st.session_state.top_p,
model=st.session_state.model,
)
pe_demo.display_result(prompt, response, "Chain-of-Thought Reasoning")
elif section == "5. Self-Consistency Techniques":
st.write("## Self-Consistency Techniques")
st.write("Generate multiple solution paths and find consensus among them.")
with st.expander("About Self-Consistency", expanded=False):
st.write("""
### Self-Consistency Techniques
Self-consistency involves generating multiple independent solutions to a problem and then selecting
the most consistent answer. This technique increases reliability for complex reasoning tasks by
mitigating the effects of randomness in the model's responses.
#### How it works:
1. **Multiple generations**: Solve the same problem multiple times with different sampling temperatures
2. **Path diversity**: Each solution may take a different reasoning approach
3. **Answer extraction**: Extract the final answer from each solution path
4. **Majority voting**: Take the most frequent answer as the final result
#### Technical implementation:
- Generate N different responses (typically 5-20) using chain-of-thought prompting
- Vary temperature/sampling parameters across generations to encourage diversity
- Parse the final answers programmatically
- Apply a consensus mechanism (typically majority voting)
#### When to use self-consistency:
- Complex mathematical or logical problems
- Tasks where accuracy is critical
- Problems with potential for different valid solution methods
- When resources allow for multiple API calls
- For high-stakes applications requiring confidence in results
#### Research findings:
- Improves accuracy on GSM8K math problems by 10-15% over standard CoT