Skip to content

Commit 7abd4d6

Browse files
author
steven
committed
1. added AProcessor.SaveMsg() function to simplify the code.
1 parent 0c60624 commit 7abd4d6

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

ailice/core/AProcessor.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,17 @@ def Prepare(self):
110110
self.interpreter.RegisterAction("_FUNCTION_CALL_DEFAULT", {"func": self.EvalFunctionCallDefault, "noEval": ["funcName", "paras"]})
111111
return
112112

113+
def SaveMsg(self, role: str, msg: str, storeMsg: str = None, logMsg: str = None, logger = None, entry: bool = False):
114+
self.conversation.Add(role=role, msg=msg, env=self.interpreter.env, entry=entry)
115+
if storeMsg:
116+
self.EvalStore(storeMsg)
117+
if logMsg and logger:
118+
logger(f"{role}_{self.name}", logMsg)
119+
return
120+
113121
def __call__(self, txt: str) -> str:
114-
self.conversation.Add(role = "USER", msg = txt, env = self.interpreter.env, entry=True)
115-
self.EvalStore(txt)
122+
self.SaveMsg(role="USER", msg=txt, storeMsg=txt, entry=True)
123+
116124
with ALoggerSection(recv=self.outputCB) as loggerSection:
117125
loggerSection(f"USER_{self.name}", txt)
118126

@@ -122,25 +130,21 @@ def __call__(self, txt: str) -> str:
122130
with ALoggerMsg(recv=self.outputCB, channel="ASSISTANT_" + self.name) as loggerMsg:
123131
ret = self.llm.Generate(prompt, proc=loggerMsg, endchecker=self.interpreter.EndChecker, temperature = config.temperature)
124132
ret = "System notification: The empty output was detected, which is usually caused by an agent error. You can urge it to resolve this issue and return meaningful information." if "" == ret.strip() else ret
125-
self.conversation.Add(role = "ASSISTANT", msg = ret, env = self.interpreter.env)
126-
self.EvalStore(ret)
133+
self.SaveMsg(role="ASSISTANT", msg=ret, storeMsg=ret)
127134
self.result = ret
128135

129136
msg = self.messenger.GetPreviousMsg()
130137
if msg != None:
131138
resp = f"Interruption. Reminder from super user: {msg}"
132-
self.conversation.Add(role = "SYSTEM", msg = resp, env = self.interpreter.env)
133-
self.EvalStore(resp)
134-
loggerSection(f"SYSTEM_{self.name}", resp)
139+
self.SaveMsg(role="SYSTEM", msg=resp, storeMsg=resp, loggerMsg=resp, logger=loggerSection)
135140
continue
136141

137142
resp = self.interpreter.EvalEntries(ret)
138143

139144
if "" != resp:
140145
self.interpreter.EvalVar(varName="returned_content_in_last_function_call", content=resp)
141-
self.conversation.Add(role = "SYSTEM", msg = "This is a system-generated message. Since the function call in your previous message has returned information, the response to this message will be handled by the backend system instead of the user. Meanwhile, your previous message has been marked as private and has not been sent to the user. Function returned: {" + resp + "}\n\nThe returned text has been automatically saved to variable 'returned_content_in_last_function_call' for quick reference.", env = self.interpreter.env)
142-
self.EvalStore("Function returned: {" + resp + "}")
143-
loggerSection(f"SYSTEM_{self.name}", resp)
146+
m = "This is a system-generated message. Since the function call in your previous message has returned information, the response to this message will be handled by the backend system instead of the user. Meanwhile, your previous message has been marked as private and has not been sent to the user. Function returned: {" + resp + "}\n\nThe returned text has been automatically saved to variable 'returned_content_in_last_function_call' for quick reference."
147+
self.SaveMsg(role="SYSTEM", msg=m, storeMsg="Function returned: {" + resp + "}", logMsg=resp, logger=loggerSection)
144148
else:
145149
return self.result
146150

0 commit comments

Comments
 (0)