Skip to content

Commit a5e7d96

Browse files
authored
Merge pull request #957 from Pingdred/no-async
No async because Piero is paniking
2 parents 3b99a54 + cf41a45 commit a5e7d96

File tree

9 files changed

+26
-50
lines changed

9 files changed

+26
-50
lines changed

core/cat/agents/base_agent.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ class AgentOutput(BaseModelDict):
1313
class BaseAgent(ABC):
1414

1515
@abstractmethod
16-
async def execute(*args, **kwargs) -> AgentOutput:
16+
def execute(*args, **kwargs) -> AgentOutput:
1717
pass

core/cat/agents/form_agent.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
class FormAgent(BaseAgent):
77

8-
async def execute(self, stray) -> AgentOutput:
8+
def execute(self, stray) -> AgentOutput:
99

1010
# get active form from working memory
1111
active_form = stray.working_memory.active_form

core/cat/agents/main_agent.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def __init__(self):
2626
else:
2727
self.verbose = False
2828

29-
async def execute(self, stray) -> AgentOutput:
29+
def execute(self, stray) -> AgentOutput:
3030
"""Execute the agents.
3131
3232
Returns
@@ -66,15 +66,15 @@ async def execute(self, stray) -> AgentOutput:
6666

6767
# run tools and forms
6868
procedures_agent = ProceduresAgent()
69-
procedures_agent_out : AgentOutput = await procedures_agent.execute(stray)
69+
procedures_agent_out : AgentOutput = procedures_agent.execute(stray)
7070
if procedures_agent_out.return_direct:
7171
return procedures_agent_out
7272

7373
# we run memory agent if:
7474
# - no procedures were recalled or selected or
7575
# - procedures have all return_direct=False
7676
memory_agent = MemoryAgent()
77-
memory_agent_out : AgentOutput = await memory_agent.execute(
77+
memory_agent_out : AgentOutput = memory_agent.execute(
7878
# TODO: should all agents only receive stray?
7979
stray, prompt_prefix, prompt_suffix
8080
)

core/cat/agents/memory_agent.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class MemoryAgent(BaseAgent):
1313

14-
async def execute(self, stray, prompt_prefix, prompt_suffix) -> AgentOutput:
14+
def execute(self, stray, prompt_prefix, prompt_suffix) -> AgentOutput:
1515

1616
prompt_variables = stray.working_memory.agent_input.model_dump()
1717
sys_prompt = prompt_prefix + prompt_suffix

core/cat/agents/procedures_agent.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ class ProceduresAgent(BaseAgent):
2424
form_agent = FormAgent()
2525
allowed_procedures: Dict[str, CatTool | CatForm] = {}
2626

27-
async def execute(self, stray) -> AgentOutput:
27+
def execute(self, stray) -> AgentOutput:
2828

2929
# Run active form if present
30-
form_output: AgentOutput = await self.form_agent.execute(stray)
30+
form_output: AgentOutput = self.form_agent.execute(stray)
3131
if form_output.return_direct:
3232
return form_output
3333

@@ -38,7 +38,7 @@ async def execute(self, stray) -> AgentOutput:
3838
log.debug(f"Procedural memories retrived: {len(procedural_memories)}.")
3939

4040
try:
41-
procedures_result: AgentOutput = await self.execute_procedures(stray)
41+
procedures_result: AgentOutput = self.execute_procedures(stray)
4242
if procedures_result.return_direct:
4343
# exit agent if a return_direct procedure was executed
4444
return procedures_result
@@ -64,7 +64,7 @@ async def execute(self, stray) -> AgentOutput:
6464
return AgentOutput()
6565

6666

67-
async def execute_procedures(self, stray):
67+
def execute_procedures(self, stray):
6868

6969
# using some hooks
7070
mad_hatter = MadHatter()
@@ -87,13 +87,13 @@ async def execute_procedures(self, stray):
8787
)
8888

8989
# Execute chain and obtain a choice of procedure from the LLM
90-
llm_action: LLMAction = await self.execute_chain(stray, procedures_prompt_template, allowed_procedures)
90+
llm_action: LLMAction = self.execute_chain(stray, procedures_prompt_template, allowed_procedures)
9191

9292
# route execution to subagents
93-
return await self.execute_subagents(stray, llm_action, allowed_procedures)
93+
return self.execute_subagents(stray, llm_action, allowed_procedures)
9494

9595

96-
async def execute_chain(self, stray, procedures_prompt_template, allowed_procedures) -> LLMAction:
96+
def execute_chain(self, stray, procedures_prompt_template, allowed_procedures) -> LLMAction:
9797

9898
# Prepare info to fill up the prompt
9999
prompt_variables = {
@@ -136,15 +136,15 @@ async def execute_chain(self, stray, procedures_prompt_template, allowed_procedu
136136
return llm_action
137137

138138

139-
async def execute_subagents(self, stray, llm_action, allowed_procedures):
139+
def execute_subagents(self, stray, llm_action, allowed_procedures):
140140
# execute chosen tool / form
141141
# loop over allowed tools and forms
142142
if llm_action.action:
143143
chosen_procedure = allowed_procedures.get(llm_action.action, None)
144144
try:
145145
if Plugin._is_cat_tool(chosen_procedure):
146146
# execute tool
147-
tool_output = await chosen_procedure._arun(llm_action.action_input, stray=stray)
147+
tool_output = chosen_procedure.run(llm_action.action_input, stray=stray)
148148
return AgentOutput(
149149
output=tool_output,
150150
return_direct=chosen_procedure.return_direct,
@@ -158,7 +158,7 @@ async def execute_subagents(self, stray, llm_action, allowed_procedures):
158158
# store active form in working memory
159159
stray.working_memory.active_form = form_instance
160160
# execute form
161-
return await self.form_agent.execute(stray)
161+
return self.form_agent.execute(stray)
162162

163163
except Exception as e:
164164
log.error(f"Error executing {chosen_procedure.procedure_type} `{chosen_procedure.name}`")

core/cat/looking_glass/stray_cat.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ def __init__(
4343

4444
self.__main_loop = main_loop
4545

46-
self.__loop = asyncio.new_event_loop()
47-
4846
def __repr__(self):
4947
return f"StrayCat(user_id={self.user_id})"
5048

@@ -342,7 +340,7 @@ def llm(self, prompt: str, stream: bool = False) -> str:
342340

343341
return output
344342

345-
async def __call__(self, message_dict):
343+
def __call__(self, message_dict):
346344
"""Call the Cat instance.
347345
348346
This method is called on the user's message received from the client.
@@ -408,7 +406,7 @@ async def __call__(self, message_dict):
408406

