(self, initial_query: str)
| 44 | return response.choices[0].message.content |
| 45 | |
| 46 | def extract_examples_from_query(self, initial_query: str) -> List[Tuple[str, str]]: |
| 47 | logger.info("Extracting examples from initial query") |
| 48 | |
| 49 | # Prepare request for logging |
| 50 | provider_request = { |
| 51 | "model": self.model, |
| 52 | "max_tokens": self.max_tokens, |
| 53 | "messages": [ |
| 54 | {"role": "system", "content": self.system_prompt}, |
| 55 | {"role": "user", "content": f""" |
| 56 | Analyze the following query and determine if it contains few-shot examples. |
| 57 | If it does, extract the examples and their corresponding answers. |
| 58 | Format the examples as a JSON array of objects, where each object has "question" and "answer" fields. |
| 59 | If there are no examples, return an empty array. |
| 60 | Enclose your response within <output></output> tags. |
| 61 | Do not put any explanation or any other reponse other than the JSON array within the <output></output> tags. |
| 62 | |
| 63 | Example output format: |
| 64 | <output> |
| 65 | [ |
| 66 | {{"question": "What is 2+2?", "answer": "4"}}, |
| 67 | {{"question": "What is the capital of France?", "answer": "Paris"}} |
| 68 | ] |
| 69 | </output> |
| 70 | |
| 71 | Query: {initial_query} |
| 72 | """} |
| 73 | ] |
| 74 | } |
| 75 | |
| 76 | response = self.client.chat.completions.create(**provider_request) |
| 77 | |
| 78 | # Log provider call if conversation logging is enabled |
| 79 | if hasattr(optillm, 'conversation_logger') and optillm.conversation_logger and self.request_id: |
| 80 | response_dict = response.model_dump() if hasattr(response, 'model_dump') else response |
| 81 | optillm.conversation_logger.log_provider_call(self.request_id, provider_request, response_dict) |
| 82 | |
| 83 | self.leap_completion_tokens += response.usage.completion_tokens |
| 84 | examples_str = self.extract_output(self._extract_content(response, "extracting examples from the query")) |
| 85 | logger.debug(f"Extracted examples: {examples_str}") |
| 86 | examples = [] |
| 87 | if examples_str: |
| 88 | try: |
| 89 | examples_list = json.loads(examples_str) |
| 90 | examples = [(example['question'], example['answer']) for example in examples_list] |
| 91 | except json.JSONDecodeError: |
| 92 | logger.warning("Failed to parse examples JSON, using empty list") |
| 93 | except KeyError: |
| 94 | logger.warning("Parsed JSON does not have the expected structure, using empty list") |
| 95 | |
| 96 | logger.debug(f"Extracted examples: {examples}") |
| 97 | return examples |
| 98 | |
| 99 | def generate_mistakes(self, examples: List[Tuple[str, str]]) -> List[Tuple[str, str, str, str]]: |
| 100 | logger.info("Generating mistakes for given examples") |
no test coverage detected