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

An error occurred while attempting to delete the message . #29086

Open
5 tasks done
minmie opened this issue Jan 8, 2025 · 2 comments
Open
5 tasks done

An error occurred while attempting to delete the message . #29086

minmie opened this issue Jan 8, 2025 · 2 comments
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@minmie
Copy link

minmie commented Jan 8, 2025

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

from langchain_core.messages import RemoveMessage

from langchain_core.tools import tool

from langchain_openai import ChatOpenAI
from pydantic import BaseModel, Field
from langgraph.prebuilt import tools_condition

model = ChatOpenAI(model="ep-20241223171230-8tv46",
                       api_key='32589e42-775a-4c85-ab34-ecf34deda5f1',
                       base_url='https://ark.cn-beijing.volces.com/api/v3',
                       streaming=True, temperature=0,
                       )

retrieve_model = ChatOpenAI(model="ep-20241223171230-8tv46",
                       api_key='32589e42-775a-4c85-ab34-ecf34deda5f1',
                       base_url='https://ark.cn-beijing.volces.com/api/v3',
                       streaming=True, temperature=0.1,
                       )

file_path1 = '../data/080901.pdf'
file_path2 = '../data/非学历证书可免考课程列表.pdf'



@tool(response_format="content_and_artifact")
def computer_science_major_plan(query: str):
    """查询知识库,该知识库包含计算机科学与技术(专升本)专业考试计划,包含开设课程、课程学分级及毕业条件。"""
    return '', ''


@tool(response_format="content_and_artifact")
def course_exemption_info(query: str):
    """查询知识库,该知识库包含福建省高等教育自学考试课程免考实施细则。"""
    return '', ''



def citation_rag_agent(state):
    """查询知识库,该知识库包含福建省高等教育自学考试课程免考实施细则。"""

    system_msg = (
        "You are an expert Q&A system!"
        "Please provide an answer based solely on the provided sources. "
        "When referencing information from a source, "
        "cite the appropriate source(s) using their corresponding numbers. "
        "Every answer should include at least one source citation. "
        "You should use format '[source number]' to cite the source!"
        "Only cite a source when you are explicitly referencing it. "
        "If none of the sources are helpful, you should indicate that. "
        "For example:\n"
        "Source 1:\n"
        "The sky is red in the evening and blue in the morning.\n"
        "Source 2:\n"
        "Water is wet when the sky is red.\n"
        "User query: When is water wet?\n"
        "Your answer: Water will be wet when the sky is red [2], "
        "which occurs in the evening [1].\n"
        "Now it's your turn. Below are several numbered sources of information:"
        "\n------\n"
        "{context}"
        "\n------\n"
    )

    prompt_template = ChatPromptTemplate.from_messages(
        [
            (
                "system",
                system_msg,
            ),
            MessagesPlaceholder(variable_name="messages"),
        ]
    )
    messages = state["messages"]
    conversation_messages = [
        message
        for message in state["messages"]
        if message.type in ("human", "system")
           or (message.type == "ai" and not message.tool_calls)
    ]
    # question = messages[-2].tool_calls[0]['args']['query']
    docs = messages[-1].content
    # Chain
    rag_chain = prompt_template | retrieve_model

    # Run
    response = rag_chain.invoke({"context": docs, "messages":conversation_messages})

    return {"messages": [response]}

tools = [computer_science_major_plan,course_exemption_info]



from typing import Annotated, Sequence
from typing_extensions import TypedDict

from langchain_core.messages import BaseMessage

from langgraph.graph.message import add_messages


class AgentState(TypedDict):
    # The add_messages function defines how an update should be processed
    # Default is to replace. add_messages says "append"
    messages: Annotated[Sequence[BaseMessage], add_messages]
    question: str


### Nodes
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
SYSTEM_PROMPT =(
    "you are helpful assistant! your name is '小安'.\n"
    "You should answer user questions base on tool response, rather than relying on your previous knowledge.\n"
    "When the user's statement is unclear or you feel confused, you can ask the user for confirmation.\n"
    "# User Information\n"
    "1. User's personal information can help you to have a personalized response.\n"
    "Below is user information:\n"
    "```\n"
    "{user_info}\n"
    "```\n"
)

USER_INFO = {
        "name": 'Arvin',
        "age": '18',
        "user hobby": ['basketball', 'listening music'],
    }
prompt_template = ChatPromptTemplate.from_messages(
        [
            (
                "system",
                SYSTEM_PROMPT,
            ),
            MessagesPlaceholder(variable_name="messages"),
        ]
    )
prompt_template_with_user = prompt_template.partial(user_info=str(USER_INFO))


def agent(state):
    """
    Invokes the agent model to generate a response based on the current state. Given
    the question, it will decide to retrieve using the retriever tool, or simply end.

    Args:
        state (messages): The current state

    Returns:
        dict: The updated state with the agent response appended to messages
    """
    print("---CALL AGENT---")

    model_with_tool = model.bind_tools(tools)
    prompt = prompt_template_with_user.invoke(state)
    response = model_with_tool.invoke(prompt)

    # We return a list, because this will get added to the existing list
    return {"messages": [response]}


