-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
130 lines (112 loc) · 3.54 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
import os
import sys
from typing import Tuple
from dotenv import load_dotenv
from revChatGPT.V1 import Chatbot
from tqdm import tqdm
def chatgpt_answer_to_prompt_file(
chatbot_instance: Chatbot,
prompt_file: str,
to_be_replaced_word: str,
string: str,
conversation_id: str | None = None,
) -> Tuple[str, str]:
answer = ""
convo_id = conversation_id or ""
with open(prompt_file) as f:
prompt = f.read().replace(to_be_replaced_word, string)
try:
for data in chatbot_instance.ask(
prompt, conversation_id=conversation_id, auto_continue=True
):
answer = data["message"]
if not convo_id:
convo_id = data["conversation_id"]
except Exception as e:
print("Error: ", e, file=sys.stderr)
finally:
return answer, convo_id
def main():
load_dotenv()
access_token = os.environ.get("OPENAI_ACCESS_TOKEN")
if not access_token:
print("Error: OPENAI_ACCESS_TOKEN not set", file=sys.stderr)
sys.exit(1)
chatbot = Chatbot(config={"access_token": access_token})
problem = ""
print("Explain your problem here\n")
while problem == "":
problem = input().strip()
conversation_id = None
iterations = 2
total_work_units = 5 + iterations * 3
progress_bar = tqdm(
total=total_work_units, desc="Processing", ncols=100, ascii=True
)
# prompt1
three_ideas, conversation_id = chatgpt_answer_to_prompt_file(
chatbot, "prompts/prompt1", "PROBLEM", problem
)
progress_bar.update(1)
evaluated_three_ideas, _ = chatgpt_answer_to_prompt_file(
chatbot, "prompts/prompt2", "3IDEAS", three_ideas, conversation_id
)
progress_bar.update(1)
winning_idea, _ = chatgpt_answer_to_prompt_file(
chatbot,
"prompts/prompt3",
"WINNINGIDEA",
evaluated_three_ideas,
conversation_id,
)
progress_bar.update(1)
for _ in range(iterations):
new_ideas = ""
with open("prompts/prompt4") as f:
prompt4 = (
f.read().replace("WINNING2", winning_idea).replace("PROBLEM", problem)
)
try:
for data in chatbot.ask(
prompt4, conversation_id=conversation_id, auto_continue=True
):
new_ideas = data["message"]
except Exception as e:
print("Error:", e, file=sys.stderr)
finally:
pass
# print("NEW IDEAS : ", new_ideas)
progress_bar.update(1)
evaluated_new_ideas, _ = chatgpt_answer_to_prompt_file(
chatbot,
"prompts/prompt5",
"WLOOP",
new_ideas,
conversation_id,
)
progress_bar.update(1)
winning_idea, _ = chatgpt_answer_to_prompt_file(
chatbot,
"prompts/prompt3",
"WINNINGIDEA",
evaluated_new_ideas,
conversation_id,
)
progress_bar.update(1)
evaluated_winner_idea, _ = chatgpt_answer_to_prompt_file(
chatbot,
"prompts/prompt6",
"WINNER",
winning_idea,
conversation_id,
)
progress_bar.update(1)
solution, _ = chatgpt_answer_to_prompt_file(
chatbot, "prompts/prompt7", "WINNER2", evaluated_winner_idea, conversation_id
)
progress_bar.update(1)
print()
print(solution)
chatbot.delete_conversation(conversation_id)
if __name__ == "__main__":
main()