File size: 5,016 Bytes
5c25aae
 
f6ae6f7
 
5c25aae
 
f6ae6f7
5c25aae
 
f6ae6f7
5c25aae
 
f6ae6f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0bcc76
 
 
f6ae6f7
d0bcc76
 
 
f6ae6f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: Prompt Optimizer
emoji: 
colorFrom: purple
colorTo: yellow
sdk: gradio
sdk_version: 5.29.0
app_file: app.py
pinned: false
license: mit
---

# ✨ Prompt Optimizer

Transform basic prompts into powerful, well-structured instructions that get better results from AI language models.

## 🎯 What This Does

1. **Input** a rough or basic prompt
2. **AI analyzes** its weaknesses (vagueness, missing context, unclear format)
3. **Receive** an optimized version with detailed explanations of changes

## 🔧 Optimization Techniques

The optimizer applies 5 key prompt engineering techniques:

| Technique | What It Does | Example |
|-----------|--------------|---------|
| **Clarity & Specificity** | Replaces vague terms with concrete details | "write about dogs" → "write about the behavioral characteristics of Golden Retrievers" |
| **Role/Persona Framing** | Adds expert context | Adds "You are a veterinarian..." |
| **Output Format Instructions** | Specifies structure and length | "Provide as a numbered list with 5 items" |
| **Constraints & Guardrails** | Sets boundaries and tone | "Use professional tone. Exclude personal anecdotes." |
| **Task Decomposition** | Breaks complex tasks into steps | Adds "First... Then... Finally..." |

## 📊 Example

**Before (Basic Prompt):**
```
write about dogs
```

**After (Optimized Prompt):**
```
You are a canine behaviorist. Write a detailed, informative article about 
the behavior, health, and nutritional needs of dogs, including their social 
structure, common health issues, and dietary requirements. 

Please provide your response in a formal tone, using paragraphs, and ensure 
the article is approximately 500 words. Include an introduction, three main 
sections (behavior, health, and nutrition), and a conclusion. 

Exclude personal anecdotes and focus on providing factual information.
```

## 🏗️ Technical Architecture
```
User Prompt

Groq API (Llama 3.3 70B)

Structured Analysis
    ├── Weakness Identification
    ├── Optimization Application
    └── Change Explanations

Optimized Prompt + Explanations
```

## 🔬 Technical Stack

| Component | Technology | Purpose |
|-----------|------------|---------|
| **LLM Backend** | Groq API | Fast inference |
| **Model** | Llama 3.3 70B Versatile | High-quality optimization |
| **Interface** | Gradio | Interactive web UI |
| **Parsing** | Structured prompting | Reliable output format |

## 📚 Research Foundation

This tool implements techniques from established prompt engineering resources:

- [OpenAI Prompt Engineering Guide](https://platform.openai.com/docs/guides/prompt-engineering)
- [Anthropic Claude Documentation](https://docs.anthropic.com/claude/docs/prompt-engineering)
- [Google's Prompt Design Strategies](https://ai.google.dev/docs/prompt_best_practices)

## 🛠️ Development Challenges

### Challenge 1: Consistent Output Parsing
**Problem:** LLM responses varied in format, making extraction unreliable.

**Solution:** Designed a strict response format with clear section markers (`**ANALYSIS:**`, `**OPTIMIZED PROMPT:**`, `**CHANGES MADE:**`) and implemented fallback parsing.

### Challenge 2: Over-Optimization
**Problem:** Initial versions over-engineered simple prompts, adding unnecessary complexity.

**Solution:** Added instruction "Don't over-engineer simple prompts - match complexity to the task" to the system prompt.

### Challenge 3: Preserving User Intent
**Problem:** Optimizer sometimes changed the core intent of the original prompt.

**Solution:** Added explicit rule "Preserve the user's original intent completely" and examples demonstrating intent preservation.

## 🚀 Local Development
```bash
# Clone the repository
git clone https://huggingface.co/spaces/Nav772/prompt-optimizer

# Set your Groq API key
export GROQ_API_KEY="your-api-key-here"

# Install dependencies
pip install -r requirements.txt

# Run locally
python app.py
```

## ⚙️ Environment Variables

This Space requires the following secret:

| Variable | Description | Required |
|----------|-------------|----------|
| `GROQ_API_KEY` | Groq API key for LLM access | Yes |

Get a free API key at [console.groq.com](https://console.groq.com/)

## 📝 Limitations

- **Context window:** Very long prompts may be truncated
- **Domain expertise:** General-purpose optimization; specialized domains may need manual refinement
- **Language:** Optimized for English prompts

## 👤 Author

**[Nav772](https://huggingface.co/Nav772)** — Built as part of an AI Engineering portfolio demonstrating prompt engineering expertise.

## 📚 Related Projects

- [Audio Language Translator](https://huggingface.co/spaces/Nav772/audio-language-translator) — Multimodal AI
- [LLM Decoding Strategy Analyzer](https://huggingface.co/spaces/Nav772/llm-decoding-strategies) — Text generation
- [RAG Document Q&A](https://huggingface.co/spaces/Nav772/rag-document-qa) — Retrieval-augmented generation

## 📄 License

MIT License