from langgraph.graph import END, StateGraph, START
from langgraph.prebuilt import ToolNode

# Define a new graph
workflow = StateGraph(AgentState)

# Define the nodes we will cycle between
workflow.add_node("agent", agent)  # agent
workflow.add_node("citation", citation_rag_agent)
tool_node = ToolNode(tools)
workflow.add_node('tools', tool_node)
workflow.add_edge(START, "agent")

# Decide whether to retrieve
workflow.add_conditional_edges(
    "agent",
    # Assess agent decision
    tools_condition,
    {
        # Translate the condition outputs to nodes in our graph
        "tools": "tools",
        END: END,
    },
)
workflow.add_edge('tools', "citation")
workflow.add_edge("citation", END)

# Compile
from langgraph.checkpoint.memory import MemorySaver

memory = MemorySaver()
graph = workflow.compile(checkpointer=memory)

CONFIG = {"configurable": {"thread_id": "abc123"}}


inputs = {
    "messages": [
        ("user", 'hi'),
    ]
}
response = graph.invoke(inputs, config=CONFIG)
print(response["messages"][-1].content)


messages = graph.get_state(CONFIG).values["messages"]
graph.update_state(CONFIG,{"messages": [RemoveMessage(id=m.id) for m in messages]})

Error Message and Stack Trace (if applicable)

Traceback (most recent call last):
  File "/home/chenjq/miniconda3/envs/RAG/lib/python3.10/contextlib.py", line 153, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/home/chenjq/miniconda3/envs/RAG/lib/python3.10/site-packages/langgraph/pregel/manager.py", line 37, in ChannelsManager
    yield (
  File "/home/chenjq/miniconda3/envs/RAG/lib/python3.10/site-packages/langgraph/pregel/__init__.py", line 1079, in update_state
    run.invoke(
  File "/home/chenjq/miniconda3/envs/RAG/lib/python3.10/site-packages/langchain_core/runnables/base.py", line 3024, in invoke
    input = context.run(step.invoke, input, config)
  File "/home/chenjq/miniconda3/envs/RAG/lib/python3.10/site-packages/langgraph/utils/runnable.py", line 184, in invoke
    ret = context.run(self.func, input, **kwargs)
  File "/home/chenjq/miniconda3/envs/RAG/lib/python3.10/site-packages/langgraph/graph/graph.py", line 95, in _route
    result = self.path.invoke(value, config)
  File "/home/chenjq/miniconda3/envs/RAG/lib/python3.10/site-packages/langgraph/utils/runnable.py", line 176, in invoke
    ret = context.run(self.func, input, **kwargs)
  File "/home/chenjq/miniconda3/envs/RAG/lib/python3.10/site-packages/langgraph/prebuilt/tool_node.py", line 636, in tools_condition
    raise ValueError(f"No messages found in input state to tool_edge: {state}")
ValueError: No messages found in input state to tool_edge: {'messages': []}

Description

I try to delete messages and this error occur.

System Info

System Information

OS: Linux
OS Version: #1 SMP Mon Oct 19 16:18:59 UTC 2020
Python Version: 3.10.12 (main, Jul 5 2023, 18:54:27) [GCC 11.2.0]

Package Information

langchain_core: 0.3.29
langchain: 0.3.14
langchain_community: 0.3.13
langsmith: 0.2.4
langchain_app: Installed. No version info available.
langchain_chroma: 0.1.4
langchain_huggingface: 0.1.2
langchain_openai: 0.2.14
langchain_text_splitters: 0.3.4
langgraph_sdk: 0.1.48

Optional packages not installed

langserve

Other Dependencies

aiohttp: 3.11.10
async-timeout: 4.0.3
chromadb: 0.5.23
dataclasses-json: 0.6.7
fastapi: 0.115.6
httpx: 0.28.1
httpx-sse: 0.4.0
huggingface-hub: 0.26.5
jsonpatch: 1.33
langsmith-pyo3: Installed. No version info available.
numpy: 1.26.4
openai: 1.58.1
orjson: 3.10.12
packaging: 24.2
pydantic: 2.10.3
pydantic-settings: 2.7.0
PyYAML: 6.0.2
requests: 2.32.3
requests-toolbelt: 1.0.0
sentence-transformers: 3.3.1
SQLAlchemy: 2.0.36
tenacity: 8.5.0
tiktoken: 0.8.0
tokenizers: 0.20.3
transformers: 4.46.3
typing-extensions: 4.12.2

@dosubot dosubot bot added the 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature label Jan 8, 2025
@Marsman1996
Copy link
Contributor

Is it OK to expose the api_key here?!

@keenborder786
Copy link
Contributor

@minmie I don't understand why you are trying to do this since you are using MemorySaver the messages are kept in memory as long as the main process is running,.,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
Development

No branches or pull requests

3 participants