Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 \<thinking\>, \<reflection> and \<output\> 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

Expand Down Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion optillm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
43 changes: 43 additions & 0 deletions optillm/reread.py
Original file line number Diff line number Diff line change
@@ -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