MCPcopy Index your code
hub / github.com/algorithmicsuperintelligence/optillm / LEAP

Class LEAP

optillm/leap.py:12–241  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10logger = logging.getLogger(__name__)
11
12class LEAP:
13 def __init__(self, system_prompt: str, client, model: str, request_config: dict = None, request_id: str = None):
14 self.system_prompt = system_prompt
15 self.client = client
16 self.model = model
17 self.request_id = request_id
18 self.low_level_principles = []
19 self.high_level_principles = []
20 self.leap_completion_tokens = 0
21
22 # Extract max_tokens from request_config with default
23 self.max_tokens = 4096
24 if request_config:
25 self.max_tokens = request_config.get('max_tokens', self.max_tokens)
26
27 def extract_output(self, text: str) -> str:
28 match = re.search(r'<output>(.*?)(?:</output>|$)', text, re.DOTALL)
29 return match.group(1).strip() if match else ""
30
31 def _extract_content(self, response, context: str) -> str:
32 """Validate a provider response and return its message content.
33
34 Guards against the empty / None / length-truncated responses that would
35 otherwise raise IndexError/TypeError downstream (e.g. when the content
36 is fed to extract_output or split). Mirrors the response-validation
37 idiom already used in moa/bon/plansearch.
38 """
39 if (response is None or
40 not response.choices or
41 response.choices[0].message.content is None or
42 response.choices[0].finish_reason == "length"):
43 raise Exception(f"LEAP: provider returned an empty, None, or truncated response while {context}")
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>

Callers 1

leapFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected