From ef3148e9512b50e9208131f56e98ca79f219515d Mon Sep 17 00:00:00 2001 From: Cyberes Date: Fri, 2 Feb 2024 23:40:33 -0700 Subject: [PATCH] fix ordering --- pers/langchain/history.py | 5 +++++ pers/langchain/tools/web_reader.py | 4 ++++ run.py | 5 ++--- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/pers/langchain/history.py b/pers/langchain/history.py index 27ac0a0..004a9dd 100644 --- a/pers/langchain/history.py +++ b/pers/langchain/history.py @@ -48,6 +48,11 @@ class HistoryManager: self._redis.rpush(self._key, pickle.dumps(FunctionMessage(name=name, content=content))) def acknowledge_stop(self): + """ + Check if the last message in the context was a function call to end_my_response. + If so, remove it and return True. + :return: + """ last_item = pickle.loads(self._redis.lrange(self._key, -1, -1)[0]) if hasattr(last_item, 'name') and last_item.name == 'end_my_response': self._redis.rpop(self._key) diff --git a/pers/langchain/tools/web_reader.py b/pers/langchain/tools/web_reader.py index 1a7d823..72e03f0 100644 --- a/pers/langchain/tools/web_reader.py +++ b/pers/langchain/tools/web_reader.py @@ -9,6 +9,10 @@ from pydantic.v1 import BaseModel, Field from pers.langchain.tools.browser import render_webpage from pers.langchain.tools.tools import PRINT_USAGE, _print_func_call +""" +Based on https://github.com/taivop/agentreader +""" + FULL_TEMPLATE = """ TITLE: {title} AUTHORS: {authors} diff --git a/run.py b/run.py index b37cb55..9d620c2 100755 --- a/run.py +++ b/run.py @@ -124,16 +124,15 @@ def main(): SystemMessage(content="Evaluate your progress on the current task. Call `end_my_response` after you have responded and are ready for the human's next message.") ) - result = agent_executor.invoke({"input": next_input, "chat_history": temp_context}, config=RunnableConfig(callbacks=[handler])) - if pers.GLOBALS.ChatHistory.acknowledge_stop(): break + result = agent_executor.invoke({"input": next_input, "chat_history": temp_context}, config=RunnableConfig(callbacks=[handler])) + # Langchain and the agent are really struggling with end_my_response. # If the agent gets confused and puts the string "end_my_response" at the end of the msg rather than calling the function, end it manually. do_stop = False output = result['output'] - if re.search(MANUAL_STOP_RE, output): output = re.sub(MANUAL_STOP_RE, '', output) do_stop = True