MCPcopy
hub / github.com/algorithmicsuperintelligence/optillm / extract_examples_from_query

Method extract_examples_from_query

optillm/leap.py:46–97  ·  view source on GitHub ↗
(self, initial_query: str)

Source from the content-addressed store, hash-verified

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")

Callers 1

solveMethod · 0.95

Calls 5

extract_outputMethod · 0.95
_extract_contentMethod · 0.95
log_provider_callMethod · 0.80
createMethod · 0.45
model_dumpMethod · 0.45

Tested by

no test coverage detected