-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathplansearch.py
More file actions
214 lines (169 loc) · 10.1 KB
/
plansearch.py
File metadata and controls
214 lines (169 loc) · 10.1 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
import logging
from typing import List, Tuple
import optillm
from optillm import conversation_logger
logger = logging.getLogger(__name__)
class PlanSearch:
def __init__(self, system_prompt: str, client, model: str, request_config: dict = None, request_id: str = None):
self.system_prompt = system_prompt
self.client = client
self.model = model
self.request_id = request_id
self.plansearch_completion_tokens = 0
# Extract max_tokens from request_config with default
self.max_tokens = 4096
if request_config:
self.max_tokens = request_config.get('max_tokens', self.max_tokens)
def generate_observations(self, problem: str, num_observations: int = 3) -> List[str]:
prompt = f"""You are an expert Python programmer. You will be given a competitive programming question
(problem specification). You will return several useful, non-obvious, and correct observations
about the problem, like hints to solve the problem. You will NOT return any code. Be as
creative as possible, going beyond what you think is intuitively correct.
Here is the competitive programming problem:
{problem}
Please provide {num_observations} observations."""
# Prepare request for logging
provider_request = {
"model": self.model,
"max_tokens": self.max_tokens,
"messages": [
{"role": "system", "content": self.system_prompt},
{"role": "user", "content": prompt}
]
}
response = self.client.chat.completions.create(**provider_request)
# Log provider call if conversation logging is enabled
if hasattr(optillm, 'conversation_logger') and optillm.conversation_logger and self.request_id:
response_dict = response.model_dump() if hasattr(response, 'model_dump') else response
optillm.conversation_logger.log_provider_call(self.request_id, provider_request, response_dict)
self.plansearch_completion_tokens += response.usage.completion_tokens
# Check for valid response with None-checking
if (response is None or
not response.choices or
response.choices[0].message.content is None or
response.choices[0].finish_reason == "length"):
logger.warning("Observations response truncated or empty, returning empty list")
return []
observations = response.choices[0].message.content.strip().split('\n')
return [obs.strip() for obs in observations if obs.strip()]
def generate_derived_observations(self, problem: str, observations: List[str], num_new_observations: int = 2) -> List[str]:
prompt = f"""You are an expert Python programmer. You will be given a competitive programming question
(problem specification) and several correct observations about the problem.
You will brainstorm several new, useful, and correct observations about the problem, derived
from the given observations. You will NOT return any code. Be as creative as possible, going
beyond what you think is intuitively correct.
Here is the competitive programming problem:
{problem}
Here are the existing observations:
{chr(10).join(f"{i+1}. {obs}" for i, obs in enumerate(observations))}
Please provide {num_new_observations} new observations derived from the existing ones."""
# Prepare request for logging
provider_request = {
"model": self.model,
"max_tokens": self.max_tokens,
"messages": [
{"role": "system", "content": self.system_prompt},
{"role": "user", "content": prompt}
]
}
response = self.client.chat.completions.create(**provider_request)
# Log provider call if conversation logging is enabled
if hasattr(optillm, 'conversation_logger') and optillm.conversation_logger and self.request_id:
response_dict = response.model_dump() if hasattr(response, 'model_dump') else response
optillm.conversation_logger.log_provider_call(self.request_id, provider_request, response_dict)
self.plansearch_completion_tokens += response.usage.completion_tokens
# Check for valid response with None-checking
if (response is None or
not response.choices or
response.choices[0].message.content is None or
response.choices[0].finish_reason == "length"):
logger.warning("Derived observations response truncated or empty, returning empty list")
return []
new_observations = response.choices[0].message.content.strip().split('\n')
return [obs.strip() for obs in new_observations if obs.strip()]
def generate_solution(self, problem: str, observations: List[str]) -> str:
prompt = f"""Here is the competitive programming problem:
{problem}
Here are the intelligent observations to help solve the problem:
{chr(10).join(f"Observation {i+1}: {obs}" for i, obs in enumerate(observations))}
Use these observations above to brainstorm a natural language solution to the problem above.
Note that your intuition may lead you astray, so come up with simple, creative ideas that
go beyond what you would usually come up with and exceeds your narrow intuition.
Quote relevant parts of the observations EXACTLY before each step of the solution. QUOTING
IS CRUCIAL."""
# Prepare request for logging
provider_request = {
"model": self.model,
"max_tokens": self.max_tokens,
"messages": [
{"role": "system", "content": self.system_prompt},
{"role": "user", "content": prompt}
]
}
response = self.client.chat.completions.create(**provider_request)
# Log provider call if conversation logging is enabled
if hasattr(optillm, 'conversation_logger') and optillm.conversation_logger and self.request_id:
response_dict = response.model_dump() if hasattr(response, 'model_dump') else response
optillm.conversation_logger.log_provider_call(self.request_id, provider_request, response_dict)
self.plansearch_completion_tokens += response.usage.completion_tokens
# Check for valid response with None-checking
if (response is None or
not response.choices or
response.choices[0].message.content is None or
response.choices[0].finish_reason == "length"):
logger.error("Solution generation response truncated or empty. Consider increasing max_tokens.")
return "Error: Response was truncated due to token limit. Please increase max_tokens or max_completion_tokens."
return response.choices[0].message.content.strip()
def implement_solution(self, problem: str, solution: str) -> str:
prompt = f"""You are an expert Python programmer. You will be given a question (problem specification)
and a natural language solution/tutorial that describes how to solve the problem. You will
generate a correct Python program that matches said specification and tutorial and passes
all tests. You will NOT return anything except for the program inside markdown codeblocks.
Problem:
{problem}
Solution:
{solution}
Please implement the solution in Python."""
# Prepare request for logging
provider_request = {
"model": self.model,
"max_tokens": self.max_tokens,
"messages": [
{"role": "system", "content": self.system_prompt},
{"role": "user", "content": prompt}
]
}
response = self.client.chat.completions.create(**provider_request)
# Log provider call if conversation logging is enabled
if hasattr(optillm, 'conversation_logger') and optillm.conversation_logger and self.request_id:
response_dict = response.model_dump() if hasattr(response, 'model_dump') else response
optillm.conversation_logger.log_provider_call(self.request_id, provider_request, response_dict)
self.plansearch_completion_tokens += response.usage.completion_tokens
# Check for valid response with None-checking
if (response is None or
not response.choices or
response.choices[0].message.content is None or
response.choices[0].finish_reason == "length"):
logger.error("Implementation response truncated or empty. Consider increasing max_tokens.")
return "Error: Response was truncated due to token limit. Please increase max_tokens or max_completion_tokens."
return response.choices[0].message.content.strip()
def solve(self, problem: str, num_initial_observations: int = 3, num_derived_observations: int = 2) -> Tuple[str, str]:
logger.info("Generating initial observations")
initial_observations = self.generate_observations(problem, num_initial_observations)
logger.info("Generating derived observations")
derived_observations = self.generate_derived_observations(problem, initial_observations, num_derived_observations)
all_observations = initial_observations + derived_observations
logger.info("Generating solution based on observations")
natural_language_solution = self.generate_solution(problem, all_observations)
logger.info("Implementing solution in Python")
python_implementation = self.implement_solution(problem, natural_language_solution)
return natural_language_solution, python_implementation
def solve_multiple(self, problem: str, n: int, num_initial_observations: int = 3, num_derived_observations: int = 2) -> List[str]:
solutions = []
for _ in range(n):
_, python_implementation = self.solve(problem, num_initial_observations, num_derived_observations)
solutions.append(python_implementation)
return solutions
def plansearch(system_prompt: str, initial_query: str, client, model: str, n: int = 1, request_config: dict = None, request_id: str = None) -> List[str]:
planner = PlanSearch(system_prompt, client, model, request_config=request_config, request_id=request_id)
return planner.solve_multiple(initial_query, n), planner.plansearch_completion_tokens