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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
import json
|
| 6 |
+
import textwrap
|
| 7 |
+
import time # For handling potential rate limits
|
| 8 |
+
|
| 9 |
+
# Load environment variables
|
| 10 |
+
load_dotenv()
|
| 11 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 12 |
+
|
| 13 |
+
# Configure Generative AI model
|
| 14 |
+
if GOOGLE_API_KEY:
|
| 15 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 16 |
+
model = genai.GenerativeModel('gemini-pro') # Or specify your model
|
| 17 |
+
else:
|
| 18 |
+
st.error(
|
| 19 |
+
"Google AI Studio API key not found. Please add it to your .env file. "
|
| 20 |
+
"You can obtain an API key from https://makersuite.google.com/."
|
| 21 |
+
)
|
| 22 |
+
st.stop() # Stop if the API key is missing
|
| 23 |
+
|
| 24 |
+
st.title("Prompt Engineering Playground")
|
| 25 |
+
st.subheader("Experiment with Prompting Techniques")
|
| 26 |
+
|
| 27 |
+
# Sidebar for explanations and resources
|
| 28 |
+
with st.sidebar:
|
| 29 |
+
st.header("Prompting Concepts")
|
| 30 |
+
st.markdown(
|
| 31 |
+
"""
|
| 32 |
+
This app demonstrates various prompt engineering techniques. Explore how different prompts
|
| 33 |
+
affect the output of a large language model.
|
| 34 |
+
"""
|
| 35 |
+
)
|
| 36 |
+
st.subheader("Key Techniques:")
|
| 37 |
+
st.markdown(
|
| 38 |
+
"""
|
| 39 |
+
- **Clear Instructions**: Provide explicit and unambiguous directions.
|
| 40 |
+
- **Delimiters**: Use special characters to separate input parts.
|
| 41 |
+
- **Structured Output**: Request output in a specific format (JSON).
|
| 42 |
+
- **Assumption Checking**: Verify conditions in the input.
|
| 43 |
+
- **Few-Shot Prompting**: Provide input-output examples.
|
| 44 |
+
- **Temperature Control**: Adjust output randomness.
|
| 45 |
+
- **Chain of Thought (CoT)**: Elicit step-by-step reasoning.
|
| 46 |
+
- **Prompt Templates**: Employ pre-defined prompt structures.
|
| 47 |
+
- **System Prompt**: Influence the model's overall behavior.
|
| 48 |
+
- **Retrieval Augmentation**: Provide external knowledge.
|
| 49 |
+
"""
|
| 50 |
+
)
|
| 51 |
+
st.subheader("Important Considerations:")
|
| 52 |
+
st.markdown(
|
| 53 |
+
"""
|
| 54 |
+
- **Context Window**: Be mindful of the maximum input length the model can handle.
|
| 55 |
+
- **Tokenization**: Understand how text is broken down into tokens.
|
| 56 |
+
- **Bias and Safety**: Be aware of potential biases in the model's output and take steps to mitigate them.
|
| 57 |
+
- **Rate Limits**: The API has usage limits. The app includes basic handling, but monitor your usage.
|
| 58 |
+
"""
|
| 59 |
+
)
|
| 60 |
+
st.subheader("Resources:")
|
| 61 |
+
st.markdown(
|
| 62 |
+
"""
|
| 63 |
+
- [Google Generative AI Course](https://developers.google.com/learn/generative-ai)
|
| 64 |
+
- [Prompt Engineering Guide](https://www.promptingguide.ai/)
|
| 65 |
+
- [Google AI Platform](https://cloud.google.com/ai-platform)
|
| 66 |
+
"""
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# --- Helper Functions ---
|
| 70 |
+
def code_block(text: str, language: str = "text") -> None:
|
| 71 |
+
"""Displays text as a formatted code block in Streamlit."""
|
| 72 |
+
st.markdown(f"```{language}\n{text}\n```", unsafe_allow_html=True)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def display_response(response: genai.GenerateContentResponse) -> None:
|
| 76 |
+
"""Displays the model's response, handling text, and error cases."""
|
| 77 |
+
if response.text:
|
| 78 |
+
st.subheader("Generated Response:")
|
| 79 |
+
st.markdown(response.text)
|
| 80 |
+
elif response.prompt_feedback:
|
| 81 |
+
st.warning(f"Prompt Feedback: {response.prompt_feedback}")
|
| 82 |
+
else:
|
| 83 |
+
st.error("No response or feedback received from the model.")
|
| 84 |
+
st.error(f"Full response object: {response}") # Print the full response for debugging
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def generate_with_retry(prompt: str, generation_config: genai.types.GenerationConfig, max_retries: int = 3, delay: int = 5) -> genai.GenerateContentResponse:
|
| 88 |
+
"""
|
| 89 |
+
Generates content with retry logic to handle potential API errors (e.g., rate limits).
|
| 90 |
+
|
| 91 |
+
Args:
|
| 92 |
+
prompt: The prompt string.
|
| 93 |
+
generation_config: The generation configuration.
|
| 94 |
+
max_retries: Maximum number of retries.
|
| 95 |
+
delay: Delay in seconds between retries.
|
| 96 |
+
|
| 97 |
+
Returns:
|
| 98 |
+
The generated response.
|
| 99 |
+
|
| 100 |
+
Raises:
|
| 101 |
+
Exception: If the generation fails after maximum retries.
|
| 102 |
+
"""
|
| 103 |
+
for i in range(max_retries):
|
| 104 |
+
try:
|
| 105 |
+
response = model.generate_content(prompt, generation_config=generation_config)
|
| 106 |
+
return response # Return the response if successful
|
| 107 |
+
except Exception as e:
|
| 108 |
+
st.warning(f"Error during generation (attempt {i + 1}/{max_retries}): {e}")
|
| 109 |
+
if i < max_retries - 1:
|
| 110 |
+
st.info(f"Retrying in {delay} seconds...")
|
| 111 |
+
time.sleep(delay) # Use time.sleep for retrying
|
| 112 |
+
else:
|
| 113 |
+
raise # Re-raise the exception after the last retry
|
| 114 |
+
raise Exception("Failed to generate content after maximum retries") #Should never reach here.
|
| 115 |
+
|
| 116 |
+
# --- Prompting Techniques Section ---
|
| 117 |
+
st.header("Experiment with Prompts")
|
| 118 |
+
|
| 119 |
+
prompt_technique = st.selectbox(
|
| 120 |
+
"Choose a Prompting Technique:",
|
| 121 |
+
[
|
| 122 |
+
"Simple Instruction",
|
| 123 |
+
"Using Delimiters",
|
| 124 |
+
"Structured Output (JSON)",
|
| 125 |
+
"Checking Assumptions",
|
| 126 |
+
"Few-Shot Prompting",
|
| 127 |
+
"Temperature Control",
|
| 128 |
+
"Chain of Thought (CoT)",
|
| 129 |
+
"Prompt Templates",
|
| 130 |
+
"System Prompt",
|
| 131 |
+
"Retrieval Augmentation" # Added Retrieval Augmentation
|
| 132 |
+
],
|
| 133 |
+
index=0,
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
prompt_input = st.text_area("Enter your prompt here:", height=150)
|
| 137 |
+
|
| 138 |
+
# Temperature slider (common to several techniques)
|
| 139 |
+
temperature = st.slider(
|
| 140 |
+
"Temperature:",
|
| 141 |
+
min_value=0.0,
|
| 142 |
+
max_value=1.0,
|
| 143 |
+
value=0.7,
|
| 144 |
+
step=0.01,
|
| 145 |
+
help="Controls the randomness of the output. Lower values are more deterministic; higher values are more creative.",
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
if st.button("Generate Response"):
|
| 149 |
+
if not prompt_input:
|
| 150 |
+
st.warning("Please enter a prompt.")
|
| 151 |
+
else:
|
| 152 |
+
with st.spinner("Generating..."):
|
| 153 |
+
generation_config = genai.types.GenerationConfig(temperature=temperature) # Create it once
|
| 154 |
+
|
| 155 |
+
try:
|
| 156 |
+
if prompt_technique == "Using Delimiters":
|
| 157 |
+
delimiter = st.text_input("Enter your delimiter (e.g., ###, ---):", "###")
|
| 158 |
+
processed_prompt = f"Here is the input, with parts separated by '{delimiter}':\n{prompt_input}\n Please process each part separately."
|
| 159 |
+
response = generate_with_retry(processed_prompt, generation_config)
|
| 160 |
+
display_response(response)
|
| 161 |
+
|
| 162 |
+
elif prompt_technique == "Structured Output (JSON)":
|
| 163 |
+
json_format = st.text_input(
|
| 164 |
+
"Describe the desired JSON format (e.g., {'name': str, 'age': int}):",
|
| 165 |
+
"{'key1': type, 'key2': type}",
|
| 166 |
+
)
|
| 167 |
+
processed_prompt = f"Please provide the output in JSON format, following this structure: {json_format}. Here is the information: {prompt_input}"
|
| 168 |
+
response = generate_with_retry(processed_prompt, generation_config)
|
| 169 |
+
try:
|
| 170 |
+
json_output = json.loads(response.text)
|
| 171 |
+
st.subheader("Generated JSON Output:")
|
| 172 |
+
st.json(json_output)
|
| 173 |
+
except json.JSONDecodeError:
|
| 174 |
+
st.error("Failed to decode JSON. Raw response:")
|
| 175 |
+
code_block(response.text, "json")
|
| 176 |
+
|
| 177 |
+
elif prompt_technique == "Checking Assumptions":
|
| 178 |
+
assumption = st.text_input(
|
| 179 |
+
"State the assumption you want the model to check:", "The text is about a historical event."
|
| 180 |
+
)
|
| 181 |
+
processed_prompt = f"First, check if the following assumption is true: '{assumption}'. Then, answer the prompt: {prompt_input}"
|
| 182 |
+
response = generate_with_retry(processed_prompt, generation_config)
|
| 183 |
+
display_response(response)
|
| 184 |
+
|
| 185 |
+
elif prompt_technique == "Few-Shot Prompting":
|
| 186 |
+
example1_input = st.text_area("Example 1 Input:", height=50)
|
| 187 |
+
example1_output = st.text_area("Example 1 Output:", height=50)
|
| 188 |
+
example2_input = st.text_area("Example 2 Input (Optional):", height=50)
|
| 189 |
+
example2_output = st.text_area("Example 2 Output (Optional):", height=50)
|
| 190 |
+
|
| 191 |
+
processed_prompt = "Here are some examples:\n"
|
| 192 |
+
processed_prompt += f"Input: {example1_input}\nOutput: {example1_output}\n"
|
| 193 |
+
if example2_input and example2_output:
|
| 194 |
+
processed_prompt += f"Input: {example2_input}\nOutput: {example2_output}\n"
|
| 195 |
+
processed_prompt += f"\nNow, answer the following:\nInput: {prompt_input}"
|
| 196 |
+
|
| 197 |
+
response = generate_with_retry(processed_prompt, generation_config)
|
| 198 |
+
display_response(response)
|
| 199 |
+
|
| 200 |
+
elif prompt_technique == "Temperature Control":
|
| 201 |
+
# The temperature slider is already handled
|
| 202 |
+
response = generate_with_retry(prompt_input, generation_config)
|
| 203 |
+
display_response(response)
|
| 204 |
+
|
| 205 |
+
elif prompt_technique == "Chain of Thought (CoT)":
|
| 206 |
+
cot_prompt = f"Let's think step by step. {prompt_input}"
|
| 207 |
+
response = generate_with_retry(cot_prompt, generation_config)
|
| 208 |
+
display_response(response)
|
| 209 |
+
|
| 210 |
+
elif prompt_technique == "Prompt Templates":
|
| 211 |
+
template_name = st.selectbox(
|
| 212 |
+
"Choose a template:",
|
| 213 |
+
["None", "Question and Answer", "Summarization", "Code Generation"],
|
| 214 |
+
index=0,
|
| 215 |
+
)
|
| 216 |
+
if template_name == "Question and Answer":
|
| 217 |
+
processed_prompt = f"Answer the following question: {prompt_input}"
|
| 218 |
+
elif template_name == "Summarization":
|
| 219 |
+
processed_prompt = f"Summarize the following text: {prompt_input}"
|
| 220 |
+
elif template_name == "Code Generation":
|
| 221 |
+
language = st.text_input("Specify the programming language", "Python")
|
| 222 |
+
processed_prompt = f"Generate {language} code for the following: {prompt_input}"
|
| 223 |
+
else:
|
| 224 |
+
processed_prompt = prompt_input
|
| 225 |
+
|
| 226 |
+
response = generate_with_retry(processed_prompt, generation_config)
|
| 227 |
+
display_response(response)
|
| 228 |
+
|
| 229 |
+
elif prompt_technique == "System Prompt":
|
| 230 |
+
system_prompt_text = st.text_area(
|
| 231 |
+
"Enter system prompt:", "You are a helpful and informative assistant.", height=100
|
| 232 |
+
)
|
| 233 |
+
user_prompt = f"{prompt_input}" # User's input
|
| 234 |
+
|
| 235 |
+
response = generate_with_retry(
|
| 236 |
+
contents=[
|
| 237 |
+
genai.Content(role="system", parts=[genai.Part(text=system_prompt_text)]), #send system prompt
|
| 238 |
+
genai.Content(role="user", parts=[genai.Part(text=user_prompt)]), # send user prompt
|
| 239 |
+
],
|
| 240 |
+
generation_config=generation_config,
|
| 241 |
+
)
|
| 242 |
+
display_response(response)
|
| 243 |
+
|
| 244 |
+
elif prompt_technique == "Retrieval Augmentation":
|
| 245 |
+
context_text = st.text_area("Enter context text (knowledge base):",
|
| 246 |
+
"This is the context the model can use to answer the question.",
|
| 247 |
+
height=150)
|
| 248 |
+
processed_prompt = f"Given the following context: \n\n {context_text} \n\n Answer the following question: {prompt_input}"
|
| 249 |
+
response = generate_with_retry(processed_prompt, generation_config)
|
| 250 |
+
display_response(response)
|
| 251 |
+
|
| 252 |
+
else: # Simple Instruction
|
| 253 |
+
response = generate_with_retry(prompt_input, generation_config)
|
| 254 |
+
display_response(response)
|
| 255 |
+
|
| 256 |
+
except Exception as e:
|
| 257 |
+
st.error(f"An error occurred: {e}")
|