-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt_engineering_agent_demo.py
More file actions
313 lines (257 loc) · 10.5 KB
/
Copy pathprompt_engineering_agent_demo.py
File metadata and controls
313 lines (257 loc) · 10.5 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
"""
prompt_engineering_agent_demo.py
Demo: Prompt Engineering Agent with Automated Prompt Refinement.
This agent helps craft high-quality prompts by iteratively generating
and validating candidate prompts using the LLM itself. In practice:
1. START: User supplies a task description or goal
2. PROPOSE: Agent generates several prompt candidates
3. VALIDATE: Each prompt is tested with sample inputs
4. CRITIQUE: LLM evaluates prompt effectiveness
5. REFINE: Agent produces better prompts based on critique
6. OUTPUT: Final prompt ready for production
Use cases:
- Building reliable few-shot prompts
- Automatically tuning templates for formality, detail, etc
- Rapid experimentation with prompt wording
This pattern is becoming popular in industry and research (OpenAI,
Anthropic, etc.) under names like "autoprompting" or "prompt
optimization". It's highly practical and can reduce prompt
engineering time by 70%.
Usage:
- pip install requests
- Set OLLAMA_HOST, OLLAMA_MODEL or OPENAI_API_KEY
- python prompt_engineering_agent_demo.py
"""
import os
import json
import random
from typing import List, Dict, Any, Tuple
from dataclasses import dataclass
try:
import requests
OLLAMA_AVAILABLE = True
except ImportError:
OLLAMA_AVAILABLE = False
try:
from openai import OpenAI
OPENAI_AVAILABLE = True
except ImportError:
OPENAI_AVAILABLE = False
# ============================================================================
# Data structures
# ============================================================================
@dataclass
class PromptCandidate:
text: str
score: float = 0.0
critique: str = ""
@dataclass
class PromptEngineeringResult:
original_goal: str
candidates: List[PromptCandidate]
final_prompt: str
# ============================================================================
# Core agent
# ============================================================================
class PromptEngineeringAgent:
"""Automates the process of designing and refining prompts."""
def __init__(
self,
llm_provider: str = "ollama",
candidates_per_round: int = 3,
max_rounds: int = 3,
):
self.llm_provider = llm_provider
self.candidates_per_round = candidates_per_round
self.max_rounds = max_rounds
def propose_prompts(self, goal: str) -> List[PromptCandidate]:
"""Ask the LLM to propose a set of candidate prompts."""
system_prompt = (
"You are a prompt engineer. Generate multiple prompt templates "
"that would elicit high-quality answers for the given task. "
"Make them clear, concise, and specific."
)
user_prompt = (
f"Task description / goal:\n{goal}\n\n"
"Provide a numbered list of candidate prompts."
)
response = call_llm(user_prompt, self.llm_provider, system_prompt)
candidates: List[PromptCandidate] = []
for line in response.split("\n"):
line = line.strip()
if not line:
continue
# remove leading numbers or bullets
candidate = line.lstrip("0123456789.- ")
candidates.append(PromptCandidate(text=candidate))
# randomly sample if more than needed
if len(candidates) > self.candidates_per_round:
candidates = random.sample(candidates, self.candidates_per_round)
return candidates
def evaluate_prompt(
self, prompt: str, examples: List[str]
) -> Tuple[float, str]:
"""Evaluate a prompt by running it on sample inputs and scoring.
returns (score, critique)"""
system_prompt = (
"You are an evaluator. Given a prompt and some sample user "
"inputs, generate expected outputs and rate how well the "
"prompt would perform. Provide a score 0-1 and a brief critique."
)
user_prompt = (
f"Prompt to evaluate:\n{prompt}\n\n"
"Sample inputs:\n" + "\n".join(f"- {e}" for e in examples) + "\n\n"
"For each input, produce an output and then give an overall score "
"(0 to 1) and a short critique of the prompt."
)
response = call_llm(user_prompt, self.llm_provider, system_prompt)
# naive parsing: look for 'score' and 'critique'
score = 0.0
critique = ""
for line in response.split("\n"):
low = line.lower()
if "score" in low and "0" in low:
import re
match = re.search(r"score[: ]*([0-9\.]+)", low)
if match:
try:
score = float(match.group(1))
except:
pass
if "critique" in low:
critique = line
# include next line if exists
idx = response.split("\n").index(line)
if idx + 1 < len(response.split("\n")):
critique += " " + response.split("\n")[idx + 1]
return score, critique
def refine_prompts(
self, candidates: List[PromptCandidate]
) -> List[PromptCandidate]:
"""Ask LLM to refine the best prompts based on critique."""
texts = "\n".join(f"- {c.text} (score={c.score})" for c in candidates)
system_prompt = (
"You are a prompt engineer. Improve the following prompts "
"taking into account their scores and critiques. "
"Return a new set of prompt candidates."
)
user_prompt = (
f"Prompt candidates with scores and critiques:\n{texts}\n\n"
"Provide an improved numbered list of prompts."
)
response = call_llm(user_prompt, self.llm_provider, system_prompt)
new_candidates: List[PromptCandidate] = []
for line in response.split("\n"):
line = line.strip()
if not line:
continue
candidate = line.lstrip("0123456789.- ")
new_candidates.append(PromptCandidate(text=candidate))
return new_candidates
def run(self, goal: str, examples: List[str]) -> PromptEngineeringResult:
"""Main orchestration method."""
all_candidates: List[PromptCandidate] = []
print(f"\nPrompt Engineering goal: {goal}\n")
candidates = self.propose_prompts(goal)
round_idx = 1
while round_idx <= self.max_rounds:
print(f"\n=== Round {round_idx} ===")
for c in candidates:
print(f"Evaluating candidate: {c.text}")
score, critique = self.evaluate_prompt(c.text, examples)
c.score = score
c.critique = critique
print(f" • score={score:.2f}, critique={critique[:60]}...")
all_candidates.extend(candidates)
# pick top 1 or 2 to refine
candidates.sort(key=lambda x: x.score, reverse=True)
best = candidates[:2]
if round_idx == self.max_rounds:
break
candidates = self.refine_prompts(best)
round_idx += 1
# final prompt is highest scoring candidate
final_prompt = max(all_candidates, key=lambda x: x.score).text
return PromptEngineeringResult(
original_goal=goal,
candidates=all_candidates,
final_prompt=final_prompt,
)
# ============================================================================
# LLM integration (same as other demos)
# ============================================================================
def call_llm(prompt: str, provider: str = "ollama", system_prompt: str = None) -> str:
if provider.lower() == "openai":
return call_openai(prompt, system_prompt)
else:
return call_ollama(prompt, system_prompt)
def call_ollama(prompt: str, system_prompt: str = None) -> str:
if not OLLAMA_AVAILABLE:
return "(Ollama not available)"
host = os.getenv("OLLAMA_HOST", "http://localhost:11434")
model = os.getenv("OLLAMA_MODEL", "phi3")
url = f"{host.rstrip('/')}/chat?model={model}"
messages = []
if system_prompt:
messages.append({"role": "system", "content": system_prompt})
messages.append({"role": "user", "content": prompt})
try:
resp = requests.post(
url,
json={"messages": messages},
headers={"Content-Type": "application/json"},
timeout=30,
)
resp.raise_for_status()
data = resp.json()
if isinstance(data, dict):
choices = data.get("choices", [])
if choices and isinstance(choices[0], dict):
msg = choices[0].get("message", {})
return msg.get("content", "").strip()
return str(data)
except Exception as e:
return f"(Error: {e})"
def call_openai(prompt: str, system_prompt: str = None) -> str:
if not OPENAI_AVAILABLE:
return "(OpenAI not available)"
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
return "(No API key)"
try:
client = OpenAI(api_key=api_key)
messages = []
if system_prompt:
messages.append({"role": "system", "content": system_prompt})
messages.append({"role": "user", "content": prompt})
response = client.chat.completions.create(
model="gpt-3.5-turbo", messages=messages, max_tokens=1500
)
return response.choices[0].message.content.strip()
except Exception as e:
return f"(Error: {e})"
# ============================================================================
# Demo execution
# ============================================================================
if __name__ == "__main__":
print("Prompt Engineering Agent Demo")
print("=" * 70)
goal = "Summarize the environmental benefits of electric vehicles."
examples = [
"Tell me the pros of using EVs in plain language.",
"List three environmental advantages of electric cars.",
]
print(f"\nGoal: {goal}")
print("Example user inputs:")
for ex in examples:
print(f" - {ex}")
agent = PromptEngineeringAgent(llm_provider="ollama")
result = agent.run(goal, examples)
print("\nFinal prompt generated:")
print(result.final_prompt)
print("\nAll candidate prompts and scores:")
for cand in result.candidates:
print(f" score={cand.score:.2f} | {cand.text}")
# Uncomment to interactively refine your own goal:
# your_goal = input("Enter prompt goal: ")
# agent.run(your_goal, examples=[input("Example: ")])