diff --git a/README.md b/README.md index 2ff6d883..6d54ec5e 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ or your own code where you want to use the results from optillm. You can use it | CoT with Reflection | `cot_reflection` | Implements chain-of-thought reasoning with \, \ and \ sections | | PlanSearch | `plansearch` | Implements a search algorithm over candidate plans for solving a problem in natural language | | LEAP | `leap` | Learns task-specific principles from few shot examples | +| ReRead | `re2` | Implements rereading to improve reasoning by processing queries twice | ## Available Parameters @@ -196,6 +197,7 @@ Authorization: Bearer your_secret_api_key ## References +- [Re-Reading Improves Reasoning in Large Language Models](https://arxiv.org/abs/2309.06275) - [In-Context Principle Learning from Mistakes](https://arxiv.org/abs/2402.05403) - [Planning In Natural Language Improves LLM Search For Code Generation](https://arxiv.org/abs/2409.03733) - [Self-Consistency Improves Chain of Thought Reasoning in Language Models](https://arxiv.org/abs/2203.11171) diff --git a/optillm.py b/optillm.py index fc1e02fa..10d77df0 100644 --- a/optillm.py +++ b/optillm.py @@ -17,6 +17,7 @@ from optillm.cot_reflection import cot_reflection from optillm.plansearch import plansearch from optillm.leap import leap +from optillm.reread import re2_approach # Setup logging logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') @@ -69,7 +70,7 @@ # List of known approaches known_approaches = ["mcts", "bon", "moa", "rto", "z3", "self_consistency", "pvg", "rstar", - "cot_reflection", "plansearch", "leap"] + "cot_reflection", "plansearch", "leap", "re2"] # Optional API key configuration to secure the proxy @app.before_request @@ -149,6 +150,8 @@ def proxy(): final_response = plansearch(system_prompt, initial_query, client, model, n=n) elif approach == 'leap': final_response = leap(system_prompt, initial_query, client, model) + elif approach == 're2': + final_response = re2_approach(system_prompt, initial_query, client, model, n=n) else: raise ValueError(f"Unknown approach: {approach}") except Exception as e: diff --git a/optillm/reread.py b/optillm/reread.py new file mode 100644 index 00000000..b254f37b --- /dev/null +++ b/optillm/reread.py @@ -0,0 +1,43 @@ +import logging + +logger = logging.getLogger(__name__) + +def re2_approach(system_prompt, initial_query, client, model, n=1): + """ + Implement the RE2 (Re-Reading) approach for improved reasoning in LLMs. + + Args: + system_prompt (str): The system prompt to be used. + initial_query (str): The initial user query. + client: The OpenAI client object. + model (str): The name of the model to use. + n (int): Number of completions to generate. + + Returns: + str or list: The generated response(s) from the model. + """ + logger.info("Using RE2 approach for query processing") + + # Construct the RE2 prompt + re2_prompt = f"{initial_query}\nRead the question again: {initial_query}" + + messages = [ + {"role": "system", "content": system_prompt}, + {"role": "user", "content": re2_prompt} + ] + + try: + response = client.chat.completions.create( + model=model, + messages=messages, + n=n + ) + + if n == 1: + return response.choices[0].message.content.strip() + else: + return [choice.message.content.strip() for choice in response.choices] + + except Exception as e: + logger.error(f"Error in RE2 approach: {str(e)}") + raise