Skip to content

Commit ff3eae8

Browse files
authored
Merge pull request #41 from lambrou/hotfix/fix-continuity
Hotfix/fix continuity
2 parents 65a789b + 3a52cf5 commit ff3eae8

File tree

6 files changed

+23
-10
lines changed

6 files changed

+23
-10
lines changed

semterm/agent/SemanticTerminalAgent.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22

33
from langchain.agents import AgentExecutor
4-
from langchain.memory import ConversationEntityMemory
4+
from langchain.memory import ConversationTokenBufferMemory
55
from langchain_openai import ChatOpenAI
66

77
from semterm.agent.TerminalAgentPrompt import PREFIX
@@ -40,9 +40,11 @@ def load_tools(self):
4040
return tools
4141

4242
def initialize_memory(self):
43-
return ConversationEntityMemory(
43+
return ConversationTokenBufferMemory(
4444
llm=self.llm,
4545
return_messages=True,
46+
input_key="input",
47+
output_key="output",
4648
chat_history_key="chat_history",
4749
)
4850

@@ -62,8 +64,9 @@ def initialize_executor(self):
6264
self.tools,
6365
memory=self.memory,
6466
max_iterations=self.max_iterations,
67+
return_intermediate_steps=True,
6568
verbose=self.verbose,
6669
)
6770

6871
def invoke(self, user_input):
69-
return self.terminal_agent_executor.invoke(input=user_input)["output"]
72+
return self.terminal_agent_executor.invoke({"input": user_input, "chat_history": []})["output"]

semterm/agent/TerminalAgentPrompt.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# flake8: noqa
2-
PREFIX = """You are a Semantic Terminal. Users will ask for you to perform tasks, expecting you to use the Terminal
2+
PREFIX = """You are a Semantic Terminal, an intelligent AI that can only respond in thoughts followed by valid JSON. Users will ask for you to perform tasks, expecting you to use the Terminal
33
tool. Use it often and go above and beyond in completing the users request. Remember that you don't have to run all your commands in one go. You can run a command, look at the
44
output, and then run a new command based on that output. You can also use the Terminal tool to run multiple commands.
55
If you need to install a program to use it, use the Human tool to get permission from the user and then install it.
@@ -39,11 +39,15 @@
3939
4040
{format_instructions}
4141
42+
CHAT HISTORY ----------
43+
44+
{{{{chat_history}}}}
45+
4246
USER'S INPUT ---------
4347
4448
{{{{input}}}}
4549
46-
Plan out what you will do to complete the task and then respond with an action.
50+
Plan out what you will do to complete the task and then respond with an action, all in one response. (Thoughts and actions need to be in one response)
4751
"""
4852

4953
TEMPLATE_TOOL_RESPONSE = """Observation:

semterm/terminal/TerminalOutputParser.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from typing import Union
44
from langchain.agents.conversational_chat.output_parser import ConvoOutputParser
55
from langchain.schema import AgentAction, AgentFinish
6+
from langchain_core.agents import AgentFinish, AgentAction
7+
68
from semterm.agent.TerminalAgentPrompt import FORMAT_INSTRUCTIONS
79
from semterm.langchain_extensions.schema import AgentMistake
810

@@ -11,14 +13,14 @@ class TerminalOutputParser(ConvoOutputParser, ABC):
1113
def get_format_instructions(self) -> str:
1214
return FORMAT_INSTRUCTIONS
1315

14-
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
16+
def parse(self, text: str) -> AgentFinish | AgentAction | AgentMistake:
1517
text = text.strip().replace("\xa0", " ")
1618
start_positions = [i for i, c in enumerate(text) if c == "{"]
1719
end_positions = [i for i, c in enumerate(text) if c == "}"]
1820

1921
for start in start_positions:
2022
for end in end_positions:
21-
if start < end: # ensure the end position is after the start
23+
if start < end:
2224
try:
2325
cleaned_output = text[start : end + 1]
2426
response = json.loads(cleaned_output)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="semterm",
5-
version="0.6.0",
5+
version="0.6.1",
66
description="The Semantic Terminal",
77
long_description="The Semantic Terminal",
88
author="Lambrou",

tests/agent/test_SemanticTerminalAgent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,4 @@ def test_invoke(self, mrkl_agent):
119119

120120
mrkl_agent.terminal_agent_executor.invoke = MagicMock()
121121
mrkl_agent.invoke(user_input)
122-
mrkl_agent.terminal_agent_executor.invoke.assert_called_with(input=user_input)
122+
mrkl_agent.terminal_agent_executor.invoke.assert_called_with({"input": user_input, "chat_history": []})

tests/agent/test_TerminalAgent.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
AIMessage,
88
SystemMessage,
99
)
10+
from langchain_core.messages import HumanMessage
11+
1012
from semterm.agent.TerminalAgent import TerminalAgent
1113
from semterm.agent.TerminalAgentPrompt import PREFIX, SUFFIX
1214

@@ -30,7 +32,9 @@ def test_create_prompt(self, terminal_agent, mock_tools):
3032
# Extract properties from the returned ChatPromptTemplate
3133
system_message_from_prompt = prompt.messages[0].format_messages()[0].content
3234
human_message_from_prompt = (
33-
prompt.messages[2].format_messages(input="test input")[0].content
35+
prompt.messages[2].format_messages(input="test input", chat_history=[HumanMessage(
36+
content="test input"
37+
)])[0].content
3438
)
3539

3640
# Assert that the properties have the expected values

0 commit comments

Comments
 (0)