Skip to content

Commit

Permalink
Merge pull request cheshire-cat-ai#781 from Pingdred/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
pieroit authored Apr 22, 2024
2 parents 1570a59 + 3c2aa1b commit 59c1391
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
19 changes: 13 additions & 6 deletions core/cat/looking_glass/agent_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async def execute_procedures_agent(self, agent_input, stray):
if "form" in out.keys():
FormClass = allowed_procedures.get(out["form"], None)
f = FormClass(stray)
stray.working_memory["forms"] = f
stray.working_memory["active_form"] = f
# let the form reply directly
out = f.next()
out["return_direct"] = True
Expand All @@ -137,12 +137,12 @@ async def execute_procedures_agent(self, agent_input, stray):

async def execute_form_agent(self, stray):

active_form = stray.working_memory.get("forms", None)
active_form = stray.working_memory.get("active_form", None)
if active_form:
log.warning(active_form._state)
# closing form if state is closed
if active_form._state == CatFormState.CLOSED:
del stray.working_memory["forms"]
del stray.working_memory["active_form"]
else:
# continue form
return active_form.next()
Expand All @@ -165,7 +165,7 @@ async def execute_memory_chain(self, agent_input, prompt_prefix, prompt_suffix,
output_key="output"
)

return await memory_chain.ainvoke(agent_input, config=RunnableConfig(callbacks=[NewTokenHandler(stray)]))
return await memory_chain.ainvoke({**agent_input, "stop":"Human:"}, config=RunnableConfig(callbacks=[NewTokenHandler(stray)]))

async def execute_agent(self, stray):
"""Instantiate the Agent with tools.
Expand Down Expand Up @@ -231,8 +231,14 @@ async def execute_agent(self, stray):
# - no procedures where recalled or selected or
# - procedures have all return_direct=False or
# - procedures agent crashed big time
if "tools_output" not in agent_input:
agent_input["tools_output"] = ""

# Save tools output
tools_output = agent_input.get("tools_output", "")
# Update agent input from working memory
agent_input = self.format_agent_input(stray)
# Add eventuals tools output
agent_input["tools_output"] = tools_output

memory_chain_output = await self.execute_memory_chain(agent_input, prompt_prefix, prompt_suffix, stray)
memory_chain_output["intermediate_steps"] = intermediate_steps

Expand Down Expand Up @@ -280,6 +286,7 @@ def format_agent_input(self, stray):
"episodic_memory": episodic_memory_formatted_content,
"declarative_memory": declarative_memory_formatted_content,
"chat_history": conversation_history_formatted_content,
"tools_output": ""
}

def agent_prompt_episodic_memories(self, memory_docs: List[Document]) -> str:
Expand Down
14 changes: 10 additions & 4 deletions core/cat/looking_glass/output_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@ def parse(self, llm_output: str) -> Union[AgentAction, AgentFinish]:
parsed_output_log = json.dumps(parsed_output, indent=4)
except Exception as e:
log.error(e)
raise OutputParserException(f"Could not parse LLM output: `{llm_output}`")
return AgentFinish(
# Return values is generally always a dictionary with a single `output` key
# It is not recommended to try anything else at the moment :)
return_values={"output": None},
log=""
)

# Extract action
action = parsed_output["action"]
action_input = str(parsed_output["action_input"])

if action_input:
action_input = action_input.strip(" ").strip('"')
if isinstance(parsed_output["action_input"], str):
action_input = parsed_output["action_input"]
elif isinstance(parsed_output["action_input"], dict):
action_input = json.dumps(parsed_output["action_input"])
else:
action_input = ""

Expand Down

0 comments on commit 59c1391

Please sign in to comment.