Skip to content

Human in the loop error #115

Open
Open
@Ankur-Kumar222

Description

@Ankur-Kumar222
  1. I've created a supervisor architecture with a bunch of sub agents.
  2. Each sub agent is a pre-built create_react_agent with a tool called human input.
  3. On LangGraph studio, the agent works perfectly.
  4. When run on the agent-chat-ui, the sub agent's assistant calls the human input tool but does not wait for the user's input and instead returns an empty {}.
  5. This happens multiple times until the agent gives up and goes back to the supervisor.
from langgraph.types import interrupt
from langgraph.prebuilt import create_react_agent
from langgraph.prebuilt.interrupt import (
    ActionRequest,
    HumanInterrupt,
    HumanInterruptConfig,
    HumanResponse,
)
import os
from langchain_openai import ChatOpenAI
from langgraph.graph import MessagesState
from langchain_core.messages import AIMessage, HumanMessage
from Agents.promptTemplates import personal_agent_system_message

class PersonalState(MessagesState):
    pass

class SupervisorState(MessagesState):
    next: str

OPENAI_API_KEY = os.environ['OPENAI_API_KEY']
LLM_MODEL = os.environ['LLM_MODEL']

llm = ChatOpenAI(model=LLM_MODEL, api_key=OPENAI_API_KEY, temperature=0, stream_usage=True)


def human_input(state: PersonalState):
    """
    This tool is used to get user input.
    """
    tool_call = state["messages"][-1].tool_calls[0]

    action_request = ActionRequest(
        action=tool_call['name'],
        args=tool_call['args'],
    )

    interrupt_config = HumanInterruptConfig(
        allow_respond=True,
    )

    description = (
        "Type your response here..."
    )

    request = HumanInterrupt(
        action_request=action_request, config=interrupt_config, description=description
    )
    response: HumanResponse = interrupt([request])[0]

    if response.get("type") == "response":

        return {
            "messages": [
                HumanMessage(
                    content=response.get('args'),
                    name="User",
                )
            ]
        }

def database_update(state: PersonalState):
    """
    This tool is used to update the database with user information.
    """

    print("Updating database with the following information:")
    print(state["messages"][-1].content)

    return {
        "messages": [
            AIMessage(
                content="Database updated successfully!",
                name="personalAgent",
            )
        ]
    }

tools = [human_input, database_update]

personalAgent = create_react_agent(llm, tools=tools, state_modifier = personal_agent_system_message)

def personal_node(state: SupervisorState):
    result = personalAgent.invoke({'messages': state["messages"][-1]})

    last_message = result["messages"][-1]

    return {
        "messages": [
            AIMessage(
                content=last_message.content,
                name="personalAgent",
            )
        ]
    }

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions