fix ordering

This commit is contained in:
Cyberes 2024-02-02 23:40:33 -07:00
parent 4a542df715
commit ef3148e951
3 changed files with 11 additions and 3 deletions

View File

@ -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)

View File

@ -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}

5
run.py
View File

@ -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