| 6 | logger = logging.getLogger(__name__) |
| 7 | |
| 8 | def cot_reflection(system_prompt, initial_query, client, model: str, return_full_response: bool=False, request_config: dict = None, request_id: str = None): |
| 9 | cot_completion_tokens = 0 |
| 10 | |
| 11 | # Extract temperature and max_tokens from request_config with defaults |
| 12 | temperature = 0.6 # Default to 0.6 as requested |
| 13 | max_tokens = 4096 # Default to 4096 as requested |
| 14 | |
| 15 | if request_config: |
| 16 | temperature = request_config.get('temperature', temperature) |
| 17 | max_tokens = request_config.get('max_tokens', max_tokens) |
| 18 | cot_prompt = f""" |
| 19 | {system_prompt} |
| 20 | |
| 21 | You are an AI assistant that uses a Chain of Thought (CoT) approach with reflection to answer queries. Follow these steps: |
| 22 | |
| 23 | 1. Think through the problem step by step within the <thinking> tags. |
| 24 | 2. Reflect on your thinking to check for any errors or improvements within the <reflection> tags. |
| 25 | 3. Make any necessary adjustments based on your reflection. |
| 26 | 4. Provide your final, concise answer within the <output> tags. |
| 27 | |
| 28 | Important: The <thinking> and <reflection> sections are for your internal reasoning process only. |
| 29 | Do not include any part of the final answer in these sections. |
| 30 | The actual response to the query must be entirely contained within the <output> tags. |
| 31 | |
| 32 | Use the following format for your response: |
| 33 | <thinking> |
| 34 | [Your step-by-step reasoning goes here. This is your internal thought process, not the final answer.] |
| 35 | <reflection> |
| 36 | [Your reflection on your reasoning, checking for errors or improvements] |
| 37 | </reflection> |
| 38 | [Any adjustments to your thinking based on your reflection] |
| 39 | </thinking> |
| 40 | <output> |
| 41 | [Your final, concise answer to the query. This is the only part that will be shown to the user.] |
| 42 | </output> |
| 43 | """ |
| 44 | |
| 45 | # Make the API call using user-provided or default parameters |
| 46 | provider_request = { |
| 47 | "model": model, |
| 48 | "messages": [ |
| 49 | {"role": "system", "content": cot_prompt}, |
| 50 | {"role": "user", "content": initial_query} |
| 51 | ], |
| 52 | "temperature": temperature, |
| 53 | "max_tokens": max_tokens |
| 54 | } |
| 55 | response = client.chat.completions.create(**provider_request) |
| 56 | |
| 57 | # Log provider call |
| 58 | if hasattr(optillm, 'conversation_logger') and optillm.conversation_logger and request_id: |
| 59 | response_dict = response.model_dump() if hasattr(response, 'model_dump') else response |
| 60 | optillm.conversation_logger.log_provider_call(request_id, provider_request, response_dict) |
| 61 | |
| 62 | # Extract the full response |
| 63 | full_response = response.choices[0].message.content |
| 64 | cot_completion_tokens += response.usage.completion_tokens |
| 65 | logger.info(f"CoT with Reflection :\n{full_response}") |