Spaces:
Runtime error
Runtime error
kailashsp
commited on
Commit
•
e9559d9
0
Parent(s):
initial: self-discover framework
Browse files- README.md +107 -0
- llm.py +55 -0
- prompts.py +105 -0
- requirements.txt +3 -0
- self_discover.py +44 -0
- task_example.py +42 -0
README.md
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
## SELF-DISCOVER FRAMEWORK
|
2 |
+
|
3 |
+
## Paper Overview [link](https://arxiv.org/pdf/2402.03620.pdf)
|
4 |
+
This project implements the paper titled "Self-Discover: Large Language Models Self-Compose Reasoning Structures," submitted on February 6, 2024, by Pei Zhou, Jay Pujara, Xiang Ren, Xinyun Chen, Heng-Tze Cheng, Quoc V. Le, Ed H. Chi, Denny Zhou, Swaroop Mishra, and Huaixiu Steven Zheng. The paper introduces SELF-DISCOVER, a framework designed to enhance the performance of Large Language Models (LLMs) on complex reasoning tasks by enabling them to self-discover task-intrinsic reasoning structures.
|
5 |
+
|
6 |
+
|
7 |
+
## Functionality (as given in paper)
|
8 |
+
- **Self-Discovery Process:** The system engages in a self-discovery process where it selects atomic reasoning modules and composes them into an explicit reasoning structure.
|
9 |
+
- **Performance Improvement:** SELF-DISCOVER significantly enhances the performance of LLMs on challenging reasoning benchmarks such as BigBench-Hard, grounded agent reasoning, and MATH, achieving up to a 32% improvement compared to conventional prompting methods like Chain of Thought (CoT).
|
10 |
+
- **Efficiency:** Despite its effectiveness, SELF-DISCOVER requires 10-40 times fewer inference computations compared to inference-intensive methods like CoT-Self-Consistency.
|
11 |
+
- **Universality:** The self-discovered reasoning structures are found to be universally applicable across different LLM model families, indicating commonalities with human reasoning patterns.
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
## Project Overview
|
16 |
+
|
17 |
+
This project consists of a Python script (`self_discover.py`) along with associated modules and prompts. It allows users to input a specific task, and then it guides them through the process of selecting, adapting, and implementing reasoning modules to tackle that task effectively.
|
18 |
+
|
19 |
+
## Implementation Details
|
20 |
+
|
21 |
+
- **Model Used:** The implementation Large Language Model (LLM) "gemini-pro" or "gpt-3.5-turbo"
|
22 |
+
- **Tasks:** The system is capable of handling various task to generate reasoning structure
|
23 |
+
- **Actions:** The system performs three main actions: SELECT, ADAPT, and IMPLEMENT.
|
24 |
+
- **SELECT:** This action involves selecting several reasoning modules crucial for solving the given task.
|
25 |
+
- **ADAPT:** The selected reasoning modules are rephrased and specified to better suit the task at hand.
|
26 |
+
- **IMPLEMENT:** The reasoning modules are operationalized into a step-by-step reasoning plan in JSON format, providing a structured approach for solving the task.
|
27 |
+
|
28 |
+
|
29 |
+
## Prerequisites
|
30 |
+
|
31 |
+
- Python 3.10
|
32 |
+
- Libraries: google-generativeai, openai, dotenv
|
33 |
+
- Input the task you want to generate a reasoning structure in task_example.py
|
34 |
+
|
35 |
+
## Installation
|
36 |
+
|
37 |
+
1. Clone this repository:
|
38 |
+
|
39 |
+
```bash
|
40 |
+
git clone https://github.com/kailashsp/SelfDiscover.git
|
41 |
+
```
|
42 |
+
|
43 |
+
2. Install the required libraries:
|
44 |
+
|
45 |
+
```bash
|
46 |
+
pip install -r requirements.txt
|
47 |
+
```
|
48 |
+
3. create a .env file
|
49 |
+
|
50 |
+
4. Open the `.env` file in a text editor.
|
51 |
+
|
52 |
+
5. Add the following line to the `.env` file:
|
53 |
+
|
54 |
+
```
|
55 |
+
GOOGLE_API_KEY=your_google_api_key_here
|
56 |
+
```
|
57 |
+
|
58 |
+
Replace `your_google_api_key_here` with your actual Google API key obtained from [google makersuite](https://makersuite.google.com/app/apikey).
|
59 |
+
Your can also use OPENAI_API_KEY as well
|
60 |
+
|
61 |
+
|
62 |
+
## Usage
|
63 |
+
|
64 |
+
1. Initialize a `SelfDiscover` object with a task:
|
65 |
+
|
66 |
+
```python
|
67 |
+
from self_disover import SelfDiscover
|
68 |
+
from task_example import task1
|
69 |
+
|
70 |
+
result = SelfDiscover(task=task1)
|
71 |
+
```
|
72 |
+
|
73 |
+
2. Call the `SelfDiscover` object:
|
74 |
+
|
75 |
+
```python
|
76 |
+
result()
|
77 |
+
```
|
78 |
+
|
79 |
+
3. Access the selected and adapted modules also implemented reasoning structure:
|
80 |
+
|
81 |
+
```python
|
82 |
+
print(f"SELECTED_MODULES : {result.selected_modules}")
|
83 |
+
print(f"ADAPTED_MODULES : {result.adapted_modules}")
|
84 |
+
print(f"REASONING_STRUCTURE : {result.reasoning_structure}")
|
85 |
+
```
|
86 |
+
|
87 |
+
## Customization
|
88 |
+
|
89 |
+
- Modify the `reasoning_modules` variable in `prompts.py` to add, remove, or modify reasoning modules.
|
90 |
+
- Adjust the prompts in `prompts.py` to customize the user interaction flow.
|
91 |
+
|
92 |
+
## How to use the reasoning JSON structure
|
93 |
+
|
94 |
+
- As mentioned in the paper
|
95 |
+
```markdown
|
96 |
+
For Stage 2, where we use the self-discovered structure to solve the task instances, we start with the prompt: “Follow the
|
97 |
+
step-by-step reasoning plan in JSON to correctly solve the task. Fill in the values following the keys by reasoning specifically
|
98 |
+
about the task given. Do not simply rephrase the keys.”, followed by the reasoning structure, and finally the task instance.
|
99 |
+
```
|
100 |
+
You can now give the task with the reasoning structure with the above prompt
|
101 |
+
|
102 |
+
## Contributing
|
103 |
+
|
104 |
+
Contributions are welcome! Feel free to open issues or pull requests with any improvements or suggestions.
|
105 |
+
|
106 |
+
---
|
107 |
+
|
llm.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import google.generativeai as genai
|
3 |
+
from openai import OpenAI
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
|
6 |
+
load_dotenv()
|
7 |
+
|
8 |
+
generation_config = {
|
9 |
+
"temperature": 0,
|
10 |
+
"top_k": 1,
|
11 |
+
"max_output_tokens": 4000,
|
12 |
+
}
|
13 |
+
|
14 |
+
|
15 |
+
class LLM:
|
16 |
+
def __init__(self, model_name) -> None:
|
17 |
+
self.model_name = model_name
|
18 |
+
self.model = self.create_model(model_name)
|
19 |
+
|
20 |
+
def create_model(self, model_name):
|
21 |
+
match model_name:
|
22 |
+
case "gemini-pro-vision":
|
23 |
+
genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
|
24 |
+
return genai.GenerativeModel(model_name)
|
25 |
+
case "gemini-pro":
|
26 |
+
genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
|
27 |
+
return genai.GenerativeModel(
|
28 |
+
model_name,generation_config=generation_config)
|
29 |
+
case "OpenAI":
|
30 |
+
return OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
31 |
+
case _:
|
32 |
+
print("Not Implemented")
|
33 |
+
|
34 |
+
def __call__(self, prompt, image=None):
|
35 |
+
if self.model_name == 'gemini-pro-vision':
|
36 |
+
response = self.model.generate_content(
|
37 |
+
[image, prompt]
|
38 |
+
)
|
39 |
+
elif self.model_name == "gemini-pro":
|
40 |
+
response = self.model.generate_content(
|
41 |
+
prompt)
|
42 |
+
return response.text
|
43 |
+
|
44 |
+
elif self.model_name == 'openai':
|
45 |
+
res = self.model.chat.completions.create(
|
46 |
+
model="gpt-3.5-turbo-1106",
|
47 |
+
response_format={"type": "json_object"},
|
48 |
+
messages=[
|
49 |
+
# {"role": "system", "content": "You are a helpful assistant."},
|
50 |
+
{"role": "user", "content": f"{prompt}"},
|
51 |
+
],
|
52 |
+
# seed=10,
|
53 |
+
temperature=0
|
54 |
+
)
|
55 |
+
return res.choices[0].message.content
|
prompts.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
reasoning_modules = """
|
2 |
+
1 How could I devise an experiment to help solve that problem?
|
3 |
+
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.
|
4 |
+
3 How could I measure progress on this problem?
|
5 |
+
4 How can I simplify the problem so that it is easier to solve?
|
6 |
+
5 What are the key assumptions underlying this problem?
|
7 |
+
6 What are the potential risks and drawbacks of each solution?
|
8 |
+
7 What are the alternative perspectives or viewpoints on this problem?
|
9 |
+
8 What are the long-term implications of this problem and its solutions?
|
10 |
+
9 How can I break down this problem into smaller, more manageable parts?
|
11 |
+
10 Critical Thinking: This style involves analyzing the problem from different perspectives, questioning assumptions, and evaluating
|
12 |
+
the evidence or information available. It focuses on logical reasoning, evidence-based decision-making, and identifying
|
13 |
+
potential biases or flaws in thinking.
|
14 |
+
11 Try creative thinking, generate innovative and out-of-the-box ideas to solve the problem. Explore unconventional solutions,
|
15 |
+
thinking beyond traditional boundaries, and encouraging imagination and originality.
|
16 |
+
12 Seek input and collaboration from others to solve the problem. Emphasize teamwork, open communication, and leveraging the
|
17 |
+
diverse perspectives and expertise of a group to come up with effective solutions.
|
18 |
+
13 Use systems thinking: Consider the problem as part of a larger system and understanding the interconnectedness of various elements.
|
19 |
+
Focuses on identifying the underlying causes, feedback loops, and interdependencies that influence the problem, and developing holistic
|
20 |
+
solutions that address the system as a whole.
|
21 |
+
14 Use Risk Analysis: Evaluate potential risks, uncertainties, and tradeoffs associated with different solutions or approaches to a
|
22 |
+
problem. Emphasize assessing the potential consequences and likelihood of success or failure, and making informed decisions based
|
23 |
+
on a balanced analysis of risks and benefits.
|
24 |
+
15 Use Reflective Thinking: Step back from the problem, take the time for introspection and self-reflection. Examine personal biases,
|
25 |
+
assumptions, and mental models that may influence problem-solving, and being open to learning from past experiences to improve
|
26 |
+
future approaches.
|
27 |
+
16 What is the core issue or problem that needs to be addressed?
|
28 |
+
17 What are the underlying causes or factors contributing to the problem?
|
29 |
+
18 Are there any potential solutions or strategies that have been tried before? If yes, what were the outcomes and lessons learned?
|
30 |
+
19 What are the potential obstacles or challenges that might arise in solving this problem?
|
31 |
+
20 Are there any relevant data or information that can provide insights into the problem? If yes, what data sources are available,
|
32 |
+
and how can they be analyzed?
|
33 |
+
21 Are there any stakeholders or individuals who are directly affected by the problem? What are their perspectives and needs?
|
34 |
+
22 What resources (financial, human, technological, etc.) are needed to tackle the problem effectively?
|
35 |
+
23 How can progress or success in solving the problem be measured or evaluated?
|
36 |
+
24 What indicators or metrics can be used?
|
37 |
+
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
|
38 |
+
theoretical problem?
|
39 |
+
26 Does the problem involve a physical constraint, such as limited resources, infrastructure, or space?
|
40 |
+
27 Is the problem related to human behavior, such as a social, cultural, or psychological issue?
|
41 |
+
28 Does the problem involve decision-making or planning, where choices need to be made under uncertainty or with competing
|
42 |
+
objectives?
|
43 |
+
29 Is the problem an analytical one that requires data analysis, modeling, or optimization techniques?
|
44 |
+
30 Is the problem a design challenge that requires creative solutions and innovation?
|
45 |
+
31 Does the problem require addressing systemic or structural issues rather than just individual instances?
|
46 |
+
32 Is the problem time-sensitive or urgent, requiring immediate attention and action?
|
47 |
+
33 What kinds of solution typically are produced for this kind of problem specification?
|
48 |
+
34 Given the problem specification and the current best solution, have a guess about other possible solutions.
|
49 |
+
35 Let's imagine the current best solution is totally wrong, what other ways are there to think about the problem specification?
|
50 |
+
36 What is the best way to modify this current best solution, given what you know about these kinds of problem specification?
|
51 |
+
37 Ignoring the current best solution, create an entirely new solution to the problem.
|
52 |
+
38 Let's think step by step.
|
53 |
+
39 Let's make a step by step plan and implement it with good notion and explanation"""
|
54 |
+
|
55 |
+
|
56 |
+
select_prompt = """
|
57 |
+
Select several reasoning modules that are crucial to utilize in order solve the given task:
|
58 |
+
All reasoning module description
|
59 |
+
{resonining_modules}
|
60 |
+
{Task}
|
61 |
+
Select several modules that are crucial for solving the tasks above
|
62 |
+
"""
|
63 |
+
|
64 |
+
adapt_prompt = """
|
65 |
+
Rephrase and specify each reasoning module so that it better helps solving the task:
|
66 |
+
SELECTED module descriptions:
|
67 |
+
{selected_modules}
|
68 |
+
{Task}
|
69 |
+
Adapt each reasoning module description to better solve the tasks:
|
70 |
+
"""
|
71 |
+
|
72 |
+
implement_prompt = """
|
73 |
+
Operationalize the reasoning modules into a step-by-step reasoning plan in JSON format:
|
74 |
+
|
75 |
+
Paired IMPLEMENT step Demonstration
|
76 |
+
Example:
|
77 |
+
This SVG path element <path d="M 55.57,80.69 L 57.38,65.80 M 57.38,65.80 L 48.90,57.46 M 48.90,57.46 L
|
78 |
+
45.58,47.78 M 45.58,47.78 L 53.25,36.07 L 66.29,48.90 L 78.69,61.09 L 55.57,80.69"/> draws a:
|
79 |
+
(A) circle (B) heptagon (C) hexagon (D) kite (E) line (F) octagon (G) pentagon(H) rectangle (I) sector (J) triangle
|
80 |
+
{ "Simplify SVG Path": ...
|
81 |
+
"Breakdown of Path Commands": {
|
82 |
+
"Move to Command (M)": "Sets the starting point for the next
|
83 |
+
command without drawing anything.",
|
84 |
+
"Line to Command (L) steps":
|
85 |
+
{"Start and end coordinates of each line segment":
|
86 |
+
"M 55.57,80.69 L 57.38,65.80: From point (55.57, 80.69) to (57.38,
|
87 |
+
65.80)"}, … and finally closing the shape at (55.57, 80.69)"}
|
88 |
+
"Critical Thinking Analysis": {
|
89 |
+
"Logical Reasoning": {
|
90 |
+
"Analysis of path continuity": "The path includes
|
91 |
+
multiple line segments that connect distinct points. The path ends by
|
92 |
+
connecting back to the starting point, indicating a closed shape.",
|
93 |
+
"Identification of closed shapes": "The final line
|
94 |
+
segment connects the last point back to the first point, which is
|
95 |
+
characteristic of a closed shape."},
|
96 |
+
…
|
97 |
+
"Final Reasoning and Decision": "With 7 distinct points all
|
98 |
+
connected in a closed path, the shape formed is a heptagon.",
|
99 |
+
"Final Answer": "B) heptagon}
|
100 |
+
|
101 |
+
ADAPTED module descriptions:
|
102 |
+
{adapted_modules}
|
103 |
+
{Task}
|
104 |
+
Implement a reasoning structure for solvers to follow step-by-dtep and arrive at correct answers
|
105 |
+
"""
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
openai==1.12.0
|
2 |
+
python-dotenv==1.0.1
|
3 |
+
google-generativeai==0.3.2
|
self_discover.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from prompts import(
|
2 |
+
select_prompt,
|
3 |
+
reasoning_modules,
|
4 |
+
adapt_prompt,
|
5 |
+
implement_prompt
|
6 |
+
)
|
7 |
+
from llm import LLM
|
8 |
+
from task_example import task1
|
9 |
+
|
10 |
+
class SelfDiscover:
|
11 |
+
def __init__(self, task) -> None:
|
12 |
+
self.llm = LLM(model_name="gemini-pro")
|
13 |
+
self.actions = ["SELECT", "ADAPT", "IMPLEMENT"]
|
14 |
+
self.task = task
|
15 |
+
|
16 |
+
def __call__(self):
|
17 |
+
for action in self.actions:
|
18 |
+
print(action)
|
19 |
+
if action == "SELECT":
|
20 |
+
print("yes")
|
21 |
+
prompt = select_prompt.replace("{Task}",self.task)
|
22 |
+
prompt = prompt.replace("{resonining_modules}", reasoning_modules)
|
23 |
+
print(prompt)
|
24 |
+
self.selected_modules = self.llm(prompt)
|
25 |
+
|
26 |
+
elif action == "ADAPT":
|
27 |
+
prompt = adapt_prompt.replace("{Task}",self.task)
|
28 |
+
prompt = prompt.replace("{selected_modules}",self.selected_modules)
|
29 |
+
print(prompt)
|
30 |
+
self.adapted_modules = self.llm(prompt)
|
31 |
+
|
32 |
+
elif action == "IMPLEMENT":
|
33 |
+
prompt = implement_prompt.replace("{Task}",self.task)
|
34 |
+
prompt = prompt.replace("{adapted_modules}", self.adapted_modules)
|
35 |
+
print(prompt)
|
36 |
+
self.reasoning_structure = self.llm(prompt)
|
37 |
+
|
38 |
+
|
39 |
+
if __name__=="__main__":
|
40 |
+
result = SelfDiscover(task=task1)
|
41 |
+
result()
|
42 |
+
print(f"SELECTED_MODULES : {result.selected_modules}")
|
43 |
+
print(f"ADAPTED_MODULES : {result.adapted_modules}")
|
44 |
+
print(f"REASONING_STRUCTURE : {result.reasoning_structure}")
|
task_example.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
task1 =""""
|
2 |
+
You will be provided with unstructured data and your task is to accurately extract property answers to a set of questions
|
3 |
+
from within the context.
|
4 |
+
The context is from a report which provides with details of subject property as key value pairs
|
5 |
+
|
6 |
+
<context>
|
7 |
+
|
8 |
+
Distance from Subject: 0.20
|
9 |
+
Comp 2
|
10 |
+
Address: Kite street,207, CA
|
11 |
+
Owner: JISHNU S
|
12 |
+
Sale Price: $455,000
|
13 |
+
Living Area: 2100
|
14 |
+
APN: 04-05664-05660-05505
|
15 |
+
Lot Area: 5000
|
16 |
+
Total Rooms: 4
|
17 |
+
Bedrooms: 2
|
18 |
+
Sale Date: 14/10/2021
|
19 |
+
Year Built: 1823
|
20 |
+
|
21 |
+
</context>
|
22 |
+
|
23 |
+
<questions>
|
24 |
+
|
25 |
+
0. what is the address of subject property
|
26 |
+
1. which city is the subject property located
|
27 |
+
2. which county is the subject property located
|
28 |
+
3. what is the owner name of the subject property
|
29 |
+
4. what id the loan number of the subject property
|
30 |
+
5. wht is the Total assessed value of subject property
|
31 |
+
6. what is the assessed value of improvements to the subject property
|
32 |
+
7. what is the assessed value of Land of the subject property
|
33 |
+
8. what is the estimated value of subject property
|
34 |
+
9. what is the date of the estimated value of the subject property
|
35 |
+
10. what is the estimated value range of the subject property
|
36 |
+
11. what is the processed date of the subject property
|
37 |
+
12. What is the confidence score of the subject property
|
38 |
+
13. what is the forecast standard deviation of the subject property
|
39 |
+
14. which state is the property located in of the subject property
|
40 |
+
|
41 |
+
</questions>
|
42 |
+
"""
|