Ali Abid commited on
Commit
a2a7b95
1 Parent(s): 9617f86
Files changed (2) hide show
  1. ui/.gitignore +1 -0
  2. ui/api.py +111 -61
ui/.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ state/*
ui/api.py CHANGED
@@ -1,22 +1,41 @@
1
  import os, sys
2
  import utils
 
 
 
3
 
4
  FILE_DIR = os.path.dirname(os.path.abspath(__file__))
5
  REPO_DIR = os.path.dirname(FILE_DIR)
6
  sys.path.append(REPO_DIR)
7
- import threading
8
- from functools import partial
9
  import time
10
 
 
11
  def get_openai_api_key():
12
  return os.getenv("OPENAI_API_KEY")
13
 
 
14
  running_apis = []
15
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  class AutoAPI:
17
  def __init__(self, openai_key, ai_name, ai_role, top_5_goals):
18
- print(openai_key)
19
  self.openai_key = openai_key
 
 
 
 
 
20
  newline = "\n"
21
  with open(os.path.join(REPO_DIR, "ai_settings.yaml"), "w") as f:
22
  f.write(
@@ -26,68 +45,99 @@ ai_name: {ai_name}
26
  ai_role: {ai_role}
27
  """
28
  )
 
 
 
 
 
 
 
29
 
30
-
31
- thread = threading.Thread(target=self.client_thread)
32
- thread.start()
33
- self.thread = thread
34
- self.pending_input = None
35
- self.awaiting_input = False
36
- self.messages = []
37
- self.last_message_read_index = -1
38
-
39
- def add_message(self, title, content):
40
- # print(f"{title}: {content}")
41
- self.messages.append((title, content))
42
-
43
- def client_thread(self):
44
- os.environ["OPENAI_API_KEY"] = self.openai_key
45
- import autogpt.config.config
46
- from autogpt.logs import logger
47
- from autogpt.cli import main
48
- import autogpt.utils
49
- from autogpt.spinner import Spinner
50
-
51
-
52
- def typewriter_log(self, title="", title_color="", content="", *args, **kwargs):
53
- self.add_message(title, content)
54
-
55
- def warn(self, message, title="", *args, **kwargs):
56
- self.add_message(title, message)
57
-
58
- def error(self, title, message="", *args, **kwargs):
59
- self.add_message(title, message)
60
-
61
- def clean_input(self, prompt=""):
62
- self.add_message(None, prompt)
63
- self.awaiting_input = True
64
- while self.pending_input is None:
65
- time.sleep(1)
66
- pending_input = self.pending_input
67
- self.pending_input = None
68
- print("Sending message:", pending_input)
69
- return pending_input
70
-
71
- def spinner_start(self):
72
- self.add_message(None, "Thinking...")
73
-
74
- logger.typewriter_log = partial(typewriter_log, self)
75
- logger.warn = partial(warn, self)
76
- logger.error = partial(error, self)
77
- autogpt.utils.clean_input = partial(clean_input, self)
78
- Spinner.spin = partial(spinner_start, self)
79
-
80
- main()
81
 
82
  def send_message(self, message="Y"):
83
- self.pending_input = message
84
- self.awaiting_input = False
 
 
85
 
86
  def get_chatbot_response(self):
87
- while (not self.awaiting_input) or self.last_message_read_index < len(self.messages) - 1:
88
- if self.last_message_read_index >= len(self.messages) - 1:
 
 
 
 
 
 
89
  time.sleep(1)
90
  else:
91
- self.last_message_read_index += 1
92
- title, content = self.messages[self.last_message_read_index]
93
- yield (f"**{title.strip()}** " if title else "") + utils.remove_color(content).replace("\n", "<br />")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os, sys
2
  import utils
3
+ import uuid
4
+ import json
5
+ import subprocess, threading
6
 
7
  FILE_DIR = os.path.dirname(os.path.abspath(__file__))
8
  REPO_DIR = os.path.dirname(FILE_DIR)
9
  sys.path.append(REPO_DIR)
 
 
10
  import time
11
 
12
+
13
  def get_openai_api_key():
14
  return os.getenv("OPENAI_API_KEY")
15
 
16
+
17
  running_apis = []
18
 
19
+
20
+ def get_state(state_file):
21
+ with open(state_file, "r") as f:
22
+ state = json.load(f)
23
+ return state
24
+
25
+
26
+ def set_state(state_file, state):
27
+ with open(state_file, "w") as f:
28
+ json.dump(state, f)
29
+
30
+
31
  class AutoAPI:
32
  def __init__(self, openai_key, ai_name, ai_role, top_5_goals):
 
33
  self.openai_key = openai_key
34
+ hex = uuid.uuid4().hex
35
+ print(hex)
36
+ self.state_file = os.path.join(FILE_DIR, "state", f"state_{hex}.json")
37
+ self.log_file = os.path.join(FILE_DIR, "state", f"log_{hex}.json")
38
+
39
  newline = "\n"
40
  with open(os.path.join(REPO_DIR, "ai_settings.yaml"), "w") as f:
41
  f.write(
 
45
  ai_role: {ai_role}
46
  """
47
  )
48
+ state = {
49
+ "pending_input": None,
50
+ "awaiting_input": False,
51
+ "messages": [],
52
+ "last_message_read_index": -1,
53
+ }
54
+ set_state(self.state_file, state)
55
 
56
+ with open(self.log_file, "w") as f:
57
+ subprocess.Popen(
58
+ [
59
+ "python",
60
+ os.path.join(REPO_DIR, "ui", "api.py"),
61
+ openai_key,
62
+ self.state_file,
63
+ ],
64
+ cwd=REPO_DIR,
65
+ stdout=f,
66
+ stderr=f,
67
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
  def send_message(self, message="Y"):
70
+ state = get_state(self.state_file)
71
+ state["pending_input"] = message
72
+ state["awaiting_input"] = False
73
+ set_state(self.state_file, state)
74
 
75
  def get_chatbot_response(self):
76
+ while True:
77
+ state = get_state(self.state_file)
78
+ if (
79
+ state["awaiting_input"]
80
+ and state["last_message_read_index"] >= len(state["messages"]) - 1
81
+ ):
82
+ break
83
+ if state["last_message_read_index"] >= len(state["messages"]) - 1:
84
  time.sleep(1)
85
  else:
86
+ state["last_message_read_index"] += 1
87
+ title, content = state["messages"][state["last_message_read_index"]]
88
+ yield (f"**{title.strip()}** " if title else "") + utils.remove_color(
89
+ content
90
+ ).replace("\n", "<br />")
91
+ set_state(self.state_file, state)
92
+
93
+
94
+ if __name__ == "__main__":
95
+ print(sys.argv)
96
+ _, openai_key, state_file = sys.argv
97
+ os.environ["OPENAI_API_KEY"] = openai_key
98
+ import autogpt.config.config
99
+ from autogpt.logs import logger
100
+ from autogpt.cli import main
101
+ import autogpt.utils
102
+ from autogpt.spinner import Spinner
103
+
104
+ def add_message(title, content):
105
+ state = get_state(state_file)
106
+ state["messages"].append((title, content))
107
+ set_state(state_file, state)
108
+
109
+ def typewriter_log(title="", title_color="", content="", *args, **kwargs):
110
+ add_message(title, content)
111
+
112
+ def warn(message, title="", *args, **kwargs):
113
+ add_message(title, message)
114
+
115
+ def error(title, message="", *args, **kwargs):
116
+ add_message(title, message)
117
+
118
+ def clean_input(prompt=""):
119
+ add_message(None, prompt)
120
+ state = get_state(state_file)
121
+ state["awaiting_input"] = True
122
+ set_state(state_file, state)
123
+ while state["pending_input"] is None:
124
+ state = get_state(state_file)
125
+ print("Waiting for input...")
126
+ time.sleep(1)
127
+ print("Got input")
128
+ pending_input = state["pending_input"]
129
+ state["pending_input"] = None
130
+ set_state(state_file, state)
131
+ return pending_input
132
+
133
+ def spinner_start():
134
+ add_message(None, "Thinking...")
135
+
136
+ logger.typewriter_log = typewriter_log
137
+ logger.warn = warn
138
+ logger.error = error
139
+ autogpt.utils.clean_input = clean_input
140
+ Spinner.spin = spinner_start
141
+
142
+ sys.argv = sys.argv[:1]
143
+ main()