-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
155 lines (131 loc) · 9.53 KB
/
main.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import json
import openai
import os
import argparse
# Initialize the argument parser
parser = argparse.ArgumentParser(description='Reasoning Task Solver')
parser.add_argument('-v', '--verbose', action='store_true',
help='Increase output verbosity')
parser.add_argument('-m', '--model', type=str, default="mistralai/Mixtral-8x7B-Instruct-v0.1",
help='The model to use for the reasoning task')
parser.add_argument('-a', '--api', type=str, default="groq", help='The API to use for the reasoning task. select groq or together')
# Parse arguments from the command line
args = parser.parse_args()
# load GROQ api key and if not available try together api key
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
TOGETHER_API_KEY = os.environ.get("TOGETHER_API_KEY")
if args.api == "groq" and GROQ_API_KEY is None:
raise ValueError("Please set GROQ_API_KEY in your environment variables")
elif args.api == "together" and TOGETHER_API_KEY is None:
raise ValueError("Please set TOGETHER_API_KEY in your environment variables")
elif args.api == "groq" and GROQ_API_KEY is not None:
client = openai.OpenAI(api_key=GROQ_API_KEY,
base_url='https://api.groq.com/openai/v1',
)
if args.model == "mistralai/Mixtral-8x7B-Instruct-v0.1":
args.model = "mixtral-8x7b-32768"
elif args.api == "together" and TOGETHER_API_KEY is not None:
client = openai.OpenAI(api_key=TOGETHER_API_KEY,
base_url='https://api.together.xyz',
)
else:
raise ValueError("Please set GROQ_API_KEY or TOGETHER_API_KEY in your environment variables")
print("Using model:", args.model)
print("Using API:", args.api)
def query_llm(prompt, max_tokens=3048):
messages = [
{ "role": "user", "content": prompt }
]
response = client.chat.completions.create(
messages=messages,
model=args.model,
max_tokens=max_tokens,
)
content = response.choices[0].message.content.strip()
return content
def select_reasoning_modules(task_description, reasoning_modules):
"""
Step 1: SELECT relevant reasoning modules for the task.
"""
prompt = f"Given the task: {task_description}, which of the following reasoning modules are relevant? Do not elaborate on why.\n\n" + "\n".join(reasoning_modules)
selected_modules = query_llm(prompt)
return selected_modules
def adapt_reasoning_modules(selected_modules, task_example):
"""
Step 2: ADAPT the selected reasoning modules to be more specific to the task.
"""
prompt = f"Without working out the full solution, adapt the following reasoning modules to be specific to our task:\n{selected_modules}\n\nOur task:\n{task_example}"
adapted_modules = query_llm(prompt)
return adapted_modules
def implement_reasoning_structure(adapted_modules, task_description):
"""
Step 3: IMPLEMENT the adapted reasoning modules into an actionable reasoning structure.
"""
prompt = f"Without working out the full solution, create an actionable reasoning structure for the task using these adapted reasoning modules:\n{adapted_modules}\n\nTask Description:\n{task_description}"
reasoning_structure = query_llm(prompt)
return reasoning_structure
# STAGE 2
def execute_reasoning_structure(reasoning_structure, task_instance):
"""
Execute the reasoning structure to solve a specific task instance.
"""
prompt = f"Using the following reasoning structure: {reasoning_structure}\n\nSolve this task, providing your final answer: {task_instance}"
solution = query_llm(prompt)
return solution
# Example usage
if __name__ == "__main__":
reasoning_modules = [
"1. How could I devise an experiment to help solve that problem?",
"2. Make a list of ideas for solving this problem, and apply them one by one to the problem to see if any progress can be made.",
"3. How could I measure progress on this problem?",
"4. How can I simplify the problem so that it is easier to solve?",
"5. What are the key assumptions underlying this problem?",
"6. What are the potential risks and drawbacks of each solution?",
"7. What are the alternative perspectives or viewpoints on this problem?",
"8. What are the long-term implications of this problem and its solutions?",
"9. How can I break down this problem into smaller, more manageable parts?",
"10. Critical Thinking: This style involves analyzing the problem from different perspectives, questioning assumptions, and evaluating the evidence or information available. It focuses on logical reasoning, evidence-based decision-making, and identifying potential biases or flaws in thinking.",
"11. Try creative thinking, generate innovative and out-of-the-box ideas to solve the problem. Explore unconventional solutions, thinking beyond traditional boundaries, and encouraging imagination and originality.",
"12. Seek input and collaboration from others to solve the problem. Emphasize teamwork, open communication, and leveraging the diverse perspectives and expertise of a group to come up with effective solutions.",
"13. Use systems thinking: Consider the problem as part of a larger system and understanding the interconnectedness of various elements. Focuses on identifying the underlying causes, feedback loops, and interdependencies that influence the problem, and developing holistic solutions that address the system as a whole.",
"14. Use Risk Analysis: Evaluate potential risks, uncertainties, and tradeoffs associated with different solutions or approaches to a problem. Emphasize assessing the potential consequences and likelihood of success or failure, and making informed decisions based on a balanced analysis of risks and benefits.",
"15. Use Reflective Thinking: Step back from the problem, take the time for introspection and self-reflection. Examine personal biases, assumptions, and mental models that may influence problem-solving, and being open to learning from past experiences to improve future approaches.",
"16. What is the core issue or problem that needs to be addressed?",
"17. What are the underlying causes or factors contributing to the problem?",
"18. Are there any potential solutions or strategies that have been tried before? If yes, what were the outcomes and lessons learned?",
"19. What are the potential obstacles or challenges that might arise in solving this problem?",
"20. Are there any relevant data or information that can provide insights into the problem? If yes, what data sources are available, and how can they be analyzed?",
"21. Are there any stakeholders or individuals who are directly affected by the problem? What are their perspectives and needs?",
"22. What resources (financial, human, technological, etc.) are needed to tackle the problem effectively?",
"23. How can progress or success in solving the problem be measured or evaluated?",
"24. What indicators or metrics can be used?",
"25. Is the problem a technical or practical one that requires a specific expertise or skill set? Or is it more of a conceptual or theoretical problem?",
"26. Does the problem involve a physical constraint, such as limited resources, infrastructure, or space?",
"27. Is the problem related to human behavior, such as a social, cultural, or psychological issue?",
"28. Does the problem involve decision-making or planning, where choices need to be made under uncertainty or with competing objectives?",
"29. Is the problem an analytical one that requires data analysis, modeling, or optimization techniques?",
"30. Is the problem a design challenge that requires creative solutions and innovation?",
"31. Does the problem require addressing systemic or structural issues rather than just individual instances?",
"32. Is the problem time-sensitive or urgent, requiring immediate attention and action?",
"33. What kinds of solution typically are produced for this kind of problem specification?",
"34. Given the problem specification and the current best solution, have a guess about other possible solutions."
"35. Let’s imagine the current best solution is totally wrong, what other ways are there to think about the problem specification?"
"36. What is the best way to modify this current best solution, given what you know about these kinds of problem specification?"
"37. Ignoring the current best solution, create an entirely new solution to the problem."
"38. Let’s think step by step."
"39. Let’s make a step by step plan and implement it with good notation and explanation."
]
task_example = "Lisa has 10 apples. She gives 3 apples to her friend and then buys 5 more apples from the store. How many apples does Lisa have now?"
while True:
task_example = input("Enter a task: ")
selected_modules = select_reasoning_modules(task_example, reasoning_modules)
if args.verbose:
print("Stage 1 SELECT: Selected Modules:\n", selected_modules)
adapted_modules = adapt_reasoning_modules(selected_modules, task_example)
if args.verbose:
print("\nStage 1 ADAPT: Adapted Modules:\n", adapted_modules)
reasoning_structure = implement_reasoning_structure(adapted_modules, task_example)
if args.verbose:
print("\nStage 1 IMPLEMENT: Reasoning Structure:\n", reasoning_structure)
result = execute_reasoning_structure(reasoning_structure, task_example)
print("\nStage 2: Final Result:\n", result)