Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

select convo msgs #189

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion backend/app/agent_types/openai_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from langgraph.prebuilt import ToolExecutor, ToolInvocation

from app.message_types import LiberalToolMessage
from app.messages import select_conversation_messages


def get_openai_agent_executor(
Expand All @@ -30,7 +31,7 @@ async def _get_messages(messages):
else:
msgs.append(m)

return [SystemMessage(content=system_message)] + msgs
return [SystemMessage(content=system_message)] + select_conversation_messages(msgs)

if tools:
llm_with_tools = llm.bind(tools=[format_tool_to_openai_tool(t) for t in tools])
Expand Down
28 changes: 28 additions & 0 deletions backend/app/messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from langchain_core.messages import HumanMessage, AIMessage, BaseMessage

from typing import Sequence

def select_conversation_messages(messages: Sequence[BaseMessage]):
"""Select only user input <> completion pairs and current scratchpad.

Ignore previous scratchpads (function calls, etc)."""
new_messages = []
_messages = []
for m in messages:
if isinstance(m, HumanMessage):
# if the last message in the existing run is NOT AIMessage, then
# that means something interrupted it, so let's ignore this
if not isinstance(_messages[-1], AIMessage):
continue
# Otherwise, we add the first (Human) and last (AI) message to the
# full list of messages
new_messages.append(_messages[0])
new_messages.append(_messages[-1])
# Start a new list of messages
_messages = [m]
else:
_messages.append(m)
# Now we add the final messages to the list of messages
# This are all messages that are part of the current scratchpad
new_messages.extend(_messages)
return new_messages
Loading