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
(system_prompt, initial_query, client, model, n=1, request_config: dict = None, request_id: str = None)
| 5 | logger = logging.getLogger(__name__) |
| 6 | |
| 7 | def re2_approach(system_prompt, initial_query, client, model, n=1, request_config: dict = None, request_id: str = None): |
| 8 | """ |
| 9 | Implement the RE2 (Re-Reading) approach for improved reasoning in LLMs. |
| 10 | |
| 11 | Args: |
| 12 | system_prompt (str): The system prompt to be used. |
| 13 | initial_query (str): The initial user query. |
| 14 | client: The OpenAI client object. |
| 15 | model (str): The name of the model to use. |
| 16 | n (int): Number of completions to generate. |
| 17 | request_config (dict): Optional configuration including max_tokens. |
| 18 | |
| 19 | Returns: |
| 20 | str or list: The generated response(s) from the model. |
| 21 | """ |
| 22 | logger.info("Using RE2 approach for query processing") |
| 23 | re2_completion_tokens = 0 |
| 24 | |
| 25 | # Extract max_tokens from request_config if provided |
| 26 | max_tokens = None |
| 27 | if request_config: |
| 28 | max_tokens = request_config.get('max_tokens') |
| 29 | |
| 30 | # Construct the RE2 prompt |
| 31 | re2_prompt = f"{initial_query}\nRead the question again: {initial_query}" |
| 32 | |
| 33 | messages = [ |
| 34 | {"role": "system", "content": system_prompt}, |
| 35 | {"role": "user", "content": re2_prompt} |
| 36 | ] |
| 37 | |
| 38 | try: |
| 39 | provider_request = { |
| 40 | "model": model, |
| 41 | "messages": messages, |
| 42 | "n": n |
| 43 | } |
| 44 | if max_tokens is not None: |
| 45 | provider_request["max_tokens"] = max_tokens |
| 46 | response = client.chat.completions.create(**provider_request) |
| 47 | |
| 48 | # Log provider call |
| 49 | if hasattr(optillm, 'conversation_logger') and optillm.conversation_logger and request_id: |
| 50 | response_dict = response.model_dump() if hasattr(response, 'model_dump') else response |
| 51 | optillm.conversation_logger.log_provider_call(request_id, provider_request, response_dict) |
| 52 | |
| 53 | if response is None or not response.choices: |
| 54 | raise Exception("re2_approach: provider returned an empty or None response") |
| 55 | re2_completion_tokens += response.usage.completion_tokens |
| 56 | if n == 1: |
| 57 | if (response.choices[0].message.content is None or |
| 58 | response.choices[0].finish_reason == "length"): |
| 59 | raise Exception("re2_approach: provider returned a None or truncated response") |
| 60 | return response.choices[0].message.content.strip(), re2_completion_tokens |
| 61 | else: |
| 62 | # Keep only choices that produced usable, non-truncated content, |
| 63 | # consistent with how the single-choice path treats a truncation. |
| 64 | return [choice.message.content.strip() for choice in response.choices |