409407
# reply with agent
410408
try:
411-
agent_output: AgentOutput = await self.main_agent.execute(self)
409+
agent_output: AgentOutput = self.main_agent.execute(self)
412410
except Exception as e:
413411
# This error happens when the LLM
414412
# does not respect prompt instructions.
@@ -472,7 +470,7 @@ async def __call__(self, message_dict):
472470

473471
def run(self, user_message_json, return_message=False):
474472
try:
475-
cat_message = self.loop.run_until_complete(self.__call__(user_message_json))
473+
cat_message = self.__call__(user_message_json)
476474
if return_message:
477475
# return the message for HTTP usage
478476
return cat_message
@@ -648,7 +646,3 @@ def main_agent(self):
648646
@property
649647
def white_rabbit(self):
650648
return CheshireCat().white_rabbit
651-
652-
@property
653-
def loop(self):
654-
return self.__loop

core/cat/mad_hatter/decorators/tool.py

+4-22
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
# All @tool decorated functions in plugins become a CatTool.
1111
# The difference between base langchain Tool and CatTool is that CatTool has an instance of the cat as attribute (set by the MadHatter)
12-
class CatTool(BaseTool):
12+
class CatTool:
1313
def __init__(
1414
self,
1515
name: str,
@@ -18,12 +18,7 @@ def __init__(
1818
examples: List[str] = [],
1919
):
2020
description = func.__doc__.strip()
21-
22-
# call parent contructor
23-
super().__init__(
24-
name=name, func=func, description=description, return_direct=return_direct
25-
)
26-
21+
2722
self.func = func
2823
self.procedure_type = "tool"
2924
self.name = name
@@ -44,21 +39,8 @@ def start_examples(self):
4439
def __repr__(self) -> str:
4540
return f"CatTool(name={self.name}, return_direct={self.return_direct}, description={self.description})"
4641

47-
# we run tools always async, even if they are not defined so in a plugin
48-
def _run(self, input_by_llm: str) -> str:
49-
pass # do nothing
50-
51-
# we run tools always async, even if they are not defined so in a plugin
52-
async def _arun(self, input_by_llm, stray):
53-
54-
# await if the tool is async
55-
if inspect.iscoroutinefunction(self.func):
56-
return await self.func(input_by_llm, cat=stray)
57-
58-
# run in executor if the tool is not async
59-
return await stray.loop.run_in_executor(
60-
None, self.func, input_by_llm, stray
61-
)
42+
def run(self, input_by_llm: str, stray) -> str:
43+
return self.func(input_by_llm, cat=stray)
6244

6345
# override `extra = 'forbid'` for Tool pydantic model in langchain
6446
class Config:

core/tests/agents/test_main_agent.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_main_agent_instantiation(main_agent):
1717
@pytest.mark.asyncio # to test async functions
1818
async def test_execute_main_agent(main_agent, stray):
1919
# empty agent execution
20-
out = await main_agent.execute(stray)
20+
out = main_agent.execute(stray)
2121
assert isinstance(out, AgentOutput)
2222
assert not out.return_direct
2323
assert out.intermediate_steps == []

core/tests/looking_glass/test_stray_cat.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_stray_nlp(stray):
2929
def test_stray_call(stray):
3030
msg = {"text": "Where do I go?", "user_id": "Alice"}
3131

32-
reply = stray.loop.run_until_complete(stray.__call__(msg))
32+
reply = stray.__call__(msg)
3333

3434
assert isinstance(reply, CatMessage)
3535
assert "You did not configure" in reply.content
@@ -57,7 +57,7 @@ def test_recall_to_working_memory(stray):
5757
msg = {"text": msg_text, "user_id": "Alice"}
5858

5959
# send message
60-
stray.loop.run_until_complete(stray.__call__(msg))
60+
stray.__call__(msg)
6161

6262
# recall after episodic memory was stored
6363
stray.recall_relevant_memories_to_working_memory(msg_text)

0 commit comments

Comments
 (0)