import math import random import logging from typing import List, Dict, Any, Tuple…
- User
import math import random import logging from typing import List, Dict, Any, Tuple import re import asyncio import aiohttp from concurrent.futures import ThreadPoolExecutor
Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logger = logging.getLogger(name)
class Node: def init(self, state: str, action: str, parent: 'Node' = None): self.state = state self.action = action self.parent = parent self.children: List[Node] = [] self.visits = 0 self.value = 0.0
class RStar: def init(self, system: str, client, model: str, max_depth: int = 3, num_rollouts: int = 5, c: float = 1.4): self.client = client self.model_name = model self.max_depth = max_depth self.num_rollouts = num_rollouts self.c = c self.actions = ["A1", "A2", "A3", "A4", "A5"] self.original_question = None self.system = system logger.debug(f"Initialized RStar with model: {model}, max_depth: {max_depth}, num_rollouts: {num_rollouts}")
async def generate_response_async(self, prompt: str) -> str: async with aiohttp.ClientSession() as session: async with session.post( f"{self.client.base_url}chat/completions", headers={"Authorization": f"Bearer {self.client.api_key}"}, json={ "model": self.model_name, "messages": [{"role": "system", "content": "You are a helpful assistant focused on solving mathematical problems. Stick to the given question and avoid introducing new scenarios."}, {"role": "user", "content": prompt}], "max_tokens": 4096, "temperature": 0.2 } ) as response: result = await response.json() return result['choices'][0]['message']['content'].strip() async def expand_async(self, node: Node, action: str) -> Node: prompt = self.create_prompt(node.state, action) new_state = await self.generate_response_async(prompt) child_node = Node(new_state, action, node) node.children.append(child_node) logger.debug(f"Expanded node with action: {action}") return child_node async def simulate_async(self, node: Node) -> float: current_node = node depth = 0 logger.debug("Starting simulation") while depth < self.max_depth: if not current_node.children: action = random.choice(self.actions) current_node = await self.expand_async(current_node, action) else: current_node = random.choice(current_node.children) depth += 1 value = self.evaluate(current_node) logger.debug(f"Simulation complete. Final value: {value}") return value async def mcts_async(self, root_state: str) -> List[Node]: root = Node(root_state, None) tasks = [] for _ in range(self.num_rollouts): tasks.append(self.mcts_rollout_async(root)) await asyncio.gather(*tasks) return self.extract_trajectories(root) async def mcts_rollout_async(self, root: Node): node = root while node.children: node, _ = self.select_action(node) action = random.choice(self.actions) if len(node.children) < len(self.actions): node = await self.expand_async(node, action) value = await self.simulate_async(node) self.backpropagate(node, value) async def solve_async(self, question: str) -> str: self.original_question = question logger.info(f"Solving question: {question}") trajectories = await self.mcts_async(question) if not trajectories: logger.warning("No trajectories found. Unable to solve the question.") return "Unable to solve the question due to insufficient reasoning paths." final_trajectory = self.select_final_trajectory(trajectories) logger.debug(f"Final trajectory: {[node.state for node in final_trajectory]}") answers = [self.extract_answer(node.state) for node in final_trajectory] final_answer = self.select_best_answer(answers) logger.info(f"Selected final answer: {final_answer}") return final_answer def generate_response(self, prompt: str) -> str: logger.debug(f"Generating response for prompt: {prompt[:100]}...") response = self.client.chat.completions.create( model=self.model_name, messages=[ {"role": "system", "content": "You are a helpful assistant focused on solving mathematical problems. Stick to the given question and avoid introducing new scenarios."}, {"role": "user", "content": prompt} ], max_tokens=4096, temperature=0.2 ) generated_response = response.choices[0].message.content.strip() logger.debug(f"Generated response: {generated_response}") return generated_response def select_action(self, node: Node) -> Tuple[Node, str]: if not node.children: action = random.choice(self.actions) logger.debug(f"Selected random action: {action}") return node, action uct_values = [] for child in node.children: if child.visits == 0: uct = float('inf') else: uct = child.value / child.visits + self.c * math.sqrt(math.log(node.visits) / child.visits) uct_values.append(uct) best_child = node.children[uct_values.index(max(uct_values))] logger.debug(f"Selected action: {best_child.action}") return best_child, best_child.action def expand(self, node: Node, action: str) -> Node: prompt = self.create_prompt(node.state, action) new_state = self.generate_response(prompt) child_node = Node(new_state, action, node) node.children.append(child_node) logger.debug(f"Expanded node with action: {action}") return child_node def simulate(self, node: Node) -> float: current_node = node depth = 0 logger.debug("Starting simulation") while depth < self.max_depth: if not current_node.children: action = random.choice(self.actions) current_node = self.expand(current_node, action) else: current_node = random.choice(current_node.children) depth += 1 value = self.evaluate(current_node) logger.debug(f"Simulation complete. Final value: {value}") return value def backpropagate(self, node: Node, value: float): logger.debug("Starting backpropagation") while node: node.visits += 1 node.value += value node = node.parent logger.debug("Backpropagation complete") def mcts(self, root_state: str) -> List[Node]: root = Node(root_state, None) logger.debug(f"Starting MCTS with {self.num_rollouts} rollouts") for i in range(self.num_rollouts): logger.debug(f"Rollout {i+1}/{self.num_rollouts}") node = root while node.children: node, _ = self.select_action(node) action = random.choice(self.actions) if len(node.children) < len(self.actions): node = self.expand(node, action) value = self.simulate(node) self.backpropagate(node, value) logger.debug("MCTS complete") return self.extract_trajectories(root) def extract_trajectories(self, root: Node) -> List[List[Node]]: logger.debug("Extracting trajectories") trajectories = [] stack = [(root, [])] while stack: node, path = stack.pop() if not node.children: trajectories.append(path + [node]) else: for child in node.children: stack.append((child, path + [node])) logger.debug(f"Extracted {len(trajectories)} trajectories") return trajectories def mutual_consistency(self, trajectory: List[Node]) -> bool: split_index = random.randint(1, len(trajectory) - 1) partial_trajectory = trajectory[:split_index] prompt = self.create_discriminator_prompt(partial_trajectory) completion = self.generate_response(prompt) is_consistent = self.compare_completions(completion, trajectory[split_index:]) logger.debug(f"Mutual consistency check: {'Passed' if is_consistent else 'Failed'}") return is_consistent def select_final_trajectory(self, trajectories: List[List[Node]]) -> List[Node]: logger.debug("Selecting final trajectory") valid_trajectories = [t for t in trajectories if self.mutual_consistency(t)] logger.debug(f"Found {len(valid_trajectories)} valid trajectories") if not valid_trajectories: logger.warning("No valid trajectories found. Selecting based on value/visits.") return max(trajectories, key=lambda t: self.trajectory_score(t)) return max(valid_trajectories, key=lambda t: self.trajectory_score(t)) def trajectory_score(self, trajectory: List[Node]) -> float: if not trajectory: return float('-inf') last_node = trajectory[-1] if last_node.visits == 0: return last_node.value # Return just the value if visits is zero return last_node.value / last_node.visits def select_best_answer(self, answers: List[Tuple[str, float]]) -> str: valid_answers = [(answer, conf) for answer, conf in answers if answer] if not valid_answers: return "Unable to determine a valid answer." # Sort by confidence and then by frequency answer_counts = {} for answer, conf in valid_answers: if answer in answer_counts: answer_counts[answer] = (answer_counts[answer][0] + 1, max(answer_counts[answer][1], conf)) else: answer_counts[answer] = (1, conf) sorted_answers = sorted(answer_counts.items(), key=lambda x: (-x[1][1], -x[1][0])) best_answer, (count, conf) = sorted_answers[0] logger.debug(f"Selected best answer: {best_answer} (count: {count}, confidence: {conf})") return best_answer def create_prompt(self, state: str, action: str) -> str: question = self.original_question if hasattr(self, 'original_question') else "the original question" prompts = { "A1": f"""Given the current state: {state} - Assistant