(self, problem: str, solution: str)
| 144 | return response.choices[0].message.content.strip() |
| 145 | |
| 146 | def implement_solution(self, problem: str, solution: str) -> str: |
| 147 | prompt = f"""You are an expert Python programmer. You will be given a question (problem specification) |
| 148 | and a natural language solution/tutorial that describes how to solve the problem. You will |
| 149 | generate a correct Python program that matches said specification and tutorial and passes |
| 150 | all tests. You will NOT return anything except for the program inside markdown codeblocks. |
| 151 | |
| 152 | Problem: |
| 153 | {problem} |
| 154 | |
| 155 | Solution: |
| 156 | {solution} |
| 157 | |
| 158 | Please implement the solution in Python.""" |
| 159 | |
| 160 | # Prepare request for logging |
| 161 | provider_request = { |
| 162 | "model": self.model, |
| 163 | "max_tokens": self.max_tokens, |
| 164 | "messages": [ |
| 165 | {"role": "system", "content": self.system_prompt}, |
| 166 | {"role": "user", "content": prompt} |
| 167 | ] |
| 168 | } |
| 169 | |
| 170 | response = self.client.chat.completions.create(**provider_request) |
| 171 | |
| 172 | # Log provider call if conversation logging is enabled |
| 173 | if hasattr(optillm, 'conversation_logger') and optillm.conversation_logger and self.request_id: |
| 174 | response_dict = response.model_dump() if hasattr(response, 'model_dump') else response |
| 175 | optillm.conversation_logger.log_provider_call(self.request_id, provider_request, response_dict) |
| 176 | self.plansearch_completion_tokens += response.usage.completion_tokens |
| 177 | |
| 178 | # Check for valid response with None-checking |
| 179 | if (response is None or |
| 180 | not response.choices or |
| 181 | response.choices[0].message.content is None or |
| 182 | response.choices[0].finish_reason == "length"): |
| 183 | logger.error("Implementation response truncated or empty. Consider increasing max_tokens.") |
| 184 | return "Error: Response was truncated due to token limit. Please increase max_tokens or max_completion_tokens." |
| 185 | |
| 186 | return response.choices[0].message.content.strip() |
| 187 | |
| 188 | def solve(self, problem: str, num_initial_observations: int = 3, num_derived_observations: int = 2) -> Tuple[str, str]: |
| 189 | logger.info("Generating initial observations") |
no test coverage detected