forked from YangLing0818/buffer-of-thought-llm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta_buffer_utilis.py
99 lines (68 loc) · 4.71 KB
/
meta_buffer_utilis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import re
import io
import sys
meta_distiller_prompt = """
As a highly professional and intelligent expert in information distillation, you excel at extracting essential information to solve problems from user input queries. You adeptly transform this extracted information into a suitable format based on the respective type of the issue. If the problem can be generalized to a higher level to solve multiple issues, further analysis and explanation will be provided upon your next response.
Please categorize and extract the crucial information required to solve the problem from the user's input query. Combining these two elements will generate distilled information. Subsequently, deliver this distilled information, based on the problem type, to your downstream meta planner. The problem type should belong to one of the six categories mentioned above, and the distilled information should include:
1. Values and information of key variables extracted from user input, which will be handed over to the respective expert for task resolution, ensuring all essential information required to solve the problem is provided.
2. The objective of the problem and corresponding constraints.
3. Extend the problem based on 1 and 2, propose a meta problem that can address the user query and handle more input and output variations. Incorporate the real-world scenario of the extended problem along with the types of key variables and information constraints from the original problem to restrict the key variables in the extended problem. After that, use the user query input key information as input to solve the problem as an example.
4. Try to transform the problem into a python algorithm problem, and provide the input parameters.
5. Your task is to distill the problem, you shouldn't give the final result or possible solution in your respond.
Please distill the information following the format below and cease response after the output of the distilled information.
Meta distiller Respond:
Distilled Information:
1. Key information:
2. Restriction: (It should be noted that the answer should strictly follow the real-world rule such as in arithmatic equation, the Priority of operator, the need of parentheses etc. So according to the distilled information, emphasize the real-world rules that need to be followed within the problem.)
3. Distilled task:
4. Python transformation:
(Optional, skip when Python tag is Not for Python) Input parameters:(The names of each variable should be clear and not confusing, and correspond to the entity names in the problem)
variable1_name = x
variable2_name = y
.....
variableN_name = z
5. Answer form: (Optional, skip when there is no specific answer form)
**Note: The generation ends here. Do not show this message in your answer !**
"""
def extract_and_execute_code(text):
# Define the start and end markers for the code block
code_start_marker = "```python"
code_end_marker = "```"
# Find the position where the Python code starts
code_start_index = text.find(code_start_marker)
if code_start_index == -1:
code_start_marker = "```"
code_start_index = text.find(code_start_marker)
# If the start marker is found, extract and execute the code
if code_start_index != -1:
# Try to find the position where the code ends
code_end_index = text.find(code_end_marker, code_start_index + len(code_start_marker))
# If the end marker is not found, assume the code continues to the end of the text
if code_end_index == -1:
code_end_index = len(text)
# Extract the code part
code_str = text[code_start_index + len(code_start_marker):code_end_index].strip()
# Create a string stream to capture the output
old_stdout = sys.stdout
new_stdout = io.StringIO()
sys.stdout = new_stdout
# Execute the extracted code
try:
exec(code_str, globals())
except Exception as e:
# Restore the original standard output
sys.stdout = old_stdout
return f"An error occurred: {e}", code_str
# Get the output from the executed code
sys.stdout = old_stdout
return new_stdout.getvalue(), code_str
else:
return "No Python code found in the provided string."
def extract_answer(text):
# Define a regular expression pattern to match the answer format
# The pattern accounts for variations in spacing and line breaks
pattern = re.compile(r"Answer:\s*(.*?)\s*$", re.DOTALL)
# Search the text for the pattern
match = pattern.search(text)
# If a match is found, return the content; otherwise, return None
return match.group(1).strip() if match else None