| 141 | return ("success", output_buffer.getvalue()) |
| 142 | |
| 143 | class Z3SymPySolverSystem: |
| 144 | def __init__(self, system_prompt: str, client, model: str, timeout: int = 30, request_config: dict = None, request_id: str = None): |
| 145 | self.system_prompt = system_prompt |
| 146 | self.model = model |
| 147 | self.client = client |
| 148 | self.timeout = timeout |
| 149 | self.solver_completion_tokens = 0 |
| 150 | self.request_id = request_id |
| 151 | |
| 152 | # Extract max_tokens from request_config with default |
| 153 | self.max_tokens = 4096 |
| 154 | if request_config: |
| 155 | self.max_tokens = request_config.get('max_tokens', self.max_tokens) |
| 156 | |
| 157 | logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') |
| 158 | |
| 159 | def process_query(self, query: str) -> str: |
| 160 | try: |
| 161 | analysis = self.analyze_query(query) |
| 162 | if "SOLVER_CAN_BE_APPLIED: True" not in analysis: |
| 163 | return self.standard_llm_inference(query), self.solver_completion_tokens |
| 164 | |
| 165 | formulation = self.extract_and_validate_expressions(analysis) |
| 166 | solver_result = self.solve_with_z3_sympy(formulation) |
| 167 | |
| 168 | return self.generate_response(query, analysis, solver_result), self.solver_completion_tokens |
| 169 | except Exception as e: |
| 170 | logging.error(f"An error occurred while processing the query with Z3 and SymPy, returning standard llm inference results: {str(e)}") |
| 171 | return self.standard_llm_inference(query), self.solver_completion_tokens |
| 172 | |
| 173 | def analyze_query(self, query: str) -> str: |
| 174 | analysis_prompt = f"""Analyze the given query and determine if it can be solved using Z3 or SymPy: |
| 175 | |
| 176 | 1. Identify variables, constraints, and objectives. |
| 177 | 2. Determine the problem type (e.g., SAT, optimization, symbolic manipulation). |
| 178 | 3. Decide if Z3, SymPy, or a combination of both is suitable. |
| 179 | |
| 180 | If Z3 or SymPy can be applied, provide Python code using the appropriate library (or both) to solve the problem. Make sure you define any additional methods you need for solving the problem. |
| 181 | The code will be executed in an environment with Z3 and SymPy available, so do not include any other libraries or modules. |
| 182 | |
| 183 | Query: {query} |
| 184 | |
| 185 | Respond with: |
| 186 | SOLVER_CAN_BE_APPLIED: [True/False] |
| 187 | |
| 188 | SOLVER_FORMULATION: |
| 189 | ```python |
| 190 | # Z3 and/or SymPy code here |
| 191 | ``` |
| 192 | |
| 193 | Analysis: |
| 194 | [Your step-by-step analysis] |
| 195 | """ |
| 196 | |
| 197 | provider_request = { |
| 198 | "model": self.model, |
| 199 | "messages": [ |
| 200 | {"role": "system", "content": self.system_prompt}, |
no outgoing calls