forked from microsoft/TaskWeaver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.py
243 lines (207 loc) · 9.11 KB
/
session.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import os
import shutil
from typing import Any, Dict, List, Literal, Optional
from injector import Injector, inject
from taskweaver.code_interpreter import CodeInterpreter, CodeInterpreterPluginOnly
from taskweaver.code_interpreter.code_executor import CodeExecutor
from taskweaver.config.module_config import ModuleConfig
from taskweaver.logging import TelemetryLogger
from taskweaver.memory import Memory, Post, Round
from taskweaver.module.event_emitter import SessionEventEmitter, SessionEventHandler
from taskweaver.planner.planner import Planner
from taskweaver.workspace.workspace import Workspace
class AppSessionConfig(ModuleConfig):
def _configure(self) -> None:
self._set_name("session")
self.code_interpreter_only = self._get_bool("code_interpreter_only", False)
self.max_internal_chat_round_num = self._get_int("max_internal_chat_round_num", 10)
self.plugin_only_mode = self._get_bool("plugin_only_mode", False)
self.experience_dir = self._get_path(
"experience_dir",
os.path.join(self.src.app_base_path, "experience"),
)
class Session:
@inject
def __init__(
self,
session_id: str,
workspace: Workspace,
app_injector: Injector,
logger: TelemetryLogger,
config: AppSessionConfig, # TODO: change to SessionConfig
) -> None:
assert session_id is not None, "session_id must be provided"
self.logger = logger
self.session_injector = app_injector.create_child_injector()
self.config = config
self.session_id: str = session_id
self.workspace = workspace.get_session_dir(self.session_id)
self.execution_cwd = os.path.join(self.workspace, "cwd")
self.init()
self.round_index = 0
self.memory = Memory(session_id=self.session_id)
self.session_var: Dict[str, str] = {}
self.event_emitter = self.session_injector.get(SessionEventEmitter)
self.session_injector.binder.bind(SessionEventEmitter, self.event_emitter)
self.planner = self.session_injector.create_object(
Planner,
{
"plugin_only": self.config.plugin_only_mode,
},
)
self.session_injector.binder.bind(Planner, self.planner)
self.code_executor = self.session_injector.create_object(
CodeExecutor,
{
"session_id": self.session_id,
"workspace": self.workspace,
"execution_cwd": self.execution_cwd,
},
)
self.session_injector.binder.bind(CodeExecutor, self.code_executor)
if self.config.plugin_only_mode:
self.code_interpreter = self.session_injector.get(CodeInterpreterPluginOnly)
else:
self.code_interpreter = self.session_injector.get(CodeInterpreter)
self.max_internal_chat_round_num = self.config.max_internal_chat_round_num
self.internal_chat_num = 0
self.logger.dump_log_file(
self,
file_path=os.path.join(self.workspace, f"{self.session_id}.json"),
)
def init(self):
if not os.path.exists(self.workspace):
os.makedirs(self.workspace)
if not os.path.exists(self.execution_cwd):
os.makedirs(self.execution_cwd)
if not os.path.exists(self.config.experience_dir):
os.makedirs(self.config.experience_dir)
self.logger.info(f"Session {self.session_id} is initialized")
def update_session_var(self, variables: Dict[str, str]):
self.session_var.update(variables)
def _send_text_message(self, message: str) -> Round:
chat_round = self.memory.create_round(user_query=message)
self.event_emitter.start_round(chat_round.id)
def _send_message(recipient: str, post: Post) -> Post:
chat_round.add_post(post)
use_back_up_engine = True if recipient == post.send_from else False
self.logger.info(f"Use back up engine: {use_back_up_engine}")
if recipient == "Planner":
reply_post = self.planner.reply(
self.memory,
prompt_log_path=os.path.join(
self.workspace,
f"planner_prompt_log_{chat_round.id}_{post.id}.json",
),
use_back_up_engine=use_back_up_engine,
)
elif recipient == "CodeInterpreter":
reply_post = self.code_interpreter.reply(
self.memory,
prompt_log_path=os.path.join(
self.workspace,
f"code_generator_prompt_log_{chat_round.id}_{post.id}.json",
),
use_back_up_engine=use_back_up_engine,
)
else:
raise Exception(f"Unknown recipient {recipient}")
return reply_post
try:
if not self.config.code_interpreter_only:
post = Post.create(message=message, send_from="User", send_to="Planner")
while True:
post = _send_message(post.send_to, post)
self.logger.info(
f"{post.send_from} talk to {post.send_to}: {post.message}",
)
self.internal_chat_num += 1
if post.send_to == "User":
chat_round.add_post(post)
self.internal_chat_num = 0
break
if self.internal_chat_num >= self.max_internal_chat_round_num:
raise Exception(
f"Internal chat round number exceeds the limit of {self.max_internal_chat_round_num}",
)
else:
post = Post.create(
message=message,
send_from="Planner",
send_to="CodeInterpreter",
)
while True:
if post.send_to == "Planner":
reply_post = Post.create(
message=post.message,
send_from="CodeInterpreter",
send_to="User",
)
chat_round.add_post(reply_post)
break
else:
post = _send_message("CodeInterpreter", post)
self.round_index += 1
chat_round.change_round_state("finished")
except Exception as e:
import traceback
stack_trace_str = traceback.format_exc()
self.logger.error(stack_trace_str)
chat_round.change_round_state("failed")
err_message = f"Cannot process your request due to Exception: {str(e)} \n {stack_trace_str}"
self.event_emitter.emit_error(err_message)
finally:
self.internal_chat_num = 0
self.logger.dump_log_file(
chat_round,
file_path=os.path.join(
self.workspace,
f"{self.session_id}_{chat_round.id}.json",
),
)
self.event_emitter.end_round(chat_round.id)
return chat_round
def send_message(
self,
message: str,
event_handler: Optional[SessionEventHandler] = None,
files: Optional[List[Dict[Literal["name", "path", "content"], Any]]] = None,
) -> Round:
message_prefix = ""
if files is not None:
file_names: List[str] = []
for file_info in files:
file_name = file_info["name"]
file_path = file_info.get("path", None)
file_content = file_info.get("content", None)
file_names.append(self._upload_file(file_name, file_path, file_content))
if len(file_names) > 0:
message_prefix += f"files added: {', '.join(file_names)}.\n"
with self.event_emitter.handle_events_ctx(event_handler):
return self._send_text_message(message_prefix + message)
def _upload_file(self, name: str, path: Optional[str] = None, content: Optional[bytes] = None) -> str:
target_name = name.split("/")[-1]
target_path = self.get_full_path(self.execution_cwd, target_name)
if path is not None:
shutil.copyfile(path, target_path)
return target_name
if content is not None:
with open(target_path, "wb") as f:
f.write(content)
return target_name
raise ValueError("path or file_content")
def get_full_path(self, *file_path: str, in_execution_cwd: bool = False) -> str:
return str(
os.path.realpath(
os.path.join(
self.workspace if not in_execution_cwd else self.execution_cwd,
*file_path, # type: ignore
),
),
)
def to_dict(self) -> Dict[str, str]:
return {
"session_id": self.session_id,
"workspace": self.workspace,
"execution_cwd": self.execution_cwd,
}